Magento2配置checkmo支付payment_action后下单跳回购物车如何处理
问题根因说明
原生Magento\OfflinePayments\Model\Checkmo属于离线支付类型,默认没有实现网关支付所需的authorize、capture逻辑,配置payment_action为authorize_capture后,下单流程调用对应方法失败触发回滚,所以会跳转回购物车。
以下是两种可落地的解决方案:
方案1:自定义支付模型支持授权捕获动作(保留原有payment_action配置)
- 第一步:在你的自定义模块中新增支付模型,继承原生Checkmo类,示例路径:
app/code/Vendor/Module/Model/Payonbill.php,代码示例:
<?php namespace Vendor\Module\Model; class Payonbill extends \Magento\OfflinePayments\Model\Checkmo { protected $_isGateway = true; // 声明为网关支付类型,支持授权捕获动作 protected $_canAuthorize = true; protected $_canCapture = true; // 重写授权方法,无真实网关逻辑直接返回实例 public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount) { return $this; } // 重写捕获方法,无真实网关逻辑直接返回实例 public function capture(\Magento\Payment\Model\InfoInterface $payment, $amount) { return $this; } }
- 第二步:修改你之前的配置,把
model节点的值替换为你自定义的模型类,保留payment_action配置即可:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <payment> <checkmo> <active>1</active> <model>Vendor\Module\Model\Payonbill</model> <order_status>processing</order_status> <title>账期支付</title> <allowspecific>0</allowspecific> <group>offline</group> <payment_action>authorize_capture</payment_action> </checkmo> </payment> </default> </config>
- 优势:完全复用原有授权捕获流程,无需额外写事件逻辑,符合Magento支付流程规范。
方案2:移除payment_action配置,通过事件自动创建发票触发目标事件
- 第一步:删除你现有配置中的
<payment_action>authorize_capture</payment_action>节点,保证下单流程正常。 - 第二步:在自定义模块中注册
sales_order_place_after事件观察者,路径app/code/Vendor/Module/etc/events.xml:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="sales_order_place_after"> <observer name="vendor_module_auto_invoice_payonbill" instance="Vendor\Module\Observer\AutoInvoiceForPayonbill" /> </event> </config>
- 第三步:编写观察者类,当订单支付方式为checkmo时自动创建发票并标记为已支付,该操作会自动触发
sales_order_invoice_pay事件:
<?php namespace Vendor\Module\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Sales\Model\Order\InvoiceFactory; use Magento\Sales\Model\Service\InvoiceService; class AutoInvoiceForPayonbill implements ObserverInterface { protected $invoiceFactory; protected $invoiceService; public function __construct( InvoiceFactory $invoiceFactory, InvoiceService $invoiceService ) { $this->invoiceFactory = $invoiceFactory; $this->invoiceService = $invoiceService; } public function execute(Observer $observer) { $order = $observer->getEvent()->getOrder(); // 只处理账期支付的订单 if ($order->getPayment()->getMethod() != 'checkmo') { return $this; } // 判断订单是否允许创建发票 if (!$order->canInvoice()) { return $this; } // 创建全额发票 $invoice = $this->invoiceFactory->create()->setOrder($order); $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE); $invoice->register(); $invoice->pay(); // 此方法会触发sales_order_invoice_pay事件 // 保存发票和订单 $order->addRelatedObject($invoice); $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING) ->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING); return $this; } }
- 优势:无需修改原有支付模型逻辑,改动量小,风险更低。
补充优化建议
如果需要和原生的支票/汇款支付区分开,建议不要直接复用checkmo的支付方法编码,在自定义模块中重新声明一个编码为payonbill的独立支付方法,继承Checkmo的逻辑即可,避免影响原有支付方式的使用。
内容的提问来源于stack exchange,提问作者Dirkos
相关产品推荐
相关产品推荐

