Happy New Year and welcome back to our Magento blog! I hope that you had a wonderful holiday. And now let’s come back to our Topic 9 with the part of Refund. A refund usually refers to the reimbursement of funds to a customer for a product or service provided. Magento framework will create a Credit Memo for the returned orders.

1. Overview

Refund, in somehow, is a reverse process of the payment process. Magento uses the mode      Mage_Sales_Model_Order_Creditmemo to work with the data of the refund.

Similar with the “Invoice” part in Topic 9 – Part 2, Creditmemo’s model also has some methods: getAllItems() to collect all refunded items, collectTotals() to calculating credit memo total.  And an important method of this model is refund():

public function refund()
{
$this->setState(self::STATE_REFUNDED);
$orderRefund = Mage::app()->getStore()->roundPrice(
$this->getOrder()->getTotalRefunded()+$this->getGrandTotal()
);
$baseOrderRefund = Mage::app()->getStore()->roundPrice(
$this->getOrder()->getBaseTotalRefunded()+$this->getBaseGrandTotal()
);
if ($baseOrderRefund > Mage::app()->getStore()->roundPrice($this->getOrder()->getBaseTotalPaid())) {

$baseAvailableRefund = $this->getOrder()->getBaseTotalPaid()- $this->getOrder()->getBaseTotalRefunded();

Mage::throwException(
Mage::helper('sales')->__('Maximum amount available to refund is %s',
$this->getOrder()->formatBasePrice($baseAvailableRefund)
)
);
}
$order = $this->getOrder();
...

if ($this->getInvoice()) {
$this->getInvoice()->setIsUsedForRefund(true);
$this->getInvoice()->setBaseTotalRefunded(
$this->getInvoice()->getBaseTotalRefunded() + $this->getBaseGrandTotal()
);
$this->setInvoiceId($this->getInvoice()->getId());
}

if (!$this->getPaymentRefundDisallowed()) {
$order->getPayment()->refund($this);
}

Mage::dispatchEvent('sales_order_creditmemo_refund', array($this->_eventObject=>$this));
return $this;
}

Refund can be conducted online or offline.  Similar to the invoice creation, creditmemo in Magento is also created automatically when the system makes refunds for an order through a pay gate (e.g. PayPal).

The table for storing refund data is similar to that of invoice, which includes: sales_flat_creditmemo, sales_flat_creditmemo_comment, sales_flat_creditmemo_grid, sales_flat_creditmemo_item.

2. Creditmemo collect total

The calculation of the totality of creditmemo (Subtotal, discount, tax…) is also similar to that of an invoice.

However, the calculation of shipping fee and grand totality of creditmemo is a little bit different. When a refund is conducted offline, the shipping fee is calculated based on the data that admins import and collected for this credit memo. Moreover, the two other boxes (Adjustment Refund and Adjustmend Fee) are added to enable the changing the values of grand totality ad credit memo. The code of collecting creditmemo’s grand totality is as follows:

class Mage_Sales_Model_Order_Creditmemo_Total_Grand extends Mage_Sales_Model_Order_Creditmemo_Total_Abstract
{
public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
{
$grandTotal = $creditmemo->getGrandTotal();
$baseGrandTotal = $creditmemo->getBaseGrandTotal();

$grandTotal+= $creditmemo->getAdjustmentPositive();
$baseGrandTotal+= $creditmemo->getBaseAdjustmentPositive();

$grandTotal-= $creditmemo->getAdjustmentNegative();
$baseGrandTotal-= $creditmemo->getBaseAdjustmentNegative();

$creditmemo->setGrandTotal($grandTotal);
$creditmemo->setBaseGrandTotal($baseGrandTotal);

$creditmemo->setAdjustment($creditmemo->getAdjustmentPositive()-$creditmemo->getAdjustmentNegative());
$creditmemo->setBaseAdjustment($creditmemo->getBaseAdjustmentPositive()-$creditmemo->getBaseAdjustmentNegative());

return $this;
}
}

I think it’s enough for today. Hope that it will helpful for you.
If you are interested in Magento tutorials, subscribe to our Magento Tutorials

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

1 Comment

  1. Refund features is really important to every serious store manager. Thanks for great article. As I tested many Magento 3rd party extensions I found that sometime there is a problem with partial refund – for example for rewards and points or comission in affiliate modules. The only option is to full refund, if I sell bulk qty-ies and custome would like to refund partially 30% of order. But I cannot cut the reward points to 0, or the commission to partner to $0. It would be great if somebody could enhance partial refund in Magento to use it in any other modules like rewards.

Write A Comment