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

Add a post merge hook to update the metadata at Stripe.

parent 1b193b7e
No related branches found
No related tags found
1 merge request!2096.8
......@@ -263,4 +263,26 @@ class CRM_Stripe_Customer {
CRM_Core_DAO::executeQuery($sql, $queryParams);
}
/**
* Update the metadata at Stripe for a given contactid
*
* @param int $contactId
* @return void
*/
public static function updateMetadataForContact(int $contactId): void {
$customers = \Civi\Api4\StripeCustomer::get()
->addWhere('contact_id', '=', $contactId)
->execute();
// Could be multiple customer_id's and/or stripe processors
foreach ($customers as $customer) {
$stripe = new CRM_Core_Payment_Stripe(null, $customer['processor_id']);
CRM_Stripe_Customer::updateMetadata(
['contact_id' => $contactId, 'processor_id' => $customer['processor_id']],
$stripe,
$customer['id']
);
}
}
}
......@@ -255,3 +255,17 @@ function stripe_civicrm_permission(&$permissions) {
$permissions['allow stripe moto payments'] = E::ts('CiviCRM Stripe: Process MOTO transactions');
}
}
/*
* Implements hook_civicrm_post().
*/
function stripe_civicrm_post($op, $objectName, $objectId, &$objectRef) {
try {
if ($objectName == 'Contact' && $op == 'merge') {
CRM_Stripe_Customer::updateMetadataForContact($objectId);
}
}
catch (Exception $e) {
\Civi::log(E::SHORT_NAME)->error('Stripe Contact Merge failed: ' . $e->getMessage());
}
}
......@@ -12,7 +12,7 @@
<title>Stripe Customer ID</title>
<type>varchar</type>
<length>255</length>
<comment>The Stripe Customer ID</comment>
<comment>Stripe Customer ID</comment>
</field>
<index>
<name>id</name>
......@@ -36,7 +36,7 @@
<name>processor_id</name>
<title>Payment Processor ID</title>
<type>int unsigned</type>
<comment>Foreign key to civicrm_payment_processor.id</comment>
<comment>ID from civicrm_payment_processor</comment>
<pseudoconstant>
<table>civicrm_payment_processor</table>
<keyColumn>id</keyColumn>
......
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