From f5d1473d0104b22f2ce4edf91676d18cf5f301fd Mon Sep 17 00:00:00 2001
From: Peter Hartmann <peter@hartmanncomputer.com>
Date: Sun, 15 Jan 2017 22:37:22 -0500
Subject: [PATCH] possible fix for #155 - makes transactions

---
 CRM/Core/Payment/Stripe.php | 154 ++++++++++++++++--------------------
 stripe.php                  |  32 ++++++++
 2 files changed, 99 insertions(+), 87 deletions(-)

diff --git a/CRM/Core/Payment/Stripe.php b/CRM/Core/Payment/Stripe.php
index d3a3793d..7c5226d4 100644
--- a/CRM/Core/Payment/Stripe.php
+++ b/CRM/Core/Payment/Stripe.php
@@ -1,5 +1,72 @@
 <?php
 
+  /**
+   * Return the stripe api public key (aka password)
+   *
+   * If this form could conceiveably now or at any time in the future
+   * contain a Stripe payment processor, return the api public key for
+   * that processor.
+   */
+  function stripe_get_key($form) {
+    if (empty($form->_paymentProcessor)) {
+      return;
+    }
+    // Only return first value if Stripe is the only/default.
+    if ($form->_paymentProcessor['payment_processor_type'] == 'Stripe') {
+      if (isset($form->_paymentProcessor['password'])) {
+        return $form->_paymentProcessor['password'];
+      }
+    }
+
+    // Otherwise we need to look through all active payprocs and find Stripe.
+    $is_test = 0;
+    if (isset($form->_mode)) {
+      $is_test = $form->_mode == 'live' ? 0 : 1;
+    }
+
+    // The _paymentProcessors array seems to be the most reliable way to find
+    // if the form is using Stripe.
+    if (!empty($form->_paymentProcessors)) {
+      foreach ($form->_paymentProcessors as $pp) {
+        if ($pp['payment_processor_type'] == 'Stripe') {
+          if (!empty($pp['password'])) {
+            return $pp['password'];
+          }
+          // We have a match.
+          return stripe_get_key_for_name($pp['name'], $is_test);
+        }
+      }
+    }
+    // Return NULL if this is not a form with Stripe involved.
+    return NULL;
+  }
+
+  /**
+   * Given a payment processor name, return the pub key.
+   */
+  function stripe_get_key_for_name($name, $is_test) {
+    try {
+      $params = array('name' => $name, 'is_test' => $is_test);
+      $results = civicrm_api3('PaymentProcessor', 'get', $params);
+      if ($results['count'] == 1) {
+        $result = array_pop($results['values']);
+        return $result['password'];
+      }
+    }
+    catch (CiviCRM_API3_Exception $e) {
+      return NULL;
+    }
+  }
+
+  /**
+   * Add publishable key and event bindings for Stripe.js.
+   */
+  function stripe_add_stripe_js($stripe_key, $form) {
+    $form->addElement('hidden', 'stripe_pub_key', $stripe_key, array('id' => 'stripe-pub-key'));
+    CRM_Core_Resources::singleton()->addScriptFile('com.drastikbydesign.stripe', 'js/civicrm_stripe.js', 0);
+  }
+
+
 /*
  * Payment Processor class for Stripe
  */
@@ -231,8 +298,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
     return $return;
   }
 
-
-
   /**
    * Implementation of hook_civicrm_validateForm().
    *
@@ -244,26 +309,7 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
    * @param $form - reference to the form object
    * @param $errors - Reference to the errors array.
    *
-   * @todo This won't run, and needs to be moved elsewhere.
    */
