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