-
Notifications
You must be signed in to change notification settings - Fork 33
/
PaymentErrorHandlerService.php
executable file
·390 lines (364 loc) · 11.5 KB
/
PaymentErrorHandlerService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* Checkout.com
* Authorized and regulated as an electronic money institution
* by the UK Financial Conduct Authority (FCA) under number 900816.
*
* PHP version 7
*
* @category Magento2
* @package Checkout.com
* @author Platforms Development Team <platforms@checkout.com>
* @copyright 2010-present Checkout.com
* @license https://opensource.org/licenses/mit-license.html MIT License
* @link https://docs.checkout.com/
*/
declare(strict_types=1);
namespace CheckoutCom\Magento2\Model\Service;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Phrase;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderPaymentRepositoryInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\OrderStatusHistoryRepositoryInterface;
/**
* Class PaymentErrorHandlerService
*/
class PaymentErrorHandlerService
{
/**
* TRANSACTION_ERROR_LABEL const
*
* @var array TRANSACTION_ERROR_LABEL
*/
const TRANSACTION_ERROR_LABEL = [
'payment_declined' => 'Failed payment authorization',
'payment_capture_declined' => 'Failed payment capture',
'payment_void_declined' => 'Failed payment void',
'payment_refund_declined' => 'Failed payment refund',
'payment_pending' => 'Failed payment request',
];
/**
* TRANSACTION_SUCCESS_DIGITS const
*
* @var string TRANSACTION_SUCCESS_DIGITS
*/
const TRANSACTION_SUCCESS_DIGITS = '10';
/**
* $transactionHandler field
*
* @var TransactionHandlerService $transactionHandler
*/
private $transactionHandler;
/**
* $orderHandler field
*
* @var OrderHandlerService $orderHandler
*/
private $orderHandler;
/**
* $orderPaymentRepository field
*
* @var OrderPaymentRepositoryInterface $orderPaymentRepository
*/
private $orderPaymentRepository;
/**
* $orderRepository field
*
* @var OrderRepositoryInterface $orderRepository
*/
private $orderRepository;
/**
* $orderStatusHistoryRepository field
*
* @var OrderStatusHistoryRepositoryInterface $orderStatusHistoryRepository
*/
private $orderStatusHistoryRepository;
/**
* PaymentErrorHandlerService constructor
*
* @param TransactionHandlerService $transactionHandler
* @param OrderHandlerService $orderHandler
* @param OrderRepositoryInterface $orderRepository
* @param OrderPaymentRepositoryInterface $orderPaymentRepository
* @param OrderStatusHistoryRepositoryInterface $orderStatusHistoryRepository
*/
public function __construct(
TransactionHandlerService $transactionHandler,
OrderHandlerService $orderHandler,
OrderRepositoryInterface $orderRepository,
OrderPaymentRepositoryInterface $orderPaymentRepository,
OrderStatusHistoryRepositoryInterface $orderStatusHistoryRepository
) {
$this->transactionHandler = $transactionHandler;
$this->orderHandler = $orderHandler;
$this->orderRepository = $orderRepository;
$this->orderPaymentRepository = $orderPaymentRepository;
$this->orderStatusHistoryRepository = $orderStatusHistoryRepository;
}
/**
* Log payment error for webhooks
*
* @param array $response
* @param OrderInterface $order
*
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function logError(array $response, OrderInterface $order): void
{
// Assign the payment instance
$payment = $order->getPayment();
// Prepare the failed transaction info
$suffix = __(
' for an amount of %1. Action ID: %2. Event ID: %3. Payment ID: %4. Error: %5 %6',
$this->prepareAmount($response->data->amount, $order),
!empty($response->data->action_id) ? $response->data->action_id : __('Not specified.'),
!empty($response->id) ? $response->id : __('Not specified.'),
!empty($response->data->id) ? $response->data->id : __('Not specified.'),
!empty($response->data->response_code) ? $response->data->response_code : __('Not specified.'),
!empty($response->data->response_summary) ? $response->data->response_summary : __('Not specified.')
);
// Add the order comment
$previousComment = $this->orderHandler->getStatusHistoryByEntity('3ds Fail', $order);
if ($previousComment && $response->type === 'payment_declined') {
$previousComment->setEntityName('order')->setComment(
__(self::TRANSACTION_ERROR_LABEL[$response->type]) . $suffix
);
$this->orderStatusHistoryRepository->save($previousComment);
} else {
// Add the order comment
$order->addStatusHistoryComment(
__(self::TRANSACTION_ERROR_LABEL[$response->type]) . $suffix
);
}
// Save the data
$this->orderPaymentRepository->save($payment);
$this->orderRepository->save($order);
}
/**
* Prepare the amount received from the gateway
*
* @param float|int $amount
* @param OrderInterface $order
*
* @return string
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function prepareAmount($amount, OrderInterface $order): string
{
// Prepare the amount
$amount = $this->transactionHandler->amountFromGateway(
$amount,
$order
);
// Get the currency
$currency = $this->orderHandler->getOrderCurrency($order);
return $amount . ' ' . $currency;
}
/**
* Log the error for 3ds declined payments
*
* @param array $response
* @param OrderInterface $order
* @param string $status
*
* @return void
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function logPaymentError(array $response, OrderInterface $order, string $status = ''): void
{
// Assign the payment instance
$payment = $order->getPayment();
// Prepare the failed transaction info
$suffix = __(
' for an amount of %1. Payment ID: %2',
$this->prepareAmount($response['amount'], $order),
$response['id']
);
// Add the order comment
$order->setHistoryEntityName('3ds Fail');
$order->addStatusHistoryComment(
__(self::TRANSACTION_ERROR_LABEL['payment_declined']) . $suffix,
$status
);
// Save the data
$this->orderPaymentRepository->save($payment);
$this->orderRepository->save($order);
}
/**
* Description getErrorMessage function
*
* @param string $responseCode
*
* @return Phrase
*/
public function getErrorMessage(string $responseCode): Phrase
{
$generalErrors = array_fill_keys(
[
"20067",
"20099",
],
__(
'The payment was declined, please try again. If the problem persists, try another card or payment method.'
)
);
$fundingErrors = array_fill_keys(
[
"20001",
"20002",
"20005",
"20010",
"20032",
"20039",
"20040",
"20044",
"20046",
"20051",
"20052",
"20053",
"20061",
"20062",
"20065",
"20075",
"20083",
"20084",
"20085",
"20091",
"20093",
"200N0",
"200O5",
"200P1",
"200P9",
"200R1",
"200R3",
"200S4",
"200T3",
"200T5",
"20103",
"20108",
"20150",
"30004",
"30021",
"30022",
"30035",
"30036",
"30038",
],
__(
'You have reached the limit allowed for this card/account, please try again with another card or payment method.'
)
);
$technicalErrors = array_fill_keys(
[
"20003",
"20060",
"20102",
"20112",
"20121",
"30016",
"30017",
"30018",
"30019",
"20006",
"20009",
"20019",
"20020",
"20021",
"20022",
"20023",
"20024",
"20025",
"20026",
"20027",
"20028",
"20029",
"20030",
"20031",
"20042",
"20058",
"20064",
"20068",
"20086",
"20088",
"20089",
"20090",
"20092",
"20094",
"20095",
"20096",
"20097",
"20098",
"200T2",
"20101",
"20104",
"20105",
"20106",
"20107",
"20109",
"20110",
"20111",
"20113",
"20114",
"20115",
"20116",
"20117",
"20118",
"20119",
"20120",
"20123",
"30015",
"30020",
"20059",
"20063",
"20066",
"20082",
"30007",
"30034",
"30037",
"4XXXX" // Fraud response codes
],
__('Something went wrong, please try again later')
);
$invalidCardErrors = array_fill_keys(
[
"20014",
"20054",
"20055",
"20056",
"20087",
"200N7",
"20100",
"30033",
"30041",
"30043",
],
__('It looks like your card is invalid or blocked, please try with another card')
);
$blockedCardErrors = array_fill_keys(
[
"20017",
"20018",
"20057",
],
__(
'It looks like this transaction has been blocked due to account holder action, please contact your bank or use another card or payment method'
)
);
$threeDsErrors = array_fill_keys(
[
"20151",
"20152",
"20154",
],
__('3DS has expired or authentication failed, please try again')
);
$messageMapper = $generalErrors + $fundingErrors + $technicalErrors + $invalidCardErrors + $blockedCardErrors + $threeDsErrors;
return $messageMapper[$responseCode] ?? __('The transaction could not be processed');
}
}