Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento PayPal Duplicate Invoice Error

Every once in a while a customer will get an error when attempting to submit an order that says PayPal gateway has rejected request. The transaction was refused as a result of a duplicate invoice ID supplied. After digging into this a bit I believe I have narrowed down the problem. In the most recent of cases, a customer had attempted to place an order 4 months ago and received an Internal Error from PayPal. I learned from speaking with PayPal that this customer's credit card had been flagged. When they attempted to place the first order, PayPal rejected it but still considered the Invoice ID that our Magento Store had provided "used".

Fast forward to today...same customer, new order. Magento STILL had the old quote from September in the sales_flat_quote table. When they logged in, it loaded the customer quote (which was still active) and attempted to use it for this order. That resulted in the Duplicate Invoice ID error.

I see in the Mage_Sales_Model_Observer class that there is a cleanExpiredQuotes method that is called from a cron job. However, this only effects quotes with "is_active" = 0. Since this quote is considered active it never got cleared out.

So clearly there's a disconnect between the Magento code and PayPal. But that's about as far as I've gotten with it. Has anyone else experienced this? If so, any suggestions?

EDIT:

I've gotten a bit further with this. I have added code to the checkout IndexController to catch the error and, if it's a Duplicate Invoice Error, it unsets the reserved_order_id in the quote a calls the saveOrderAction again. This causes the quote to reserve a new order ID, which it then submits to PayPal. The problem I'm having now is that when it tries the second time with the new Invoice Number, all the totals are 0. I tried setting the totals_collected_flag to false so it re-collects the totals, but they are always 0 the second time through. More specifically, the totals in the Mage_Sales_Model_Quote_Address are 0, which is what the Mage_Sales_Model_Order ends up using. The totals in the Mage_Sales_Model_Quote are correct, but they get overwritten in the collectTotals() method of the quote.

Obviously, after the first attempt, something is unsetting all the values, but I don't know what or where. If anyone has any ideas I'd love to hear them!

like image 518
Mageician Avatar asked Jan 12 '12 20:01

Mageician


1 Answers

  1. Log in to your Paypal account
  2. Go to Profile > Payment Receiving Preferences
  3. Under Block accidental payments select No, allow multiple payments per invoice ID

enter image description here

like image 127
george Avatar answered Sep 28 '22 17:09

george