|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* MIT License |
|
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SprykerEco\Zed\Payone\Business\Payment\Hook; |
|
9
|
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\CheckoutErrorTransfer; |
|
|
|
|
|
|
11
|
|
|
use Generated\Shared\Transfer\CheckoutResponseTransfer; |
|
|
|
|
|
|
12
|
|
|
use Generated\Shared\Transfer\OrderTransfer; |
|
|
|
|
|
|
13
|
|
|
use Generated\Shared\Transfer\QuoteTransfer; |
|
|
|
|
|
|
14
|
|
|
use Generated\Shared\Transfer\TotalsTransfer; |
|
|
|
|
|
|
15
|
|
|
use Orm\Zed\Payone\Persistence\SpyPaymentPayone; |
|
|
|
|
|
|
16
|
|
|
use SprykerEco\Shared\Payone\PayoneApiConstants; |
|
17
|
|
|
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainerInterface; |
|
18
|
|
|
use SprykerEco\Zed\Payone\Business\Api\Response\Container\AbstractResponseContainer; |
|
19
|
|
|
use SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer; |
|
20
|
|
|
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface; |
|
21
|
|
|
use SprykerEco\Zed\Payone\Business\Payment\MethodMapper\KlarnaPaymentMapper; |
|
22
|
|
|
use SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface; |
|
23
|
|
|
use SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface; |
|
24
|
|
|
use SprykerEco\Zed\Payone\Business\Payment\RequestSender\PayoneBaseAuthorizeSenderInterface; |
|
25
|
|
|
use SprykerEco\Zed\Payone\PayoneConfig; |
|
26
|
|
|
use SprykerEco\Zed\Payone\Persistence\PayoneRepositoryInterface; |
|
27
|
|
|
|
|
28
|
|
|
class PostSaveHook implements PostSaveHookInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var \SprykerEco\Zed\Payone\Persistence\PayoneRepositoryInterface |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $payoneRepository; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $paymentMapperReader; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $payoneRequestProductDataMapper; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var \SprykerEco\Zed\Payone\Business\Payment\RequestSender\PayoneBaseAuthorizeSenderInterface |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $baseAuthorizeSender; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param \SprykerEco\Zed\Payone\Persistence\PayoneRepositoryInterface $payoneRepository |
|
52
|
|
|
* @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface $paymentMapperReader |
|
53
|
|
|
* @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface $payoneRequestProductDataMapper |
|
54
|
|
|
* @param \SprykerEco\Zed\Payone\Business\Payment\RequestSender\PayoneBaseAuthorizeSenderInterface $baseAuthorizeSender |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct( |
|
57
|
|
|
PayoneRepositoryInterface $payoneRepository, |
|
58
|
|
|
PaymentMapperReaderInterface $paymentMapperReader, |
|
59
|
|
|
PayoneRequestProductDataMapperInterface $payoneRequestProductDataMapper, |
|
60
|
|
|
PayoneBaseAuthorizeSenderInterface $baseAuthorizeSender |
|
61
|
|
|
) { |
|
62
|
|
|
$this->payoneRepository = $payoneRepository; |
|
63
|
|
|
$this->paymentMapperReader = $paymentMapperReader; |
|
64
|
|
|
$this->payoneRequestProductDataMapper = $payoneRequestProductDataMapper; |
|
65
|
|
|
$this->baseAuthorizeSender = $baseAuthorizeSender; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
70
|
|
|
* @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse |
|
71
|
|
|
* |
|
72
|
|
|
* @return \Generated\Shared\Transfer\CheckoutResponseTransfer |
|
73
|
|
|
*/ |
|
74
|
|
|
public function executePostSaveHook(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponse): CheckoutResponseTransfer |
|
75
|
|
|
{ |
|
76
|
|
|
if ($quoteTransfer->getPayment()->getPaymentProvider() !== PayoneConfig::PROVIDER_NAME) { |
|
77
|
|
|
return $checkoutResponse; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$checkoutResponse = $this->checkApiLogRedirectAndError($quoteTransfer, $checkoutResponse); |
|
81
|
|
|
|
|
82
|
|
|
return $this->checkApiAuthorizationRedirectAndError($quoteTransfer, $checkoutResponse); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @deprecated Use {@link \SprykerEco\Zed\Payone\Business\Payment\Hook\PostSaveHook::executePostSaveHook()} instead. |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
89
|
|
|
* @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Generated\Shared\Transfer\CheckoutResponseTransfer |
|
92
|
|
|
*/ |
|
93
|
|
|
public function postSaveHook(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponse): CheckoutResponseTransfer |
|
94
|
|
|
{ |
|
95
|
|
|
if ($quoteTransfer->getPayment()->getPaymentProvider() !== PayoneConfig::PROVIDER_NAME) { |
|
96
|
|
|
return $checkoutResponse; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->checkApiLogRedirectAndError($quoteTransfer, $checkoutResponse); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @deprecated Use {@link \SprykerEco\Zed\Payone\Business\Payment\Hook\PostSaveHook::executePostSaveHook()} instead. |
|
104
|
|
|
* |
|
105
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
106
|
|
|
* @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse |
|
107
|
|
|
* |
|
108
|
|
|
* @return \Generated\Shared\Transfer\CheckoutResponseTransfer |
|
109
|
|
|
*/ |
|
110
|
|
|
public function executeCheckoutPostSaveHook( |
|
111
|
|
|
QuoteTransfer $quoteTransfer, |
|
112
|
|
|
CheckoutResponseTransfer $checkoutResponse |
|
113
|
|
|
): CheckoutResponseTransfer { |
|
114
|
|
|
if ($quoteTransfer->getPayment()->getPaymentProvider() !== PayoneConfig::PROVIDER_NAME) { |
|
115
|
|
|
return $checkoutResponse; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->checkApiAuthorizationRedirectAndError($quoteTransfer, $checkoutResponse); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
123
|
|
|
* @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse |
|
124
|
|
|
* |
|
125
|
|
|
* @return \Generated\Shared\Transfer\CheckoutResponseTransfer |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function checkApiLogRedirectAndError(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponse): CheckoutResponseTransfer |
|
128
|
|
|
{ |
|
129
|
|
|
$apiLogTransfer = $this->payoneRepository->findLastApiLogByOrderId($quoteTransfer->getPayment()->getPayone()->getFkSalesOrder()); |
|
130
|
|
|
|
|
131
|
|
|
if (!$apiLogTransfer) { |
|
132
|
|
|
return $checkoutResponse; |
|
133
|
|
|
} |
|
134
|
|
|
$redirectUrl = $apiLogTransfer->getRedirectUrl(); |
|
135
|
|
|
|
|
136
|
|
|
if ($redirectUrl !== null) { |
|
137
|
|
|
$checkoutResponse->setIsExternalRedirect(true); |
|
138
|
|
|
$checkoutResponse->setRedirectUrl($redirectUrl); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$errorCode = $apiLogTransfer->getErrorCode(); |
|
142
|
|
|
|
|
143
|
|
|
if ($errorCode) { |
|
144
|
|
|
$checkoutResponse->addError($this->createCheckoutErrorTransfer($apiLogTransfer->getErrorMessageUser(), $errorCode)); |
|
145
|
|
|
$checkoutResponse->setIsSuccess(false); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $checkoutResponse; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
153
|
|
|
* @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse |
|
154
|
|
|
* |
|
155
|
|
|
* @return \Generated\Shared\Transfer\CheckoutResponseTransfer |
|
156
|
|
|
*/ |
|
157
|
|
|
protected function checkApiAuthorizationRedirectAndError(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponse): CheckoutResponseTransfer |
|
158
|
|
|
{ |
|
159
|
|
|
$paymentEntity = $this->payoneRepository->createPaymentPayoneQueryByOrderId($checkoutResponse->getSaveOrder()->getIdSalesOrder())->findOne(); |
|
160
|
|
|
$paymentMethodMapper = $this->paymentMapperReader->getRegisteredPaymentMethodMapper($paymentEntity->getPaymentMethod()); |
|
161
|
|
|
$requestContainer = $this->getPostSaveHookRequestContainer($paymentMethodMapper, $paymentEntity, $quoteTransfer); |
|
162
|
|
|
|
|
163
|
|
|
if ($paymentEntity->getPaymentMethod() === PayoneApiConstants::PAYMENT_METHOD_KLARNA) { |
|
164
|
|
|
$requestContainer = $this->payoneRequestProductDataMapper->mapProductData($quoteTransfer, $requestContainer); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$rawResponse = $this->payoneRepository->getPreauthorizedPaymentByReference($requestContainer->getReference()); |
|
|
|
|
|
|
168
|
|
|
$responseContainer = $rawResponse === [] ? $this->baseAuthorizeSender->performAuthorizationRequest($paymentEntity, $requestContainer) : new AuthorizationResponseContainer($rawResponse); |
|
|
|
|
|
|
169
|
|
|
|
|
170
|
|
|
if ($responseContainer->getErrorcode()) { |
|
171
|
|
|
$checkoutErrorTransfer = $this->createCheckoutErrorTransfer($responseContainer->getCustomermessage(), $responseContainer->getErrorcode()); |
|
172
|
|
|
$checkoutResponse->addError($checkoutErrorTransfer); |
|
173
|
|
|
$checkoutResponse->setIsSuccess(false); |
|
174
|
|
|
|
|
175
|
|
|
return $checkoutResponse; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if (!$responseContainer->getRedirecturl()) { |
|
179
|
|
|
return $checkoutResponse; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$checkoutResponse->setIsExternalRedirect(true); |
|
183
|
|
|
$checkoutResponse->setRedirectUrl($responseContainer->getRedirecturl()); |
|
184
|
|
|
|
|
185
|
|
|
return $checkoutResponse; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param string $errorMessage |
|
190
|
|
|
* @param string $errorCode |
|
191
|
|
|
* |
|
192
|
|
|
* @return \Generated\Shared\Transfer\CheckoutErrorTransfer |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function createCheckoutErrorTransfer(string $errorMessage, string $errorCode): CheckoutErrorTransfer |
|
195
|
|
|
{ |
|
196
|
|
|
$checkoutErrorTransfer = new CheckoutErrorTransfer(); |
|
197
|
|
|
|
|
198
|
|
|
$checkoutErrorTransfer->setMessage($errorMessage); |
|
199
|
|
|
$checkoutErrorTransfer->setErrorCode($errorCode); |
|
200
|
|
|
|
|
201
|
|
|
return $checkoutErrorTransfer; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface $paymentMethodMapper |
|
206
|
|
|
* @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity |
|
207
|
|
|
* @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer |
|
208
|
|
|
* |
|
209
|
|
|
* @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainerInterface |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function getPostSaveHookRequestContainer( |
|
212
|
|
|
PaymentMethodMapperInterface $paymentMethodMapper, |
|
213
|
|
|
SpyPaymentPayone $paymentEntity, |
|
214
|
|
|
QuoteTransfer $quoteTransfer |
|
215
|
|
|
): AuthorizationContainerInterface { |
|
216
|
|
|
if (method_exists($paymentMethodMapper, 'mapPaymentToPreAuthorization')) { |
|
217
|
|
|
if ($paymentMethodMapper instanceof KlarnaPaymentMapper) { |
|
218
|
|
|
return $paymentMethodMapper->mapKlarnaPaymentToPreAuthorization($paymentEntity, $quoteTransfer); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $paymentMethodMapper->mapPaymentToPreAuthorization($paymentEntity); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$orderTransfer = (new OrderTransfer()) |
|
225
|
|
|
->setTotals( |
|
226
|
|
|
(new TotalsTransfer()) |
|
227
|
|
|
->setGrandTotal($quoteTransfer->getTotals()->getGrandTotal()) |
|
228
|
|
|
); |
|
229
|
|
|
|
|
230
|
|
|
return $paymentMethodMapper->mapPaymentToAuthorization($paymentEntity, $orderTransfer); |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
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