Skip to content

Commit e735947

Browse files
update: implement browser back button handling and configuration for order cancellation
1 parent 4fc5a2b commit e735947

File tree

16 files changed

+293
-37
lines changed

16 files changed

+293
-37
lines changed

Controller/Redirect/Process.php

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,22 @@ protected function handleFailed($statusCode)
516516
$this->removeCoupon();
517517
$this->removeAmastyGiftcardOnFailed();
518518

519+
// Detect browser back button scenario
520+
$isBrowserBack = ($statusCode === $this->helper->getStatusCode('BUCKAROO_MAGENTO2_STATUSCODE_CANCELLED_BY_USER'));
521+
522+
// Check configuration for browser back behavior
523+
$store = $this->order->getStore();
524+
$shouldCancelOnBrowserBack = (bool) $this->accountConfig->getCancelOnBrowserBack($store);
525+
526+
$this->logger->addDebug(sprintf(
527+
'%s - Handle Failed Check | Order: %s | statusCode: %s | isBrowserBack: %s | shouldCancelOnBrowserBack: %s',
528+
__METHOD__,
529+
$this->order->getIncrementId(),
530+
$statusCode,
531+
var_export($isBrowserBack, true),
532+
var_export($shouldCancelOnBrowserBack, true)
533+
));
534+
519535
if (!$this->getSkipHandleFailedRecreate()) {
520536
if (!$this->quoteRecreate->recreate($this->quote, $this->response)) {
521537
$this->logger->addError('Could not recreate the quote.');
@@ -534,7 +550,7 @@ protected function handleFailed($statusCode)
534550
$this->helper->getStatusCode('BUCKAROO_MAGENTO2_ORDER_FAILED') => 'Unfortunately an error occurred while processing your payment. Please try again. If this error persists, please choose a different payment method.',
535551
$this->helper->getStatusCode('BUCKAROO_MAGENTO2_STATUSCODE_FAILED') => 'Unfortunately an error occurred while processing your payment. Please try again. If this error persists, please choose a different payment method.',
536552
$this->helper->getStatusCode('BUCKAROO_MAGENTO2_STATUSCODE_REJECTED') => 'Unfortunately an error occurred while processing your payment. Please try again. If this error persists, please choose a different payment method.',
537-
$this->helper->getStatusCode('BUCKAROO_MAGENTO2_STATUSCODE_CANCELLED_BY_USER') => 'According to our system, you have canceled the payment. If this is not the case, please contact us.'
553+
$this->helper->getStatusCode('BUCKAROO_MAGENTO2_STATUSCODE_CANCELLED_BY_USER') => 'Payment cancelled. You can try again using the same or a different payment method.',
538554
];
539555

540556
$this->addErrorMessage(__($statusCodeAddErrorMessage[$statusCode] ?? 'An error occurred while processing your payment.'));
@@ -544,12 +560,45 @@ protected function handleFailed($statusCode)
544560
return $this->redirectFailure();
545561
}
546562

547-
// Cancel the order and log an error if it fails
548-
if (!$this->cancelOrder($statusCode, $statusCodeAddErrorMessage[$statusCode])) {
549-
$this->logger->addError('Could not cancel the order.');
563+
// For browser back button, check configuration
564+
if ($isBrowserBack && !$shouldCancelOnBrowserBack) {
565+
$this->logger->addDebug(sprintf(
566+
'%s - Browser Back Button Detected - Order left in pending state (config: cancel_on_browser_back = disabled). Quote recreated for retry. Order: %s',
567+
__METHOD__,
568+
$this->order->getIncrementId()
569+
));
570+
571+
// Add a status history comment to track this
572+
$this->order->addCommentToStatusHistory(
573+
__('Customer returned using browser back button. Order left pending for push notification.'),
574+
false,
575+
false
576+
);
577+
$this->order->save();
578+
579+
} else {
580+
// For actual failures OR if config says to cancel on browser back, cancel the order as before
581+
if ($isBrowserBack) {
582+
$this->logger->addDebug(sprintf(
583+
'%s - Browser Back Button Detected - Order will be canceled (config: cancel_on_browser_back = enabled). Order: %s',
584+
__METHOD__,
585+
$this->order->getIncrementId()
586+
));
587+
}
588+
589+
if (!$this->cancelOrder($statusCode, $statusCodeAddErrorMessage[$statusCode])) {
590+
$this->logger->addError('Could not cancel the order.');
591+
}
550592
}
551593

552-
$this->logger->addDebug(__METHOD__ . '|8|');
594+
$this->logger->addDebug(sprintf(
595+
'%s - Redirect Failure | Order: %s | isBrowserBack: %s | shouldCancelOnBrowserBack: %s',
596+
__METHOD__,
597+
$this->order->getIncrementId(),
598+
var_export($isBrowserBack, true),
599+
var_export($shouldCancelOnBrowserBack, true)
600+
));
601+
553602
return $this->redirectFailure();
554603
}
555604

