Skip to content

Commit 8d9232d

Browse files
authored
Merge pull request #132 from xendit/TPI-7724/magento-checkout-failure-error
Fix Xendit checkout failed error for multishipping
2 parents 19c73c9 + 25c7875 commit 8d9232d

File tree

7 files changed

+78
-87
lines changed

7 files changed

+78
-87
lines changed

Xendit/M2Invoice/Controller/Checkout/AbstractAction.php

+10
Original file line numberDiff line numberDiff line change
@@ -532,4 +532,14 @@ protected function getPreferredMethod()
532532
}
533533
return $preferredMethod;
534534
}
535+
536+
/**
537+
* @param string $orderByIncrementId
538+
* @return Order
539+
*/
540+
public function getOrderByIncrementId(string $orderByIncrementId): Order
541+
{
542+
$order = $this->orderFactory->create();
543+
return $order->loadByIncrementId($orderByIncrementId);
544+
}
535545
}

Xendit/M2Invoice/Controller/Checkout/Failure.php

+23-29
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,47 @@
22

33
namespace Xendit\M2Invoice\Controller\Checkout;
44

5+
use Magento\Sales\Model\Order;
6+
57
/**
68
* Class Failure
79
* @package Xendit\M2Invoice\Controller\Checkout
810
*/
911
class Failure extends AbstractAction
1012
{
11-
1213
/**
1314
* @return \Magento\Framework\App\ResponseInterface|\Magento\Framework\Controller\ResultInterface|void
1415
* @throws \Magento\Framework\Exception\LocalizedException
1516
* @throws \Magento\Framework\Exception\NoSuchEntityException
1617
*/
1718
public function execute()
1819
{
19-
$orderIds = explode('-', $this->getRequest()->get('order_id'));
20-
$type = $this->getRequest()->get('type');
20+
$orderCanceled = false;
21+
$orderIds = $this->getRequest()->get('order_ids') ?? [];
22+
if (!is_array($orderIds)) {
23+
$this->getMessageManager()->addErrorMessage(__('Invalid parameters'));
24+
return $this->_redirect('checkout/cart');
25+
}
2126

22-
if ($type == 'multishipping') {
27+
$restoreQuote = null;
28+
try {
2329
foreach ($orderIds as $orderId) {
24-
$order = $this->getOrderFactory()->create();
25-
$order ->load($orderId);
26-
27-
if ($order) {
28-
$this->getLogger()->debug('Requested order cancelled by customer. OrderId: ' . $order->getIncrementId());
29-
$this->cancelOrder($order, "customer cancelled the payment.");
30-
31-
$quoteId = $order->getQuoteId();
32-
$quote = $this->getQuoteRepository()->get($quoteId);
33-
34-
$this->getCheckoutHelper()->restoreQuote($quote); //restore cart
30+
$order = $this->getOrderByIncrementId($orderId);
31+
if ($order->getState() == Order::STATE_CANCELED) {
32+
$orderCanceled = true;
33+
$restoreQuote = $this->getQuoteRepository()->get($order->getQuoteId());
3534
}
3635
}
37-
} else { //onepage
38-
$order = $this->getOrderById($this->getRequest()->get('order_id'));
39-
40-
if ($order) {
41-
$this->getLogger()->debug('Requested order cancelled by customer. OrderId: ' . $order->getIncrementId());
42-
$this->cancelOrder($order, "customer cancelled the payment.");
43-
44-
$quoteId = $order->getQuoteId();
45-
$quote = $this->getQuoteRepository()->get($quoteId);
46-
47-
$this->getCheckoutHelper()->restoreQuote($quote); //restore cart
48-
}
36+
} catch (\Exception $e) {
37+
$this->getMessageManager()->addErrorMessage($e->getMessage());
38+
return $this->_redirect('checkout/cart');
4939
}
5040

51-
$this->getMessageManager()->addWarningMessage(__("Xendit payment failed. Please click on 'Update Shopping Cart'."));
52-
$this->_redirect('checkout/cart');
41+
// Add error message if order canceled
42+
if ($orderCanceled && !empty($restoreQuote)) {
43+
$this->getCheckoutHelper()->restoreQuote($restoreQuote);
44+
$this->getMessageManager()->addWarningMessage(__("Xendit payment failed. Please click on 'Update Shopping Cart'."));
45+
}
46+
return $this->_redirect('checkout/cart');
5347
}
5448
}

Xendit/M2Invoice/Controller/Checkout/Invoice.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private function getApiRequestData($order)
111111
'payment_methods' => json_encode([strtoupper($preferredMethod)]),
112112
'platform_callback_url' => $this->getXenditCallbackUrl(),
113113
'success_redirect_url' => $this->getDataHelper()->getSuccessUrl(),
114-
'failure_redirect_url' => $this->getDataHelper()->getFailureUrl($orderId),
114+
'failure_redirect_url' => $this->getDataHelper()->getFailureUrl([$orderId]),
115115
'customer' => (object)[
116116
'given_names' => $order->getCustomerFirstname() ?: 'n/a',
117117
'surname' => $order->getCustomerLastname() ?: 'n/a',

Xendit/M2Invoice/Controller/Checkout/InvoiceMultishipping.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function execute()
109109
'payment_methods' => json_encode([strtoupper($preferredMethod)]),
110110
'platform_callback_url' => $this->getXenditCallbackUrl(),
111111
'success_redirect_url' => $this->getDataHelper()->getSuccessUrl(true),
112-
'failure_redirect_url' => $this->getDataHelper()->getFailureUrl(implode('-', $orderIncrementIds), true),
112+
'failure_redirect_url' => $this->getDataHelper()->getFailureUrl($orderIncrementIds),
113113
'customer' => (object)[
114114
'given_names' => $order->getCustomerFirstname() ?: 'n/a',
115115
'surname' => $order->getCustomerLastname() ?: 'n/a',

Xendit/M2Invoice/Controller/Checkout/Notification.php

+5-21
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,15 @@ private function checkOrder($order, $callbackPayload, $invoice, $callbackDescrip
311311
} else { //FAILED or EXPIRED
312312
$orderState = Order::STATE_CANCELED;
313313
if ($order->getStatus() != $orderState) {
314-
$this->getCheckoutHelper()->cancelCurrentOrder(
315-
"Order #" . ($order->getId()) . " was rejected by Xendit. Transaction #$transactionId."
316-
);
314+
$this->getCheckoutHelper()
315+
->cancelOrder($order, __("Order #%1 was rejected by Xendit. Transaction #%2.", $order->getId(), $transactionId));
317316
$this->getCheckoutHelper()->restoreQuote(); //restore cart
318317
}
318+
319319
$order->addStatusHistoryComment(
320320
"Xendit payment " . strtolower($paymentStatus) . ". Transaction ID: $transactionId"
321-
)->save();
322-
321+
);
322+
$this->orderRepository->save($order);
323323
return $this->responseError(__('Transaction not paid'), __('FAILED'));
324324
}
325325
}
@@ -391,20 +391,4 @@ protected function getCheckoutHelper()
391391
{
392392
return $this->checkoutHelper;
393393
}
394-
395-
/**
396-
* @param $orderId
397-
* @return Order|null
398-
*/
399-
protected function getOrderById($orderId)
400-
{
401-
$order = $this->orderFactory->create()->load($orderId);
402-
if (!$order->getId() || $orderId !== $order->getId()) {
403-
$order = $this->orderFactory->create()->loadByIncrementId($orderId);
404-
if (!$order->getId()) {
405-
return null;
406-
}
407-
}
408-
return $order;
409-
}
410394
}

Xendit/M2Invoice/Helper/Checkout.php

+32-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Magento\Checkout\Model\Session;
66
use Magento\Quote\Api\CartRepositoryInterface;
7+
use Magento\Sales\Api\OrderRepositoryInterface;
78
use Magento\Sales\Model\Order;
89
use Magento\Sales\Model\OrderFactory;
910

@@ -28,20 +29,28 @@ class Checkout
2829
*/
2930
private $quoteRepository;
3031

32+
/**
33+
* @var OrderRepositoryInterface
34+
*/
35+
private $orderRepository;
36+
3137
/**
3238
* Checkout constructor.
3339
* @param Session $session
3440
* @param OrderFactory $order
3541
* @param CartRepositoryInterface $quoteRepository
42+
* @param OrderRepositoryInterface $orderRepository
3643
*/
3744
public function __construct(
3845
Session $session,
3946
OrderFactory $order,
40-
CartRepositoryInterface $quoteRepository
47+
CartRepositoryInterface $quoteRepository,
48+
OrderRepositoryInterface $orderRepository
4149
) {
4250
$this->session = $session;
4351
$this->orderFactory = $order;
4452
$this->quoteRepository = $quoteRepository;
53+
$this->orderRepository = $orderRepository;
4554
}
4655

4756
/**
@@ -56,8 +65,8 @@ public function cancelCurrentOrder($comment)
5665
$order = $this->session->getLastRealOrder();
5766

5867
if ($order->getId() && $order->getState() != Order::STATE_CANCELED) {
59-
$order->registerCancellation($comment)->save();
60-
return true;
68+
$order->registerCancellation($comment);
69+
return $this->orderRepository->save($order);
6170
}
6271
return false;
6372
}
@@ -88,8 +97,23 @@ public function cancelOrderById($orderId, $comment)
8897
$order = $this->orderFactory->create()->loadByIncrementId($orderId);
8998

9099
if ($order->getId() && $order->getState() != Order::STATE_CANCELED) {
91-
$order->registerCancellation($comment)->save();
92-
return true;
100+
$order->registerCancellation($comment);
101+
return $this->orderRepository->save($order);
102+
}
103+
return false;
104+
}
105+
106+
/**
107+
* @param Order $order
108+
* @param string $comment
109+
* @return bool
110+
* @throws \Magento\Framework\Exception\LocalizedException
111+
*/
112+
public function cancelOrder(Order $order, string $comment = "")
113+
{
114+
if ($order->getState() != Order::STATE_CANCELED) {
115+
$order->registerCancellation($comment);
116+
return $this->orderRepository->save($order);
93117
}
94118
return false;
95119
}
@@ -104,15 +128,13 @@ public function cancelOrderById($orderId, $comment)
104128
public function processOrdersFailedPayment($orderIds, $failureReason = 'Unexpected Error with empty charge')
105129
{
106130
foreach ($orderIds as $key => $value) {
107-
$order = $this->orderFactory->create();
108-
$order ->load($value);
109-
131+
$order = $this->orderRepository->get($value);
110132
$orderState = Order::STATE_CANCELED;
111-
$order ->setState($orderState)
133+
$order->setState($orderState)
112134
->setStatus($orderState)
113135
->addStatusHistoryComment("Order #" . $value . " was rejected by Xendit because " . $failureReason);
114-
$order ->save();
115136

137+
$order = $this->orderRepository->save($order);
116138
$quoteId = $order->getQuoteId();
117139
$quote = $this->quoteRepository->get($quoteId);
118140
$this->restoreQuote($quote);

Xendit/M2Invoice/Helper/Data.php

+6-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Magento\Framework\DB\Transaction as DbTransaction;
1212
use Magento\Framework\Filesystem\Driver\File;
1313
use Magento\Framework\Stdlib\DateTime\DateTimeFactory;
14-
use Magento\Framework\UrlInterface;
1514
use Magento\Framework\View\Asset\Repository as AssetRepository;
1615
use Magento\Quote\Model\QuoteFactory;
1716
use Magento\Quote\Model\QuoteManagement;
@@ -189,33 +188,15 @@ public function getSuccessUrl($isMultishipping = false)
189188
}
190189

191190
/**
192-
* @param $orderId
193-
* @param bool $isMultishipping
194-
* @return string
195-
* @throws \Magento\Framework\Exception\NoSuchEntityException
196-
*/
197-
public function getFailureUrl($orderId, $isMultishipping = false)
198-
{
199-
$baseUrl = $this->getStoreManager()->getStore()->getBaseUrl() . "xendit/checkout/failure?order_id=$orderId";
200-
if ($isMultishipping) {
201-
$baseUrl .= '&type=multishipping';
202-
}
203-
return $baseUrl;
204-
}
205-
206-
/**
207-
* @param $orderId
208-
* @param bool $isMultishipping
191+
* @param array $orderIds
209192
* @return string
210-
* @throws \Magento\Framework\Exception\NoSuchEntityException
211193
*/
212-
public function getThreeDSResultUrl($orderId, $isMultishipping = false)
194+
public function getFailureUrl(array $orderIds)
213195
{
214-
$baseUrl = $this->getStoreManager()->getStore()->getBaseUrl() . "xendit/checkout/threedsresult?order_id=$orderId";
215-
if ($isMultishipping) {
216-
$baseUrl .= "&type=multishipping";
217-
}
218-
return $baseUrl;
196+
$parameters = http_build_query([
197+
'order_ids' => $orderIds
198+
]);
199+
return $this->_getUrl('xendit/checkout/failure', ['_query' => $parameters]);
219200
}
220201

221202
/**

0 commit comments

Comments
 (0)