From 52efe677f9a891af02348e2cee45916a9a3b4b3a Mon Sep 17 00:00:00 2001
From: Matthew Wire <devel@mrwire.co.uk>
Date: Tue, 17 Oct 2017 22:17:55 +0100
Subject: [PATCH] Cleanup, load js.stripe.com for events

---
 js/civicrm_stripe.js |  5 ++++-
 stripe.php           | 26 +++++---------------------
 2 files changed, 9 insertions(+), 22 deletions(-)

diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index d75a04f0..06694b2b 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -50,7 +50,10 @@
 
   // Re-prep form when we've loaded a new payproc
   $( document ).ajaxComplete(function( event, xhr, settings ) {
-    if (settings.url.match("/civicrm/payment/form?")) {
+    // /civicrm/payment/form? occurs when a payproc is selected on page
+    // /civicrm/contact/view/participant occurs when payproc is first loaded on event credit card payment
+    if ((settings.url.match("/civicrm/payment/form?"))
+       || (settings.url.match("/civicrm/contact/view/participant?"))) {
       loadStripeBillingBlock();
     }
   });
diff --git a/stripe.php b/stripe.php
index 162a4dd4..31b642df 100644
--- a/stripe.php
+++ b/stripe.php
@@ -60,7 +60,7 @@ function stripe_civicrm_install() {
     ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
   ");
 
-  return _stripe_civix_civicrm_install();
+  _stripe_civix_civicrm_install();
 }
 
 /**
@@ -73,7 +73,7 @@ function stripe_civicrm_uninstall() {
   CRM_Core_DAO::executeQuery("DROP TABLE civicrm_stripe_plans");
   CRM_Core_DAO::executeQuery("DROP TABLE civicrm_stripe_subscriptions");
 
-  return _stripe_civix_civicrm_uninstall();
+  _stripe_civix_civicrm_uninstall();
 }
 
 /**
@@ -96,7 +96,7 @@ function stripe_civicrm_enable() {
     <br />Webhook path to enter in Stripe:<br/><em>$webookhook_path</em>
     <br />");
 
-  return _stripe_civix_civicrm_enable();
+  _stripe_civix_civicrm_enable();
 }
 
 /**
@@ -149,7 +149,7 @@ function stripe_civicrm_managed(&$entities) {
     ),
   );
 
-  return _stripe_civix_civicrm_managed($entities);
+  _stripe_civix_civicrm_managed($entities);
 }
 
 /**
@@ -188,20 +188,6 @@ function stripe_civicrm_managed(&$entities) {
     }
   }
 
-  /**
-   * Implementation of hook_civicrm_alterContent
-   *
-   * Adding civicrm_stripe.js in a way that works for webforms and Civi forms.
-   *
-   * @return void
-   */
-  function stripe_civicrm_alterContent( &$content, $context, $tplName, &$object ) {
-    if ($context == 'form' && !empty($object->_paymentProcessor['class_name'])) {
-      $stripeJSURL = CRM_Core_Resources::singleton()->getUrl('com.drastikbydesign.stripe', 'js/civicrm_stripe.js');
-      $content .= "<script src='{$stripeJSURL}'></script>";
-    }
-  }
-
 /**
  * Add stripe.js to forms, to generate stripe token
  * @param $formName
@@ -209,10 +195,8 @@ function stripe_civicrm_managed(&$entities) {
  */
 function stripe_civicrm_buildForm($formName, &$form) {
   if (!empty($form->_paymentProcessor['class_name'])) {
-    //CRM_Core_Resources::singleton()->addScriptUrl('https://js.stripe.com/v2/', 10, 'page-body');
-  }
-  if (/*$form->isBackOffice &&*/ !empty($form->_paymentProcessor['class_name'])) {
     // civicrm_stripe.js is not included on backend form renewal unless we add it here.
+    CRM_Core_Resources::singleton()->addScriptUrl('https://js.stripe.com/v2/', 10, 'page-body');
     CRM_Core_Resources::singleton()->addScriptFile('com.drastikbydesign.stripe', 'js/civicrm_stripe.js');
   }
 }
-- 
GitLab