Model/ConfigProvider/Account.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @method mixed getSuccessRedirect()
4040
* @method mixed getFailureRedirect()
4141
* @method mixed getCancelOnFailed()
42+
* @method mixed getCancelOnBrowserBack()
4243
* @method mixed getDigitalSignature()
4344
* @method mixed getDebugTypes()
4445
* @method mixed getDebugEmail()
@@ -69,6 +70,7 @@ class Account extends AbstractConfigProvider
6970
const XPATH_ACCOUNT_FAILURE_REDIRECT_TO_CHECKOUT = 'buckaroo_magento2/account/failure_redirect_to_checkout';
7071
const XPATH_ACCOUNT_CANCEL_ON_FAILED = 'buckaroo_magento2/account/cancel_on_failed';
7172
const XPATH_ACCOUNT_DIGITAL_SIGNATURE = 'buckaroo_magento2/account/digital_signature';
73+
const XPATH_ACCOUNT_CANCEL_ON_BROWSER_BACK = 'buckaroo_magento2/account/cancel_on_browser_back';
7274
const XPATH_ACCOUNT_DEBUG_TYPES = 'buckaroo_magento2/account/debug_types';
7375
const XPATH_ACCOUNT_DEBUG_EMAIL = 'buckaroo_magento2/account/debug_email';
7476
const XPATH_ACCOUNT_LIMIT_BY_IP = 'buckaroo_magento2/account/limit_by_ip';
@@ -129,6 +131,7 @@ public function getConfig($store = null)
129131
'failure_redirect' => $this->getFailureRedirect($store),
130132
'failure_redirect_to_checkout' => $this->getFailureRedirectToCheckout($store),
131133
'cancel_on_failed' => $this->getCancelOnFailed($store),
134+
'cancel_on_browser_back' => $this->getCancelOnBrowserBack($store),
132135
'digital_signature' => $this->getDigitalSignature($store),
133136
'debug_types' => $this->getDebugTypes($store),
134137
'debug_email' => $this->getDebugEmail($store),

Model/Method/AbstractMethod.php

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,29 @@ public function order(InfoInterface $payment, $amount)
793793

794794
$this->saveTransactionData($response[0], $payment, $this->closeOrderTransaction, true);
795795

796+
$order = $payment->getOrder();
797+
if (!empty($order) && !empty($order->getId())) {
798+
try {
799+
$payment->save();
800+
801+
$this->logger2->addDebug(sprintf(
802+
'[%s] Payment transaction persisted successfully for order: %s, transaction ID: %s',
803+
__METHOD__,
804+
$order->getIncrementId(),
805+
$payment->getTransactionId()
806+
));
807+
} catch (\Exception $e) {
808+
$this->logger2->addError(sprintf(
809+
'[%s] Failed to persist payment transaction - Order: %s, Transaction ID: %s, Error: %s',
810+
__METHOD__,
811+
$order->getIncrementId(),
812+
$payment->getTransactionId(),
813+
$e->getMessage()
814+
));
815+
throw $e;
816+
}
817+
}
818+
796819
// SET REGISTRY BUCKAROO REDIRECT
797820
$this->_registry->unregister('buckaroo_response');
798821
$this->_registry->register('buckaroo_response', $response);
@@ -801,7 +824,6 @@ public function order(InfoInterface $payment, $amount)
801824
$this->setPaymentInTransit($payment, false);
802825
}
803826

804-
$order = $payment->getOrder();
805827
$this->helper->setRestoreQuoteLastOrder($order->getId());
806828

807829
$this->eventManager->dispatch('buckaroo_order_after', ['order' => $order]);
@@ -2811,16 +2833,36 @@ private function cancelPreviousPendingOrder(InfoInterface $payment)
28112833
$order = $orderRepository->get((int)$orderId);
28122834

28132835
if($order->getState() === Order::STATE_NEW) {
2814-
$orderManagement = $this->objectManager->get(OrderManagementInterface::class);
2815-
$orderManagement->cancel($order->getEntityId());
2816-
$order->addCommentToStatusHistory(
2817-
__('Canceled on browser back button')
2818-
)
2819-
->setIsCustomerNotified(false)
2820-
->setEntityName('invoice')
2821-
->save();
2822-
}
2836+
$this->logger2->addDebug(sprintf(
2837+
'[%s] Canceling previous pending order: %s (browser back scenario)',
2838+
__METHOD__,
2839+
$order->getIncrementId()
2840+
));
28232841

2842+
try {
2843+
$order->cancel()->save();
2844+
2845+
$order->addCommentToStatusHistory(
2846+
__('Canceled on browser back button - customer placed a new order.')
2847+
)
2848+
->setIsCustomerNotified(false)
2849+
->save();
2850+
2851+
$this->logger2->addDebug(sprintf(
2852+
'[%s] Successfully canceled order: %s and restored stock',
2853+
__METHOD__,
2854+
$order->getIncrementId()
2855+
));
2856+
} catch (\Exception $e) {
2857+
$this->logger2->addError(sprintf(
2858+
'[%s] Failed to cancel order: %s - Error: %s',
2859+
__METHOD__,
2860+
$order->getIncrementId(),
2861+
$e->getMessage()
2862+
));
2863+
throw $e;
2864+
}
2865+
}
28242866

28252867
} catch (\Throwable $th) {
28262868
$this->logger2->addError(__METHOD__." ".(string)$th);

0 commit comments

Comments
 (0)