amzn / amazon-payments-magento-2-plugin

Extension to enable Amazon Pay on Magento 2
https://amzn.github.io/amazon-payments-magento-2-plugin/
Apache License 2.0
109 stars 77 forks source link

Issue: Order confirmation email send to customer for declined payment. #1251

Open mohit-sharma-rp opened 4 weeks ago

mohit-sharma-rp commented 4 weeks ago

What I expected

Order confirmation email should send only if payment is confirmed.

What happened instead

Customer receiving order confirmation email for declined payment.

Steps to reproduce the issue

Your setup

More details about the issue:

The issue is happening due to the AmazonPay module is placing an order before the payment is initialized here https://github.com/amzn/amazon-payments-magento-2-plugin/blob/658f83185f63ff36d5a6416e7f4740a293412c62/Model/CheckoutSessionManagement.php#L873 It uses Magento's placeOrder method to place the order, and I found that Magento triggers the order confirmation email after the order is placed via the event sales_model_service_quote_submit_success through the observer https://github.com/magento/magento2/blob/df07984301dc3aa80958544f20d1c28141a1e105/app/code/Magento/Quote/Observer/SubmitObserver.php#L62

So in this case, when a user places an order with AmazonPay, they will always receive the order confirmation email without the payment status being checked.

Solution:

I have created a beforeExecute plugin for the observer (sales_model_service_quote_submit_success) to prevent the order confirmation email from being triggered before the payment is completed by AmazonPay.

public function beforeExecute(SubmitObserver $subject, Observer $observer): array
    {
        $order = $observer->getEvent()->getOrder();
        $payment = $order->getPayment();
        $method = $payment->getMethodInstance();
        $methodCode = $method->getCode();
        if (stripos($methodCode, 'amazon_payment') !== false) {
            $order->setCanSendNewEmailFlag(false);
         }
         return [$observer];
      }

Trigger an order confirmation email after payment confirmed here https://github.com/amzn/amazon-payments-magento-2-plugin/blob/658f83185f63ff36d5a6416e7f4740a293412c62/Model/CheckoutSessionManagement.php#L642

sgabhart22 commented 4 weeks ago

Thanks for raising the issue @mohit-sharma-rp , this is a problem we're aware of and have a fix ready for an upcoming release. You've suggested a different approach though, we'll compare the two and see if this looks like a better solution!

Thanks again, Spencer