Magento自定义支付网关不触发“授权”或“捕获”方法


Magento Custom Payment Gateway not firing 'authorize' or 'capture' methods

所以,horray - 我正在尝试创建一个新的自定义支付网关。它旨在通过第三方 API 进行身份验证/捕获,但不需要重定向到第三方站点。

据我了解,当在Magento中下订单/最终确定订单时,网关设置为"授权和捕获",它应该从网关模型中触发"捕获"方法。目前,它没有这样做。

当然,如果我专门从"管理订单"视图中捕获,它会尝试捕获,但这需要在结帐时立即发生(同样,据我了解,它已经应该了)。

在我的网关模型中,我有以下内容(为便于阅读而截断):

<?php
class Example_Gateway_Model_Payment extends Mage_Payment_Model_Method_Cc
{
    protected $_code = 'example';
    protected $_isGateway = true;
    protected $_canAuthorize = true;
    protected $_canCapture = true;
    protected $_canUseInternal = true;
    protected $_canUseCheckout = true;
    // This is an empty block class that extends Mage_Payment_Block_Form_Cc
    protected $_formBlockType = 'example/form_example';
    public function authorize(Varien_Object $payment, $amount)
    {
        Mage::log('Authorizing!');
    }
    public function capture(Varien_Object $payment, $amount)
    {
        Mage::log('** Capturing **');
        // Third-party API stuff would go here, with exceptions being thrown if the gateway determines they've provided an invalid card, etc.
    }
    public function assignData($data)
    {
        Mage::log('Assigning Data');
    }
}

这种支付模型本身绝对有效 - 我获得assignData()validate()的日志记录输出,以及__construct()添加它。但无论我做什么,捕获或授权方法都不会在实际下订单时触发。

我的配置.xml读起来有点像这样:

<?xml version="1.0"?>
<config>
    <modules>
        <Example_Gateway>
            <version>0.0.5</version>
        </Example_Gateway>
    </modules>
    <global>
        <blocks>
            <gateway>
                <class>Example_Gateway_Block</class>
            </gateway>
        </blocks>
        <models>
            <gateway>
                <class>Example_Gateway_Model</class>
            </gateway>
        </models>
        <helpers>
            <gateway>
                <class>Example_Gateway_Helper</class>
            </gateway>
        </helpers>
    </global>
    <frontend>
      <!-- Snip.. Nothing special here -->
    </frontend>
    <default>
        <payment>
            <gateway>
                <sort_order>0</sort_order>
                <model>gateway/payment</model>
                <enabled>1</enabled>
                <order_staus>processing</order_status>
                <payment_action>authorize_capture</payment_action>
                <cctypes>VI,MC,AE,DI</cctypes>
                <useccv>1</useccv>
            </gateway>
        </payment>
    </default>
</config>

我不认为需要资源模型,因为我不需要任何额外的表;我的期望是它只会使用sales_flat_order_payment和相关表来存储任何与网关相关/提供的数据(txn id 等)

同样,我只是扩展默认的 CC 块以获取标准付款表单。

我错过了什么?它必须是我忽略的小而简单的东西。

提前感谢!


更新:到目前为止,我已经实现了一种解决方法,该解决方法使用手动调用 capture() 方法的 checkout_type_onepage_save_order 事件的观察者 - 但我很确定这不是正确的方法。

我认为如果网关设置为authorize_capture,Magento应该在初始订单保存时自动调用capture()并没有错,对吧..?

解决了!你需要这个:

protected $_isInitializeNeeded      = false;

我不知道为什么这是必要的,但在这一点上,我已经放弃了试图弄清楚 magento 的原因,转而真正完成工作。我遇到了与您完全相同的问题,当我通过源代码跟踪它时,我发现当isInitializeNeed返回true时,Payment.php没有调用_authorize。因此,将这条线粘贴到您的模型中,它将起作用。

我认为该方法应该是:"authorize_capture"而不是配置中所述的"捕获"

<payment_action>authorize_capture</payment_action>

诸如此类:

public function authorize_capture(Varien_Object $payment, $amount)
{
    Mage::log('** Capturing **');
    // Third-party API stuff would go here, with exceptions being thrown if the gateway determines they've provided an invalid card, etc.
}

我有一个类似的问题,"授权"方法根本没有被触发,因为"authorize_action"是空的。我能够通过在方法本身中对其进行硬编码来解决此问题。调用"getConfigPaymentAction"来获取授权方法。

public function getConfigPaymentAction() {
    return 'authorize';
} 

好吧,我使用观察器手动调用捕获方法。
不是最优雅的解决方案,但它运行良好。