1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Braintree; |
4
|
|
|
|
5
|
|
|
use Braintree\Exception\NotFound; |
6
|
|
|
use Braintree\Transaction; |
7
|
|
|
use Symfony\Component\HttpFoundation\Request; |
8
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class PaymentsController |
13
|
|
|
* |
14
|
|
|
* @package App\Controller\Braintree |
15
|
|
|
* |
16
|
|
|
* @Route("/braintree/payments", name="braintree-payments-") |
17
|
|
|
*/ |
18
|
|
|
class PaymentsController extends AbstractController |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @Route("/dropui", name="dropui", methods={"GET"}) |
22
|
|
|
* |
23
|
|
|
* @return Response |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function dropUI() |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
$clientToken = $this->braintreeService->getPaymentService()->getClientToken(); |
28
|
|
|
return $this->render('braintree/payments/dropui.html.twig', [ |
29
|
|
|
'clientToken' => $clientToken, |
30
|
|
|
'name' => 'Drop UI' |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @Route("/hosted-fields", name="hosted-fields", methods={"GET"}) |
36
|
|
|
* |
37
|
|
|
* @return Response |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
public function hostedFields() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$clientToken = $this->braintreeService->getPaymentService()->getClientToken(); |
42
|
|
|
return $this->render('braintree/payments/hosted-fields.html.twig', [ |
43
|
|
|
'clientToken' => $clientToken, |
44
|
|
|
'name' => 'Hosted Fields' |
45
|
|
|
]); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @Route("/apm", name="apm", methods={"GET"}) |
50
|
|
|
* |
51
|
|
|
* @return Response |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function apm() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$clientToken = $this->braintreeService->getPaymentService()->getClientToken(); |
56
|
|
|
return $this->render('braintree/payments/apm.html.twig', [ |
57
|
|
|
'clientToken' => $clientToken, |
58
|
|
|
'name' => 'APM', |
59
|
|
|
'paypalClientId' => $this->getParameter('PAYPAL_SDK_CLIENT_ID'), |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @Route("/3ds", name="three-ds", methods={"GET"}) |
65
|
|
|
* |
66
|
|
|
* @return Response |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function threeDS() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$clientToken = $this->braintreeService->getPaymentService()->getClientToken(); |
71
|
|
|
return $this->render('braintree/payments/3ds.html.twig', [ |
72
|
|
|
'clientToken' => $clientToken, |
73
|
|
|
'name' => 'DropIn 3DS', |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @Route("/payload", name="payload", methods={"POST"}) |
79
|
|
|
* |
80
|
|
|
* @return Response |
81
|
|
|
*/ |
82
|
|
|
public function paymentsPayload() |
83
|
|
|
{ |
84
|
|
|
$request = Request::createFromGlobals(); |
85
|
|
|
$payload = $request->request->all(); |
86
|
|
|
return $this->render('default/dump.html.twig', [ |
87
|
|
|
'result' => (object) $payload, |
88
|
|
|
'raw_result' => false, |
89
|
|
|
]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @Route("/transaction", name="create", methods={"POST"}) |
94
|
|
|
* |
95
|
|
|
* @return Response |
96
|
|
|
*/ |
97
|
|
|
public function createTransaction() |
98
|
|
|
{ |
99
|
|
|
$request = Request::createFromGlobals(); |
100
|
|
|
$paymentNonce = $request->request->get('payment_nonce'); |
101
|
|
|
$amount = $request->request->get('amount'); |
102
|
|
|
$deviceData = $request->request->get('device_data'); |
103
|
|
|
$sale = $this->braintreeService->getPaymentService()->createSale($amount, $paymentNonce, $deviceData); |
104
|
|
|
/** @var Transaction $transaction */ |
105
|
|
|
$transaction = $sale->transaction; |
106
|
|
|
return $this->render('default/dump-input-id.html.twig', [ |
107
|
|
|
'result' => $sale, |
108
|
|
|
'raw_result' => false, |
109
|
|
|
'result_id' => $transaction->id |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @Route("/transaction/{transactionId}/capture", name="capture", methods={"POST"}) |
115
|
|
|
* |
116
|
|
|
* @param string $transactionId |
117
|
|
|
* |
118
|
|
|
* @return Response |
119
|
|
|
*/ |
120
|
|
|
public function captureTransaction(string $transactionId) |
121
|
|
|
{ |
122
|
|
|
$request = Request::createFromGlobals(); |
123
|
|
|
$amount = $request->request->get('amount'); |
124
|
|
|
$capture = $this->braintreeService->getPaymentService()->captureSale($transactionId, $amount); |
125
|
|
|
return $this->render('default/dump.html.twig', [ |
126
|
|
|
'result' => (object) $capture, |
127
|
|
|
'raw_result' => false, |
128
|
|
|
]); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @Route("/{transactionId}", name="get", methods={"GET"}) |
133
|
|
|
* |
134
|
|
|
* @param string $transactionId |
135
|
|
|
* @return Response |
136
|
|
|
* @throws NotFound |
137
|
|
|
*/ |
138
|
|
|
public function getTransaction(string $transactionId) |
139
|
|
|
{ |
140
|
|
|
$transaction = $this->braintreeService->getPaymentService()->getTransaction($transactionId); |
141
|
|
|
return $this->render('default/dump.html.twig', [ |
142
|
|
|
'result' => (object) $transaction, |
143
|
|
|
'raw_result' => false, |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.