Prevent multiple clicks of submit order

Posts: 34
Joined: 09/28/2007

Greetings all,

Is it possible to prevent someone from clicking 'submit order' more than once?

For some reason or another, I just had my ubercart store charge a customer twice on their credit card using PayPal WPP.

Any ideas?

TIA,
txcrew

Posts: 1212
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

This would be a useful addition. It'd be a really small snippet of jQuery code that would set the "submit" element to disabled.

Either that or using hook_form_alter(). There is some good discussion here:

http://drupal.org/node/69048

Although this is the type of feature I'd love to see rolled into core. We have had quite a few duplicate orders on our site, too.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 4686
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

huh... interesting. I had no clue this was happening. Smiling

It'd be a very simple bit of JS I can attach to the submit order button. It'll be in the next release.

Posts: 4686
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

So... I change line 1623 in uc_cart.module from:

<?php
  $form
['submit'] = array('#type' => 'submit', '#value' => variable_get('uc_checkout_submit_button', t('Submit order')));
?>

To:

<?php
  $form
['submit'] = array(
   
'#type' => 'submit',
   
'#value' => variable_get('uc_checkout_submit_button', t('Submit order')),
   
'#attributes' => array('onclick' => '$(this).click(function() { return false; });'),
  );
?>

When someone clicks the button, it will get a new onclick function that returns false, thereby canceling any duplicate form submissions. My only question is what if there is a timeout or something else? Then they'll sit there click "Submit Order" over and over with no result.

Any thoughts? Should I keep this mod?

Posts: 69
Joined: 01/23/2008

Hi Ryan,
When the button is clicked may be the button text should changed to something like "submitting...". This will inform the user.

Posts: 4686
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

I'll try, but I wonder if changing the text like that will cause the submit to misfire. Puzzled At first I simply tried disabling the button, but then it just wouldn't submit the form any more. Maybe I can just throw that circular throbber up beside the button.

Posts: 4686
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Follow-up: Can anyone tracking this issue go try checkout on the Livetest and let me know what you think? My only beef with it is the jarring jump to the left, but that's not a huge deal to me since we aren't expecting folks to click the button again. Thoughts? Should this stay in? Should I just make it a simple contrib?

Posts: 34
Joined: 09/28/2007

If disabling the button is not an option, the throbber looks good. I could live with the jump to the left because it will show the user who might have clicked it twice that they caused something to start working by clicking once, and in turn will prevent the second click (hopefully).

Posts: 4686
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Well, now with the change, a second click won't do anything anyways. I disable form submission for any subsequent clicks... which I guess means they're up a creek w/o a paddle if it times out? Puzzled

Posts: 1212
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

Timing out could be an issue, but after reading more on d.o I realized that there seem to be issues of doing it this way with certain browsers? Also not having JS enabled would do this (but wouldn't that break Ubercart in general?)

One thought I had would be to add some kind of checks-and-balances using the form's token as a key. If more than one insert into uc_orders comes from the same form, then either just overwrite that order row (which is how regular forum and page editing forms work, from what I've read) or ignore the new insert altogether. That way it doesn't matter the client-side issues since it's taken care of on the server end.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 4686
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Hmm.. that part of a form really only deals with moving an existing order to the post-checkout state's default status and invoking hook_order() w/ the submit $op. There shouldn't be a danger of duplicate orders anywhere.

Perhaps I can simply log in the order's data whether or not the submit has happened and not invoke the hook if it's already happened. Although, as of right now, there's no way for someone to checkout without JS enabled anyways.

I think I'll go ahead and make my solution a contrib pending review, since I'd like to put up a release candidate in the near future and don't want this to hold it up.

Posts: 1212
Joined: 08/14/2007
Bug FinderEarly adopter... addicted to alphas.Getting busy with the Ubercode.

That's what I thought. Well you're more well-versed in Drupal than I am. Whichever you think is best. To be honest, now that I think about it, I think we've only had a few duplicate orders and I think it's been caused more by credit cards being declined, and someone submitting again (after reloading the checkout page perhaps). But I guess it's better to be safe than sorry, so long as it doesn't break the checkout process altogether Smiling

I will test your contrib once it's up.

--

"Pain don't hurt." - Dalton

Mike Nelson's RiffTrax! www.rifftrax.com

Posts: 4686
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Alrighty, this is in Checkout Enhancements.