From 4a83f40d51c0313b876618ed1605922425d721bb Mon Sep 17 00:00:00 2001
From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Fri, 4 Nov 2022 20:39:40 +0000
Subject: [PATCH] Update tests and move some functions to new StripeCustomer
 BAO

---
 CRM/Stripe/BAO/StripeCustomer.php             | 23 +++++++-------
 .../Action/StripeCustomer/UpdateStripe.php    |  2 --
 Civi/Api4/StripeCustomer.php                  | 10 +++++++
 stripe.php                                    | 30 ++++++++++++++-----
 tests/phpunit/CRM/Stripe/ApiTest.php          |  6 ++++
 tests/phpunit/CRM/Stripe/BaseTest.php         |  4 +++
 tests/phpunit/CRM/Stripe/MergeTest.php        |  4 ++-
 7 files changed, 59 insertions(+), 20 deletions(-)

diff --git a/CRM/Stripe/BAO/StripeCustomer.php b/CRM/Stripe/BAO/StripeCustomer.php
index 23c96cdf..3abb14ae 100644
--- a/CRM/Stripe/BAO/StripeCustomer.php
+++ b/CRM/Stripe/BAO/StripeCustomer.php
@@ -57,7 +57,7 @@ class CRM_Stripe_BAO_StripeCustomer extends CRM_Stripe_DAO_StripeCustomer {
    * @throws \CiviCRM_API3_Exception
    * @throws \Civi\Payment\Exception\PaymentProcessorException
    */
-  public static function updateMetadata($params, $stripe, $stripeCustomerID) {
+  public static function updateMetadata(array $params, \CRM_Core_Payment_Stripe $stripe, string $stripeCustomerID) {
     $requiredParams = ['contact_id'];
     foreach ($requiredParams as $required) {
       if (empty($params[$required])) {
@@ -81,24 +81,27 @@ class CRM_Stripe_BAO_StripeCustomer extends CRM_Stripe_DAO_StripeCustomer {
   /**
    * Update the metadata at Stripe for a given contactid
    *
-   * @param int $contactId
-   * @param int $processorId optional
+   * @param int $contactID
+   *
    * @return void
    */
-  public static function updateMetadataForContact(int $contactId, int $processorId = NULL): void {
+  public static function updateMetadataForContact(int $contactID): void {
     $customers = StripeCustomer::get(FALSE)
-      ->addWhere('contact_id', '=', $contactId);
-    if ($processorId) {
-      $customers = $customers->addWhere('processor_id', '=', $processorId);
-    }
-    $customers = $customers->execute();
+      ->addWhere('contact_id', '=', $contactID)
+      ->execute();
 
     // Could be multiple customer_id's and/or stripe processors
     foreach ($customers as $customer) {
       /** @var CRM_Core_Payment_Stripe $stripe */
+      \Civi\Api4\StripeCustomer::updateStripe(FALSE)
+        ->setPaymentProcessorID($customer['processor_id'])
+        ->setContactID($contactID)
+        ->setCustomerID($customer['customer_id'])
+        ->execute()
+        ->first();
       $stripe = \Civi\Payment\System::singleton()->getById($customer['processor_id']);
       CRM_Stripe_BAO_StripeCustomer::updateMetadata(
-        ['contact_id' => $contactId, 'processor_id' => $customer['processor_id']],
+        ['contact_id' => $contactID, 'processor_id' => $customer['processor_id']],
         $stripe,
         $customer['customer_id']
       );
diff --git a/Civi/Api4/Action/StripeCustomer/UpdateStripe.php b/Civi/Api4/Action/StripeCustomer/UpdateStripe.php
index be3853d2..b59a073e 100644
--- a/Civi/Api4/Action/StripeCustomer/UpdateStripe.php
+++ b/Civi/Api4/Action/StripeCustomer/UpdateStripe.php
@@ -87,8 +87,6 @@ class UpdateStripe extends \Civi\Api4\Generic\AbstractAction {
 
     /** @var \CRM_Core_Payment_Stripe $paymentProcessor */
     $paymentProcessor = \Civi\Payment\System::singleton()->getById($this->paymentProcessorID);
-    // $stripeCustomer = $paymentProcessor->stripeClient->customers->retrieve($this->customerID);
-
     $stripeCustomer = \CRM_Stripe_BAO_StripeCustomer::updateMetadata(['contact_id' => $this->contactID, 'email' => $this->email, 'description' => $this->description], $paymentProcessor, $this->customerID);
 
     $result->exchangeArray($stripeCustomer->toArray());
diff --git a/Civi/Api4/StripeCustomer.php b/Civi/Api4/StripeCustomer.php
index 831db6e7..a2f7fd0e 100644
--- a/Civi/Api4/StripeCustomer.php
+++ b/Civi/Api4/StripeCustomer.php
@@ -20,4 +20,14 @@ class StripeCustomer extends Generic\DAOEntity {
       ->setCheckPermissions($checkPermissions);
   }
 
+  /**
+   * @param bool $checkPermissions
+   *
+   * @return \Civi\Api4\Action\StripeCustomer\UpdateStripe
+   */
+  public static function updateStripe($checkPermissions = TRUE) {
+    return (new Action\StripeCustomer\UpdateStripe(__CLASS__, __FUNCTION__))
+      ->setCheckPermissions($checkPermissions);
+  }
+
 }
diff --git a/stripe.php b/stripe.php
index 686483d0..6f6d8692 100644
--- a/stripe.php
+++ b/stripe.php
@@ -260,12 +260,28 @@ function stripe_civicrm_permission(&$permissions) {
  * Implements hook_civicrm_post().
  */
 function stripe_civicrm_post($op, $objectName, $objectId, &$objectRef) {
-  try {
-    if ($objectName == 'Contact' && $op == 'merge') {
-      CRM_Stripe_BAO_StripeCustomer::updateMetadataForContact($objectId);
-    }
-  }
-  catch (Exception $e) {
-    \Civi::log(E::SHORT_NAME)->error('Stripe Contact Merge failed: ' . $e->getMessage());
+  switch ($objectName) {
+    case 'Contact':
+      if ($op === 'merge') {
+        try {
+          CRM_Stripe_BAO_StripeCustomer::updateMetadataForContact($objectId);
+        }
+        catch (Exception $e) {
+          \Civi::log(E::SHORT_NAME)->error('Stripe Contact Merge failed: ' . $e->getMessage());
+        }
+      }
+    case 'Email':
+      if ($op === 'edit') {
+        try {
+          CRM_Stripe_BAO_StripeCustomer::updateMetadataForContact($objectRef->contact_id);
+        }
+        catch (Exception $e) {
+          \Civi::log(E::SHORT_NAME)->error('Stripe Contact update email failed: ' . $e->getMessage());
+        }
+      }
+
+    default:
+      return;
   }
+
 }
diff --git a/tests/phpunit/CRM/Stripe/ApiTest.php b/tests/phpunit/CRM/Stripe/ApiTest.php
index 8c05452d..b43027cb 100644
--- a/tests/phpunit/CRM/Stripe/ApiTest.php
+++ b/tests/phpunit/CRM/Stripe/ApiTest.php
@@ -184,6 +184,12 @@ class CRM_Stripe_ApiTest extends CRM_Stripe_BaseTest {
       ->willReturn(
         new PropertySpy('customers.retrieve', ['id' => 'cus_mock'])
       );
+    $stripeClient->customers
+      ->method('update')
+      ->with($this->equalTo('cus_mock'))
+      ->willReturn(
+        new PropertySpy('customers.update', ['id' => 'cus_mock'])
+      );
 
     $mockPlan = $this->createMock('Stripe\\Plan');
     $mockPlan
diff --git a/tests/phpunit/CRM/Stripe/BaseTest.php b/tests/phpunit/CRM/Stripe/BaseTest.php
index 9fb5f8ca..7838e0f2 100644
--- a/tests/phpunit/CRM/Stripe/BaseTest.php
+++ b/tests/phpunit/CRM/Stripe/BaseTest.php
@@ -388,6 +388,10 @@ class PropertySpy implements ArrayAccess, Iterator, Countable, JsonSerializable
     return array_key_exists(key($this->_props), $this->_props);
   }
 
+  public function toArray() {
+    return $this->_props;
+  }
+
   public function __construct($name, $props) {
     $this->_name = $name;
     foreach ($props as $k => $v) {
diff --git a/tests/phpunit/CRM/Stripe/MergeTest.php b/tests/phpunit/CRM/Stripe/MergeTest.php
index 460f2d95..3d5f0703 100644
--- a/tests/phpunit/CRM/Stripe/MergeTest.php
+++ b/tests/phpunit/CRM/Stripe/MergeTest.php
@@ -21,11 +21,12 @@ use Civi\Test\CiviEnvBuilder;
  * @group headless
  */
 require_once('BaseTest.php');
+require_once('ApiTest.php');
 class CRM_Stripe_MergeTest extends CRM_Stripe_ApiTest {
 
   /**
    * Test contact merging
-   * 
+   *
    * So far, only looks at the Civi side of things
    */
   public function testMerge():void {
@@ -83,6 +84,7 @@ class CRM_Stripe_MergeTest extends CRM_Stripe_ApiTest {
       ->first();
     $this->assertEquals($res['is_deleted'], 1);
 
+
     // update saved contact_id
     $this->contactID = $new_id;
 
-- 
GitLab