1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Controller; |
4
|
|
|
|
5
|
|
|
use FOS\RestBundle\Controller\Annotations; |
6
|
|
|
use FOS\RestBundle\Controller\FOSRestController; |
7
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Payment; |
8
|
|
|
use Loevgaard\DandomainAltapayBundle\Handler\PaymentHandler; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
|
12
|
|
|
class ApiController extends FOSRestController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @Annotations\Get("/api/payment/{id}/capture") |
16
|
|
|
* |
17
|
|
|
* @param Request $request |
18
|
|
|
* @param string|int $id Can be both altapay id or order id |
19
|
|
|
* |
20
|
|
|
* @return Response |
21
|
|
|
*/ |
22
|
|
|
public function paymentCaptureAction(Request $request, $id) |
23
|
|
|
{ |
24
|
|
|
$data = [ |
25
|
|
|
'error' => false, |
26
|
|
|
'captured_amount' => null, |
27
|
|
|
'refunded_amount' => null, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
$payment = $this->findPaymentByOrderIdOrAltapayId($id); |
31
|
|
|
$paymentHandler = $this->getPaymentHandler(); |
32
|
|
|
$res = $paymentHandler->capture($payment, $request->query->get('amount')); |
33
|
|
|
|
34
|
|
|
if ($res->isSuccessful()) { |
35
|
|
|
$transactions = $res->getTransactions(); |
36
|
|
|
if (count($transactions)) { |
37
|
|
|
$data['captured_amount'] = $transactions[0]->getCapturedAmount(); |
38
|
|
|
$data['refunded_amount'] = $transactions[0]->getRefundedAmount(); |
39
|
|
|
} |
40
|
|
|
} else { |
41
|
|
|
$data['error'] = true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$view = $this->view($data); |
45
|
|
|
$view->setTemplate('@LoevgaardDandomainAltapay/api/payment_capture.html.twig'); |
46
|
|
|
|
47
|
|
|
return $this->handleView($view); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @Annotations\Post("/api/payment/{id}/refund") |
52
|
|
|
* |
53
|
|
|
* @param Request $request |
54
|
|
|
* @param string|int $id Can be both altapay id or order id |
55
|
|
|
* |
56
|
|
|
* @return Response |
57
|
|
|
*/ |
58
|
|
|
public function paymentRefundAction(Request $request, $id) |
59
|
|
|
{ |
60
|
|
|
$data = [ |
61
|
|
|
'error' => false, |
62
|
|
|
'captured_amount' => null, |
63
|
|
|
'refunded_amount' => null, |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
$payment = $this->findPaymentByOrderIdOrAltapayId($id); |
67
|
|
|
$paymentHandler = $this->getPaymentHandler(); |
68
|
|
|
$res = $paymentHandler->refund($payment, $request->query->get('amount')); |
69
|
|
|
|
70
|
|
|
if ($res->isSuccessful()) { |
71
|
|
|
$transactions = $res->getTransactions(); |
72
|
|
|
if (count($transactions)) { |
73
|
|
|
$data['captured_amount'] = $transactions[0]->getCapturedAmount(); |
74
|
|
|
$data['refunded_amount'] = $transactions[0]->getRefundedAmount(); |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
$data['error'] = true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$view = $this->view($data); |
81
|
|
|
$view->setTemplate('@LoevgaardDandomainAltapay/api/payment_capture.html.twig'); |
82
|
|
|
|
83
|
|
|
return $this->handleView($view); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string|int $id |
88
|
|
|
* |
89
|
|
|
* @return Payment |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
private function findPaymentByOrderIdOrAltapayId($id): Payment |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$paymentRepository = $this->get('loevgaard_dandomain_altapay.payment_repository'); |
94
|
|
|
$payment = $paymentRepository->findByOrderIdOrAltapayId($id); |
95
|
|
|
if (!$payment) { |
96
|
|
|
throw $this->createNotFoundException(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $payment; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return PaymentHandler |
104
|
|
|
*/ |
105
|
|
|
private function getPaymentHandler(): PaymentHandler |
106
|
|
|
{ |
107
|
|
|
return $this->get('loevgaard_dandomain_altapay.payment_handler'); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
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.