1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Loevgaard\DandomainAltapayBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Loevgaard\AltaPay\Callback\Xml as XmlCallback; |
6
|
|
|
use Loevgaard\AltaPay\Entity\Transaction; |
7
|
|
|
use Loevgaard\DandomainAltapayBundle\Annotation\LogHttpTransaction; |
8
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\Payment; |
9
|
|
|
use Loevgaard\DandomainAltapayBundle\Entity\PaymentRepository; |
10
|
|
|
use Loevgaard\DandomainAltapayBundle\Exception\CallbackException; |
11
|
|
|
use Loevgaard\DandomainAltapayBundle\Exception\NotAllowedIpException; |
12
|
|
|
use Loevgaard\DandomainAltapayBundle\Exception\PaymentException; |
13
|
|
|
use Loevgaard\DandomainAltapayBundle\PsrHttpMessage\DiactorosTrait; |
14
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
15
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
16
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
17
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @Route("/callback") |
23
|
|
|
*/ |
24
|
|
|
class CallbackController extends Controller |
25
|
|
|
{ |
26
|
|
|
use DiactorosTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @Method("POST") |
30
|
|
|
* @Route("/form", name="loevgaard_dandomain_altapay_callback_form") |
31
|
|
|
* |
32
|
|
|
* @LogHttpTransaction() |
33
|
|
|
* |
34
|
|
|
* @param Request $request |
35
|
|
|
* |
36
|
|
|
* @return Response |
37
|
|
|
*/ |
38
|
|
|
public function formAction(Request $request) |
39
|
|
|
{ |
40
|
|
|
$payment = $this->handleCallback($request); |
41
|
|
|
$siteSettings = $this |
42
|
|
|
->get('loevgaard_dandomain_altapay.site_settings_provider') |
43
|
|
|
->findBySiteIdIndexedBySetting($payment->getLanguageId()); |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
return $this->render('@LoevgaardDandomainAltapay/callback/form.html.twig', [ |
48
|
|
|
'payment' => $payment, |
49
|
|
|
'siteSettings' => $siteSettings |
50
|
|
|
]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @Method("POST") |
55
|
|
|
* @Route("/ok", name="loevgaard_dandomain_altapay_callback_ok") |
56
|
|
|
* |
57
|
|
|
* @LogHttpTransaction() |
58
|
|
|
* |
59
|
|
|
* @param Request $request |
60
|
|
|
* |
61
|
|
|
* @return RedirectResponse |
62
|
|
|
*/ |
63
|
|
|
public function okAction(Request $request) |
64
|
|
|
{ |
65
|
|
|
$payment = $this->handleCallback($request); |
66
|
|
|
|
67
|
|
|
$url = $payment->getFullCallBackOkUrl() |
68
|
|
|
.'&PayApiCompleteOrderChecksum='.$request->cookies->getAlnum( |
69
|
|
|
$this->getParameter('loevgaard_dandomain_altapay.cookie_checksum_complete') |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
return $this->redirect($url); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @Method("POST") |
77
|
|
|
* @Route("/fail", name="loevgaard_dandomain_altapay_callback_fail") |
78
|
|
|
* |
79
|
|
|
* @LogHttpTransaction() |
80
|
|
|
* |
81
|
|
|
* @param Request $request |
82
|
|
|
* |
83
|
|
|
* @return Response |
84
|
|
|
*/ |
85
|
|
|
public function failAction(Request $request) |
86
|
|
|
{ |
87
|
|
|
$this->handleCallback($request); |
88
|
|
|
|
89
|
|
|
return $this->render('@LoevgaardDandomainAltapay/callback/fail.html.twig'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @Method("POST") |
94
|
|
|
* @Route("/redirect", name="loevgaard_dandomain_altapay_callback_redirect") |
95
|
|
|
* |
96
|
|
|
* @LogHttpTransaction() |
97
|
|
|
* |
98
|
|
|
* @param Request $request |
99
|
|
|
* |
100
|
|
|
* @return Response |
101
|
|
|
*/ |
102
|
|
|
public function redirectAction(Request $request) |
103
|
|
|
{ |
104
|
|
|
$this->handleCallback($request); |
105
|
|
|
|
106
|
|
|
return $this->render('@LoevgaardDandomainAltapay/callback/redirect.html.twig'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @Method("POST") |
111
|
|
|
* @Route("/open", name="loevgaard_dandomain_altapay_callback_open") |
112
|
|
|
* |
113
|
|
|
* @LogHttpTransaction() |
114
|
|
|
* |
115
|
|
|
* @param Request $request |
116
|
|
|
* |
117
|
|
|
* @return Response |
118
|
|
|
*/ |
119
|
|
|
public function openAction(Request $request) |
120
|
|
|
{ |
121
|
|
|
$this->handleCallback($request); |
122
|
|
|
|
123
|
|
|
return $this->render('@LoevgaardDandomainAltapay/callback/open.html.twig'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @Method("POST") |
128
|
|
|
* @Route("/notification", name="loevgaard_dandomain_altapay_callback_notification") |
129
|
|
|
* |
130
|
|
|
* @LogHttpTransaction() |
131
|
|
|
* |
132
|
|
|
* @param Request $request |
133
|
|
|
* |
134
|
|
|
* @return Response |
135
|
|
|
*/ |
136
|
|
|
public function notificationAction(Request $request) |
137
|
|
|
{ |
138
|
|
|
$this->handleCallback($request); |
139
|
|
|
|
140
|
|
|
return new Response('OK'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @Method("POST") |
145
|
|
|
* @Route("/verify-order", name="loevgaard_dandomain_altapay_callback_verify_order") |
146
|
|
|
* |
147
|
|
|
* @LogHttpTransaction() |
148
|
|
|
* |
149
|
|
|
* @param Request $request |
150
|
|
|
* |
151
|
|
|
* @return Response |
152
|
|
|
*/ |
153
|
|
|
public function verifyOrderAction(Request $request) |
154
|
|
|
{ |
155
|
|
|
$this->handleCallback($request); |
156
|
|
|
|
157
|
|
|
return new Response('OK'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Request $request |
162
|
|
|
* |
163
|
|
|
* @return Payment |
164
|
|
|
* |
165
|
|
|
* @throws PaymentException |
166
|
|
|
*/ |
167
|
|
|
protected function handleCallback(Request $request) |
168
|
|
|
{ |
169
|
|
|
$payment = $this->getPaymentFromRequest($request); |
170
|
|
|
$callbackHandler = $this->get('loevgaard_dandomain_altapay.altapay_callback_handler'); |
171
|
|
|
|
172
|
|
|
$psrRequest = $this->createPsrRequest($request); |
173
|
|
|
$callback = $callbackHandler->handleCallback($psrRequest); |
174
|
|
|
|
175
|
|
|
if ($callback instanceof XmlCallback) { |
176
|
|
|
$transactions = $callback->getTransactions(); |
177
|
|
|
if (isset($transactions[0])) { |
178
|
|
|
/** @var Transaction $transaction */ |
179
|
|
|
$transaction = $transactions[0]; |
180
|
|
|
|
181
|
|
|
$paymentRepository = $this->getPaymentRepository(); |
182
|
|
|
|
183
|
|
|
$payment |
184
|
|
|
->setAltapayId($transaction->getPaymentId()) |
185
|
|
|
->setCardStatus($transaction->getCardStatus()) |
186
|
|
|
->setCreditCardToken($transaction->getCreditCardToken()) |
187
|
|
|
->setCreditCardMaskedPan($transaction->getCreditCardMaskedPan()) |
188
|
|
|
->setThreeDSecureResult($transaction->getThreeDSecureResult()) |
189
|
|
|
->setLiableForChargeback($transaction->getLiableForChargeback()) |
190
|
|
|
->setBlacklistToken($transaction->getBlacklistToken()) |
191
|
|
|
->setShop($transaction->getShop()) |
192
|
|
|
->setTerminal($transaction->getTerminal()) |
193
|
|
|
->setTransactionStatus($transaction->getTransactionStatus()) |
194
|
|
|
->setReasonCode($transaction->getReasonCode()) |
195
|
|
|
->setMerchantCurrency($transaction->getMerchantCurrency()) |
196
|
|
|
->setMerchantCurrencyAlpha($transaction->getMerchantCurrencyAlpha()) |
197
|
|
|
->setCardHolderCurrency($transaction->getCardHolderCurrency()) |
198
|
|
|
->setCardHolderCurrencyAlpha($transaction->getCardHolderCurrencyAlpha()) |
199
|
|
|
->setReservedAmount($transaction->getReservedAmount()) |
200
|
|
|
->setCapturedAmount($transaction->getCapturedAmount()) |
201
|
|
|
->setRefundedAmount($transaction->getRefundedAmount()) |
202
|
|
|
->setRecurringDefaultAmount($transaction->getRecurringDefaultAmount()) |
203
|
|
|
->setCreatedDate($transaction->getCreatedDate()) |
204
|
|
|
->setUpdatedDate($transaction->getUpdatedDate()) |
205
|
|
|
->setPaymentNature($transaction->getPaymentNature()) |
206
|
|
|
->setSupportsRefunds($transaction->getPaymentNatureService()->isSupportsRefunds()) |
207
|
|
|
->setSupportsRelease($transaction->getPaymentNatureService()->isSupportsRelease()) |
208
|
|
|
->setSupportsMultipleCaptures($transaction->getPaymentNatureService()->isSupportsMultipleCaptures()) |
209
|
|
|
->setSupportsMultipleRefunds($transaction->getPaymentNatureService()->isSupportsMultipleRefunds()) |
210
|
|
|
->setFraudRiskScore($transaction->getFraudRiskScore()) |
211
|
|
|
->setFraudExplanation($transaction->getFraudExplanation()) |
212
|
|
|
; |
213
|
|
|
|
214
|
|
|
$paymentRepository->persist($payment); |
215
|
|
|
$paymentRepository->flush(); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$allowedIps = $this->container->getParameter('loevgaard_dandomain_altapay.altapay_ips'); |
220
|
|
|
if ('prod' === $this->container->get('kernel')->getEnvironment() && !in_array($request->getClientIp(), $allowedIps)) { |
221
|
|
|
throw NotAllowedIpException::create('IP `'.$request->getClientIp().'` is not an allowed IP.', $request, $payment); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $payment; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param Request $request |
229
|
|
|
* |
230
|
|
|
* @return Payment |
231
|
|
|
* |
232
|
|
|
* @throws CallbackException |
233
|
|
|
*/ |
234
|
|
|
protected function getPaymentFromRequest(Request $request) |
235
|
|
|
{ |
236
|
|
|
$paymentId = $request->cookies->getInt($this->getParameter('loevgaard_dandomain_altapay.cookie_payment_id')); |
237
|
|
|
$paymentRepository = $this->getPaymentRepository(); |
238
|
|
|
|
239
|
|
|
/** @var Payment $payment */ |
240
|
|
|
$payment = $paymentRepository->find($paymentId); |
241
|
|
|
|
242
|
|
|
if (!$payment) { |
243
|
|
|
throw new CallbackException('Payment '.$paymentId.' does not exist'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $payment; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return PaymentRepository |
251
|
|
|
*/ |
252
|
|
|
protected function getPaymentRepository() |
253
|
|
|
{ |
254
|
|
|
return $this->container->get('loevgaard_dandomain_altapay.payment_repository'); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|