Skip to content

Instantly share code, notes, and snippets.

@herbdool
Created May 29, 2018 00:16
Show Gist options
  • Save herbdool/b26dcd73219692be100a08f4e18bec3b to your computer and use it in GitHub Desktop.
Save herbdool/b26dcd73219692be100a08f4e18bec3b to your computer and use it in GitHub Desktop.
Stripe backend payments fix
diff --git a/CRM/Core/Form/Stripe.php b/CRM/Core/Form/Stripe.php
index 828e2ea..4b4963e 100644
--- a/CRM/Core/Form/Stripe.php
+++ b/CRM/Core/Form/Stripe.php
@@ -8,9 +8,35 @@ class CRM_Core_Form_Stripe extends CRM_Core_Form {
/**
* Function to access protected payProcessors array in event registraion forms
+ * to see if any of them are stripe processors.
*/
- public static function get_ppids(&$form) {
- $payprocessorIds = $form->_paymentProcessors;
- return $payprocessorIds;
+ public static function get_stripe_ppids(&$form, $live = 1) {
+ $is_test = $live == 1 ? 0 : 1;
+ $stripe_ppids = array();
+ // On Contribution pages, this property is public, on membership pages it is
+ // private. Check if it is available.
+ if(isset($form->_paymentProcessors)) {
+ foreach($form->_paymentProcessors as $k => $v) {
+ if($v['object'] && $v['object']->_processorName == 'Stripe') {
+ if($v['object']->_islive == $live) {
+ $stripe_ppids[] = $k;
+ }
+ }
+ }
+ }
+ if(isset($form->_processors)) {
+ foreach($form->_processors as $k => $v) {
+ $sql = "SELECT pp.id FROM civicrm_payment_processor pp JOIN
+ civicrm_payment_processor_type ppt ON pp.payment_processor_type_id =
+ ppt.id AND ppt.name = 'Stripe' AND pp.is_active = 1 AND ppt.is_active = 1
+ AND pp.id = %0 AND is_test = %1";
+ $params = array(0 => array($k, 'Integer'), 1 => array($is_test, 'Integer'));
+ $dao = CRM_Core_DAO::executeQuery($sql, $params);
+ if($dao->N == 1) {
+ $stripe_ppids[] = $k;
+ }
+ }
+ }
+ return $stripe_ppids;
}
}
diff --git a/CRM/Core/Payment/Stripe.php b/CRM/Core/Payment/Stripe.php
index 9ca4fab..d97e48f 100644
--- a/CRM/Core/Payment/Stripe.php
+++ b/CRM/Core/Payment/Stripe.php
@@ -237,13 +237,23 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
* @param $form - reference to the form object
*/
public function buildForm(&$form) {
- $stripe_ppid = self::get_stripe_ppid($form);
+ $stripe_ppid = $this->get_stripe_ppid($form);
// Add the ID to our form so our js can tell if Stripe has been selected.
$form->addElement('hidden', 'stripe_id', $stripe_ppid, array('id' => 'stripe-id'));
$stripe_key = self::stripe_get_key($stripe_ppid);
+ if (get_class($form) == 'CRM_Financial_Form_Payment') {
+ // This means the user has selected a different payment processor to
+ // an existing form. Don't add a second stripe-pub-key - instead add a
+ // new one that overrides the first
+ $form->addElement('hidden', 'stripe_pub_key_updated', $stripe_key, array('id' => 'stripe-pub-key-updated'));
+ CRM_Core_Error::debug_log_message("Updating public key: $stripe_key");
+ }
+ else {
$form->addElement('hidden', 'stripe_pub_key', $stripe_key, array('id' => 'stripe-pub-key'));
+ CRM_Core_Error::debug_log_message("Adding public key: $stripe_key");
+ }
$params = $form->get('params');
// Contrib forms store this in $params, Event forms in $params[0].
@@ -273,7 +283,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
}
}
- public static function get_stripe_ppid($form) {
+ public function get_stripe_ppid($form) {
if (empty($form->_paymentProcessor)) {
return;
}
@@ -282,15 +292,15 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
if (in_array(get_class($form), array('CRM_Financial_Form_Payment', 'CRM_Contribute_Form_Contribution'))) {
return $stripe_ppid = $form->_paymentProcessor['id'];
}
+ elseif (!empty($form->_paymentProcessorID)) {
+ return $form->_paymentProcessorID;
+ }
else {
// Find a Stripe pay processor ascociated with this Civi form and find the ID.
- // $payProcessors = $form->_paymentProcessors;
- $payProcessors = CRM_Core_Form_Stripe::get_ppids($form);
- foreach ($payProcessors as $payProcessor) {
- if ($payProcessor['class_name'] == 'Payment_Stripe') {
- return $stripe_ppid = $payProcessor['id'];
- break;
- }
+ $stripePayProcessors = CRM_Core_Form_Stripe::get_stripe_ppids($form, $this->_islive);
+ if (count($stripePayProcessors) > 0) {
+ // Return the first one.
+ return $stripePayProcessors[0];
}
}
// None of the payprocessors are Stripe.
diff --git a/js/civicrm_stripe.js b/js/civicrm_stripe.js
index 9798150..4437852 100644
--- a/js/civicrm_stripe.js
+++ b/js/civicrm_stripe.js
@@ -44,15 +44,18 @@
Stripe.setPublishableKey($('#stripe-pub-key').val());
});
+ // 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 {
- if (!($('.stripe-payment-form').length)) {
- $('#crm-main-content-wrapper form').addClass('stripe-payment-form');
+ $('#crm-container > form').addClass('stripe-payment-form');
}
}
+
$form = $('form.stripe-payment-form');
if (isWebform) {
$submit = $form.find('.button-primary');
@@ -211,6 +214,13 @@
var cc_month = $form.find('#credit_card_exp_date\\[M\\]').val();
var cc_year = $form.find('#credit_card_exp_date\\[Y\\]').val();
}
+ // Check for an updated Stripe public key (if the user chose a different
+ // Stripe payment processor).
+
+ if ($('#stripe-pub-key-updated').length && $('#stripe-pub-key-updated').val() != $('#stripe-pub-key').val() ) {
+ Stripe.setPublishableKey($('#stripe-pub-key-updated').val());
+ }
+
Stripe.card.createToken({
name: $form.find('#billing_first_name').val() + ' ' + $form.find('#billing_last_name').val(),
address_zip: $form.find('#billing_postal_code-5').val(),
diff --git a/sites/all/extensions/com.drastikbydesign.stripe/stripe.php b/sites/all/extensions/com.drastikbydesign.stripe/stripe.php
index f770101..30805b5 100644
--- a/stripe.php
+++ b/stripe.php
@@ -2,6 +2,8 @@
require_once 'stripe.civix.php';
+static $_stripe_scripts_added;
+
/**
* Implementation of hook_civicrm_config().
*/
@@ -196,10 +198,40 @@ function stripe_civicrm_managed(&$entities) {
* @return void
*/
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');
+ global $_stripe_scripts_added;
+ /* Adding stripe js:
+ * - Webforms don't get scripts added by hook_civicrm_buildForm so we have to user alterContent
+ * - (Webforms still call buildForm and it looks like they are added but they are not,
+ * which is why we check for $object instanceof CRM_Financial_Form_Payment here to ensure that
+ * Webforms always have scripts added).
+ * - Almost all forms have context = 'form' and a paymentprocessor object.
+ * - Membership backend form is a 'page' and has a _isPaymentProcessor=true flag.
+ *
+ */
+ if (($context == 'form' && !empty($object->_paymentProcessor['class_name']))
+ || (($context == 'page') && !empty($object->_isPaymentProcessor))) {
+ if (!$_stripe_scripts_added || $object instanceof CRM_Financial_Form_Payment) {
+ $stripeJSURL = CRM_Core_Resources::singleton()
+ ->getUrl('com.drastikbydesign.stripe', 'js/civicrm_stripe.js');
$content .= "<script src='{$stripeJSURL}'></script>";
+ $_stripe_scripts_added = TRUE;
+ }
}
}
+ /**
+ * Add stripe.js to forms, to generate stripe token
+ * hook_civicrm_alterContent is not called for all forms (eg. CRM_Contribute_Form_Contribution on backend)
+ * @param $formName
+ * @param $form
+ */
+ function stripe_civicrm_buildForm($formName, &$form) {
+ global $_stripe_scripts_added;
+ if (!empty($form->_paymentProcessor['class_name'])) {
+ if (!$_stripe_scripts_added) {
+ CRM_Core_Resources::singleton()
+ ->addScriptFile('com.drastikbydesign.stripe', 'js/civicrm_stripe.js');
+ }
+ $_stripe_scripts_added = TRUE;
+ }
}
+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment