Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mohit-sharma-rp opened this issue Oct 23, 2024 · 1 comment

Comments

@mohit-sharma-rp
Copy link

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

  • Added a product to the cart
  • Go to the cart page, and click on the Amazon Pay button to proceed to checkout.
  • Use a credit card in Amazon Pay where the payment is declined.

Your setup

  • Magento version: 2.4.6-p7
  • Amazon Pay Extension Version: 5.17.1
  • Magento Edition: Enterprise Cloud

More details about the issue:

The issue is happening due to the AmazonPay module is placing an order before the payment is initialized here

$orderId = $this->cartManagement->placeOrder($quote->getId());

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

protected function setProcessing($payment, $payInvoice = true)

@sgabhart22
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants