【问题标题】:Magento - How can I run code when my order is canceled or refundedMagento - 我的订单被取消或退款时如何运行代码
【发布时间】:2011-06-15 08:19:26
【问题描述】:

如果订单被取消或退款,我的支付模块需要向支付服务发送通知。我假设订单页面上的“取消”按钮(在管理后端)将取消订单,并且“信用备忘录”按钮(在创建发票后)将退还订单。

如何在这些事件上运行我的代码?我尝试在我的付款方式模型中使用 cancel() 方法,但代码没有运行。

【问题讨论】:

  • 我可能会离开这里,但您会再次收取费用,但金额为负数吗?原始费用:5.00 美元,取消/退款费用 - 5.00 美元只是一个想法

标签: php magento magento-1.4


【解决方案1】:

您的付款方式似乎未使用交易或未创建授权交易 ID。这是支付网关开发中常见的初学者错误。

要通过在线操作启用您的支付网关,您需要在您的支付方式中实现类似的内容:

class MyBest_Payment_Model_Method extends Mage_Payment_Model_Method_Abstract
{
    protected $_canAuthorize            = true; // Set true, if you have authorization step.
    protected $_canCapture              = true; // Set true, if you payment method allows to perform capture transaction (usally only credit cards methods)
    protected $_canRefund               = true; // Set true, if online refunds are available
    protected $_canVoid                 = true; // Set true, if you can cancel authorization via API online

    public function authorize(Varien_Object $payment, $amount)
    { 

        // ... You payment method authorization goes here ...
        // Here goes retrieving or generation non-zero, 
        // non-null value as transaction ID. 
        $transactionId = $api->someCall(); 
        // Setting tranasaction id to payment object
        // It is improtant, if you want perform online actions 
        // in future with this order!
        $payment->setTransactionId($transactionId); 

        // ... some other your actions ... 
        return $this;
    }

    public function void(Varien_Object $payment)
    {
        // ... some actions for sending cancel notification to your payment gateway
    }

    public function refund(Varien_Object $payment, $amount)
    {
        // ... some actions for performing an online refund ...
    }
}

【讨论】:

  • 我已经尝试过了,但效果并不好。退款() 方法似乎只适用于通过发票页面完成的在线退款(不是订单页面上的贷项通知单按钮)。根本没有调用 void() 方法,也没有调用 authorize() - 没有创建事务
  • @Troubled Magento 用户,您应该在订单上使用它并设置适当的交易。旧订单将不起作用,因为当时没有存储此类数据。顺便说一句,订单页面上的 crreadit memo 按钮创建离线退款(即退款而不发送通知)。在授权方法中,您应该创建一个事务,而不是 Magento,因为事务取决于您的网关。
  • @Troubled Magento 用户,只有在您打开授权交易时才会调用 void 方法,即,如果没有捕获付款(没有发票)或者它能够执行部分捕获(发票不包括所有订单项目)。好像 Magento 支付系统需要好好解释一下,可惜现在没有足够的时间写文章...
  • 嗨,您注意到退款和作废功能需要“setTransactionId”才能正常工作,但是在最新版本的 Magento (1.6 / 1.7) 中,我只能在付款表中找到 last_trans_id在数据库中,这是改变还是我遗漏了什么?
  • @sg3s setTransactionId() 开始在 transactions 表中创建新记录,它不再存储在 order_payment 表中,最后一个 trans id 是对 transactions 表中可用的交易的引用。
【解决方案2】:

在 Magento 的付款处理阶段不会触发任何可观察的事件。相反,您为要实现的任何网关定义一个类,然后定义 Magento 将自动调用的方法,因为订单通过各种支付方式。

查看基本的抽象支付类以查看在支付处理期间将调用的各种方法。在您的课程中定义相同的方法,以便在您想要的任何时间点连接到付款流程。

File: app/code/core/Mage/Payment/Model/Method/Abstract.php

class abstract class Mage_Payment_Model_Method_Abstract
{

    /**
     * Authorize
     *
     * @param   Varien_Object $orderPayment
     * @return  Mage_Payment_Model_Abstract
     */
    public function authorize(Varien_Object $payment, $amount)
    ...     
    /**
     * Capture payment
     *
     * @param   Varien_Object $orderPayment
     * @return  Mage_Payment_Model_Abstract
     */
    public function capture(Varien_Object $payment, $amount)    
    ... 

    /**
     * Void payment
     *
     * @param   Varien_Object $invoicePayment
     * @return  Mage_Payment_Model_Abstract
     */
    public function void(Varien_Object $payment)
    ...    

    /**
     * Refund money
     *
     * @param   Varien_Object $invoicePayment
     * @return  Mage_Payment_Model_Abstract
     */
    //public function refund(Varien_Object $payment, $amount)
    public function refund(Varien_Object $payment, $amount)
    ...


    /**
     * Cancel payment (GoogleCheckout)
     *
     * @param   Varien_Object $invoicePayment
     * @return  Mage_Payment_Model_Abstract
     */
    public function cancel(Varien_Object $payment)
    ...

我没有做很多支付网关实现,但我猜refund 是您想要用于贷项通知单的方法,而capture 是用于发票的方法。看起来 cancel 方法是特定于 Google Checkout 的。用一些日志记录函数在你的类中定义所有五个,如果你想确定的话,可以遍历你的开发系统上的一些假订单。

【讨论】:

    【解决方案3】:

    Magento 有可能有用的事件挂钩。可以在here 找到事件列表(我认为有点过时)。还有一篇关于万磁王事件如何运作的有用文章here

    此外,查看现有的付款扩展程序可能很有用。如果 Google Checkout 发送类似事件以取消订单,我不会感到惊讶。扩展存储库将有很多支付方式可供查看。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 2018-03-14
      • 2012-03-10
      • 2015-01-28
      • 2019-08-31
      • 2017-07-30
      • 2014-09-07
      相关资源
      最近更新 更多