-  /*public function stripe_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
-    if (empty($form->_paymentProcessor['payment_processor_type'])) {
-      return;
-    }
-    // If Stripe is active here.
-    if (isset($form->_elementIndex['stripe_token'])) {
-      if ($form->elementExists('credit_card_number')) {
-        $cc_field = $form->getElement('credit_card_number');
-        $form->removeElement('credit_card_number', true);
-        $form->addElement($cc_field);
-      }
-      if ($form->elementExists('cvv2')) {
-        $cvv2_field = $form->getElement('cvv2');
-        $form->removeElement('cvv2', true);
-        $form->addElement($cvv2_field);
-      }
-    }
-  }*/
 
   /**
    * Implementation of hook_civicrm_buildForm().
@@ -296,72 +342,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
     }
   }
 
-  /**
-   * Return the stripe api public key (aka password)
-   *
-   * If this form could conceiveably now or at any time in the future
-   * contain a Stripe payment processor, return the api public key for
-   * that processor.
-   */
-  public function stripe_get_key($form) {
-    if (empty($form->_paymentProcessor)) {
-      return;
-    }
-    // Only return first value if Stripe is the only/default.
-    if ($form->_paymentProcessor['payment_processor_type'] == 'Stripe') {
-      if (isset($form->_paymentProcessor['password'])) {
-        return $form->_paymentProcessor['password'];
-      }
-    }
-
-    // Otherwise we need to look through all active payprocs and find Stripe.
-    $is_test = 0;
-    if (isset($form->_mode)) {
-      $is_test = $form->_mode == 'live' ? 0 : 1;
-    }
-
-    // The _paymentProcessors array seems to be the most reliable way to find
-    // if the form is using Stripe.
-    if (!empty($form->_paymentProcessors)) {
-      foreach ($form->_paymentProcessors as $pp) {
-        if ($pp['payment_processor_type'] == 'Stripe') {
-          if (!empty($pp['password'])) {
-            return $pp['password'];
-          }
-          // We have a match.
-          return stripe_get_key_for_name($pp['name'], $is_test);
-        }
-      }
-    }
-    // Return NULL if this is not a form with Stripe involved.
-    return NULL;
-  }
-
-  /**
-   * Given a payment processor name, return the pub key.
-   */
-  public function stripe_get_key_for_name($name, $is_test) {
-    try {
-      $params = array('name' => $name, 'is_test' => $is_test);
-      $results = civicrm_api3('PaymentProcessor', 'get', $params);
-      if ($results['count'] == 1) {
-        $result = array_pop($results['values']);
-        return $result['password'];
-      }
-    }
-    catch (CiviCRM_API3_Exception $e) {
-      return NULL;
-    }
-  }
-
-  /**
-   * Add publishable key and event bindings for Stripe.js.
-   */
-  public function stripe_add_stripe_js($stripe_key, $form) {
-    $form->addElement('hidden', 'stripe_pub_key', $stripe_key, array('id' => 'stripe-pub-key'));
-    CRM_Core_Resources::singleton()->addScriptFile('com.drastikbydesign.stripe', 'js/civicrm_stripe.js', 0);
-  }
-
   /**
    * Submit a payment using Stripe's PHP API:
    * https://stripe.com/docs/api?lang=php
diff --git a/stripe.php b/stripe.php
index f4617c32..3ee4ecdd 100644
--- a/stripe.php
+++ b/stripe.php
@@ -151,3 +151,35 @@ function stripe_civicrm_managed(&$entities) {
 
   return _stripe_civix_civicrm_managed($entities);
 }
+
+/**
+   * Implementation of hook_civicrm_validateForm().
+   *
+   * Prevent server validation of cc fields
+   *
+   * @param $formName - the name of the form
+   * @param $fields - Array of name value pairs for all 'POST'ed form values
+   * @param $files - Array of file properties as sent by PHP POST protocol
+   * @param $form - reference to the form object
+   * @param $errors - Reference to the errors array.
+   *
+*/
+
+ function stripe_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
+    if (empty($form->_paymentProcessor['payment_processor_type'])) {
+      return;
+    }
+    // If Stripe is active here.
+    if (isset($form->_elementIndex['stripe_token'])) {
+      if ($form->elementExists('credit_card_number')) {
+        $cc_field = $form->getElement('credit_card_number');
+        $form->removeElement('credit_card_number', true);
+        $form->addElement($cc_field);
+      }
+      if ($form->elementExists('cvv2')) {
+        $cvv2_field = $form->getElement('cvv2');
+        $form->removeElement('cvv2', true);
+        $form->addElement($cvv2_field);
+      }
+    }
+  }
-- 
GitLab