How to fix paypal partial refund bug in nopCommerce

There is a "known" paypal partial refund bug in nopCommerce 3.50 version that has been reported by several nop users.

Issues (reported by users):

1) Anytime when a store admin issues a partial refund via paypal, the nopCommerce dashboard automatically shows that the entire total amount has been refunded

2) The order notes does show the correct issued partial refund amount

3) The issue leads to incorrect profit / sales numbers because even the cancelled orders are there as a part of the sales / profit section in nopCommerce


Here is the bug in the code:  
src/Plugins/Nop.Plugin.Payments.PayPalDirect/Controllers/PaymentPayPalDirectController.cs

public class PaymentPayPalStandardController  -> public ActionResult IPNHandler()
total = decimal.Parse(values["mc_gross"], new CultureInfo("en-US"));
 
 
case PaymentStatus.Refunded:
{
    if (_orderProcessingService.CanRefundOffline(order))
    {
        _orderProcessingService.RefundOffline(order);
    }
}



A quick fix will be as follows (alternative fix):

case PaymentStatus.Refunded:
{
    if (order.OrderTotal == Math.Abs(total))
    {
        if (_orderProcessingService.CanRefundOffline(order))
        {
            _orderProcessingService.RefundOffline(order);
        }
    }
    else
    {
        if (_orderProcessingService.CanPartiallyRefundOffline(order, Math.Abs(total)))
        {
            _orderProcessingService.PartiallyRefundOffline(order, Math.Abs(total));
        }
    }  
}




A complete fix (in respect of nopCommerce code) will be:

{
    var totalToRefund = Math.Abs(mc_gross);
    if (totalToRefund > 0 && Math.Round(totalToRefund, 2).Equals(Math.Round(order.OrderTotal, 2)))
      {
         //refund
        if (_orderProcessingService.CanRefundOffline(order))
       {
       _orderProcessingService.RefundOffline(order);
         }
           }
       else
    {
    //partial refund
    if (_orderProcessingService.CanPartiallyRefundOffline(order, totalToRefund))
          {
              _orderProcessingService.PartiallyRefundOffline(order, totalToRefund);
            }
}
}


For complete solution, please see the changeset: https://nopcommerce.codeplex.com/SourceControl/changeset/1e3b16cf3fe2af5d43f676af2bedf39f64a4e9aa

Hope it helps!

About Author

Written By Lavish Kumar

Based out of New York, USA, Lavish Kumar is a full stack web developer by profession and founder of Striving Programmers, a trusted community for developers that offers a wealth of articles and forums to assist individuals with improving their software development skills.

Leave your comment