From 3768ccfe8f741d935c62e6594fd9049675f0af3b Mon Sep 17 00:00:00 2001
From: Matthew Wire <devel@mrwire.co.uk>
Date: Tue, 17 Oct 2017 21:45:47 +0100
Subject: [PATCH] Prevent multiple submission

---
 js/civicrm_stripe.js | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index 1b9ca7f1..d75a04f0 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -5,7 +5,6 @@
 (function($, CRM) {
 
   var buttonText;
-  var stripeFormSubmitted = false;
 
   // Response from Stripe.createToken.
   function stripeResponseHandler(status, response) {
@@ -26,13 +25,12 @@
         + '</ul>'
         + '</div>');
 
-      stripeFormSubmitted = false;
+      $form.data('submitted', false);
       $submit.prop('disabled', false).attr('value', buttonText);
     }
     else {
       var token = response['id'];
       // Update form with the token & submit.
-//      copyCCDetails($form);
       removeCCDetails($form);
       // We use the credit_card_number field to pass token as this is reliable.
       // Inserting an input field is unreliable on ajax forms and often gets missed from POST request for some reason.
@@ -69,7 +67,7 @@
 
     // Get the form containing payment details
     $form = getBillingForm();
-    if (!$form) {
+    if (!$form.length) {
       debugging('No billing form!');
       return;
     }
@@ -106,19 +104,26 @@
     $form.unbind('submit');
 
     // Intercept form submission.
-    $(document).on('submit', $form, function(event) {
-      event.preventDefault();
+    $form.on('submit', function(event) {
       submit(event);
     });
 
     function submit(event) {
-      if (stripeFormSubmitted === true) {
-        debugging('already submitted');
+      event.preventDefault();
+
+      $form = getBillingForm();
+      if ($form.data('submitted') === true) {
+        // Previously submitted - don't submit again
+        alert('Form already submitted. Please wait.');
         return true;
+      } else {
+        // Mark it so that the next submit can be ignored
+        // ADDED requirement that form be valid
+        if($form.valid()) {
+          $form.data('submitted', true);
+        }
       }
-      stripeFormSubmitted = true;
 
-      $form = getBillingForm();
       $submit = getBillingSubmit();
       var isWebform = getIsWebform();
 
-- 
GitLab