From 1ee49d4de9183626643cd87a953e74fd848f0c3f Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Wed, 15 Dec 2021 14:36:43 +0000
Subject: [PATCH] Switch final instances of static Stripe library access to
 object

---
 CRM/Stripe/Customer.php |  2 +-
 CRM/Stripe/Upgrader.php | 13 +++++++------
 stripe.php              |  5 ++---
 utils/fix-issue-44.php  |  6 ++++--
 4 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/CRM/Stripe/Customer.php b/CRM/Stripe/Customer.php
index 9b8d3302..92254ba6 100644
--- a/CRM/Stripe/Customer.php
+++ b/CRM/Stripe/Customer.php
@@ -180,7 +180,7 @@ class CRM_Stripe_Customer {
     $stripeCustomerParams = self::getStripeCustomerMetadata($params);
 
     try {
-      $stripeCustomer = \Stripe\Customer::update($stripeCustomerID, $stripeCustomerParams);
+      $stripeCustomer = $stripe->stripeClient->customers->update($stripeCustomerID, $stripeCustomerParams);
     }
     catch (Exception $e) {
       $err = CRM_Core_Payment_Stripe::parseStripeException('create_customer', $e, FALSE);
diff --git a/CRM/Stripe/Upgrader.php b/CRM/Stripe/Upgrader.php
index c5cac2e7..549abd4b 100644
--- a/CRM/Stripe/Upgrader.php
+++ b/CRM/Stripe/Upgrader.php
@@ -158,21 +158,22 @@ class CRM_Stripe_Upgrader extends CRM_Stripe_Upgrader_Base {
     $dbName = DB::connect($config->dsn)->_db;
 
     $null_count =  CRM_Core_DAO::executeQuery('SELECT COUNT(*) FROM civicrm_stripe_subscriptions where subscription_id IS NULL');
-    if ( $null_count == 0 ) {
+    if ($null_count == 0) {
       $this->ctx->log->info('Skipped civicrm_stripe update 5004.  No nulls found in column subscription_id in our civicrm_stripe_subscriptions table.');
     }
     else {
       $customer_infos = CRM_Core_DAO::executeQuery("SELECT customer_id,processor_id
       FROM `civicrm_stripe_subscriptions`;");
-      while ( $customer_infos->fetch() ) {
+      while ($customer_infos->fetch()) {
         $processor_id = $customer_infos->processor_id;
         $customer_id = $customer_infos->customer_id;
         try {
-          $processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $processor_id]));
+          /** @var \CRM_Core_Payment_Stripe $paymentProcessor */
+          $paymentProcessor = \Civi\Payment\System::singleton()->getById($processor_id);
 
-          $subscription = \Stripe\Subscription::all([
-            'customer'=> $customer_id,
-            'limit'=>1,
+          $subscription = $paymentProcessor->stripeClient->subscriptions->all([
+            'customer' => $customer_id,
+            'limit' => 1,
           ]);
         }
         catch (Exception $e) {
diff --git a/stripe.php b/stripe.php
index c7581cd2..8339fbcf 100644
--- a/stripe.php
+++ b/stripe.php
@@ -150,7 +150,6 @@ function stripe_civicrm_buildForm($formName, &$form) {
 
       /** @var \CRM_Core_Payment_Stripe $paymentProcessor */
       $paymentProcessor = \Civi\Payment\System::singleton()->getById($form->_paymentProcessor['id']);
-      $paymentProcessor->setAPIParams();
       try {
         $jsVars = [
           'id' => $form->_paymentProcessor['id'],
@@ -164,7 +163,7 @@ function stripe_civicrm_buildForm($formName, &$form) {
         switch (substr($paymentIntent['stripe_intent_id'], 0, 2)) {
           case 'pi':
             // pi_ Stripe PaymentIntent
-            $stripePaymentIntent = \Stripe\PaymentIntent::retrieve($paymentIntent['stripe_intent_id']);
+            $stripePaymentIntent = $paymentProcessor->stripeClient->paymentIntents->retrieve($paymentIntent['stripe_intent_id']);
             // We need the confirmation_method to decide whether to use handleCardAction (manual) or handleCardPayment (automatic) on the js side
             $jsVars['paymentIntentID'] = $stripePaymentIntent->id;
             $jsVars['intentStatus'] = $stripePaymentIntent->status;
@@ -173,7 +172,7 @@ function stripe_civicrm_buildForm($formName, &$form) {
 
           case 'se':
             // seti_ Stripe SetupIntent
-            $stripeSetupIntent = \Stripe\SetupIntent::retrieve($paymentIntent['stripe_intent_id']);
+            $stripeSetupIntent = $paymentProcessor->stripeClient->setupIntents->retrieve($paymentIntent['stripe_intent_id']);
             $jsVars['setupIntentID'] = $stripeSetupIntent->id;
             $jsVars['setupIntentNextAction'] = $stripeSetupIntent->next_action;
             $jsVars['setupIntentClientSecret'] = $stripeSetupIntent->client_secret;
diff --git a/utils/fix-issue-44.php b/utils/fix-issue-44.php
index 966441d2..60d149be 100644
--- a/utils/fix-issue-44.php
+++ b/utils/fix-issue-44.php
@@ -51,10 +51,12 @@ while ($dao->fetch()) {
   if (!$paymentProcessor) {
     echo "Failed to find a stripe payment processor for recurring contrib $dao->contribution_recur_id\n";
   }
-  $processor = new CRM_Core_Payment_Stripe('', civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $paymentProcessor['id']]));
+
+  /** @var \CRM_Core_Payment_Stripe $processor */
+  $processor = \Civi\Payment\System::singleton()->getById($paymentProcessor['id']);
 
   try {
-    $results = Charge::retrieve(['id' => $dao->trxn_id]);
+    $results = $processor->stripeClient->charges->retrieve($dao->trxn_id);
     //print json_encode($results, JSON_PRETTY_PRINT);
     if (empty($results->created)) {
       echo " Failed to retrieve a charge created date\n";
-- 
GitLab