Hi

I know this was a major issue with D6 (and the trail of fixing were massive) but I'm running D7 with Ubercart 7.x-3.8 and I am seeing the problem that ran and ran on D6. I can't see anyone else posting about this in D7 but apologies if I've missed something.

Anyway the issue: A customer places an order via the checkout and pays through PayPal (I am using PayPal WPS) when I was testing it happened once in 10 (live not sandbox) orders but now since launching the checkout on the public it is happening once every two orders. Basically it takes the payment once from PayPal but sends 2 order notifications and reduces the stock twice. essentially the order is duplicated.

This is frustrating when a customer orders a number of products as we have to go in and manually add the wrongly subtracted stock back in.

This did not happen when I just used PayPal Express Checkout rather than the full checkout functionality of Ubercart. However I don't really want to switch back to PayPal Express Checkout as I don't think the user experience is as good.

I can see in the logs that PayPal sends the IPN (for the orders that duplicate) twice and there is a message from uc_Paypal that "IPN Transaction has been processed before" I am guessing it is at this point that the duplicate order gets posted and the stock deprecated again? But how do I fix this?

Thanks and again apologies if this has been addressed elsewhere for D7 but I couldn't find it.

Happy to post more info as needed.

Comments

Allthegearnoidea’s picture

So as an update to this having tested over a number of orders. It seems this duplication of the order (and double deprecating of stock) only happens when in PayPal I switch on "Auto Return for Website Payments" under Website Payment Preferences.

Not sure why this would be (and it doesn't happen consistently). I'd prefer to have people return to my site after paying with PayPal rather than just ending at PayPal but at least by turning this function off in PayPal it seems to stop the issue.

TR’s picture

Priority: Major » Normal

Is this a clean install of D7 Ubercart or is it an upgrade from an old version prior to the previous fixes? Have you modified any of the stock rules? Do you have any contributed modules which affect stock or modify the checkout process? The stock reduction is independent of the auto return (or is supposed to be) because we can't assume that will ever happen.

You might want to take a look at the other issues in this queue related to IPN, especially #2143347: PayPal stuck in Pending due to IPN received twice from PayPal - it's possible what you're seeing is related to one of those other issues.

Allthegearnoidea’s picture

Hi TR

Thanks for the response

it is a clean install of D7 Ubercart but i do have a couple of other modules installed Out of Stock Notificator and and Stock Notify.

Thanks for the link I'll take a look.

At the moment the issue is sporadic so it is not too arduous to correct the stock issue when it happens.

TR’s picture

Status: Active » Postponed (maintainer needs more info)
Allthegearnoidea’s picture

Just as a thought on this. If my website was slow to send a response when PayPal sends the IPN is it possible that that could cause the duplicate order? i.e. PayPal sends IPN, website is slow to respond so PayPal sends IPN again website response happens and transaction completes twice? Though if that was the cae shouldn't there be a failsafe in Ubercart that says IPN had already been received to prevent the workflow of deprecating stock happening twice?

It's the only other thing I can think of as sometimes my site is slow and though I don't have any empirical data to show there is an increase in duplicate orders at these times, gut feel says it's around those times.

Anyway just some random thoughts...

kunoichi’s picture

I wonder if it's to do with the rules used by Ubercart to decrease stock and send out order notifications on checkout complete. Looking at the uc_paypal_ipn function, it does complete the ubercart order using uc_cart_complete_sale which in turn calls
rules_invoke_event('uc_checkout_complete', $order);

alh’s picture

Very interesting as we are seeing a similar issue. Our instance of Ubercart is 7.x-3.8 and was upgraded from 6. Once in a while, an order will be see a PayPal IPN come back (just one in our situation) and it will trigger two order confirmation emails to the customer and decrements the stock levels twice. This both confuses the customer and requires us to watch for these events as our stock levels will be wrong. This is a relatively rare event which makes it all the more difficult to diagnose. Our rules for Ubercart are pretty standard:

E-mail an order update notification
Machine name: uc_order_update_email_customer, Weight: 0
E-mail requested for order status update Default edit disable clone export

Update order status on full payment
Machine name: uc_payment_received, Weight: 0
A payment gets entered for an order Default edit disable clone export

Decrement stock upon order submission
Machine name: uc_stock_decrement_on_order, Weight: 0
Customer completes checkout Default edit disable clone export

In terms of emails in the log here is a normal order:

smtp 06/09/2015 - 13:00 Sending mail to: service@xxx Anonymous (not verified) ***Admin email***
smtp 06/09/2015 - 13:00 Sending mail to: xxx@hotmail.com Anonymous (not verified) ***Order receipt***
smtp 06/09/2015 - 13:00 Sending mail to: xxx25@hotmail.com Anonymous (not verified) ***Account setup***
uc_paypal 06/09/2015 - 13:00 IPN transaction verified. Anonymous (not verified)
uc_paypal 06/09/2015 - 13:00 Receiving IPN at URL for order 5183. Array... Anonymous (not verified)

And the most recent duplicate event:

smtp 06/09/2015 - 21:37 Sending mail to: service@xxx Anonymous (not verified) ***Admin email duplicate***
smtp 06/09/2015 - 21:37 Sending mail to: yyy@gmail.com Anonymous (not verified) ***Order receipt duplicate***
smtp 06/09/2015 - 21:37 Sending mail to: service@xxx Anonymous (not verified) ***Admin email***
smtp 06/09/2015 - 21:37 Sending mail to: yyy@gmail.com Anonymous (not verified) ***Order receipt***
uc_paypal 06/09/2015 - 21:37 IPN transaction verified. Anonymous (not verified)
smtp 06/09/2015 - 21:37 Sending mail to: yyy@gmail.com Anonymous (not verified) ***Account setup***
uc_paypal 06/09/2015 - 21:37 Receiving IPN at URL for order 5189. Array...

Perhaps it has something to do with the ordering of these events. Need to puzzle over this a bit more. If anyone has suggestions on what to look at or any further diagnostics we could set up be glad to do so.

alh’s picture

Interesting. The duplicate emails and stock decrements happened again. It is usually a rare event but now once each day. I am going to have to consider a patch to be sure the email/stock decrement isn't done twice. Have to look at the code. Here in sequence is an order with duplicate emails/stock decrement and the one before it which worked perfectly. Very strange.

smtp 06/10/2015 - 18:40 Sending mail to: service@ Anonymous (not verified)
smtp 06/10/2015 - 18:40 Sending mail to: service@ Anonymous (not verified)
smtp 06/10/2015 - 18:40 Sending mail to: @magma.ca Anonymous (not verified)
smtp 06/10/2015 - 18:40 Sending mail to: @magma.ca Anonymous (not verified)
smtp 06/10/2015 - 18:40 Sending mail to: @magma.ca Anonymous (not verified)
uc_paypal 06/10/2015 - 18:40 IPN transaction verified. Anonymous (not verified)
uc_paypal 06/10/2015 - 18:40 Receiving IPN at URL for order 5193. Array... Anonymous (not verified)

and the normal:

smtp 06/10/2015 - 17:28 Sending mail to: service@ Anonymous (not verified)
smtp 06/10/2015 - 17:28 Sending mail to: @hotmail.com Anonymous (not verified)
smtp 06/10/2015 - 17:28 Sending mail to: @hotmail.com Anonymous (not verified)
uc_paypal 06/10/2015 - 17:28 IPN transaction verified. Anonymous (not verified)
uc_paypal 06/10/2015 - 17:28 Receiving IPN at URL for order 5191. Array... Anonymous (not verified)

Allthegearnoidea’s picture

alh

This is exactly what I am seeing, and as you are experiencing, we are getting at least one order a day doing this.

There is no rhyme or reason as to when it will duplicate or not.

If you are able to come up with a patch I'd happily be a test bed but my skills at development are not anywhere near good enough to develop the patch itself.

tinker’s picture

This could be related to #1669968: hook_uc_checkout_complete() is called outside of checkout process (when payment is entered) since Paypal code effectively calls uc_cart_complete_sale() twice during payment processing. Once through uc_payment_enter() and the second time directly. This could trigger the checkout complete rules twice if a new user account is being created during checkout. That would explain the intermittent nature of the bug as it would not occur for existing user account (whether logged in or not).

Allthegearnoidea’s picture

tinker

I think you could be right. I will do some checks as my site as I pretty much know which customers are existing and which are new so I can see if the new ones were where the duplication happens.

Thanks for the link too. Do you think the patch that you posted there should fix this?

Thanks again

Allthegearnoidea’s picture

tinker

You are spot on, new orders where an account is created are where the duplicated stock deprecation happens.

alh’s picture

tinker,

yes - thanks so much for pointing out your fine work on the patch. The problem is very intermittent - just happened again that I am truly at a loss as to how to narrow it down further. I will however re-read the thread you started and dive into the patch. This could be quite a breakthrough if this is the problem and will likely have to implement the patch, test then wait and see if this is actually the root cause as I haven't been able to diagnose it on my own so far.

Appreciate your contribution!

Allthegearnoidea’s picture

tinker and alh

So, I ran a test on my site before applying the patch found at: https://www.drupal.org/node/1669968 comment #37. I placed an order whereby Ubercart created the account as well as created the order and the order duplicated and the stock decreased twice.

I also tested placing an order as a registered user and the order went through once and decreased the stock once. Pretty much confirming our suspicion that it is when the new account and order are created at the same time that we get the issue.

I then applied your patch tinker. I have since run a couple of orders involving "new" accounts in so much as Ubercart creates the new account and the order at the same time and I can report.... That the orders have not duplicated and the stock has not been deprecated twice. (I am yet to find any unwanted side effects of the applied patch).

This is only after a couple of tests from my side. I will leave the site to run for a while taking orders and see what happens but from my before and after test it seems your patch is a fix!

Thank you so much for your efforts tinker, if this has permanently fixed the issue it will be fantastic! So thank you once again. I'll update after a couple of weeks of orders coming in.

tinker’s picture

@Allthegearnoidea, Thanks for testing and reporting back on patch #37 from #1669968: hook_uc_checkout_complete() is called outside of checkout process (when payment is entered). I am very happy to hear that it appears to be working for this issue too. I should warn against applying the patch to a live site before thorough testing as I have not run the 7.x version on a live site and have not been using PayPal payment processing. I have however been running the 6.x patch for over a year without issue with Authorize.net and ran automated simpletest on the 7.x version without issue. The only real difference between the versions is the way passwords are stored for new accounts. There could be a problem with the 7.x version not displaying the new user's password on the checkout completion page and instead will show 'Your password' text. Please keep an eye out for this and let me know if it happens. If the patch does indeed fix this issue could you please post your future findings in the other issue queue to expedite the patch being accepted.

@alh If you could also test the patch it would be of great help. With confirmation of fix from you we could close this issue as a duplicate and advance the other issue.

alh’s picture

Hi guys,

I have applied the patch to our test site and ran some test orders. I am however having some difficulty as the Instant Payment Notifications from PayPal on the test site are not working properly. This has of course nothing to do with the patch but the extreme difficulty we have with PayPal`s sandbox which delivers IPNs that are different than the live site! So on Monday, I am going to do a work around to ignore the invalid fields in the sandbox IPNs. Assuming all goes well on that front, I will try it on our live site. I should add that even with the difficulty with the sandbox IPNs, orders are being processed correctly. I will report in more detail once I get the workaround in place.

In the meantime, we are not seeing the annoying duplicate email and stock decrements on our live sites right now. I am baffled as to why that is to be honest. It would be great if this was predictable, i.e. found only with a certain class of users but not in our case.

But hope springs eternal that we can beat this one into submission.

alh’s picture

OK, figured out what the IPN problem was. Wrong URL being used to verify IPNs in the sandbox. Once fixed, IPNs are being processed properly, email sent and stock decrements being done. I observer however two errors in the logs:

Notice: Undefined variable: order in uc_payment_enter() (line 496 of /home/anytests/public_html/sites/all/modules/ubercart/payment/uc_payment/uc_payment.module)

and

Notice: Trying to get property of non-object in uc_payment_enter() (line 496 of /home/anytests/public_html/sites/all/modules/ubercart/payment/uc_payment/uc_payment.module).

This error occurred during the processing of the sandbox IPN hence was generated due to a call to the URL for the order:

test-site/uc_paypal/ipn/4827

So something a bit amiss here and will dig deeper to see what the problem is.

tinker’s picture

@alh Your errors cannot be caused by the patch since variable $order is defined before it is used and no $order->property call exists in the function uc_payment_enter(). What is the code at line 496 in file uc_payment.module

alh’s picture

Hi tinker and Allthegearnoidea,

you are absolutely right and my bad. Another patch that I was testing and should have backed out before testing this one! I will do so today and report back.

alh’s picture

Hi again,
rolled back the other patch, re-installed this one and ran a test order. Worked perfectly on the test site. This was a new user who checked out anonymously. Order emails sent, stock decrement happened and order flipped to payment received as expected. I am going to run another couple of test order with other states and will report back but this is very encouraging.

alh’s picture

So have now run three tests in the sandbox and all went well. Tested:

Anonymous check-out with a new customer as mentioned above
Anonymous check-out with a returning customer
Check-out with the customer logged in

I am now considering going live with this on one of the three sites. Start there then if all goes well migrate to the others.

Thanks again tinker and will continue to report back.

Allthegearnoidea’s picture

alh and tinker

So after almost a week of testing I can report back the following:

1) tinker you are right. Some of the orders where the new account is created comes up with the "Your Password" text rather than the password. This happens in the email confirmation and the confirmation screen after order completion. It doesn't do it for all orders.

2) after about 20 orders sadly I still get the duplicated order and stock decreases.

What I have noticed though is it definitely ONLY happens on new accounts. If it happened on every new account that would be handy but it seems to be inconsistent.

So it seems to me that you are along the right lines tinker. But there must be something else happening that is posting the order twice.

For now I have rolled back the patch.

Be interesting to see what set of results you get alh.

tinker’s picture

@Allthegearnoidea, Did you notice a decrease in the amount of orders with duplicate stock decrement? If so it may mean you have a compound issue and uc_paypal code needs a fix too. I suggest that you add some code that logs execution of uc_cart_complete_sale() per order as that is the rules event that triggers stock decrement.

You could just add an admin comment to uc_cart_complete_sale()

function uc_cart_complete_sale($order, $login = FALSE) {
  global $user;
  uc_order_comment_save($order->order_id, 0, "uc_cart_complete_sale triggered.", 'admin');

The check the orders that have duplicate stock decrement and see in the admin comments if it was triggered more than once. From the uc_paypal code it looks like it should only be triggered once after the IPN verification.

As to the 'Your Password' issue - this only happens when a new account is created and the user supplied a password during checkout. There is no way around it since we cannot store the unencrypted password and cannot decrypt the password later.

Allthegearnoidea’s picture

tinker, thanks for following up.

Excuse my noviceness (made up word), but should I be adding that code to the uc_cart.module or somewhere else?

Thanks

tinker’s picture

@Allthegearnoidea, Yes that code is in ubercart/uc_cart/uc_cart.module
Search for 'function uc_cart_complete_sale' (around line 854)
Add the following line at the top of the function.

uc_order_comment_save($order->order_id, 0, 'DEBUG: uc_cart_complete_sale triggered.', 'admin');

Please note that if you do this without the patch you will surely get duplicate execution logged for new users. With the patch you should only get one execution. For orders with patch and more then one 'uc_cart_complete_sale' you can copy and paste admin comments (removing any sensitive data) and I might be able to tell if there is a problem with uc_paypal. Here's what I get with test credit gateway.

Before patch:
2015-07-16 11:19 - Credit card charged: $34.00
2015-07-16 11:19 - DEBUG: uc_cart_complete_sale triggered.
2015-07-16 11:19 - The stock level for SWTDU3 has been decreased to 4.
2015-07-16 11:19 - DEBUG: uc_cart_complete_sale triggered.
2015-07-16 11:19 - Order created through website.

After patch:
2015-07-16 11:17 - Credit card charged: $34.00
2015-07-16 11:17 - DEBUG: uc_cart_complete_sale triggered.
2015-07-16 11:17 - The stock level for SWTDU3 has been decreased to 3.
2015-07-16 11:17 - Order created through website.

Allthegearnoidea’s picture

tinker

Fantastic thanks for the guidance.

I'll reapply the patch and then add the code. I'll post any findings I get on here.

I'm away on business for the next couple of weeks so it'll be a little while before I can do this but as soon as I have and I have some results I'll post here.

Thanks again for sticking with this.

Allthegearnoidea’s picture

tinker

I have reapplied patches and added the debug code.

I've placed some test orders but it is currently stubbornly refusing to duplicate orders so I'll let it run for a while and see what happens.

I'll post Admin Comments as soon as I have something of interest.

Allthegearnoidea’s picture

As ever just as I post that I can't get it to duplicate, it duplicates.

So, here's the log for a new customer (creating a new user) ordering, when the order doesn't duplicate and the stock doesn't get taken twice:

08/07/2015 - 13:17 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:17 - The stock level for xxxxxxxxxx has been decreased to 2.
08/07/2015 - 13:17 - Order created through website.
08/07/2015 - 13:17 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:17 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:17 - PayPal IPN reported a payment of 0.99 GBP.

Now the log for a new customer (so creating new user) ordering, when the order duplicates and the stock gets taken twice:

08/07/2015 - 13:42 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:42 - The stock level for 10000237 has been decreased to 1.
08/07/2015 - 13:42 - Order created through website.
08/07/2015 - 13:42 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:42 - The stock level for xxxxxxx has been decreased to 2.
08/07/2015 - 13:42 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:42 - PayPal IPN reported a payment of 0.99 GBP.

Finally the log for an existing customer where order doesn't duplicate:

08/07/2015 - 13:54 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:54 - The stock level for xxxx has been decreased to 2.
08/07/2015 - 13:54 - Order created through website.
08/07/2015 - 13:55 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:55 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:55 - PayPal IPN reported a payment of 0.99 GBP.

I don't know if this gives you anything to go on at all?

tinker’s picture

@Allthegearnoidea, Are you sure you have the patch applied? The double side by side Debug messages suggests you did not.

08/07/2015 - 13:17 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:17 - DEBUG: uc_cart_complete_sale triggered.
08/07/2015 - 13:17 - PayPal IPN reported a payment of 0.99 GBP.

On top of that there is definitely an issue with workflow and paypal payment. It is clear that the order is being completed once when the customer checks out then again (twice) when the paypal payment is confirmed. The twice execution when payment is entered should be fixed by patch but the complete sale on checkout is a paypal issue. I would think that the order would not trigger complete sale and go into limbo until payment is completed. Your stock is currently being decreased before the customer has paid. If you look at my log using Authnet you can see that payment is entered before checkout sale is executed (before and after patch).

Allthegearnoidea’s picture

Hi tinker

I thought it was applied but will go over it again in case I mucked it up. Is there a line in one of the 3 files that gets patched that I can easily look at to see if the patch applied. E.g. check to see that on line 854 if uc_cart x has been changed to y?

I will check my workflows too.

Thanks for the heads up.

tinker’s picture

@allthegearnoidea, You can search for function " uc_cart_user_ensure". If it exists then the patch is applied. FYI, The patch file has all the details of what has changed. [[ SEE COMMENTS ]]

--- a/payment/uc_payment/uc_payment.module [[ SOURCE FILE ]]
+++ b/payment/uc_payment/uc_payment.module [[ TARGET FILE ]]
@@ -509,11 +509,13 @@ [[ LINE NUMBER, CHARACTER NUMBER ]]
     ->execute();
 
   $order = uc_order_load($order_id, TRUE);
+  // Account is the user who entered the payment and not neccesarily the order
+  // customer.
   $account = user_load($uid);
 
-  // Ensure user has an account before payment is made. [[ - STUFF REMOVED ]]
+  // Ensure customer account is linked to order. [[ + STUFF ADDED ]]
   if (module_exists('uc_cart')) {
-    uc_cart_complete_sale($order);
+    uc_cart_user_ensure($order);
   }
Allthegearnoidea’s picture

Thanks tinker

I will check my install and see if the patch applied.

Thanks for your patience.. Patching is not my strong point, so appreciate your guidance.