From 3c7ee57ef9f9ed041a96fc6f78473f55c564630b Mon Sep 17 00:00:00 2001
From: "Matthew Wire (MJW Consulting)" <mjw@mjwconsult.co.uk>
Date: Thu, 4 Apr 2019 16:34:55 +0100
Subject: [PATCH] Remove is_live parameter from civicrm_stripe_customers. We
 can derive from payment processor id

---
 CRM/Core/Payment/Stripe.php   |  1 -
 CRM/Stripe/Customer.php       | 43 +++++++++++++++--------------------
 api/v3/StripeCustomer.php     | 19 +++-------------
 api/v3/StripeSubscription.php |  8 -------
 sql/auto_install.sql          |  1 -
 5 files changed, 21 insertions(+), 51 deletions(-)

diff --git a/CRM/Core/Payment/Stripe.php b/CRM/Core/Payment/Stripe.php
index beb6c4dd..5e322728 100644
--- a/CRM/Core/Payment/Stripe.php
+++ b/CRM/Core/Payment/Stripe.php
@@ -443,7 +443,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
     $customerParams = [
       'contact_id' => $contactId,
       'card_token' => $card_token,
-      'is_live' => !$this->_paymentProcessor['is_test'],
       'processor_id' => $this->_paymentProcessor['id'],
       'email' => $email,
     ];
