From 1137a75680fa234e78a0896df32f2b3b397307eb Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Sat, 7 Oct 2017 20:36:56 +0100
Subject: [PATCH] Always load civicrm_stripe.js if we have a payment processor
 on the page. This makes sure we always generate a token if the selected
 processor was stripe

---
 js/civicrm_stripe.js | 14 ++++++++++++--
 stripe.php           |  6 ++----
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index f8552e91..d42de560 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -41,7 +41,14 @@
   // Prepare the form.
   $(document).ready(function() {
     Stripe.setPublishableKey($('#stripe-pub-key').val());
+    loadStripeBillingBlock();
+  });
+
+  CRM.$('input[name="payment_processor_id"]').change(function() {
+    loadStripeBillingBlock();
+  });
 
+  function loadStripeBillingBlock() {
     // Check for form marked as a stripe-payment-form by the server.
     if (!($('form.stripe-payment-form').length)) {
       // If there isn't one look for it.
@@ -49,8 +56,11 @@
         isWebform = true;
         $('form.webform-client-form').addClass('stripe-payment-form');
       }
+      else if ($('#crm-container form').length) {
+        $('#crm-container form').addClass('stripe-payment-form');
+      }
       else {
-        $('#crm-container > form').addClass('stripe-payment-form');
+        return;
       }
     }
 
@@ -223,7 +233,7 @@
       debugging('debug: ywkvh (Getting Stripe token)');
       return false;
     });
-  });
+  }
 }(cj, CRM));
 
 function debugging (errorCode) {
diff --git a/stripe.php b/stripe.php
index 6568e7b1..f8555526 100644
--- a/stripe.php
+++ b/stripe.php
@@ -195,10 +195,8 @@ function stripe_civicrm_managed(&$entities) {
    */
   function stripe_civicrm_alterContent( &$content, $context, $tplName, &$object ) {
     if($context == 'form' && !empty($object->_paymentProcessor['class_name'])) {
-      if($object->_paymentProcessor['class_name'] == 'Payment_Stripe') {
-        $stripeJSURL = CRM_Core_Resources::singleton()->getUrl('com.drastikbydesign.stripe', 'js/civicrm_stripe.js');
-        $content .= "<script src='{$stripeJSURL}'></script>";
-      }
+      $stripeJSURL = CRM_Core_Resources::singleton()->getUrl('com.drastikbydesign.stripe', 'js/civicrm_stripe.js');
+      $content .= "<script src='{$stripeJSURL}'></script>";
     }
   }
 
-- 
GitLab