From b117d93cd39e811ec7e3b96e8d7b17a5d9002e29 Mon Sep 17 00:00:00 2001
From: Matthew Wire <devel@mrwire.co.uk>
Date: Fri, 18 May 2018 15:26:19 +0100
Subject: [PATCH] Fixes for drupal webform following testing

---
 README.md            | 27 +++++++++++++++++++--
 js/civicrm_stripe.js | 57 ++++++++++++++++++++++++--------------------
 2 files changed, 56 insertions(+), 28 deletions(-)

diff --git a/README.md b/README.md
index ae7f8130..dcb5d995 100644
--- a/README.md
+++ b/README.md
@@ -18,7 +18,9 @@ All configuration is in the standard Payment Processors settings area in CiviCRM
 You will enter your "Publishable" & "Secret" key given by stripe.com.  
 
 ## Installation
-Download and install extension via CiviCRM's "Manage Extensions" page.  
+Download and install extension via CiviCRM's "Manage Extensions" page.
+
+To use with Drupal Webform you **MUST** apply the following patch to webform_civicrm: https://github.com/colemanw/webform_civicrm/pull/100  
 
 ## Webhook and Recurring Contributions
 
@@ -89,9 +91,17 @@ Expects a drupal (demo) site installed at http://localhost:8001
 1. Offline Membership, alternate PP: A membership/contribution is created for Arlyne Adams with alternate PP.
 1. Offline Event Registration, default PP: A participant record/contribution is created for Arlyne Adams with default PP.
 1. Offline Event Registration, alternate PP: A participant record/contribution is created for Arlyne Adams with alternate PP.
+1. Online Contribution Stripe Default Only: A new contribution record is created.
+1. Online Contribution Page 2xStripe, Test proc, use Stripe Alt: A new contribution record is created. **FAIL:
+Error Oops! Looks like there was an error. Payment Response: 
+Type: invalid_request_error
+Code: resource_missing
+Message: No such token: Stripe Token**
+1. Online Contribution Page Stripe Default, Pay Later: A new contribution record is created.
+1. Test Webform: A new contribution is created. *Partial test only*
 
 ONLINE contribution, event registration tests
-ONLINE webform test
+
 
 ### Manual Tests
 
@@ -103,6 +113,19 @@ ONLINE webform test
 1. Test online event registration.
 1. Test online event registration (cart checkout).
 
+#### Drupal Webform Tests
+TODO: Add these as Katalon tests.
+
+1. Webform with single payment processor (Stripe) - Amount = 0.
+1. Webform with single payment processor (Stripe) - Amount > 0.
+1. Webform with multiple payment processor (Stripe selected) - Amount = 0.
+1. Webform with multiple payment processor (Stripe selected) - Amount > 0.
+1. Webform with multiple payment processor (Pay Later selected) - Amount = 0.
+1. Webform with multiple payment processor (Pay Later selected) - Amount > 0.
+1. Webform with multiple payment processor (Non-stripe processor selected) - Amount = 0.
+1. Webform with multiple payment processor (Non-stripe processor selected) - Amount > 0.
+
+
 ## Credits / Maintenance
 
 ### Original Author
diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index bf7f8a51..b424ad8f 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -209,20 +209,26 @@
         return false;
       }
 
+      var isWebform = getIsWebform();
+
       // Handle multiple payment options and Stripe not being chosen.
-      if ($form.find(".crm-section.payment_processor-section").length > 0) {
-        var extMode = $('#ext-mode').val();
+      if (isWebform) {
         var stripeProcessorId = $('#stripe-id').val();
-        var chosenProcessorId = $form.find('input[name="payment_processor_id"]:checked').val();
-
-        // Bail if we're not using Stripe or are using pay later (option value '0' in payment_processor radio group).
-        if ((chosenProcessorId !== stripeProcessorId) || (chosenProcessorId === 0)) {
-          debugging('debug: Not a Stripe transaction, or pay-later');
-          return true;
+        var chosenProcessorId = $form.find('input[name="submitted[civicrm_1_contribution_1_contribution_payment_processor_id]"]:checked').val();
+      }
+      else {
+        if ($form.find(".crm-section.payment_processor-section").length > 0) {
+          var stripeProcessorId = $('#stripe-id').val();
+          var chosenProcessorId = $form.find('input[name="payment_processor_id"]:checked').val();
         }
       }
+      // Bail if we're not using Stripe or are using pay later (option value '0' in payment_processor radio group).
+      if ((chosenProcessorId !== stripeProcessorId) || (chosenProcessorId === 0)) {
+        debugging('Not a Stripe transaction, or pay-later');
+        return true;
+      }
       else {
-        debugging('debug: Stripe is the selected payprocessor');
+        debugging('Stripe is the selected payprocessor');
       }
 
       $form = getBillingForm();
@@ -239,23 +245,18 @@
       }
 
       $submit = getBillingSubmit();
-      var isWebform = getIsWebform();
 
       if (isWebform) {
-        var $processorFields = $('.civicrm-enabled[name$="civicrm_1_contribution_1_contribution_payment_processor_id]"]');
-
-        $totalElement = $('#wf-crm-billing-total');
-        if ($totalElement.length) {
-          // Handle old and new jQuery conventions (https://api.jquery.com/data/#data-html5)
-          // The second form is the new form as of jQuery 1.4.3 (jQuery tries to convert string
-          // numbers to integers).
-          if ($totalElement.data('data-amount') === '0' || $totalElement.data('amount') === 0 ) {
-            debugging('webform total is 0');
-            return true;
-          }
+        // If we have selected Stripe but amount is 0 we don't submit via Stripe
+        if ($('#billing-payment-block').is(':hidden')) {
+          debugging('no payment processor on webform');
+          return true;
         }
+
+        // If we have more than one processor (user-select) then we have a set of radio buttons:
+        var $processorFields = $('[name="submitted[civicrm_1_contribution_1_contribution_payment_processor_id]"]');
         if ($processorFields.length) {
-          if ($processorFields.filter(':checked').val() === '0') {
+          if ($processorFields.filter(':checked').val() === '0' || $processorFields.filter(':checked').val() === 0) {
             debugging('no payment processor selected');
             return true;
           }
@@ -265,7 +266,7 @@
       // If there's no credit card field, no use in continuing (probably wrong
       // context anyway)
       if (!$form.find('#credit_card_number').length) {
-        debugging('debug: No credit card field');
+        debugging('No credit card field');
         return true;
       }
       // Lock to prevent multiple submissions
@@ -296,7 +297,7 @@
         exp_month: cc_month,
         exp_year: cc_year
       }, stripeResponseHandler);
-      debugging('debug: Getting Stripe token');
+      debugging('Getting Stripe token');
       return false;
     }
   }
@@ -306,7 +307,11 @@
   }
 
   function getBillingForm() {
-    return $('input#stripe-pub-key').closest('form');
+    var $billingForm = $('input#stripe-pub-key').closest('form');
+    if (!$billingForm.length && getIsWebform()) {
+      $billingForm = $('.webform-client-form');
+    }
+    return $billingForm;
   }
 
   function getBillingSubmit() {
@@ -339,7 +344,7 @@
 
   function debugging (errorCode) {
     // Uncomment the following to debug unexpected returns.
-    console.log(errorCode);
+    console.log('civicrm_stripe.js: ' + errorCode);
   }
 
 }(cj, CRM));
-- 
GitLab