diff --git a/CRM/Stripe/Customer.php b/CRM/Stripe/Customer.php
index a1eed976..c8e94c3c 100644
--- a/CRM/Stripe/Customer.php
+++ b/CRM/Stripe/Customer.php
@@ -11,7 +11,7 @@ class CRM_Stripe_Customer {
    * @throws \Civi\Payment\Exception\PaymentProcessorException
    */
   public static function find($params) {
-    $requiredParams = ['is_live', 'processor_id'];
+    $requiredParams = ['processor_id'];
     foreach ($requiredParams as $required) {
       if (empty($params[$required])) {
         throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (find): Missing required parameter: ' . $required);
@@ -22,18 +22,17 @@ class CRM_Stripe_Customer {
     }
     $queryParams = [
       1 => [$params['contact_id'], 'String'],
-      2 => [$params['is_live'] ? 1 : 0, 'Boolean'],
-      3 => [$params['processor_id'], 'Positive'],
+      2 => [$params['processor_id'], 'Positive'],
     ];
     
 
     return CRM_Core_DAO::singleValueQuery("SELECT id
       FROM civicrm_stripe_customers
-      WHERE contact_id = %1 AND is_live = %2 AND processor_id = %3", $queryParams);
+      WHERE contact_id = %1 AND processor_id = %3", $queryParams);
   }
 
   /**
-   * Find the details (contact_id, is_live, processor_id) for an existing Stripe customer in the CiviCRM database
+   * Find the details (contact_id, processor_id) for an existing Stripe customer in the CiviCRM database
    *
    * @param string $stripeCustomerId
    *
@@ -44,28 +43,26 @@ class CRM_Stripe_Customer {
       1 => [$stripeCustomerId, 'String'],
     ];
 
-    $dao = CRM_Core_DAO::executeQuery("SELECT contact_id, is_live, processor_id
+    $dao = CRM_Core_DAO::executeQuery("SELECT contact_id, processor_id
       FROM civicrm_stripe_customers
       WHERE id = %1", $queryParams);
     $dao->fetch();
     return [
       'contact_id' => $dao->contact_id,
-      'is_live' => $dao->is_live,
       'processor_id' => $dao->processor_id,
     ];
   }
 
   /**
-   * Find the details (contact_id, is_live, processor_id) for an existing Stripe customer in the CiviCRM database
+   * Find the details (contact_id, processor_id) for an existing Stripe customer in the CiviCRM database
    *
    * @param string $stripeCustomerId
    *
    * @return array|null
    */
-  public static function getAll($isLive, $processorId, $options = []) {
+  public static function getAll($processorId, $options = []) {
     $queryParams = [
-      1 => [$isLive ? 1 : 0, 'Boolean'],
-      2 => [$processorId, 'Integer'],
+      1 => [$processorId, 'Integer'],
     ];
 
     $limitClause = '';
@@ -79,7 +76,7 @@ class CRM_Stripe_Customer {
     $customerIds = [];
     $dao = CRM_Core_DAO::executeQuery("SELECT id
       FROM civicrm_stripe_customers
-      WHERE is_live = %1 AND processor_id = %2 {$limitClause}", $queryParams);
+      WHERE processor_id = %2 {$limitClause}", $queryParams);
     while ($dao->fetch()) {
       $customerIds[] = $dao->id;
     }
@@ -94,7 +91,7 @@ class CRM_Stripe_Customer {
    * @throws \Civi\Payment\Exception\PaymentProcessorException
    */
   public static function add($params) {
-    $requiredParams = ['contact_id', 'customer_id', 'is_live', 'processor_id'];
+    $requiredParams = ['contact_id', 'customer_id', 'processor_id'];
     foreach ($requiredParams as $required) {
       if (empty($params[$required])) {
         throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (add): Missing required parameter: ' . $required);
@@ -104,11 +101,10 @@ class CRM_Stripe_Customer {
     $queryParams = [
       1 => [$params['contact_id'], 'String'],
       2 => [$params['customer_id'], 'String'],
-      3 => [$params['is_live'] ? 1 : 0, 'Boolean'],
-      4 => [$params['processor_id'], 'Integer'],
+      3 => [$params['processor_id'], 'Integer'],
     ];
     CRM_Core_DAO::executeQuery("INSERT INTO civicrm_stripe_customers
-          (contact_id, id, is_live, processor_id) VALUES (%1, %2, %3, %4)", $queryParams);
+          (contact_id, id, processor_id) VALUES (%1, %2, %3)", $queryParams);
   }
 
   /**
@@ -120,7 +116,7 @@ class CRM_Stripe_Customer {
    * @throws \Civi\Payment\Exception\PaymentProcessorException
    */
   public static function create($params, $paymentProcessor) {
-    $requiredParams = ['contact_id', 'card_token', 'is_live', 'processor_id'];
+    $requiredParams = ['contact_id', 'card_token', 'processor_id'];
     // $optionalParams = ['email'];
     foreach ($requiredParams as $required) {
       if (empty($params[$required])) {
@@ -153,7 +149,6 @@ class CRM_Stripe_Customer {
     $params = [
       'contact_id' => $params['contact_id'],
       'customer_id' => $stripeCustomer->id,
-      'is_live' => $params['is_live'],
       'processor_id' => $params['processor_id'],
     ];
     self::add($params);
@@ -169,7 +164,7 @@ class CRM_Stripe_Customer {
    * @throws \Civi\Payment\Exception\PaymentProcessorException
    */
   public static function delete($params) {
-    $requiredParams = ['is_live', 'processor_id'];
+    $requiredParams = ['processor_id'];
     foreach ($requiredParams as $required) {
       if (empty($params[$required])) {
         throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (delete): Missing required parameter: ' . $required);
@@ -182,20 +177,18 @@ class CRM_Stripe_Customer {
     if (!empty($params['id'])) {
       $queryParams = [
         1 => [$params['id'], 'String'],
-        2 => [$params['is_live'] ? 1 : 0, 'Boolean'],
-        3 => [$params['processor_id'], 'Integer'],
+        2 => [$params['processor_id'], 'Integer'],
       ];
       $sql = "DELETE FROM civicrm_stripe_customers
-            WHERE id = %1 AND is_live = %2 AND processor_id = %3";
+            WHERE id = %1 AND processor_id = %3";
     }
     else {
       $queryParams = [
         1 => [$params['contact_id'], 'String'],
-        2 => [$params['is_live'] ? 1 : 0, 'Boolean'],
-        3 => [$params['processor_id'], 'Integer'],
+        2 => [$params['processor_id'], 'Integer'],
       ];
       $sql = "DELETE FROM civicrm_stripe_customers
-            WHERE contact_id = %1 AND is_live = %2 AND processor_id = %3";
+            WHERE contact_id = %1 AND processor_id = %3";
     }
     CRM_Core_DAO::executeQuery($sql, $queryParams);
   }
diff --git a/api/v3/StripeCustomer.php b/api/v3/StripeCustomer.php
index 3d7cec84..180d83f2 100644
--- a/api/v3/StripeCustomer.php
+++ b/api/v3/StripeCustomer.php
@@ -19,8 +19,6 @@ function _civicrm_api3_stripe_customer_get_spec(&$spec) {
   $spec['id']['type'] = CRM_Utils_Type::T_STRING;
   $spec['contact_id']['title'] = ts("CiviCRM Contact ID");
   $spec['contact_id']['type'] = CRM_Utils_Type::T_INT;
-  $spec['is_live']['title'] = ts("Is live processor");
-  $spec['is_live']['type'] = CRM_Utils_Type::T_BOOLEAN;
   $spec['processor_id']['title'] = ts("Payment Processor ID");
   $spec['processor_id']['type'] = CRM_Utils_Type::T_INT;
 }
@@ -51,11 +49,6 @@ function civicrm_api3_stripe_customer_get($params) {
         $index++;
         break;
 
-      case 'is_live':
-        $where[$index] = "{$key}=%{$index}";
-        $whereParam[$index] = [$value, 'Boolean'];
-        $index++;
-        break;
     }
   }
 
@@ -70,7 +63,6 @@ function civicrm_api3_stripe_customer_get($params) {
     $result = [
       'id' => $dao->id,
       'contact_id' => $dao->contact_id,
-      'is_live' => $dao->is_live,
       'processor_id' => $dao->processor_id,
     ];
     if ($dao->email) {
@@ -93,9 +85,6 @@ function _civicrm_api3_stripe_customer_delete_spec(&$spec) {
   $spec['id']['type'] = CRM_Utils_Type::T_STRING;
   $spec['contact_id']['title'] = ts("CiviCRM Contact ID");
   $spec['contact_id']['type'] = CRM_Utils_Type::T_INT;
-  $spec['is_live']['title'] = ts("Is live processor");
-  $spec['is_live']['type'] = CRM_Utils_Type::T_BOOLEAN;
-  $spec['is_live']['api.required'] = TRUE;
   $spec['processor_id']['title'] = ts("Payment Processor ID");
   $spec['processor_id']['type'] = CRM_Utils_Type::T_INT;
   $spec['processor_id']['api.required'] = TRUE;
@@ -198,8 +187,6 @@ function _civicrm_api3_stripe_customer_updatestripemetadata_spec(&$spec) {
   $spec['id']['api.required'] = FALSE;
   $spec['dryrun']['api.required'] = TRUE;
   $spec['dryrun']['type'] = CRM_Utils_Type::T_BOOLEAN;
-  $spec['is_live']['api.required'] = FALSE;
-  $spec['is_live']['type'] = CRM_Utils_Type::T_BOOLEAN;
   $spec['processor_id']['api.required'] = FALSE;
   $spec['processor_id']['type'] = CRM_Utils_Type::T_INT;
 }
@@ -222,10 +209,10 @@ function civicrm_api3_stripe_customer_updatestripemetadata($params) {
   // Check params
   if (empty($params['id'])) {
     // We're doing an update on all stripe customers
-    if (!isset($params['is_live']) || !isset($params['processor_id'])) {
-      throw new CiviCRM_API3_Exception('Missing required parameters is_live and/or processor_id when using without a customer id');
+    if (!isset($params['processor_id'])) {
+      throw new CiviCRM_API3_Exception('Missing required parameters processor_id when using without a customer id');
     }
-    $customerIds = CRM_Stripe_Customer::getAll($params['is_live'], $params['processor_id'], $params['options']);
+    $customerIds = CRM_Stripe_Customer::getAll($params['processor_id'], $params['options']);
   }
   else {
     $customerIds = [$params['id']];
diff --git a/api/v3/StripeSubscription.php b/api/v3/StripeSubscription.php
index d1fa2045..2717747d 100644
--- a/api/v3/StripeSubscription.php
+++ b/api/v3/StripeSubscription.php
@@ -19,8 +19,6 @@ function _civicrm_api3_stripe_subscription_get_spec(&$spec) {
   $spec['customer_id']['type'] = CRM_Utils_Type::T_STRING;
   $spec['contribution_recur_id']['title'] = ts("Contribution Recur ID");
   $spec['contribution_recur_id']['type'] = CRM_Utils_Type::T_INT;
-  $spec['is_live']['title'] = ts("Is live processor");
-  $spec['is_live']['type'] = CRM_Utils_Type::T_BOOLEAN;
   $spec['processor_id']['title'] = ts("Payment Processor ID");
   $spec['processor_id']['type'] = CRM_Utils_Type::T_INT;
   $spec['end_time_id']['title'] = ts("End Time");
@@ -57,11 +55,6 @@ function civicrm_api3_stripe_subscription_get($params) {
         $index++;
         break;
 
-      case 'is_live':
-        $where[$index] = "{$key}=%{$index}";
-        $whereParam[$index] = [$value, 'Boolean'];
-        $index++;
-        break;
     }
   }
 
@@ -78,7 +71,6 @@ function civicrm_api3_stripe_subscription_get($params) {
       'subscription_id' => $dao->subscription_id,
       'customer_id' => $dao->customer_id,
       'contribution_recur_id' => $dao->contribution_recur_id,
-      'is_live' => $dao->is_live,
       'processor_id' => $dao->processor_id,
       'end_time' => $dao->end_time,
     ];
diff --git a/sql/auto_install.sql b/sql/auto_install.sql
index daadd2d2..2ca7777e 100644
--- a/sql/auto_install.sql
+++ b/sql/auto_install.sql
@@ -2,7 +2,6 @@
   CREATE TABLE IF NOT EXISTS `civicrm_stripe_customers` (
     `id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
     `contact_id` int(10) UNSIGNED DEFAULT NULL COMMENT 'FK ID from civicrm_contact',
-    `is_live` tinyint(4) NOT NULL COMMENT 'Whether this is a live or test transaction',
     `processor_id` int(10) DEFAULT NULL COMMENT 'ID from civicrm_payment_processor',
     UNIQUE KEY `id` (`id`),
     CONSTRAINT `FK_civicrm_stripe_customers_contact_id` FOREIGN KEY (`contact_id`)
-- 
GitLab