From 2016de7b94c9ca8feceb851ef7cbb117fc63a251 Mon Sep 17 00:00:00 2001
From: Matthew Wire <devel@mrwire.co.uk>
Date: Fri, 25 Aug 2017 11:29:52 +0100
Subject: [PATCH] Backend membership renewal fails with stripe token not passed
 because stripe.js is not included

---
 js/civicrm_stripe.js | 17 ++++++++++-------
 stripe.php           | 12 ++++++++++++
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index d16c5a04..8cb587dc 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -44,15 +44,18 @@
       Stripe.setPublishableKey($('#stripe-pub-key').val());
     });
 
-    if ($('.webform-client-form').length) {
-      isWebform = true;
-      $('form.webform-client-form').addClass('stripe-payment-form');
-    }
-    else {
-      if (!($('.stripe-payment-form').length)) {
-        $('#crm-main-content-wrapper form').addClass('stripe-payment-form');
+    // 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.
+      if ($('.webform-client-form').length) {
+        isWebform = true;
+        $('form.webform-client-form').addClass('stripe-payment-form');
+      }
+      else {
+        $('#crm-container > form').addClass('stripe-payment-form');
       }
     }
+    
     $form   = $('form.stripe-payment-form');
     if (isWebform) {
       $submit = $form.find('.button-primary');
diff --git a/stripe.php b/stripe.php
index 1c7e9f14..e766e9fc 100644
--- a/stripe.php
+++ b/stripe.php
@@ -204,6 +204,18 @@ function stripe_civicrm_managed(&$entities) {
     }
   }
 
+/**
+ * Add stripe.js to forms, to generate stripe token
+ * @param $formName
+ * @param $form
+ */
+function stripe_civicrm_buildForm($formName, &$form) {
+  if (($formName == 'CRM_Member_Form_MembershipRenewal') && !empty($form->_paymentProcessor['class_name'])) {
+    // civicrm_stripe.js is not included on backend form renewal unless we add it here.
+    CRM_Core_Resources::singleton()->addScriptFile('com.drastikbydesign.stripe', 'js/civicrm_stripe.js');
+  }
+}
+
 /*
  * Implementation of hook_idsException.
  *
-- 
GitLab