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