1
1
<?php
2
2
3
3
namespace CodeFin \Http \Controllers \Site ;
4
+
4
5
use CodeFin \Http \Controllers \Controller ;
6
+ use CodeFin \Http \Requests \SubscriptionCreateRequest ;
7
+ use CodeFin \Iugu \Exceptions \AbstractIuguException ;
8
+ use CodeFin \Iugu \Exceptions \IuguCustomerException ;
9
+ use CodeFin \Iugu \Exceptions \IuguPaymentMethodException ;
10
+ use CodeFin \Iugu \Exceptions \IuguSubscriptionException ;
11
+ use CodeFin \Iugu \IuguSubscriptionManager ;
5
12
use CodeFin \Repositories \Interfaces \PlanRepository ;
13
+ use Illuminate \Support \Facades \Auth ;
6
14
7
15
class SubscriptionsController extends Controller
8
16
{
9
17
/**
10
18
* @var PlanRepository
11
19
*/
12
20
private $ planRepository ;
21
+ /**
22
+ * @var IuguSubscriptionManager
23
+ */
24
+ private $ iuguSubscriptionManager ;
13
25
14
- public function __construct (PlanRepository $ planRepository )
26
+ public function __construct (PlanRepository $ planRepository, IuguSubscriptionManager $ iuguSubscriptionManager )
15
27
{
16
28
$ this ->planRepository = $ planRepository ;
29
+ $ this ->iuguSubscriptionManager = $ iuguSubscriptionManager ;
17
30
}
18
31
19
32
public function create ()
20
33
{
21
- $ plan = $ this ->planRepository ->find ( 1 );
34
+ $ plan = $ this ->planRepository ->all ()-> first ( );
22
35
return view ('site.subscriptions.create ' , compact ('plan ' ));
23
36
}
24
37
25
- public function store ()
38
+ public function store (SubscriptionCreateRequest $ request )
39
+ {
40
+ $ plan = $ this ->planRepository ->all ()->first ();
41
+
42
+ try {
43
+ $ this ->iuguSubscriptionManager ->create (
44
+ Auth::user (), $ plan , $ request ->all ()
45
+ );
46
+ } catch (AbstractIuguException $ e ) {
47
+ $ request ->session ()->flash ('error ' ,$ this ->getMessageException ($ e ));
48
+ return redirect ()->route ('site.subscriptions.create ' );
49
+ }
50
+
51
+ return redirect ()->route ('site.subscriptions.successfully ' );
52
+ }
53
+
54
+ public function successfully ()
26
55
{
56
+ return view ('site.subscriptions.successfully ' );
57
+ }
27
58
59
+ protected function getMessageException ($ exception )
60
+ {
61
+ if ($ exception instanceof IuguCustomerException) {
62
+ return 'Erro ao processar cliente. Contacte o atendimento para mais detalhes. ' ;
63
+ } elseif ($ exception instanceof IuguPaymentMethodException) {
64
+ return 'Erro ao salvar método de pagamento. Contacte o atendimento para mais detalhes. ' ;
65
+ }elseif ($ exception instanceof IuguSubscriptionException) {
66
+ return 'Erro ao processar assinatura. Contacte o atendimento para mais detalhes. ' ;
67
+ }
28
68
}
29
69
}
0 commit comments