We are currently using the PayPal Braintree module on our Magento 2 website. We need to find an event that triggers when a payment fails during checkout. The issue we are facing is that when a customer applies a coupon code and the payment fails, the coupon code is marked as used (with a value of 1), even though the order is not successful.
We have tried the following events to handle this situation:
1.sales_order_save_before
2.sales_order_place_before
3.sales_order_payment_place_end
4.checkout_submit_before
5.sales_order_payment_cancel
6.sales_order_payment_place_failed
7.checkout_onepage_controller_failure_action
events.xml
<event name="sales_order_payment_cancel">
<observer name="galaxy_order_observer" instance="GalaxyCustomObserverPaymentcancel"/>
</event>
PaymentCancel.php
<?php
namespace GalaxyCustomObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
use MagentoSalesModelOrder;
use MagentoSalesRuleModelResourceModelCoupon as CouponResource;
use MagentoSalesRuleModelCouponFactory;
class Paymentcancel implements ObserverInterface
{
protected $couponFactory;
protected $couponResource;
public function __construct(
CouponFactory $couponFactory,
CouponResource $couponResource
) {
$this->couponFactory = $couponFactory;
$this->couponResource = $couponResource;
}
public function execute(Observer $observer)
{
$writer = new Zend_Log_Writer_Stream(BP . '/var/log/coupon_code.log');
$customLogger = new Zend_Log();
$customLogger->addWriter($writer);
$customLogger->info('---Start payment cancel--------------eleven-pc----------------------');
$order = $observer->getEvent()->getOrder();
// Confirm order details are accessible
if ($order && $order->getId()) {
$customLogger->info('Event triggered for order ID: ' . $order->getId());
$customLogger->info('Order state: ' . $order->getState());
$customLogger->info('Order status: ' . $order->getStatus());
$couponCode = $order->getCouponCode();
// Check if the order is being set to "canceled" and a coupon code was used
if ($couponCode && $order->getState() == Order::STATE_CANCELED) {
$customLogger->info('couponCode: ' . $couponCode);
$customLogger->info('---Processing coupon reset for canceled order---');
$coupon = $this->couponFactory->create()->load($couponCode, 'code');
if ($coupon->getId()) {
// Decrease the usage count by 1
$customLogger->info('---Coupon found, resetting usage count---');
$coupon->setTimesUsed($coupon->getTimesUsed() - 1);
$this->couponResource->save($coupon);
}
}
} else {
$customLogger->info('Order not found or incomplete order data.');
}
$customLogger->info('---End sales_order_save_commit_after---');
}
}
Unfortunately, none of these events seem to work for this case.
Has anyone else faced this issue, or can you suggest an event that will be triggered when payment fails, allowing us to reset the coupon code or handle this scenario appropriately?