How to cancel orders using the payment method in magento

I am creating an Open Source extension for Magento. This is in the very early stages. I am struggling with the problem of canceling orders. I found some solution here.

Magento - How can I run the code when my order is canceled or returned .

But whenever I cancel an order, it does not call either a void (in the case of payment authorization only) or a refund (in the case of a payment using authorization).

When I use capture-return, he says that the order cannot be reversed.

When I use authorize-void, it says the order is canceled. But the Void () function was not called at all. I saved some functions of Mage :: Log () inside. Which do not appear in the log file.

I do not understand what is wrong.

Here is the code. This is a payment method model.

<?php 
class Package_Cashondelivery_Model_Createorder extends Mage_Payment_Model_Method_Abstract
{
    protected $_code = 'cashondelivery';
    protected $_canCapture = true;
    protected $_canUseCheckout = true;
    protected $_canFetchTransactionInfo     = true;
    protected $_isGateway                   = true;
    protected $_canUseInternal = true;
    protected $_canVoid    = true;
    protected $_canRefund = true;

    public function validate()
    {

        $paymentInfo = $this->getInfoInstance();
         if ($paymentInfo instanceof Mage_Sales_Model_Order_Payment) {
             $postCode = $paymentInfo->getOrder()->getBillingAddress()->getPostcode();

         } 
         else {
             $postCode = $paymentInfo->getQuote()->getBillingAddress()->getPostcode();
         }
         $res=Api->validatePostCode($postCode);
         $r = $res=='false'? FALSE : TRUE; 
         if (!$r) {
             Mage::throwException($this->_getHelper()->__('Sorry ! Service is not available in your area'));
         }
         return $this;
    }

    public function authorize(Varien_Object $payment, $amount)
    {
        -------------------------------
        -------------------------------
        -------------------------------
        #This is working fine
        $transactionId = Api->someCall();
        $payment->setTransactionId();
       ------------------------------- 
       -------------------------------
       -------------------------------
       -------------------------------
       -------------------------------
       -------------------------------
        return $this;
    }

    public function void(Varien_Object $payment)
    {
        if (!$this->canVoid($payment)) {
            Mage::throwException($this->_getHelper()->__('Void action is not available.'));
        }
        -------------------------------
        -------------------------------
        -------------------------------
        -------------------------------
        Mage::Log('Starting Void here....');
        $transactionId = $Payment->getTransactionId();
        Api->cancelOrder($transactionId);
        return $this;
        -------------------------------
        -------------------------------
        -------------------------------
    }
}
?>

.

<?xml version="1.0"?>
<config>
    <modules>
       <Package_Cashondelivery>
<!-- declare module version information for database updates -->
          <version>0.1.0</version>
       </Package_Cashondelivery>
    </modules>
    <global>
<!-- declare model group for new module -->
        <models>
<!-- model group alias to be used in Mage::getModel('newmodule/...') -->
            <cashondelivery>
<!-- base class name for the model group -->
                <class>Package_Cashondelivery_Model</class>
            </cashondelivery>    
        </models>
        <helpers>
            <cashondelivery>
                <class>Package_Cashondelivery_Helper</class>
            </cashondelivery>
        </helpers> 
<!-- declare resource setup for new module -->
        <resources>
<!-- resource identifier -->
            <cashondelivery_setup>
<!-- specify that this resource is a setup resource and used for upgrades -->
                <setup>
<!-- which module to look for install/upgrade files in -->
                    <module>Package_Cashondelivery</module>
                </setup>
<!-- specify database connection for this resource -->
                <connection>
<!-- do not create new connection, use predefined core setup connection -->
                    <use>core_setup</use>
                </connection>
            </cashondelivery_setup>
            <cashondelivery_write>
                <connection>
                  <use>core_write</use>
                </connection>
            </cashondelivery_write>
            <cashondelivery_read>
               <connection>
                <use>core_read</use>
              </connection>
            </cashondelivery_read>
        </resources>
    </global>
<!-- declare default configuration values for this module -->
    <default>
        <payment>
            <cashondelivery>
                <active>1</active>
                <model>cashondelivery/createorder</model>
                <order_status>Processing</order_status>
                <payment_action>authorize</payment_action>
                <title>Cash On Delivery</title>
                <example_uri>services.example.com</example_uri>
            </cashondelivery>
         </payment>
    </default>
</config>

- , .

+1
3

, "", , .

, ? , , , . , , . , ?

0

" " " " , . , -.

, , creditmemo .

, void function, IDE, Netbeans, X-Debug, cancelAction voidPaymentAction Mage_Adminhtml_Sales_OrderController. , .

, void - ...

Mage_Adminhtml_Sales_OrderControllervoidPaymentAction: 629
Mage_Sales_Model_Order_Payment: 602
Mage_Sales_Model_Order_Payment_void: 1088
Package_Cashondelivery_Model_Createorder:

, , , , void .

0

The problem is that Magento really doesn’t call either Void or Refund when clicking on cancel. When you press void, it invokes the void method, and when you click on return, it calls the return method. And guess that when you click on cancel, it actually calls the cancel method.

I agree that when you click cancel, you really want to revoke authorization, but come in handy to have separate methods in case you want to do something else. So you can do something like this:

/*
 * Your class stuff
 */

public function cancel(Varien_Object $payment){

    // void the order if canceled
    $this->void($payment);

    return $this;
}

public function void(Varien_Object $payment){

    /* Whatever you call to void a payment in your gateway */

}
0
source

Source: https://habr.com/ru/post/1787749/


All Articles