【问题标题】:How to redirect from payment gateway to success page using the callback URL in Magento 2.x如何使用 Magento 2.x 中的回调 URL 从支付网关重定向到成功页面
【发布时间】:2020-04-28 04:32:29
【问题描述】:

我已在我的 magento 网站中集成了 Razorpay 支付网关。它在网络和移动浏览器中工作得非常好。但是,当我尝试使用应用内浏览器(来自 Instagram、Facebook)进行付款时,我遇到了空白页问题。所以我找到了需要将回调 URL 和其他输入参数一起传递给支付网关的解决方案。这里卡住了,这里的回调URL应该是什么?谁能帮我解决这个问题!

这是我的代码:

/app/code/Razorpay/Magento/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js

define(
    [
        'Magento_Checkout/js/view/payment/default',
        'Magento_Checkout/js/model/quote',
        'jquery',
        'ko',
        'Magento_Checkout/js/model/payment/additional-validators',
        'Magento_Checkout/js/action/set-payment-information',
        'mage/url',
        'Magento_Customer/js/model/customer',
        'Magento_Checkout/js/action/place-order',
        'Magento_Checkout/js/model/full-screen-loader',
        'Magento_Ui/js/model/messageList'
    ],
    function (Component, quote, $, ko, additionalValidators, setPaymentInformationAction, url, customer, placeOrderAction, fullScreenLoader, messageList) {
        'use strict';

        return Component.extend({
            defaults: {
                template: 'Razorpay_Magento/payment/razorpay-form',
                razorpayDataFrameLoaded: false,
                rzp_response: {}
            },
            getMerchantName: function() {
                return window.checkoutConfig.payment.razorpay.merchant_name;
            },

            getKeyId: function() {
                return window.checkoutConfig.payment.razorpay.key_id;
            },

            context: function() {
                return this;
            },

            isShowLegend: function() {
                return true;
            },

            getCode: function() {
                return 'razorpay';
            },

            isActive: function() {
                return true;
            },

            isAvailable: function() {
                return this.razorpayDataFrameLoaded;
            },

            handleError: function (error) {
                if (_.isObject(error)) {
                    this.messageContainer.addErrorMessage(error);
                } else {
                    this.messageContainer.addErrorMessage({
                        message: error
                    });
                }
            },

            initObservable: function() {
                var self = this._super();              //Resolves UI Error on Checkout


                if(!self.razorpayDataFrameLoaded) {
                    $.getScript("https://checkout.razorpay.com/v1/checkout.js", function() {
                        self.razorpayDataFrameLoaded = true;
                    });
                }

                return self;
            },

            /**
             * @override
             */
             /** Process Payment */
            preparePayment: function (context, event) {

                if(!additionalValidators.validate()) {   //Resolve checkout aggreement accept error
                    return false;
                }

                var self = this,
                    billing_address,
                    rzp_order_id;

                fullScreenLoader.startLoader();
                this.messageContainer.clear();

                this.amount = quote.totals()['base_grand_total'] * 100;
                billing_address = quote.billingAddress();

                this.user = {
                    name: billing_address.firstname + ' ' + billing_address.lastname,
                    contact: billing_address.telephone,
                };

                if (!customer.isLoggedIn()) {
                    this.user.email = quote.guestEmail;
                }
                else
                {
                    this.user.email = customer.customerData.email;
                }

                this.isPaymentProcessing = $.Deferred();

                $.when(this.isPaymentProcessing).done(
                    function () {
                        self.placeOrder();
                    }
                ).fail(
                    function (result) {
                        self.handleError(result);
                    }
                );

                self.getRzpOrderId();

                return;
            },

            getRzpOrderId: function () {
                var self = this;

                $.ajax({
                    type: 'POST',
                    url: url.build('razorpay/payment/order'),

                    /**
                     * Success callback
                     * @param {Object} response
                     */
                    success: function (response) {
                        fullScreenLoader.stopLoader();
                        if (response.success) {
                            self.renderIframe(response);
                        } else {
                            self.isPaymentProcessing.reject(response.message);
                        }
                    },

                    /**
                     * Error callback
                     * @param {*} response
                     */
                    error: function (response) {
                        fullScreenLoader.stopLoader();
                        self.isPaymentProcessing.reject(response.message);
                    }
                });
            },

            renderIframe: function(data) {
                var self = this;

                this.merchant_order_id = data.order_id;

                var options = {
                    key: self.getKeyId(),
                    name: self.getMerchantName(),
                    amount: data.amount,
                    handler: function (data) {
                        self.rzp_response = data;
                        self.placeOrder(data);
                    },
                    order_id: data.rzp_order,
                    modal: {
                        ondismiss: function() {
                            self.isPaymentProcessing.reject("Payment Closed");
                        }
                    },
                    notes: {
                        merchant_order_id: '',
                        merchant_quote_id: data.order_id
                    },
                    prefill: {
                        name: this.user.name,
                        contact: this.user.contact,
                        email: this.user.email
                    },
                    callback_url: url.build('rest/default/V1/carts/mine/payment-information', {}),
                    _: {
                        integration: 'magento',
                        integration_version: data.module_version,
                        integration_parent_version: data.maze_version,
                    }
                };

                if (data.quote_currency !== 'INR')
                {
                    options.display_currency = data.quote_currency;
                    options.display_amount = data.quote_amount;
                }

                this.rzp = new Razorpay(options);

                this.rzp.open();
            },

            getData: function() {
                return {
                    "method": this.item.method,
                    "po_number": null,
                    "additional_data": {
                        rzp_payment_id: this.rzp_response.razorpay_payment_id,
                        order_id: this.merchant_order_id,
                        rzp_signature: this.rzp_response.razorpay_signature
                    }
                };
            }
        });
    }


);

上面代码中renderIframe函数会将参数传递给支付网关,这里我需要传递回调URL。我尝试将其设置为rest/default/V1/carts/mine/payment-information,但出现401 Unauthorised 错误。请帮我解决这个问题。

【问题讨论】:

    标签: php magento2 payment-gateway


    【解决方案1】:

    我对亚马逊支付也做了同样的事情。 我记得你应该对这部分代码采取行动:

    function () {
       self.placeOrder();
    }
    

    我是如何改变的

    function () {
        $.when(self.placeOrder()).done(
            function () {
               ///redirect;
            }
    }
    

    但是当我在浏览器上尝试时,我仍然遇到了一些错误的情况,然后我决定通过使用 jquery 放置一个事件来解决这个问题:

    $( document ).ajaxStop(function() {
        //redirect
    });
    

    这是正确的,因为在所有 ajax 完成后,我们得到了订单。是时候重定向了。

    希望这会有所帮助。

    【讨论】:

    • 在这里感谢您的帮助。正如我上面提到的,重定向流程在 Web 浏览器和移动浏览器中也可以正常工作。唯一的问题在于应用内浏览器,因为 razorpay 支付网关需要 callback_url 重定向到成功页面(正如他们在文档 razorpay.com/docs/payment-gateway/callback-url 中提到的那样)。那么我应该在 callbak_url 的位置给出什么?
    猜你喜欢
    • 2013-03-21
    • 1970-01-01
    • 2013-01-21
    • 1970-01-01
    • 2012-06-02
    • 2021-04-28
    • 2016-04-17
    • 2014-07-15
    • 2016-04-03
    相关资源
    最近更新 更多