Magento 2-没有重定向到外部付款提供商 [英] Magento 2 - No redirect to external payment provder

查看:157
本文介绍了Magento 2-没有重定向到外部付款提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我开始研究Magento 2的支付模块.经过多次尝试和错误,我现在有一个可以配置且有限制的支付模块.

Today I started working on a payment module for Magento 2. After a lot of try and errors I now have a payment module that can be configured and has restrictions.

现在,下一步是在下订单后启动交易请求.在Magento 1中,我将使用getOrderPlaceRedirectUrl()返回将消费者重定向到的网址.

Now, the next step would be to start a transaction request after placing the order. Within Magento 1 I would use getOrderPlaceRedirectUrl() to return an url where the consumer would be redirected to.

当我使用Magento 2尝试此操作时,将调用该函数,但不会重定向到返回URL.

When I try this with Magento 2 then the function is called but no redirect is done to the return url.

有人知道这在Magento 2中是否已更改,或者我做错了吗?

Does anyone know if this has changed within Magento 2 or what I'm doing wrong?

函数如下:

public function getOrderPlaceRedirectUrl(){


    return $this->_urlBuilder->getUrl('module/controller/method');

}

推荐答案

我遇到了同样的问题.在花了几天时间尝试调试getOrderPlaceRedirectUrl之后,我最终完成了重定向到付款网关的JavaScript(有点像hack)版本. 我的猜测是Magento 2根本没有在新的结帐流程中实现该功能.我可能是错的,因此请尝试与Magento团队联系,是否有某种设计的方法可以做到这一点.我根本没有更多时间去调查.

I've faced the same problem. After spending few days trying to debug getOrderPlaceRedirectUrl I've ended up doing the JavaScript ( kind of a hack ) version of redirection to payment gateway. My guess is that Magento 2 simply didn't implement that yet in new checkout flow. I could be wrong, so try to check with Magento team if there's some designed way to do this. I simply didn't have more time to investigate.

我所做的是修改付款方式JavaScript渲染器文件,并在那里实现我的重定向.像这样:

What I've done, is to modify the payment method JavaScript renderer file, and implement my redirection there. Something like this:

/*browser:true*/
/*global define*/
define(
[
    'jquery',
    'Magento_Checkout/js/view/payment/default',
    'Magento_Checkout/js/action/place-order',
    'Magento_Checkout/js/action/select-payment-method',
    'Magento_Customer/js/model/customer',
    'Magento_Checkout/js/checkout-data',
    'Magento_Checkout/js/model/payment/additional-validators',
    'mage/url',
],
function (
    $,
    Component,
    placeOrderAction,
    selectPaymentMethodAction,
    customer,
    checkoutData,
    additionalValidators,
    url) {
    'use strict';

    return Component.extend({
        defaults: {
            template: 'My_Module/payment/form-template'
        },

        placeOrder: function (data, event) {
            if (event) {
                event.preventDefault();
            }
            var self = this,
                placeOrder,
                emailValidationResult = customer.isLoggedIn(),
                loginFormSelector = 'form[data-role=email-with-possible-login]';
            if (!customer.isLoggedIn()) {
                $(loginFormSelector).validation();
                emailValidationResult = Boolean($(loginFormSelector + ' input[name=username]').valid());
            }
            if (emailValidationResult && this.validate() && additionalValidators.validate()) {
                this.isPlaceOrderActionAllowed(false);
                placeOrder = placeOrderAction(this.getData(), false, this.messageContainer);

                $.when(placeOrder).fail(function () {
                    self.isPlaceOrderActionAllowed(true);
                }).done(this.afterPlaceOrder.bind(this));
                return true;
            }
            return false;
        },

        selectPaymentMethod: function() {
            selectPaymentMethodAction(this.getData());
            checkoutData.setSelectedPaymentMethod(this.item.method);
            return true;
        },

        afterPlaceOrder: function () {
            window.location.replace(url.build('mymodule/standard/redirect/'));
        }
    });
}
);

afterPlaceOrder是此处的主要修改. 单击下单"后,将立即重定向到内部控制器"mymodule/standard/redirect".然后使用此控制器来构造您的外部重定向,通常是POST表单提交到付款网关页面.

afterPlaceOrder is the key modification here. This will redirect to internal controller "mymodule/standard/redirect" right after Place order is clicked. Then use this controller to construct your external redirection, usually POST form submitted to payment gateway page.

重要的是要意识到:

  1. "mymodule/standard/redirect"将不会从订单页面接收任何数据,因此,仅当您可以构造外部重定向而在结帐过程中客户没有输入数据时,这种方法才可行.
  2. "mymodule/standard/redirect"基本上是硬编码在JavaScript文件中的,因此是"hack"引用.

这篇关于Magento 2-没有重定向到外部付款提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