|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Apache OSL-2 |
|
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SprykerEco\Yves\AmazonPay\Controller; |
|
9
|
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\AmazonpayPaymentTransfer; |
|
|
|
|
|
|
11
|
|
|
use Generated\Shared\Transfer\CheckoutErrorTransfer; |
|
|
|
|
|
|
12
|
|
|
use Generated\Shared\Transfer\CheckoutResponseTransfer; |
|
|
|
|
|
|
13
|
|
|
use Generated\Shared\Transfer\QuoteTransfer; |
|
|
|
|
|
|
14
|
|
|
use Spryker\Shared\Config\Config; |
|
15
|
|
|
use Spryker\Yves\Kernel\Controller\AbstractController; |
|
16
|
|
|
use SprykerEco\Shared\AmazonPay\AmazonPayConfig; |
|
17
|
|
|
use SprykerEco\Shared\AmazonPay\AmazonPayConstants; |
|
18
|
|
|
use SprykerEco\Yves\AmazonPay\Plugin\Provider\AmazonPayControllerProvider; |
|
19
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @method \SprykerEco\Client\AmazonPay\AmazonPayClientInterface getClient() |
|
25
|
|
|
* @method \SprykerEco\Yves\AmazonPay\AmazonPayFactory getFactory() |
|
26
|
|
|
*/ |
|
27
|
|
|
class PaymentController extends AbstractController |
|
28
|
|
|
{ |
|
29
|
|
|
public const URL_PARAM_REFERENCE_ID = 'reference_id'; |
|
30
|
|
|
public const URL_PARAM_SELLER_ID = 'seller_id'; |
|
31
|
|
|
public const URL_PARAM_ACCESS_TOKEN = 'access_token'; |
|
32
|
|
|
public const URL_PARAM_SHIPMENT_METHOD_ID = 'shipment_method_id'; |
|
33
|
|
|
public const QUOTE_TRANSFER = 'quoteTransfer'; |
|
34
|
|
|
public const SHIPMENT_METHODS = 'shipmentMethods'; |
|
35
|
|
|
public const SELECTED_SHIPMENT_METHOD_ID = 'selectedShipmentMethodId'; |
|
36
|
|
|
public const AMAZONPAY_CONFIG = 'amazonpayConfig'; |
|
37
|
|
|
public const IS_ASYNCHRONOUS = 'isAsynchronous'; |
|
38
|
|
|
public const CART_ITEMS = 'cartItems'; |
|
39
|
|
|
public const SUCCESS = 'success'; |
|
40
|
|
|
public const ERROR_AMAZONPAY_PAYMENT_FAILED = 'amazonpay.payment.failed'; |
|
41
|
|
|
public const IS_AMAZON_PAYMENT_INVALID = 'isAmazonPaymentInvalid'; |
|
42
|
|
|
public const ADDRESS_BOOK_MODE = 'addressBookMode'; |
|
43
|
|
|
public const ORDER_REFERENCE = 'orderReferenceId'; |
|
44
|
|
|
|
|
45
|
|
|
public const PSD2_DATA = 'psd2Data'; |
|
46
|
|
|
public const PSD2_DATA_KEY_AJAX_ENDPOINT = 'psd2AjaxEndpoint'; |
|
47
|
|
|
public const PSD2_DATA_KEY_SELLER_ID = 'amazonSellerId'; |
|
48
|
|
|
public const PSD2_DATA_KEY_AMAZON_ORDER_REFERENCE_ID = 'amazonOrderReferenceId'; |
|
49
|
|
|
public const PSD2_DATA_KEY_AMAZON_FAILURE_URL = 'amazonFailureUrl'; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
53
|
|
|
* |
|
54
|
|
|
* @return \Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
|
55
|
|
|
*/ |
|
56
|
|
|
public function checkoutAction(Request $request) |
|
57
|
|
|
{ |
|
58
|
|
|
$response = $this->executeCheckoutAction($request); |
|
59
|
|
|
|
|
60
|
|
|
if (!is_array($response)) { |
|
|
|
|
|
|
61
|
|
|
return $response; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->view($response, [], '@AmazonPay/views/checkout/checkout.twig'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
69
|
|
|
* |
|
70
|
|
|
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function executeCheckoutAction(Request $request) |
|
73
|
|
|
{ |
|
74
|
|
|
$quoteTransfer = $this->getFactory() |
|
75
|
|
|
->getQuoteClient() |
|
76
|
|
|
->getQuote(); |
|
77
|
|
|
|
|
78
|
|
|
if (!$this->isAllowedCheckout($quoteTransfer) || !$this->isRequestComplete($request)) { |
|
79
|
|
|
$this->addAmazonPayErrorFromQuote($quoteTransfer); |
|
80
|
|
|
|
|
81
|
|
|
return $this->buildRedirectInternalResponse(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->storeAmazonPaymentIntoQuote($request, $quoteTransfer); |
|
85
|
|
|
|
|
86
|
|
|
$data = [ |
|
87
|
|
|
static::QUOTE_TRANSFER => $quoteTransfer, |
|
88
|
|
|
static::CART_ITEMS => $this->getCartItems($quoteTransfer), |
|
89
|
|
|
static::AMAZONPAY_CONFIG => $this->getAmazonPayConfig(), |
|
90
|
|
|
static::PSD2_DATA => $this->preparePSD2Data($quoteTransfer), |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
if ($this->isAmazonPaymentInvalid($quoteTransfer)) { |
|
94
|
|
|
$data[static::ORDER_REFERENCE] = $this->getAmazonPaymentOrderReferenceId($quoteTransfer); |
|
95
|
|
|
$data[static::ADDRESS_BOOK_MODE] = AmazonPayConfig::DISPLAY_MODE_READONLY; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($quoteTransfer->getAmazonpayPayment()->getOrderReferenceStatus()) { |
|
99
|
|
|
$data[static::ADDRESS_BOOK_MODE] = AmazonPayConfig::DISPLAY_MODE_READONLY; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $data; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
107
|
|
|
* |
|
108
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setOrderReferenceAction(Request $request) |
|
111
|
|
|
{ |
|
112
|
|
|
$quoteTransfer = $this->getFactory() |
|
113
|
|
|
->getQuoteClient() |
|
114
|
|
|
->getQuote(); |
|
115
|
|
|
|
|
116
|
|
|
if (!$this->isAmazonPayment($quoteTransfer)) { |
|
117
|
|
|
return $this->buildRedirectInternalResponse(); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
$quoteTransfer->getAmazonpayPayment() |
|
121
|
|
|
->setOrderReferenceId( |
|
122
|
|
|
$request->request->get(static::URL_PARAM_REFERENCE_ID) |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
$this->saveQuote($quoteTransfer); |
|
126
|
|
|
|
|
127
|
|
|
return $this->createJsonResponse([static::SUCCESS => true]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
132
|
|
|
* |
|
133
|
|
|
* @return \Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getShipmentMethodsAction(Request $request) |
|
136
|
|
|
{ |
|
137
|
|
|
$response = $this->executeShipmentMethodsAction($request); |
|
138
|
|
|
|
|
139
|
|
|
if (!is_array($response)) { |
|
|
|
|
|
|
140
|
|
|
return $response; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $this->view($response, [], '@AmazonPay/views/get-shipment-methods/get-shipment-methods.twig'); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
148
|
|
|
* |
|
149
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
150
|
|
|
*/ |
|
151
|
|
|
public function confirmPurchaseAction(Request $request): Response |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
|
|
$quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote(); |
|
154
|
|
|
$quoteTransfer = $this->getClient()->confirmPurchase($quoteTransfer); |
|
155
|
|
|
|
|
156
|
|
|
$amazonpayPaymentTransfer = $quoteTransfer->getAmazonpayPayment(); |
|
157
|
|
|
|
|
158
|
|
|
if (!$amazonpayPaymentTransfer->getResponseHeader()->getIsSuccess() |
|
159
|
|
|
&& $amazonpayPaymentTransfer->getResponseHeader()->getConstraints()->count() |
|
160
|
|
|
) { |
|
161
|
|
|
$this->addErrorMessage($amazonpayPaymentTransfer->getResponseHeader()->getErrorMessage()); |
|
162
|
|
|
$payload = [ |
|
163
|
|
|
static::SUCCESS => false, |
|
164
|
|
|
'url' => $this->getApplication()->url(AmazonPayControllerProvider::CHECKOUT, [ |
|
165
|
|
|
static::URL_PARAM_REFERENCE_ID => $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId(), |
|
166
|
|
|
static::URL_PARAM_ACCESS_TOKEN => $quoteTransfer->getAmazonpayPayment()->getAddressConsentToken(), |
|
167
|
|
|
]), |
|
168
|
|
|
]; |
|
169
|
|
|
|
|
170
|
|
|
return $this->createJsonResponse($payload, 302); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
$this->saveQuote($quoteTransfer); |
|
174
|
|
|
|
|
175
|
|
|
if (!$this->isOrderStatusOpen($quoteTransfer)) { |
|
176
|
|
|
return $this->createJsonResponse([static::SUCCESS => false], 400); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return $this->createJsonResponse([static::SUCCESS => true]); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
184
|
|
|
* |
|
185
|
|
|
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
|
186
|
|
|
*/ |
|
187
|
|
|
protected function executeShipmentMethodsAction(Request $request) |
|
|
|
|
|
|
188
|
|
|
{ |
|
189
|
|
|
$quoteTransfer = $this->getFactory() |
|
190
|
|
|
->getQuoteClient() |
|
191
|
|
|
->getQuote(); |
|
192
|
|
|
|
|
193
|
|
|
if (!$this->isAmazonPayment($quoteTransfer)) { |
|
194
|
|
|
$this->addAmazonPayErrorFromQuote($quoteTransfer); |
|
195
|
|
|
|
|
196
|
|
|
return $this->buildRedirectInternalResponse(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$quoteTransfer = $this->getClient() |
|
200
|
|
|
->addSelectedAddressToQuote($quoteTransfer); |
|
201
|
|
|
$this->saveQuote($quoteTransfer); |
|
202
|
|
|
$shipmentMethods = $this->getFactory() |
|
203
|
|
|
->getShipmentClient() |
|
204
|
|
|
->getAvailableMethods($quoteTransfer); |
|
205
|
|
|
|
|
206
|
|
|
return [ |
|
207
|
|
|
static::SELECTED_SHIPMENT_METHOD_ID => $this->getCurrentShipmentMethodId($quoteTransfer), |
|
208
|
|
|
static::SHIPMENT_METHODS => $shipmentMethods->getMethods(), |
|
209
|
|
|
static::IS_AMAZON_PAYMENT_INVALID => $this->isAmazonPaymentInvalid($quoteTransfer), |
|
210
|
|
|
]; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
215
|
|
|
* |
|
216
|
|
|
* @return \Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
|
217
|
|
|
*/ |
|
218
|
|
|
public function updateShipmentMethodAction(Request $request) |
|
219
|
|
|
{ |
|
220
|
|
|
$response = $this->executeShipmentMethodAction($request); |
|
221
|
|
|
|
|
222
|
|
|
if (!is_array($response)) { |
|
|
|
|
|
|
223
|
|
|
return $response; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $this->view($response, [], '@AmazonPay/views/shipment-method/shipment-method.twig'); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
231
|
|
|
* |
|
232
|
|
|
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
|
233
|
|
|
*/ |
|
234
|
|
|
protected function executeShipmentMethodAction(Request $request) |
|
235
|
|
|
{ |
|
236
|
|
|
$quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote(); |
|
237
|
|
|
|
|
238
|
|
|
if (!$this->isAmazonPayment($quoteTransfer)) { |
|
239
|
|
|
$this->addAmazonPayErrorFromQuote($quoteTransfer); |
|
240
|
|
|
|
|
241
|
|
|
return $this->buildRedirectInternalResponse(); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
$quoteTransfer->getShipment()->setShipmentSelection( |
|
245
|
|
|
$request->request->get(static::URL_PARAM_SHIPMENT_METHOD_ID) |
|
246
|
|
|
); |
|
247
|
|
|
$quoteTransfer = $this->getClient() |
|
248
|
|
|
->addSelectedShipmentMethodToQuote($quoteTransfer); |
|
249
|
|
|
$quoteTransfer = $this->getFactory() |
|
250
|
|
|
->getCalculationClient()->recalculate($quoteTransfer); |
|
251
|
|
|
$this->saveQuote($quoteTransfer); |
|
252
|
|
|
|
|
253
|
|
|
return [ |
|
254
|
|
|
static::QUOTE_TRANSFER => $quoteTransfer, |
|
255
|
|
|
]; |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
260
|
|
|
* |
|
261
|
|
|
* @return \Spryker\Yves\Kernel\View\View|\Symfony\Component\HttpFoundation\RedirectResponse |
|
262
|
|
|
*/ |
|
263
|
|
|
public function successAction(Request $request) |
|
264
|
|
|
{ |
|
265
|
|
|
$response = $this->executeSuccessAction($request); |
|
266
|
|
|
|
|
267
|
|
|
$quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote(); |
|
268
|
|
|
|
|
269
|
|
|
if ($quoteTransfer->getTotals() === null) { |
|
270
|
|
|
return $this->view($response, [], '@AmazonPay/views/success/success.twig'); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
$checkoutResponseTransfer = $this->getFactory()->getCheckoutClient()->placeOrder($quoteTransfer); |
|
274
|
|
|
|
|
275
|
|
|
if (!$checkoutResponseTransfer->getIsSuccess()) { |
|
276
|
|
|
return $this->redirectResponseInternal(AmazonPayControllerProvider::CHECKOUT, [ |
|
277
|
|
|
static::URL_PARAM_REFERENCE_ID => $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId(), |
|
278
|
|
|
static::URL_PARAM_ACCESS_TOKEN => $quoteTransfer->getAmazonpayPayment()->getAddressConsentToken(), |
|
279
|
|
|
]); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
$quoteTransfer = $this->getClient()->authorizeOrder($quoteTransfer); |
|
283
|
|
|
|
|
284
|
|
|
$this->saveQuote($quoteTransfer); |
|
285
|
|
|
|
|
286
|
|
|
$state = $quoteTransfer->getAmazonpayPayment()->getAuthorizationDetails()->getAuthorizationStatus()->getState(); |
|
287
|
|
|
$reasonCode = $quoteTransfer->getAmazonpayPayment()->getAuthorizationDetails()->getAuthorizationStatus()->getReasonCode(); |
|
288
|
|
|
|
|
289
|
|
|
if ($state === AmazonPayConfig::STATUS_TRANSACTION_TIMED_OUT |
|
290
|
|
|
&& $reasonCode === AmazonPayConfig::REASON_CODE_TRANSACTION_TIMED_OUT |
|
291
|
|
|
&& !$this->getAmazonPayConfig()->getCaptureNow() |
|
292
|
|
|
) { |
|
293
|
|
|
$quoteTransfer->getAmazonpayPayment()->setIsReauthorizingAsync(true); |
|
294
|
|
|
$quoteTransfer = $this->getClient()->authorizeOrder($quoteTransfer); |
|
295
|
|
|
$state = $quoteTransfer->getAmazonpayPayment()->getAuthorizationDetails()->getAuthorizationStatus()->getState(); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
if ($state === AmazonPayConfig::STATUS_TRANSACTION_TIMED_OUT || $state === AmazonPayConfig::STATUS_DECLINED) { |
|
299
|
|
|
return $this->redirectResponseInternal(AmazonPayControllerProvider::PAYMENT_FAILED); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
if (!$this->isAuthSucceeded($state) || $checkoutResponseTransfer->getIsSuccess() === false) { |
|
303
|
|
|
return $this->redirectResponseInternal(AmazonPayControllerProvider::CHECKOUT, [ |
|
304
|
|
|
static::URL_PARAM_REFERENCE_ID => $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId(), |
|
305
|
|
|
static::URL_PARAM_ACCESS_TOKEN => $quoteTransfer->getAmazonpayPayment()->getAddressConsentToken(), |
|
306
|
|
|
]); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
$this->getFactory()->getCustomerClient()->markCustomerAsDirty(); |
|
310
|
|
|
$this->getFactory()->getCartClient()->clearQuote(); |
|
311
|
|
|
|
|
312
|
|
|
return $this->view($response, [], '@AmazonPay/views/success/success.twig'); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
317
|
|
|
* |
|
318
|
|
|
* @return array|\Symfony\Component\HttpFoundation\RedirectResponse |
|
319
|
|
|
*/ |
|
320
|
|
|
protected function executeSuccessAction(Request $request) |
|
|
|
|
|
|
321
|
|
|
{ |
|
322
|
|
|
return [ |
|
323
|
|
|
static::IS_ASYNCHRONOUS => $this->isAsynchronous(), |
|
324
|
|
|
static::AMAZONPAY_CONFIG => $this->getAmazonPayConfig(), |
|
325
|
|
|
]; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
330
|
|
|
* |
|
331
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
|
332
|
|
|
*/ |
|
333
|
|
|
public function paymentFailedAction(Request $request): Response |
|
|
|
|
|
|
334
|
|
|
{ |
|
335
|
|
|
$this->getFactory()->getMessengerClient()->addErrorMessage(static::ERROR_AMAZONPAY_PAYMENT_FAILED); |
|
336
|
|
|
|
|
337
|
|
|
$this->clearAmazonpayQuoteData(); |
|
338
|
|
|
|
|
339
|
|
|
return $this->buildRedirectInternalResponse(); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
344
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
345
|
|
|
* |
|
346
|
|
|
* @return void |
|
347
|
|
|
*/ |
|
348
|
|
|
protected function storeAmazonPaymentIntoQuote(Request $request, QuoteTransfer $quoteTransfer) |
|
349
|
|
|
{ |
|
350
|
|
|
$amazonPaymentTransfer = $this->buildAmazonPaymentTransfer($request, $quoteTransfer); |
|
351
|
|
|
|
|
352
|
|
|
$quoteTransfer->setAmazonpayPayment($amazonPaymentTransfer); |
|
353
|
|
|
$quoteTransfer = $this->getClient() |
|
354
|
|
|
->handleCartWithAmazonPay($quoteTransfer); |
|
355
|
|
|
$this->saveQuote($quoteTransfer); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
360
|
|
|
* |
|
361
|
|
|
* @return void |
|
362
|
|
|
*/ |
|
363
|
|
|
protected function saveQuote(QuoteTransfer $quoteTransfer) |
|
364
|
|
|
{ |
|
365
|
|
|
$this->getFactory() |
|
366
|
|
|
->getQuoteClient() |
|
367
|
|
|
->setQuote($quoteTransfer); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
372
|
|
|
* |
|
373
|
|
|
* @return int|null |
|
374
|
|
|
*/ |
|
375
|
|
|
protected function getCurrentShipmentMethodId(QuoteTransfer $quoteTransfer) |
|
376
|
|
|
{ |
|
377
|
|
|
if ($quoteTransfer->getShipment() === null || $quoteTransfer->getShipment()->getMethod() === null) { |
|
378
|
|
|
return null; |
|
379
|
|
|
} |
|
380
|
|
|
|
|
381
|
|
|
return $quoteTransfer->getShipment()->getMethod()->getIdShipmentMethod(); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
386
|
|
|
* |
|
387
|
|
|
* @return \ArrayObject|\Generated\Shared\Transfer\ItemTransfer[] |
|
388
|
|
|
*/ |
|
389
|
|
|
protected function getCartItems(QuoteTransfer $quoteTransfer) |
|
390
|
|
|
{ |
|
391
|
|
|
return $quoteTransfer->getItems(); |
|
392
|
|
|
} |
|
393
|
|
|
|
|
394
|
|
|
/** |
|
395
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
396
|
|
|
* |
|
397
|
|
|
* @return bool |
|
398
|
|
|
*/ |
|
399
|
|
|
protected function isRequestComplete(Request $request) |
|
400
|
|
|
{ |
|
401
|
|
|
return ( |
|
402
|
|
|
$request->query->get(static::URL_PARAM_REFERENCE_ID) !== null && |
|
403
|
|
|
$request->query->get(static::URL_PARAM_ACCESS_TOKEN) !== null |
|
404
|
|
|
); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
409
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
410
|
|
|
* |
|
411
|
|
|
* @return \Generated\Shared\Transfer\AmazonpayPaymentTransfer |
|
412
|
|
|
*/ |
|
413
|
|
|
protected function buildAmazonPaymentTransfer(Request $request, QuoteTransfer $quoteTransfer) |
|
414
|
|
|
{ |
|
415
|
|
|
$amazonPaymentTransfer = $quoteTransfer->getAmazonpayPayment() ?: new AmazonpayPaymentTransfer(); |
|
416
|
|
|
|
|
417
|
|
|
$amazonPaymentTransfer->setOrderReferenceId($request->query->get(static::URL_PARAM_REFERENCE_ID)); |
|
418
|
|
|
$amazonPaymentTransfer->setAddressConsentToken($request->query->get(static::URL_PARAM_ACCESS_TOKEN)); |
|
419
|
|
|
$amazonPaymentTransfer->setFailureMFARedirectUrl($this->getApplication()->url(AmazonPayControllerProvider::CHECKOUT, [ |
|
420
|
|
|
static::URL_PARAM_REFERENCE_ID => $request->query->get(static::URL_PARAM_REFERENCE_ID), |
|
421
|
|
|
static::URL_PARAM_ACCESS_TOKEN => $request->query->get(static::URL_PARAM_ACCESS_TOKEN), |
|
422
|
|
|
])); |
|
423
|
|
|
$amazonPaymentTransfer->setSuccessMFARedirectUrl($this->getApplication()->url(AmazonPayControllerProvider::SUCCESS)); |
|
424
|
|
|
|
|
425
|
|
|
return $amazonPaymentTransfer; |
|
426
|
|
|
} |
|
427
|
|
|
|
|
428
|
|
|
/** |
|
429
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
430
|
|
|
* |
|
431
|
|
|
* @return bool |
|
432
|
|
|
*/ |
|
433
|
|
|
protected function isAllowedCheckout(QuoteTransfer $quoteTransfer) |
|
434
|
|
|
{ |
|
435
|
|
|
return $quoteTransfer->getTotals() !== null; |
|
436
|
|
|
} |
|
437
|
|
|
|
|
438
|
|
|
/** |
|
439
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
440
|
|
|
* |
|
441
|
|
|
* @return bool |
|
442
|
|
|
*/ |
|
443
|
|
|
protected function isLogoutRedirect(QuoteTransfer $quoteTransfer) |
|
444
|
|
|
{ |
|
445
|
|
|
if ($this->isAmazonPaymentInvalid($quoteTransfer)) { |
|
446
|
|
|
return false; |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
if ($this->getAmazonPayConfig()->getCaptureNow() && |
|
450
|
|
|
$quoteTransfer->getAmazonpayPayment() !== null |
|
451
|
|
|
&& $quoteTransfer->getAmazonpayPayment()->getResponseHeader() !== null |
|
452
|
|
|
&& !$quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getIsSuccess() |
|
453
|
|
|
) { |
|
454
|
|
|
return true; |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
return false; |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
462
|
|
|
* |
|
463
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
|
464
|
|
|
*/ |
|
465
|
|
|
protected function buildRedirectExternalResponse(Request $request) |
|
466
|
|
|
{ |
|
467
|
|
|
if ($request->headers->get('Referer') === null) { |
|
|
|
|
|
|
468
|
|
|
return $this->buildRedirectInternalResponse(); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
return $this->redirectResponseExternal($request->headers->get('Referer')); |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponseTransfer |
|
476
|
|
|
* |
|
477
|
|
|
* @return void |
|
478
|
|
|
*/ |
|
479
|
|
|
protected function setCheckoutErrorMessages(CheckoutResponseTransfer $checkoutResponseTransfer) |
|
480
|
|
|
{ |
|
481
|
|
|
foreach ($checkoutResponseTransfer->getErrors() as $checkoutErrorTransfer) { |
|
482
|
|
|
$this->addErrorMessage( |
|
483
|
|
|
$this->translateCheckoutErrorMessage($checkoutErrorTransfer) |
|
484
|
|
|
); |
|
485
|
|
|
} |
|
486
|
|
|
} |
|
487
|
|
|
|
|
488
|
|
|
/** |
|
489
|
|
|
* @param \Generated\Shared\Transfer\CheckoutErrorTransfer $checkoutErrorTransfer |
|
490
|
|
|
* |
|
491
|
|
|
* @return string |
|
492
|
|
|
*/ |
|
493
|
|
|
protected function translateCheckoutErrorMessage(CheckoutErrorTransfer $checkoutErrorTransfer): string |
|
494
|
|
|
{ |
|
495
|
|
|
$checkoutErrorMessage = $checkoutErrorTransfer->getMessage(); |
|
496
|
|
|
|
|
497
|
|
|
return $this->getFactory()->getGlossaryStorageClient()->translate( |
|
498
|
|
|
$checkoutErrorMessage, |
|
499
|
|
|
$this->getLocale(), |
|
500
|
|
|
$checkoutErrorTransfer->getParameters() |
|
501
|
|
|
) ?: $checkoutErrorMessage; |
|
502
|
|
|
} |
|
503
|
|
|
|
|
504
|
|
|
/** |
|
505
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
506
|
|
|
* |
|
507
|
|
|
* @return void |
|
508
|
|
|
*/ |
|
509
|
|
|
protected function addAmazonPayErrorFromQuote(QuoteTransfer $quoteTransfer) |
|
510
|
|
|
{ |
|
511
|
|
|
if ($quoteTransfer->getAmazonpayPayment() === null |
|
512
|
|
|
|| $quoteTransfer->getAmazonpayPayment()->getResponseHeader() === null |
|
513
|
|
|
|| $quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getErrorMessage() === null) { |
|
514
|
|
|
return; |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
$this->addErrorMessage( |
|
518
|
|
|
$quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getErrorMessage() |
|
519
|
|
|
); |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
/** |
|
523
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
524
|
|
|
* |
|
525
|
|
|
* @return bool |
|
526
|
|
|
*/ |
|
527
|
|
|
protected function isAmazonPayment(QuoteTransfer $quoteTransfer) |
|
528
|
|
|
{ |
|
529
|
|
|
return $quoteTransfer->getAmazonpayPayment() !== null; |
|
530
|
|
|
} |
|
531
|
|
|
|
|
532
|
|
|
/** |
|
533
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse |
|
534
|
|
|
*/ |
|
535
|
|
|
protected function buildRedirectInternalResponse() |
|
536
|
|
|
{ |
|
537
|
|
|
return $this->redirectResponseInternal($this->getPaymentRejectRoute()); |
|
538
|
|
|
} |
|
539
|
|
|
|
|
540
|
|
|
/** |
|
541
|
|
|
* @return string |
|
542
|
|
|
*/ |
|
543
|
|
|
protected function getPaymentRejectRoute() |
|
544
|
|
|
{ |
|
545
|
|
|
return Config::get(AmazonPayConstants::PAYMENT_REJECT_ROUTE); |
|
546
|
|
|
} |
|
547
|
|
|
|
|
548
|
|
|
/** |
|
549
|
|
|
* @return bool |
|
550
|
|
|
*/ |
|
551
|
|
|
protected function isAsynchronous() |
|
552
|
|
|
{ |
|
553
|
|
|
return $this->getAmazonPayConfig()->getAuthTransactionTimeout() > 0 |
|
554
|
|
|
&& !$this->getAmazonPayConfig()->getCaptureNow(); |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
/** |
|
558
|
|
|
* @return \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface |
|
559
|
|
|
*/ |
|
560
|
|
|
protected function getAmazonPayConfig() |
|
561
|
|
|
{ |
|
562
|
|
|
return $this->getFactory()->createAmazonPayConfig(); |
|
563
|
|
|
} |
|
564
|
|
|
|
|
565
|
|
|
/** |
|
566
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
567
|
|
|
* |
|
568
|
|
|
* @return bool |
|
569
|
|
|
*/ |
|
570
|
|
|
protected function isAmazonPaymentInvalid(QuoteTransfer $quoteTransfer) |
|
571
|
|
|
{ |
|
572
|
|
|
if ($quoteTransfer->getAmazonpayPayment()->getResponseHeader() !== null |
|
573
|
|
|
&& $quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getIsInvalidPaymentMethod()) { |
|
574
|
|
|
return true; |
|
575
|
|
|
} |
|
576
|
|
|
|
|
577
|
|
|
return false; |
|
578
|
|
|
} |
|
579
|
|
|
|
|
580
|
|
|
/** |
|
581
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
582
|
|
|
* |
|
583
|
|
|
* @return string|null |
|
584
|
|
|
*/ |
|
585
|
|
|
protected function getAmazonPaymentOrderReferenceId(QuoteTransfer $quoteTransfer) |
|
586
|
|
|
{ |
|
587
|
|
|
if ($quoteTransfer->getAmazonpayPayment() !== null && $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId() !== null) { |
|
588
|
|
|
return $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId(); |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
return null; |
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
/** |
|
595
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
596
|
|
|
* |
|
597
|
|
|
* @return array |
|
598
|
|
|
*/ |
|
599
|
|
|
protected function preparePSD2Data(QuoteTransfer $quoteTransfer): array |
|
600
|
|
|
{ |
|
601
|
|
|
return [ |
|
602
|
|
|
static::PSD2_DATA_KEY_AJAX_ENDPOINT => $this->getApplication()->path(AmazonPayControllerProvider::CONFIRM_PURCHASE), |
|
603
|
|
|
static::PSD2_DATA_KEY_SELLER_ID => $this->getAmazonPayConfig()->getSellerId(), |
|
604
|
|
|
static::PSD2_DATA_KEY_AMAZON_ORDER_REFERENCE_ID => $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId(), |
|
605
|
|
|
static::PSD2_DATA_KEY_AMAZON_FAILURE_URL => $this->getApplication()->path(AmazonPayControllerProvider::PAYMENT_FAILED), |
|
606
|
|
|
]; |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
/** |
|
610
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
611
|
|
|
* |
|
612
|
|
|
* @return void |
|
613
|
|
|
*/ |
|
614
|
|
|
protected function clearAmazonpayQuoteData(): void |
|
615
|
|
|
{ |
|
616
|
|
|
$quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote(); |
|
617
|
|
|
|
|
618
|
|
|
$amazonpayPaymentTransfer = new AmazonpayPaymentTransfer(); |
|
619
|
|
|
|
|
620
|
|
|
$quoteTransfer->setAmazonpayPayment($amazonpayPaymentTransfer); |
|
621
|
|
|
|
|
622
|
|
|
$this->getFactory()->getQuoteClient()->setQuote($quoteTransfer); |
|
623
|
|
|
} |
|
624
|
|
|
|
|
625
|
|
|
/** |
|
626
|
|
|
* @param string $state |
|
627
|
|
|
* |
|
628
|
|
|
* @return bool |
|
629
|
|
|
*/ |
|
630
|
|
|
protected function isAuthSucceeded(string $state): bool |
|
631
|
|
|
{ |
|
632
|
|
|
return in_array($state, [ |
|
633
|
|
|
AmazonPayConfig::STATUS_OPEN, |
|
634
|
|
|
AmazonPayConfig::STATUS_PENDING, |
|
635
|
|
|
AmazonPayConfig::STATUS_CLOSED, |
|
636
|
|
|
]); |
|
637
|
|
|
} |
|
638
|
|
|
|
|
639
|
|
|
/** |
|
640
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
641
|
|
|
* |
|
642
|
|
|
* @return bool |
|
643
|
|
|
*/ |
|
644
|
|
|
protected function isOrderStatusOpen(QuoteTransfer $quoteTransfer): bool |
|
645
|
|
|
{ |
|
646
|
|
|
return $quoteTransfer->getAmazonpayPayment()->getOrderReferenceStatus()->getState() === AmazonPayConfig::STATUS_OPEN; |
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
/** |
|
650
|
|
|
* @param array $payload |
|
651
|
|
|
* @param int $statusCode |
|
652
|
|
|
* |
|
653
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse |
|
654
|
|
|
*/ |
|
655
|
|
|
protected function createJsonResponse(array $payload, int $statusCode = 200): JsonResponse |
|
656
|
|
|
{ |
|
657
|
|
|
return new JsonResponse($payload, $statusCode); |
|
658
|
|
|
} |
|
659
|
|
|
} |
|
660
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths