From 788c831b3e245af2cf698db9f0ab1f452dee5723 Mon Sep 17 00:00:00 2001 From: Aidan Saunders <aidan.saunders@squiffle.uk> Date: Thu, 25 Aug 2022 15:39:06 +0100 Subject: [PATCH] Update StripeCustomer.updatestripemetadata to use API4 and consistent metadata --- CRM/Stripe/Customer.php | 6 ++--- CRM/Stripe/Upgrader.php | 3 ++- api/v3/StripeCustomer.php | 49 ++++++++++++++++++++------------------- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/CRM/Stripe/Customer.php b/CRM/Stripe/Customer.php index aa21e470..806d1b39 100644 --- a/CRM/Stripe/Customer.php +++ b/CRM/Stripe/Customer.php @@ -164,7 +164,7 @@ class CRM_Stripe_Customer { * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception */ - private static function getStripeCustomerMetadata($params) { + public static function getStripeCustomerMetadata($params) { $contactDisplayName = Contact::get(FALSE) ->addSelect('display_name') ->addWhere('id', '=', $params['contact_id']) @@ -181,7 +181,7 @@ class CRM_Stripe_Customer { 'email' => $params['email'] ?? '', 'metadata' => [ 'CiviCRM Contact ID' => $params['contact_id'], - 'CiviCRM URL' => CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$params['contact_id']}", TRUE, NULL, TRUE, FALSE, TRUE), + 'CiviCRM URL' => CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$params['contact_id']}", TRUE, NULL, FALSE, FALSE, TRUE), 'CiviCRM Version' => CRM_Utils_System::version() . ' ' . $extVersion, ], ]; @@ -239,7 +239,7 @@ class CRM_Stripe_Customer { // Could be multiple customer_id's and/or stripe processors foreach ($customers as $customer) { - $stripe = new CRM_Core_Payment_Stripe(NULL, $customer['processor_id']); + $stripe = \Civi\Payment\System::singleton()->getById($customer['processor_id']); CRM_Stripe_Customer::updateMetadata( ['contact_id' => $contactId, 'processor_id' => $customer['processor_id']], $stripe, diff --git a/CRM/Stripe/Upgrader.php b/CRM/Stripe/Upgrader.php index 3120d457..d2f4178c 100644 --- a/CRM/Stripe/Upgrader.php +++ b/CRM/Stripe/Upgrader.php @@ -422,7 +422,8 @@ class CRM_Stripe_Upgrader extends CRM_Stripe_Upgrader_Base { public function upgrade_6803() { $this->ctx->log->info('Applying Stripe update 5028. In civicrm_stripe_customers database table, rename id to customer_id, add new id column'); if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_stripe_customers', 'customer_id')) { - CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_stripe_customers RENAME COLUMN id TO customer_id"); + // ALTER TABLE ... RENAME COLUMN only in MySQL8+ + CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_stripe_customers CHANGE COLUMN id customer_id varchar(255) COMMENT 'Stripe Customer ID'"); if (CRM_Core_BAO_SchemaHandler::checkIfIndexExists('civicrm_stripe_customers', 'id')) { CRM_Core_DAO::executeQuery("ALTER TABLE civicrm_stripe_customers RENAME INDEX id TO customer_id"); } diff --git a/api/v3/StripeCustomer.php b/api/v3/StripeCustomer.php index 18c8c71c..e1fe97ac 100644 --- a/api/v3/StripeCustomer.php +++ b/api/v3/StripeCustomer.php @@ -135,50 +135,51 @@ function civicrm_api3_stripe_customer_updatestripemetadata($params) { if (!isset($params['dryrun'])) { throw new CiviCRM_API3_Exception('Missing required parameter dryrun'); } - // Check params - if (empty($params['id'])) { + $customers = \Civi\Api4\StripeCustomer::get(); + if (isset($params['options']['limit'])) { + $customers = $customers->setLimit($params['options']['limit']); + } + if (isset($params['options']['offset'])) { + $customers = $customers->setOffset($params['options']['offset']); + } + + if ($params['customer_id']) { + $customers = $customers->addWhere('customer_id', '=', $params['customer_id']); + } + else { // We're doing an update on all stripe customers 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['processor_id'], $params['options']); - } - else { - $customerIds = [$params['id']]; + else { + $customers = $customers->addWhere('processor_id', '=', $params['processor_id']); + } } - foreach ($customerIds as $customerId) { - $customerParams = CRM_Stripe_Customer::getParamsForCustomerId($customerId); - if (empty($customerParams['contact_id'])) { - throw new CiviCRM_API3_Exception('Could not find contact ID for stripe customer: ' . $customerId); + + $customers = $customers->execute(); + + foreach ($customers as $customer) { + if (!$customer['contact_id']) { + throw new CiviCRM_API3_Exception('Could not find contact ID for stripe customer: ' . $customer['customer_id']); } /** @var \CRM_Core_Payment_Stripe $paymentProcessor */ - $paymentProcessor = \Civi\Payment\System::singleton()->getById($customerParams['processor_id']); + $paymentProcessor = \Civi\Payment\System::singleton()->getById($customer['processor_id']); // Get the stripe customer from stripe try { - $paymentProcessor->stripeClient->customers->retrieve($customerId); + $paymentProcessor->stripeClient->customers->retrieve($customer['customer_id']); } catch (Exception $e) { $err = CRM_Core_Payment_Stripe::parseStripeException('retrieve_customer', $e); throw new PaymentProcessorException('Failed to retrieve Stripe Customer: ' . $err['code']); } - // Get the contact display name - $contactDisplayName = civicrm_api3('Contact', 'getvalue', [ - 'return' => 'display_name', - 'id' => $customerParams['contact_id'], - ]); - - // Currently we set the description and metadata - $stripeCustomerParams = [ - 'description' => $contactDisplayName . ' (CiviCRM)', - 'metadata' => ['civicrm_contact_id' => $customerParams['contact_id']], - ]; + $stripeCustomerParams = CRM_Stripe_Customer::getStripeCustomerMetadata($customer); // Update the stripe customer object at stripe if (!$params['dryrun']) { - $paymentProcessor->stripeClient->customers->update($customerId, $stripeCustomerParams); + $paymentProcessor->stripeClient->customers->update($customer['customer_id'], $stripeCustomerParams); $results[] = $stripeCustomerParams; } else { -- GitLab