我遇到了同样的问题。在花了几天时间尝试调试 getOrderPlaceRedirectUrl 之后,我最终完成了重定向到支付网关的 JavaScript(有点像 hack)版本。我的猜测是 Magento 2 根本没有在新的结帐流程中实现这一点。我可能是错的,因此请尝试与 Magento 团队核实是否有某种设计的方法可以做到这一点。我根本没有更多的时间去调查。
我所做的是修改付款方式 JavaScript 渲染器文件,并在那里实现我的重定向。像这样的东西:
/*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 表单提交到支付网关页面。
这里重要的是要意识到:
- “mymodule/standard/redirect”不会从订单页面接收任何数据,因此只有在结账过程中无需客户输入数据即可构建外部重定向时,此方法才有可能。
- “mymodule/standard/redirect”基本上是硬编码在 JavaScript 文件中,因此是“hack”参考。