From 4d12caa51685170bae3130f4c83b738206aa0fc2 Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Wed, 5 Feb 2020 14:14:12 +0000
Subject: [PATCH] Add support for sweetalert library on form validation errors

---
 js/civicrm_stripe.js | 51 +++++++++++++++++++++++++++++++++-----------
 1 file changed, 39 insertions(+), 12 deletions(-)

diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index fc450014..c695e920 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -76,6 +76,7 @@ CRM.$(function($) {
       submitButtons[i].removeAttribute('disabled');
     }
     triggerEvent('crmBillingFormNotValid');
+    notifyUser('error', ts('Oops...'), result.error.message);
   }
 
   function handleCardPayment() {
@@ -341,6 +342,7 @@ CRM.$(function($) {
         document.querySelector('#billing-payment-block').scrollIntoView();
         window.scrollBy(0, -50);
         triggerEvent('crmBillingFormNotValid');
+        notifyUser('error', ts('Oops...'), ts('Please correct the errors on the form and submit again'));
         return false;
       }
 
@@ -621,18 +623,6 @@ CRM.$(function($) {
     return false;
   }
 
-  function debugging(errorCode) {
-    // Uncomment the following to debug unexpected returns.
-    if ((typeof(CRM.vars.stripe) === 'undefined') || (Boolean(CRM.vars.stripe.jsDebug) === true)) {
-      console.log(new Date().toISOString() + ' civicrm_stripe.js: ' + errorCode);
-    }
-  }
-
-  function triggerEvent(event) {
-    debugging('Firing Event: ' + event);
-    $(form).trigger(event);
-  }
-
   function addDrupalWebformActionElement(submitAction) {
     var hiddenInput = null;
     if (document.getElementById('action') !== null) {
@@ -666,4 +656,41 @@ CRM.$(function($) {
     return null;
   }
 
+  /**
+   * Output debug information
+   * @param {string} errorCode
+   */
+  function debugging(errorCode) {
+    // Uncomment the following to debug unexpected returns.
+    if ((typeof(CRM.vars.stripe) === 'undefined') || (Boolean(CRM.vars.stripe.jsDebug) === true)) {
+      console.log(new Date().toISOString() + ' civicrm_stripe.js: ' + errorCode);
+    }
+  }
+
+  /**
+   * Trigger a jQuery event
+   * @param {string} event
+   */
+  function triggerEvent(event) {
+    debugging('Firing Event: ' + event);
+    $(form).trigger(event);
+  }
+
+  /**
+   * If we have the sweetalert2 library popup a nice message to the user.
+   *   Otherwise do nothing
+   * @param {string} icon
+   * @param {string} title
+   * @param {string} text
+   */
+  function notifyUser(icon, title, text) {
+    if (typeof Swal === 'function') {
+      Swal.fire({
+        icon: icon,
+        title: title,
+        text: text,
+      });
+    }
+  }
+
 });
-- 
GitLab