Skip to content

Commit 7a75c67

Browse files
Merge pull request #70 from xendit/TPI-2944/implement-error
TPI-2944/implement error
2 parents 7d0613c + 8758137 commit 7a75c67

File tree

19 files changed

+171
-146
lines changed

19 files changed

+171
-146
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 2.6.0 (2021-01-19)
4+
5+
Improvements:
6+
7+
- Standardize Error Message
8+
39
## 2.5.0 (2020-12-16)
410

511
Improvements:

Xendit/M2Invoice/Controller/Checkout/AbstractAction.php

+24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Magento\Framework\App\Action\Action;
66
use Magento\Framework\App\Action\Context;
77
use Magento\Framework\Controller\Result\JsonFactory;
8+
use Magento\Framework\Controller\ResultFactory;
89
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\Phrase;
911
use Magento\Framework\UrlInterface;
1012
use Magento\Checkout\Model\Session;
1113
use Magento\Sales\Model\OrderFactory;
@@ -242,4 +244,26 @@ protected function getXenditCallbackUrl() {
242244

243245
return $baseUrl . 'xendit/checkout/notification';
244246
}
247+
248+
protected function throwXenditAPIError($errorResponse) {
249+
$message = $errorResponse['message'];
250+
251+
if (isset($errorResponse['code'])) {
252+
$message .= ' Code: ' . $errorResponse['code'];
253+
}
254+
255+
throw new \Magento\Framework\Exception\LocalizedException(
256+
new Phrase($message)
257+
);
258+
}
259+
260+
protected function redirectToCart($failureReason = 'UNEXPECTED_PLUGIN_ISSUE') {
261+
$failureReasonInsight = $this->getDataHelper()->failureReasonInsight($failureReason);
262+
$this->getMessageManager()->addErrorMessage(__(
263+
$failureReasonInsight
264+
));
265+
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
266+
$resultRedirect->setUrl($this->_url->getUrl('checkout/cart'), [ '_secure'=> false ]);
267+
return $resultRedirect;
268+
}
245269
}

Xendit/M2Invoice/Controller/Checkout/CCMultishipping.php

+2-17
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,6 @@ public function execute()
174174
}
175175
}
176176

177-
private function redirectToCart($failureReason)
178-
{
179-
$failureReasonInsight = $this->getDataHelper()->failureReasonInsight($failureReason);
180-
$this->getMessageManager()->addErrorMessage(__(
181-
$failureReasonInsight
182-
));
183-
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
184-
$resultRedirect->setUrl($this->_url->getUrl('checkout/cart'));
185-
return $resultRedirect;
186-
}
187-
188177
private function requestHostedPayment($requestData)
189178
{
190179
$hostedPaymentUrl = $this->getDataHelper()->getCheckoutUrl() . "/payment/xendit/hosted-payments";
@@ -268,15 +257,11 @@ private function handle3DSFlow($requestData, $payment, $orderIds, $orders)
268257
/**
269258
* $orderIds = prefixless order IDs
270259
*/
271-
private function processFailedPayment($orderIds, $failureReason = 'Unexpected Error with empty charge')
260+
private function processFailedPayment($orderIds, $failureReason = 'UNEXPECTED_PLUGIN_ISSUE')
272261
{
273262
$this->getCheckoutHelper()->processOrdersFailedPayment($orderIds, $failureReason);
274263

275-
$failureReasonInsight = $this->getDataHelper()->failureReasonInsight($failureReason);
276-
$this->getMessageManager()->addErrorMessage(__(
277-
$failureReasonInsight
278-
));
279-
$this->_redirect('checkout/cart', array('_secure'=> false));
264+
return $this->redirectToCart($failureReason);
280265
}
281266

282267
private function processSuccessfulPayment($orders, $payment, $charge)

Xendit/M2Invoice/Controller/Checkout/Failure.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public function execute()
2121
$this->shouldCancelOrder($order);
2222
}
2323

24-
$this->getMessageManager()->addWarningMessage(__("Xendit payment failed. Please click on 'Update Shopping Cart'."));
25-
$this->_redirect('checkout/cart');
24+
return $this->redirectToCart("Xendit payment failed. Please click on 'Update Shopping Cart'.");
2625
}
2726

