1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Paypal; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use PayPal\Api\Invoice; |
7
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class PaymentsController |
15
|
|
|
* @package App\Controller\Paypal |
16
|
|
|
* |
17
|
|
|
* @Route("/paypal/payments", name="paypal-payments-") |
18
|
|
|
*/ |
19
|
|
|
class PaymentsController extends AbstractController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @Route("/ppcp", name="ppcp", methods={"GET"}, defaults={"action" = "ppcp"}) |
23
|
|
|
* @Route("/pay-ins", name="pay-ins", methods={"GET"}, defaults={"action" = "pay-ins"}) |
24
|
|
|
* @Route("/pay-outs", name="pay-outs", methods={"GET"}, defaults={"action" = "pay-outs"}) |
25
|
|
|
* @Route("/bopis", name="bopis", methods={"GET"}, defaults={"action" = "bopis"}) |
26
|
|
|
* @Route("/subscriptions", name="subscriptions", methods={"GET"}, defaults={"action" = "subscriptions"}) |
27
|
|
|
* @Route("/invoices", name="invoices", methods={"GET"}, defaults={"action" = "invoices"}) |
28
|
|
|
* @Route("/ecs", name="ecs", methods={"GET"}, defaults={"action" = "ecs"}) |
29
|
|
|
* @Route("/paylater", name="paylater", methods={"GET"}, defaults={"action" = "paylater"}) |
30
|
|
|
* @Route("/msp", name="msp", methods={"GET"}, defaults={"action" = "msp"}) |
31
|
|
|
* |
32
|
|
|
* @param string $action |
33
|
|
|
* |
34
|
|
|
* @return Response | RedirectResponse |
35
|
|
|
*/ |
36
|
|
|
public function payments(string $action) |
37
|
|
|
{ |
38
|
|
|
return $this->render('paypal/payments/'. $action .'.html.twig'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @Route("/{paymentId}/capture", name="capture", methods={"POST"}) |
43
|
|
|
* @param string $paymentId |
44
|
|
|
* @return Response | RedirectResponse |
45
|
|
|
*/ |
46
|
|
|
public function capture(string $paymentId) |
47
|
|
|
{ |
48
|
|
|
$capture = $this->paypalService->getPaymentService()->capturePayment($paymentId); |
49
|
|
|
return $this->render('default/dump-input-id.html.twig', [ |
50
|
|
|
'raw_result' => false, |
51
|
|
|
'result' => $capture, |
52
|
|
|
'result_id' => 'payment-id' |
53
|
|
|
]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @Route("/pay-outs", name="pay-outs-create", methods={"POST"}) |
58
|
|
|
* |
59
|
|
|
* @return Response | RedirectResponse |
60
|
|
|
*/ |
61
|
|
|
public function payoutsCreate() |
62
|
|
|
{ |
63
|
|
|
$request = Request::createFromGlobals(); |
64
|
|
|
$subject = $request->request->get('subject', null); |
65
|
|
|
$note = $request->request->get('note', null); |
66
|
|
|
$email = $request->request->get('email', null); |
67
|
|
|
$itemId = $request->request->get('item-id', null); |
68
|
|
|
$currency = $request->request->get('currency', null); |
69
|
|
|
$amount = $request->request->get('amount', null); |
70
|
|
|
$payout = $this->paypalService |
71
|
|
|
->getPayoutService() |
72
|
|
|
->createPayout($subject, $note, $email, $itemId, $amount, $currency); |
73
|
|
|
return $this->render('default/dump-input-id.html.twig', [ |
74
|
|
|
'raw_result' => false, |
75
|
|
|
'result' => $payout, |
76
|
|
|
'result_id' => $payout->getBatchHeader()->getPayoutBatchId() |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @Route("/pay-outs/{statusId}", name="pay-outs-refresh", methods={"GET"}) |
82
|
|
|
* |
83
|
|
|
* @param string $statusId |
84
|
|
|
* @return Response | RedirectResponse |
85
|
|
|
*/ |
86
|
|
|
public function payoutsRefresh(string $statusId) |
87
|
|
|
{ |
88
|
|
|
$payout = $this->paypalService->getPayoutService()->getPayout($statusId); |
89
|
|
|
return $this->render('default/dump-input-id.html.twig', [ |
90
|
|
|
'raw_result' => false, |
91
|
|
|
'result' => $payout, |
92
|
|
|
'result_id' => $payout->getBatchHeader()->getPayoutBatchId() |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @Route("/invoices", name="invoices-create", methods={"POST"}) |
98
|
|
|
* |
99
|
|
|
* @return Response | RedirectResponse |
100
|
|
|
*/ |
101
|
|
|
public function invoicesCreate() |
102
|
|
|
{ |
103
|
|
|
$request = Request::createFromGlobals(); |
104
|
|
|
$inputForm = $request->request->all(); |
105
|
|
|
$invoice = $this->paypalService->getInvoiceService()->createInvoice($inputForm); |
106
|
|
|
if ($invoice instanceof Invoice) { |
107
|
|
|
$this->paypalService->getInvoiceService()->sendInvoice($invoice); |
108
|
|
|
$invoiceQR = $this->paypalService->getInvoiceService()->getInvoiceQRHTML($invoice); |
109
|
|
|
} |
110
|
|
|
if (isset($invoiceQR)) { |
111
|
|
|
return $this->render('default/dump-input-id.html.twig', [ |
112
|
|
|
'raw_result' => true, |
113
|
|
|
'result' => $invoiceQR, |
114
|
|
|
'result_id' => $invoice->getId() |
115
|
|
|
]); |
116
|
|
|
} |
117
|
|
|
return new JsonResponse('Error creating the Invoice, please check the logs'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @Route("/ppcp", name="ppcp", methods={"GET"}, defaults={"action" = "ppcp"}) |
122
|
|
|
* |
123
|
|
|
* @param string $action |
124
|
|
|
* @return Response |
125
|
|
|
* @throws Exception |
126
|
|
|
*/ |
127
|
|
|
public function ppcp(string $action): Response |
128
|
|
|
{ |
129
|
|
|
$clientToken = $this->paypalService->getIdentityService()->getClientToken(); |
130
|
|
|
return $this->render( |
131
|
|
|
'paypal/payments/'. $action .'.html.twig', |
132
|
|
|
[ |
133
|
|
|
'clientToken' => $clientToken, |
134
|
|
|
] |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @Route("/invoices/{invoiceId}", name="invoices-refresh", methods={"GET"}) |
140
|
|
|
* @param string $invoiceId |
141
|
|
|
* @return Response | RedirectResponse |
142
|
|
|
*/ |
143
|
|
|
public function invoicesRefresh(string $invoiceId) |
144
|
|
|
{ |
145
|
|
|
$invoice = $this->paypalService->getInvoiceService()->getInvoice($invoiceId); |
146
|
|
|
return $this->render('default/dump-input-id.html.twig', [ |
147
|
|
|
'raw_result' => false, |
148
|
|
|
'result' => $invoice, |
149
|
|
|
'result_id' => $invoice->getId() |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @Route("/{paymentId}/ecs-result", name="ecs-result", methods={"POST"}) |
155
|
|
|
* @param string $paymentId |
156
|
|
|
* @return Response | RedirectResponse |
157
|
|
|
*/ |
158
|
|
|
public function ecsResult(string $paymentId) |
159
|
|
|
{ |
160
|
|
|
$capture = $this->paypalService->getPaymentService()->capturePayment($paymentId); |
161
|
|
|
return $this->render('paypal/payments/ecs-results.twig', [ |
162
|
|
|
'capture' => [ |
163
|
|
|
'payer' => $capture->payer, |
164
|
|
|
'shipping' => $capture->purchase_units[0]->shipping, |
165
|
|
|
], |
166
|
|
|
]); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @Route("/create", name="create", methods={"POST"}) |
171
|
|
|
* @return JsonResponse |
172
|
|
|
*/ |
173
|
|
|
public function create(): JsonResponse |
174
|
|
|
{ |
175
|
|
|
$request = Request::createFromGlobals(); |
176
|
|
|
$requestBody = $request->getContent(); |
177
|
|
|
$headers = ['PayPal-Request-Id' => time()]; |
178
|
|
|
$order = $this->paypalService->getPaymentService()->createOrder($requestBody, $headers); |
179
|
|
|
return new JsonResponse($order); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|