1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Paypal; |
4
|
|
|
|
5
|
|
|
use App\Service\PaypalService; |
6
|
|
|
use App\Service\SessionService; |
7
|
|
|
use PayPal\Api\Invoice; |
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class AuthenticatedController |
17
|
|
|
* @package App\Controller\Paypal |
18
|
|
|
* |
19
|
|
|
* @Route("/paypal") |
20
|
|
|
*/ |
21
|
|
|
class AuthenticatedController extends AbstractController |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var SessionService |
25
|
|
|
*/ |
26
|
|
|
protected $sessionService; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var PaypalService |
30
|
|
|
*/ |
31
|
|
|
protected $paypalService; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* DefaultController constructor. |
35
|
|
|
* @param SessionService $sessionService |
36
|
|
|
* @param PaypalService $paypalService |
37
|
|
|
*/ |
38
|
|
|
public function __construct(SessionService $sessionService, PaypalService $paypalService) |
39
|
|
|
{ |
40
|
|
|
$this->sessionService = $sessionService; |
41
|
|
|
$this->paypalService = $paypalService; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @Route("/logged-in/account", name="account") |
46
|
|
|
* |
47
|
|
|
* @return Response | RedirectResponse |
48
|
|
|
*/ |
49
|
|
|
public function account() |
50
|
|
|
{ |
51
|
|
|
if (!$this->sessionService->isActive()) { |
52
|
|
|
return $this->redirectToRoute('index'); |
53
|
|
|
} |
54
|
|
|
$refreshToken = $this->sessionService->getRefreshToken(); |
55
|
|
|
$myTransactions = $this->paypalService |
56
|
|
|
->getReportingService() |
57
|
|
|
->getUserTransactionsFromRefreshToken($refreshToken); |
58
|
|
|
if ($refreshToken) { |
|
|
|
|
59
|
|
|
$userInfo = $this->paypalService |
60
|
|
|
->getIdentityService() |
61
|
|
|
->getUserInfoFromRefreshToken($refreshToken); |
62
|
|
|
if ($userInfo) { |
63
|
|
|
return $this->render('paypal/authenticated/account.html.twig', [ |
64
|
|
|
'name' => $userInfo->getName(), |
65
|
|
|
'email' => $userInfo->getEmail(), |
66
|
|
|
'userInfo' => $userInfo, |
67
|
|
|
'transactions' => $myTransactions, |
68
|
|
|
]); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
return $this->redirectToRoute('index'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @Route("/logged-in/payments", name="payments") |
76
|
|
|
* |
77
|
|
|
* @return Response | RedirectResponse |
78
|
|
|
*/ |
79
|
|
|
public function payments() |
80
|
|
|
{ |
81
|
|
|
if (!$this->sessionService->isActive()) { |
82
|
|
|
return $this->redirectToRoute('index'); |
83
|
|
|
} |
84
|
|
|
return $this->render('paypal/authenticated/payments.html.twig', [ |
85
|
|
|
'PAYPAL_SDK_CLIENT_ID' => |
86
|
|
|
$this->sessionService->session->get('PAYPAL_SDK_CLIENT_ID') ?? |
87
|
|
|
$this->getParameter('PAYPAL_SDK_CLIENT_ID'), |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @Route("/logged-in/payments/capture/{paymentId}", name="payments-capture") |
93
|
|
|
* @param string $paymentId |
94
|
|
|
* @return Response | RedirectResponse |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function paymentsCapture(string $paymentId) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
if (!$this->sessionService->isActive()) { |
99
|
|
|
return $this->redirectToRoute('index'); |
100
|
|
|
} |
101
|
|
|
$capture = $this->paypalService->getPaymentService()->capturePayment($paymentId); |
102
|
|
|
return $this->render('paypal/authenticated/result.html.twig', [ |
103
|
|
|
'raw_result' => false, |
104
|
|
|
'result' => $capture, |
105
|
|
|
'result_id' => 'payment-id' |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @Route("/logged-in/payouts", name="payouts") |
111
|
|
|
* |
112
|
|
|
* @return Response | RedirectResponse |
113
|
|
|
*/ |
114
|
|
|
public function payouts() |
115
|
|
|
{ |
116
|
|
|
if (!$this->sessionService->isActive()) { |
117
|
|
|
return $this->redirectToRoute('index'); |
118
|
|
|
} |
119
|
|
|
return $this->render('paypal/authenticated/payouts.html.twig'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @Route("/logged-in/payouts/create", name="payouts-create") |
124
|
|
|
* |
125
|
|
|
* @return Response | RedirectResponse |
126
|
|
|
*/ |
127
|
|
|
public function payoutsCreate() |
128
|
|
|
{ |
129
|
|
|
if (!$this->sessionService->isActive()) { |
130
|
|
|
return $this->redirectToRoute('index'); |
131
|
|
|
} |
132
|
|
|
$request = Request::createFromGlobals(); |
133
|
|
|
$subject = $request->request->get('subject', null); |
134
|
|
|
$note = $request->request->get('note', null); |
135
|
|
|
$email = $request->request->get('email', null); |
136
|
|
|
$itemId = $request->request->get('item-id', null); |
137
|
|
|
$currency = $request->request->get('currency', null); |
138
|
|
|
$amount = $request->request->get('amount', null); |
139
|
|
|
$payout = $this->paypalService |
140
|
|
|
->getPayoutService() |
141
|
|
|
->createPayout($subject, $note, $email, $itemId, $amount, $currency); |
142
|
|
|
return $this->render('paypal/authenticated/result.html.twig', [ |
143
|
|
|
'raw_result' => false, |
144
|
|
|
'result' => $payout, |
145
|
|
|
'result_id' => $payout->getBatchHeader()->getPayoutBatchId() |
146
|
|
|
]); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @Route("/logged-in/payouts/{statusId}", name="payouts-refresh") |
151
|
|
|
* @param string $statusId |
152
|
|
|
* @return Response | RedirectResponse |
153
|
|
|
*/ |
154
|
|
View Code Duplication |
public function payoutsRefresh(string $statusId) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
if (!$this->sessionService->isActive()) { |
157
|
|
|
return $this->redirectToRoute('index'); |
158
|
|
|
} |
159
|
|
|
$payout = $this->paypalService->getPayoutService()->getPayout($statusId); |
160
|
|
|
return $this->render('paypal/authenticated/result.html.twig', [ |
161
|
|
|
'raw_result' => false, |
162
|
|
|
'result' => $payout, |
163
|
|
|
'result_id' => $payout->getBatchHeader()->getPayoutBatchId() |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @Route("/logged-in/invoices", name="invoices") |
169
|
|
|
* |
170
|
|
|
* @return Response | RedirectResponse |
171
|
|
|
*/ |
172
|
|
|
public function invoices() |
173
|
|
|
{ |
174
|
|
|
if (!$this->sessionService->isActive()) { |
175
|
|
|
return $this->redirectToRoute('index'); |
176
|
|
|
} |
177
|
|
|
return $this->render('paypal/authenticated/invoices.html.twig'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @Route("/logged-in/invoices/create", name="invoices-create") |
182
|
|
|
* |
183
|
|
|
* @return Response | RedirectResponse |
184
|
|
|
*/ |
185
|
|
|
public function invoicesCreate() |
186
|
|
|
{ |
187
|
|
|
if (!$this->sessionService->isActive()) { |
188
|
|
|
return $this->redirectToRoute('index'); |
189
|
|
|
} |
190
|
|
|
$request = Request::createFromGlobals(); |
191
|
|
|
$inputForm = $request->request->all(); |
192
|
|
|
$invoice = $this->paypalService->getInvoiceService() |
193
|
|
|
->createInvoice($inputForm); |
194
|
|
|
if ($invoice instanceof Invoice) { |
195
|
|
|
$this->paypalService->getInvoiceService()->sendInvoice($invoice); |
196
|
|
|
$invoiceQR = $this->paypalService->getInvoiceService() |
197
|
|
|
->getInvoiceQRHTML($invoice); |
198
|
|
|
} |
199
|
|
|
if (isset($invoiceQR)) { |
200
|
|
|
return $this->render('paypal/authenticated/result.html.twig', [ |
201
|
|
|
'raw_result' => true, |
202
|
|
|
'result' => $invoiceQR, |
203
|
|
|
'result_id' => $invoice->getId() |
204
|
|
|
]); |
205
|
|
|
} |
206
|
|
|
return new JsonResponse('Error creating the Invoice, please check the logs'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @Route("/logged-in/invoices/{invoiceId}", name="invoices-refresh") |
211
|
|
|
* @param string $invoiceId |
212
|
|
|
* @return Response | RedirectResponse |
213
|
|
|
*/ |
214
|
|
View Code Duplication |
public function invoicesRefresh(string $invoiceId) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
if (!$this->sessionService->isActive()) { |
217
|
|
|
return $this->redirectToRoute('index'); |
218
|
|
|
} |
219
|
|
|
$invoice = $this->paypalService->getInvoiceService()->getInvoice($invoiceId); |
220
|
|
|
return $this->render('paypal/authenticated/result.html.twig', [ |
221
|
|
|
'raw_result' => false, |
222
|
|
|
'result' => $invoice, |
223
|
|
|
'result_id' => $invoice->getId() |
224
|
|
|
]); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: