Magento - How can I run code when my order is canc

2019-04-11 10:20发布

问题:

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.

回答1:

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


回答2:

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.



回答3:

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!