How to change order status when refund in magento?
Asked Answered
B

4

8

I am working on Magento 1.7 version. I placed a order and make payment using Paypal and refund the amount offline. Order status changed following:-

  1. Pending Payment
  2. Invoice #100000001 created
  3. Processing (IPN "Completed".Registered notification about captured amount of £1. Transaction ID: "0CT123456789874521". )
  4. Processing (Notified customer about invoice #100000001. )
  5. Credit memo #100000001 created
  6. Processing (Refunded amount of £1 offline. )
  7. Processing (IPN "Refunded". Note: Maximum amount available to refund is £0.00 )
  8. Processing (Test order has been refunded.)

Order Status is still showing processing but it should be completed.

Buckra answered 30/4, 2014 at 9:12 Comment(0)
L
6

In Magento, an order is only marked as Complete once you create an invoice and shipment for it. When you create a credit memo for an order, it instead would be marked as Closed.

If you try to set an order as complete or closed directly using the setStatus method, you will get an exception: The Order State "complete" must not be set manually. Again, these states should be set automatically by Magento.

That being said, if you really want to set these manually, you can get around it like so:

$order->setData('state', 'complete');
$order->setStatus('complete');
$history = $order->addStatusHistoryComment('Manually set order to Complete.', false);
$history->setIsCustomerNotified(false);
$order->save();

You can have a look at this stackoverflow thread for some more info.

Legerdemain answered 5/5, 2014 at 11:45 Comment(4)
Thanks for your answer but I need it done automatically. It should be closed after create credit memo but it is not happening.And when returned from payp[al it is showing like:- IPN "Refunded". Note: Maximum amount available to refund is <span class="price">£0.00</span>Buckra
Refunding an order in Magento CE does not cause an actual payment refund. You must refund the payment on your own. The credit memo is only a record used to track the refunds and to provide proper values in the reports. You need to set the status to complete once the refund is made.Postmortem
Yes I know Refund offline in magento is only for maintain the record.But magento provide this that when a credit memo generated order status changed to closed. Unfortunately, This is not happening with all orders.Buckra
If this is only happening with some orders, there must be something else we don't know about.. check your logs around the time in which one of these orders had the issue; also, maybe a third party module is getting in the way? Try disabling your extensions one by one.Legerdemain
M
3

I've been investigating this issue and it seems that it is actually a rounding problem. After creating a credit memo the order status should be closed but in my case also some refunded orders kept their original status.

When creating a credit memo two functions Mage_Sales_Model_Order::canCreditmemo() and Mage_Sales_Model_Order_Invoice::canRefund() are called. They both return false if the difference between grand total and refunded amount is less than 0.0001.

In my testing this wasn't the case for some refunded orders irrespective to the used payment method. Increasing the value to 0.001 in both functions resulted in a closed order status. This also explains why only some orders kept their state and some are closed correctly depending on price and tax amount.

I solved the issue by overriding both Magento core classes in local and replaced the following lines with this:

Mage_Sales_Model_Order:

if (abs($this->getStore()->roundPrice($this->getTotalPaid()) - $this->getTotalRefunded()) < .001) {
    return false;
}

Mage_Sales_Model_Order_Invoice:

if (abs($this->getBaseGrandTotal() - $this->getBaseTotalRefunded()) < .001) {
    return false;
}

I hope this helps others because it took me some time to trace down that bug.

Morphosis answered 6/5, 2015 at 12:54 Comment(1)
I needed to increase the value to 0.005 because there were orders with a greater difference than 0.001 between grand total and refunded amount.Morphosis
A
2

Have a look at Mage_Sales_Model_Order_Payment::refund() where the refund is processed:

    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, $message);
    Mage::dispatchEvent('sales_order_payment_refund', array('payment' => $this, 'creditmemo' => $creditmemo));

After the order state was set to processing the event sales_order_payment_refund is dispatched. You could write an observer which listens to this event. If the payment method was PayPal you could update the order state as described in the answer by Cristian Quiroz.

Alright answered 5/5, 2014 at 18:35 Comment(6)
This happens for only few orders.Don't know why.No common issue in all there.In 1000 of orders it happens in 20 orders.After refund order status should be closed but this does not happens.Buckra
The event is dispatched only for 20 in 1000 creditmemos? There is nothing in logs? Maybe a "Lock wait timeout"?Alright
It should be by default. Magento provide functionality that when a credit memo generated order status changed to closed.This is happening with all orders except 20 orders out of 100 creditmemos.Some are placed using paypal some with sagepay.Buckra
Please show the order comment history. Is it the same for closed and none closed orders? Is always the full order amount refunded?Alright
My question has oder comment history. And this is full refundBuckra
Maybe the event sales_order_creditmemo_refund suits your needs better? It doesn't depend on the payment method (@see Mage_Sales_Model_Order_Creditmemo::refund()).Alright
S
2

at app\code\core\Mage\Payment\Model\Method\Abstract.php

you can set order status as complete

public function processBeforeRefund($invoice, $payment)
{
    // Add your code to set order as complete

    $payment->setRefundTransactionId($invoice->getTransactionId());
    return $this;
}

Hope this helps..

Selfgoverned answered 9/5, 2014 at 12:11 Comment(2)
Thanks,But it should be done By default as magento do this.And this is working with other orders except for some ordersBuckra
Is it possible to create a status "Refunded" and using it instead of "Closed"? Here in Brazil we use "Closed" for "Shipped", in our language obviously. So, I would like to have a "Refunded" status to make clear to the customer about what happened with his order. Any helps?Siobhansion

© 2022 - 2024 — McMap. All rights reserved.