2827
private function shouldCancelOrder($order) {

Xendit/M2Invoice/Controller/Checkout/Invoice.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Xendit\M2Invoice\Controller\Checkout;
44

55
use Magento\Sales\Model\Order;
6+
use Magento\Framework\Controller\ResultFactory;
67
use Magento\Framework\Phrase;
78
use Xendit\M2Invoice\Enum\LogDNA_Level;
89
use Xendit\M2Invoice\Enum\LogDNALevel;
@@ -21,17 +22,23 @@ public function execute()
2122
) {
2223
$this->changePendingPaymentStatus($order);
2324
$invoice = $this->createInvoice($apiData);
25+
26+
if (isset($invoice['error_code'])) {
27+
$this->throwXenditAPIError($invoice);
28+
}
29+
2430
$this->addInvoiceData($order, $invoice);
2531
$redirectUrl = $this->getXenditRedirectUrl($invoice, $apiData['preferred_method']);
2632

2733
$resultRedirect = $this->getRedirectFactory()->create();
2834
$resultRedirect->setUrl($redirectUrl);
2935
return $resultRedirect;
3036
} elseif ($order->getState() === Order::STATE_CANCELED) {
31-
$this->_redirect('checkout/cart');
37+
return $this->redirectToCart('Order is cancelled, please try again.');
3238
} else {
3339
$this->getLogger()->debug('Order in unrecognized state: ' . $order->getState());
34-
$this->_redirect('checkout/cart');
40+
41+
return $this->redirectToCart('Order state is unrecognized, please try again.');
3542
}
3643
} catch (\Exception $e) {
3744
$message = 'Exception caught on xendit/checkout/invoice: ' . $e->getMessage();
@@ -42,10 +49,7 @@ public function execute()
4249
$this->getLogDNA()->log(LogDNALevel::ERROR, $message, $apiData);
4350

4451
$this->cancelOrder($order, $e->getMessage());
45-
$this->getMessageManager()->addErrorMessage(__(
46-
"There was an error in the Xendit payment. Failure reason: Unexpected Error"
47-
));
48-
$this->_redirect('checkout/cart', [ '_secure'=> false ]);
52+
return $this->redirectToCart($e->getMessage());
4953
}
5054
}
5155

Xendit/M2Invoice/Controller/Checkout/InvoiceMultishipping.php

+9-12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ public function execute()
6262

6363
$invoice = $this->createInvoice($requestData);
6464

65+
if (isset($invoice['error_code'])) {
66+
$this->throwXenditAPIError($invoice);
67+
}
68+
6569
$this->addInvoiceData($orders, $invoice);
6670

6771
$redirectUrl = $this->getXenditRedirectUrl($invoice, $preferredMethod);
@@ -72,7 +76,11 @@ public function execute()
7276
} catch (\Exception $e) {
7377
$message = 'Exception caught on xendit/checkout/redirect: ' . $e->getMessage();
7478
$this->getLogger()->info($message);
75-
return $this->redirectToCart("There was an error in the Xendit payment. Failure reason: Unexpected Error");
79+
80+
foreach ($orders as $order) {
81+
$this->cancelOrder($order, $e->getMessage());
82+
}
83+
return $this->redirectToCart($e->getMessage());
7684
}
7785
}
7886

@@ -112,15 +120,4 @@ private function addInvoiceData($orders, $invoice)
112120
$order->save();
113121
}
114122
}
115-
116-
private function redirectToCart($failureReason)
117-
{
118-
$failureReasonInsight = $this->getDataHelper()->failureReasonInsight($failureReason);
119-
$this->getMessageManager()->addErrorMessage(__(
120-
$failureReasonInsight
121-
));
122-
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
123-
$resultRedirect->setUrl($this->_url->getUrl('checkout/cart'), [ '_secure'=> false ]);
124-
return $resultRedirect;
125-
}
126123
}

Xendit/M2Invoice/Controller/Checkout/ProcessHosted.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ public function handlePaymentFailure($order, $message, $reason, $shouldRedirect
101101

102102
$this->cancelOrder($order, $reason);
103103

104-
$this->getMessageManager()->addErrorMessage(__(
105-
"There was an error in the Xendit payment. Failure reason: $reason"
106-
));
107-
108104
if ($shouldRedirect) {
109-
return $this->_redirect('checkout/cart', [ '_secure'=> false ]);
105+
return $this->redirectToCart($message);
106+
} else {
107+
$this->getMessageManager()->addErrorMessage(__(
108+
"There was an error in the Xendit payment. Failure reason: $reason"
109+
));
110110
}
111111
}
112112
}

Xendit/M2Invoice/Controller/Checkout/Redirect.php

+2-13
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ public function execute()
7474

7575
$this->cancelOrder($order, 'No payment recorded');
7676

