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?
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With