Magento – Order confirmation email sent before redirecting to third party payment gateway in Magento 2

magento2paymentpayment-gateway

I have created one third party Payment gateway.

It is working fine except, It is sending two order confirmation email. One email I have added in my custom module success controller to send email after validate Transaction Id. But one default email is also triggered by magento after place order.

Below is My Payment module payment.js file:

define(
    [
        'jquery',
        'ko',
        'mage/url',
        'Magento_Checkout/js/view/payment/default',
        'Magento_Checkout/js/model/quote',
        'Magento_Customer/js/model/customer'
    ],
    function ($, ko, url, Component, quote, customer) {
        'use strict';
        return Component.extend({
            defaults: {
                template: 'Stack_Custompay/payment/custompay-form'
            },
            redirectAfterPlaceOrder: false,
            /** Returns payment method instructions */
            getInstructions: function() {
                return window.checkoutConfig.payment.instructions[this.item.method];
            },
            afterPlaceOrder: function () {
                $.ajax({
                    type: 'POST',
                    url: url.build('custompay/payment/order'),
                    success: function (response) {
                        if (response.success) {
                            var order_id = response.order_id;
                            var amount = response.amount;
                            var billing_address = quote.billingAddress();
                            var name =  billing_address.firstname;
                            var contact = billing_address.telephone;
                            var email = customer.customerData.email;
                            var data = {
                              amount: amount,
                              email: email,
                              contact: contact,
                              order_id:order_id
                            };
                            custompay.createPayment(data);
                        }
                    },
                });
            }
        });
    }
);

I have checked many posts and found there is some option to disable default order email by plugin. but I think it is not the standard approach. I read, there are some changes we need to perform in payment.js file to stop these confirmation email as implemeneted in braintree payment gateway.

how I could achieve this in the right way? Any help would be appreciated. Thanks.

Best Answer

Magento uses \Magento\Sales\Model\Order\Email\Sender\OrderSender to send order emails.

I would suggest replacing OrderService dependency in the \Magento\Quote\Observer\Webapi\SubmitObserver by your own implementation via di.xml in the following way:

class CustomOrderSender extends \Magento\Sales\Model\Order\Email\Sender\OrderSender
{
    public function send(Order $order, $forceSyncMode = false)
    {
        // custom implementation
    }
}

and specify it in di.xml:

<type name="Magento\Quote\Observer\Webapi\SubmitObserver">
    <arguments>
        <argument name="orderSender" xsi:type="object">NS\CustomOrderSender</argument>
    </arguments>
</type>

So, you don't need to use your own controller and an email will be sent only once.