77-
return $this->redirectToCart("There was an error in the Xendit payment. Failure reason: Unexpected Error");
77+
return $this->redirectToCart();
7878
} catch (\Exception $e) {
7979
$message = 'Exception caught on xendit/checkout/redirect: ' . $e->getMessage();
8080
$this->getLogDNA()->log(LogDNALevel::ERROR, $message);
8181

8282
$this->cancelOrder($order, $e->getMessage());
8383

84-
return $this->redirectToCart("There was an error in the Xendit payment. Failure reason: Unexpected Error");
84+
return $this->redirectToCart($e->getMessage());
8585
}
8686
}
8787

@@ -105,15 +105,4 @@ private function getEwalletStatus($ewalletType, $externalId)
105105

106106
return $response['status'];
107107
}
108-
109-
private function redirectToCart($failureReason)
110-
{
111-
$failureReasonInsight = $this->getDataHelper()->failureReasonInsight($failureReason);
112-
$this->getMessageManager()->addErrorMessage(__(
113-
$failureReasonInsight
114-
));
115-
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
116-
$resultRedirect->setUrl($this->_url->getUrl('checkout/cart'), [ '_secure'=> false ]);
117-
return $resultRedirect;
118-
}
119108
}

Xendit/M2Invoice/Controller/Checkout/ThreeDSResult.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function execute()
5353
$hosted3DS = $this->getThreeDSResult($hosted3DSId);
5454

5555
if ('VERIFIED' !== $hosted3DS['status']) {
56-
return $this->processFailedPayment($orderIds, 'Authentication process failed. Please try again.');
56+
return $this->processFailedPayment($orderIds, 'AUTHENTICATION_FAILED');
5757
}
5858

5959
$charge = $this->createCharge($hosted3DS, $orderId);
@@ -141,14 +141,10 @@ private function processXenditPayment($charge, $orders, $orderIds, $isMultishipp
141141
/**
142142
* $orderIds = prefixless order IDs
143143
*/
144-
private function processFailedPayment($orderIds, $failureReason = 'Unexpected Error')
144+
private function processFailedPayment($orderIds, $failureReason = 'UNEXPECTED_PLUGIN_ISSUE')
145145
{
146146
$this->getCheckoutHelper()->processOrdersFailedPayment($orderIds, $failureReason);
147147

148-
$failureReasonInsight = $this->getDataHelper()->failureReasonInsight($failureReason);
149-
$this->getMessageManager()->addErrorMessage(__(
150-
$failureReasonInsight
151-
));
152-
$this->_redirect('checkout/cart', array('_secure'=> false));
148+
return $this->redirectToCart($failureReason);
153149
}
154150
}

Xendit/M2Invoice/Helper/ApiRequest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function getHeaders($isPublicRequest, $preferredMethod = null, $customHe
8585
'Content-Type' => 'application/json',
8686
'x-plugin-name' => 'MAGENTO2',
8787
'user-agent' => 'Magento 2 Module',
88-
'x-plugin-version' => '2.5.0'
88+
'x-plugin-version' => '2.6.0'
8989
];
9090

9191
if ($preferredMethod !== null) {

Xendit/M2Invoice/Helper/Data.php

+18-29
Original file line numberDiff line numberDiff line change
@@ -214,37 +214,26 @@ public function getXenditSubscriptionCallbackUrl($isMultishipping = false) {
214214
*/
215215
public function failureReasonInsight($failureReason)
216216
{
217+
$cardDeclinedReason = 'The card you are trying to use has been declined. Please try again with a different card.';
217218
switch ($failureReason) {
218219
case 'CARD_DECLINED':
219-
case 'STOLEN_CARD': return 'The bank that issued this card declined the payment but didn\'t tell us why.
220-
Try another card, or try calling your bank to ask why the card was declined.';
221-
case 'INSUFFICIENT_BALANCE': return "Your bank declined this payment due to insufficient balance. Ensure
222-
that sufficient balance is available, or try another card";
223-
case 'INVALID_CVN': return "Your bank declined the payment due to incorrect card details entered. Try to
224-
enter your card details again, including expiration date and CVV";
225-
case 'INACTIVE_CARD': return "This card number does not seem to be enabled for eCommerce payments. Try
226-
another card that is enabled for eCommerce, or ask your bank to enable eCommerce payments for your card.";
227-
case 'EXPIRED_CARD': return "Your bank declined the payment due to the card being expired. Please try
228-
another card that has not expired.";
229-
case 'PROCESSOR_ERROR': return 'We encountered issue in processing your card. Please try again with another card';
230-
case 'USER_DID_NOT_AUTHORIZE_THE_PAYMENT':
231-
return 'Please complete the payment request within 60 seconds.';
232-
case 'USER_DECLINED_THE_TRANSACTION':
233-
return 'You rejected the payment request, please try again when needed.';
234-
case 'PHONE_NUMBER_NOT_REGISTERED':
235-
return 'Your number is not registered in OVO, please register first or contact OVO Customer Service.';
236-
case 'EXTERNAL_ERROR':
237-
return 'There is a technical issue happens on OVO, please contact the merchant to solve this issue.';
238-
case 'SENDING_TRANSACTION_ERROR':
239-
return 'Your transaction is not sent to OVO, please try again.';
240-
case 'EWALLET_APP_UNREACHABLE':
241-
return 'Do you have OVO app on your phone? Please check your OVO app on your phone and try again.';
242-
case 'REQUEST_FORBIDDEN_ERROR':
243-
return 'Your merchant disable OVO payment from his side, please contact your merchant to re-enable it
244-
before trying it again.';
245-
case 'DEVELOPMENT_MODE_PAYMENT_ACKNOWLEDGED':
246-
return 'Development mode detected. Please refer to our documentations for successful payment
247-
simulation';
220+
return $cardDeclinedReason . ' Code: 200011';
221+
case 'STOLEN_CARD':
222+
return $cardDeclinedReason . ' Code: 200013';
223+
case 'INSUFFICIENT_BALANCE':
224+
return $cardDeclinedReason . ' Code: 200012';
225+
case 'INVALID_CVN':
226+
return 'Please verify that all credit card information is correct. Code: 200015';
227+
case 'INACTIVE_CARD':
228+
return $cardDeclinedReason . ' Code: 200014';
229+
case 'EXPIRED_CARD':
230+
return 'The card you are trying to use has expired. Please try again with a different card. Code: 200010';
231+
case 'PROCESSOR_ERROR':
232+
return 'We encountered an issue processing your checkout, please contact us. Code: 200009';
233+
case 'AUTHENTICATION_FAILED':
234+
return 'Authentication process failed. Please try again. Code: 200001';
235+
case 'UNEXPECTED_PLUGIN_ISSUE':
236+
return 'We encountered an issue processing your checkout, please contact us. Code: 999999';
248237
default: return $failureReason;
249238
}
250239
}

Xendit/M2Invoice/Model/Payment/AbstractInvoice.php

-10
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,11 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
106106
$chosenMethods = $this->dataHelper->getChosenMethods();
107107
$currentCode = $this->_code;
108108

109-
if ($currentCode === 'cchosted') {
110-
$currentCode = 'cc';
111-
}
112-
113109
if (!in_array($currentCode, explode(',', $chosenMethods))) {
114110
return false;
115111
}
116112
}
117113

118-
$cardPaymentType = $this->dataHelper->getCardPaymentType();
119-
120-
if (($cardPaymentType === 'popup' && $this->methodCode === 'CCHOSTED') || $this->methodCode === 'CC_INSTALLMENT' || $this->methodCode === 'CC_SUBSCRIPTION') {
121-
return true;
122-
}
123-
124114
try {
125115
$availableMethod = $this->getAvailableMethods();
126116

Xendit/M2Invoice/Model/Payment/CC.php

+7-22
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
115115
$chosenMethods = $this->dataHelper->getChosenMethods();
116116
$currentCode = $this->_code;
117117

118-
if ($currentCode === 'cchosted') {
119-
$currentCode = 'cc';
120-
}
121-
122118
if (!in_array($currentCode, explode(',', $chosenMethods))) {
123119
return false;
124120
}
@@ -128,25 +124,9 @@ public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
128124

129125
if ($cardPaymentType === 'form') {
130126
return true;
131-
} else {
132-
return false;
133127
}
134128

135-
try {
136-
$availableMethod = $this->getAvailableMethods();
137-
138-
if (empty($availableMethod)) {
139-
return true;
140-
}
141-
142-
if (!in_array(strtoupper($this->methodCode), $availableMethod)) {
143-
return false;
144-
}
145-
146-
return true;
147-
} catch (\Exception $e) {
148-
return true;
149-
}
129+
return false;
150130
}
151131

152132
public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount)
@@ -260,8 +240,13 @@ public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount
260240
return $this;
261241
}
262242
else if ($chargeError) {
243+
$message = $charge['message'];
244+
if (isset($charge['code'])) {
245+
$message .= ' Code: ' . $charge['code'];
246+
}
247+
263248
throw new \Magento\Framework\Exception\LocalizedException(
264-
__($charge['message'])
249+
new Phrase($message)
265250
);
266251
}
267252

0 commit comments

Comments
 (0)