Skip to content
Snippets Groups Projects
Commit 788c831b authored by ayduns's avatar ayduns Committed by mattwire
Browse files

Update StripeCustomer.updatestripemetadata to use API4 and consistent metadata

parent 9d1131cf
No related branches found
No related tags found
1 merge request!2096.8
......@@ -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,
......
......@@ -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");
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment