I want to set the $order->uid to a newly created user's uid in a payment submit handler but whenever the handler returns FALSE and payment fails, the form is regenerated, I get the expected error, but the shopping cart contents disappear. The order however looks like it's ready for processing again.
The functionality is that anonymous user's can buy products. I'm creating a Drupal account for them right away so I can later use their uid in the payment gateway unique order_id (The default Rules won't work as I need it immediately). Should I avoid changing the uid for the order? but I was hoping to use that to store the newly created uid. I guess I could just store it in a variable and then delete it or something.
What about form_state['storage'] type of functionality with commerce payment submit forms? What is the best practice there?
Comments
Comment #1
blasthaus commentedOk so now with a bit of research, I think I found a solution for storage. It seems like I must create a new table to store the user's uid along with a unique order_id (for payment gateway) and the order ID along with a status field. It's a bit cumbersome, bc I will have to check this table if the form is submitted a second time. In the end I believe it's still possible to alter the order->uid but only after a successful transaction. Is this ok to do? Also the commerce_customer_profile uid needs to be altered as well since it will be 0 for anonymous users.
Presently I see no other nifty alternatives to storing any values upon form submit as no variables are passed by reference. It would be great to have something we could use for these kind of special case scenarios. I want to allow anonymous users the ability to check out, then upon the first form submission attempt, create their account and store their uid somewhere. As I mentioned, I want this uid in order to stash it in the unique order_id for the payment gateway transaction. In the meantime for anyone else in this boat here's the direction I'm headed:
doesn't work
here's the direction I'm headed (taken from drupal.org/project/commerce_ezypay)
Comment #2
blasthaus commentedSo just reporting back that this is not a bug but something bugging me, sorry! The above code after $order_wrapper does not work. However what does work is the default rule Assign an anonymous order to a pre-existing user triggers once the order completes checkout and compares the email of the order with the Drupal user table and the order->uid and all the profile and billing references get updated to reflect the user if there is a match. I may note here that it is theoretically possible to have more than one Drupal account with the same email, esp if you are creating any user programmatically (like myself) so I deal with that in validation. Whew! I just wish I had known that 3 days ago :-)
One thing to note was I had to adjust the rule Send an order notification e-mail weight to be lower than the one above (along with any other rules acting on this event, otherwise if someone changed their email after a failed transaction it would fire too soon and send to the initial email address entered for some reason.
Marking this as closed.
I will mention for the record that I had to deal with several special cases in the submit_form handler in order to accommodate the for anonymous users the ability to buy subscription products where the subscription product requires a Drupal user->uid. This creates a bit of code bloat on the function, but everything does work. I will be posting a commerce module soon with this code if anyone is interested. What happens is we set up an account for the email address on the form upon the first form-submit, but store their "status" during the checkout process. Here are the special use cases I was able to solve: