Magento - 当我的订单取消或退款时如何运行代码

发布于 2024-10-14 12:43:34 字数 144 浏览 2 评论 0原文

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

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

My payment module is required to sent notifications to the payment service if an order is canceled or refunded. I assume that the "Cancel" button on the order page (in the administration backend) will cancel the order, and that the "Credit memo" button (after an invoice has been created) will refund the order.

How do I run my code on these events? I tried using the cancel() method in my payment method model, but the code did not run.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

楠木可依 2024-10-21 12:43:34

您的付款方式似乎未使用交易或未创建授权交易 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 ...
    }
}

Seems like your payment method is not using transactions or does not create authorization transaction id. It is common beginner mistake in Payment gateways development.

To enable your payment gateway with online actions you need to implement something like this in your payment method:

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 ...
    }
}
秋叶绚丽 2024-10-21 12:43:34

在 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 特有的。使用一些日志记录功能在您的类中定义所有五个,如果您想确定的话,可以在您的开发系统上查看一些假订单。

No observable events are fired during the Payment processing stage of Magento. Instead, you define a class for whatever gateway you're implementing, and then define methods that Magento will automatically call as an order makes it's way through the various payment methods.

Poke around the base abstract payment class to see the various methods that will be called during payment processing. Define the same methods in your class to hook into the payment process at whatever point you'd like.

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)
    ...

I don't do a lot of Payment Gateway implementations, but I'm guessing that refund is the method you want for credit memos, and capture is the one for invoices. It looks like the cancel method is something specific to Google Checkout. Define all five in your class with some logging functions and walk though some fake orders on your development system if you want to know for sure.

羅雙樹 2024-10-21 12:43:34

Magento 的事件挂钩可能会有所帮助。可以在此处找到事件列表(我认为有点过时)。还有一篇关于 Magneto 事件如何工作的有用文章 这里

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

祝你好运!

Magento has event hooks that might be helpful. A list of events (a bit outdated, I think) can be found here. And there is a useful article about how Magneto's events work here.

Also, looking at existing payment extensions could be useful. I would not be surprised if similar events are dispatched by Google Checkout for order cancellations. The Extension repository will have lots of payment methods to look at.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文