Skip to content
Snippets Groups Projects
Commit cf9e7619 authored by mattwire's avatar mattwire
Browse files

Add StripeCustomer.updatestripemetadata API

parent 7cb6528d
Branches
Tags
No related merge requests found
......@@ -32,6 +32,52 @@ class CRM_Stripe_Customer {
WHERE contact_id = %1 AND is_live = %2 AND processor_id = %3", $queryParams);
}
/**
* Find the details (contact_id, is_live, processor_id) for an existing Stripe customer in the CiviCRM database
*
* @param string $stripeCustomerId
*
* @return array|null
*/
public static function getParamsForCustomerId($stripeCustomerId) {
$queryParams = [
1 => [$stripeCustomerId, 'String'],
];
$dao = CRM_Core_DAO::executeQuery("SELECT contact_id, is_live, processor_id
FROM civicrm_stripe_customers
WHERE id = %1", $queryParams);
$dao->fetch();
return [
'contact_id' => $dao->contact_id,
'is_live' => $dao->is_live,
'processor_id' => $dao->processor_id,
];
}
/**
* Find the details (contact_id, is_live, processor_id) for an existing Stripe customer in the CiviCRM database
*
* @param string $stripeCustomerId
*
* @return array|null
*/
public static function getAll($isLive, $processorId) {
$queryParams = [
1 => [$isLive ? 1 : 0, 'Boolean'],
2 => [$processorId, 'Integer'],
];
$customerIds = [];
$dao = CRM_Core_DAO::executeQuery("SELECT id
FROM civicrm_stripe_customers
WHERE is_live = %1 AND processor_id = %2", $queryParams);
while ($dao->fetch()) {
$customerIds[] = $dao->id;
}
return $customerIds;
}
/**
* Add a new Stripe customer to the CiviCRM database
*
......
......@@ -5,6 +5,8 @@
*
*/
use CRM_Stripe_ExtensionUtil as E;
/**
* StripeCustomer.Get API specification
*
......@@ -153,3 +155,85 @@ function civicrm_api3_stripe_customer_updatecontactids($params) {
return civicrm_api3_create_success($counts);
}
function _civicrm_api3_stripe_customer_updatestripemetadata_spec(&$spec) {
$spec['id']['title'] = E::ts("Stripe Customer ID");
$spec['id']['description'] = E::ts('If set only this customer will be updated, otherwise we try and update ALL customers');
$spec['id']['type'] = CRM_Utils_Type::T_STRING;
$spec['id']['api.required'] = FALSE;
$spec['dryrun']['api.required'] = TRUE;
$spec['dryrun']['type'] = CRM_Utils_Type::T_BOOLEAN;
$spec['is_live']['api.required'] = FALSE;
$spec['is_live']['type'] = CRM_Utils_Type::T_BOOLEAN;
$spec['processor_id']['api.required'] = FALSE;
$spec['processor_id']['type'] = CRM_Utils_Type::T_INT;
}
/**
* This allows us to update the metadata held by stripe about our CiviCRM payments
* Older versions of stripe extension did not set anything useful in stripe except email
* Now we set a description including the name + metadata holding contact id.
*
* @param $params
*
* @return array
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
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'])) {
// We're doing an update on all stripe customers
if (!isset($params['is_live']) || !isset($params['processor_id'])) {
throw new CiviCRM_API3_Exception('Missing required parameters is_live and/or processor_id when using without a customer id');
}
$customerIds = CRM_Stripe_Customer::getAll($params['is_live'], $params['processor_id']);
}
else {
$customerIds = [$params['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);
}
$paymentProcessor = \Civi\Payment\System::singleton()
->getById($customerParams['processor_id']);
$paymentProcessor->setAPIParams();
// Get the stripe customer from stripe
try {
$stripeCustomer = \Stripe\Customer::retrieve($customerId);
} catch (Exception $e) {
$err = CRM_Core_Payment_Stripe::parseStripeException('retrieve_customer', $e, FALSE);
$errorMessage = CRM_Core_Payment_Stripe::handleErrorNotification($err, NULL);
throw new \Civi\Payment\Exception\PaymentProcessorException('Failed to retrieve Stripe Customer: ' . $errorMessage);
}
// 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']],
];
// Update the stripe customer object at stripe
if (!$params['dryrun']) {
\Stripe\Customer::update($customerId, $stripeCustomerParams);
$results[] = $stripeCustomerParams;
}
else {
$results[] = $stripeCustomerParams;
}
}
return civicrm_api3_create_success($results, $params);
}
......@@ -20,5 +20,6 @@ The api commands are:
### Additionally for upgrading:
* `StripeCustomer.updatecontactids` - Used to migrate civicrm_stripe_customer table to match on contact_id instead of email address.
* `StripeCustomer.updatestripemetadata` - Used to update stripe customers that were created using an older version of the extension (adds name to description and contact_id as a metadata field).
* `StripeSubscription.updatetransactionids` - Used to migrate civicrm_stripe_subscriptions to use recurring contributions directly.
* `StripeSubscription.copytrxnidtoprocessorid` - Used to copy trxn_id to processor_id in civicrm_contribution_recur table so we can use cancelSubscription. Hopefully this won't be needed in future versions of CiviCRM if we can pass more sensible values to the cancelSubscription function.
\ No newline at end of file
......@@ -12,7 +12,7 @@
<author>Matthew Wire (MJW Consulting)</author>
<email>mjw@mjwconsult.co.uk</email>
</maintainer>
<releaseDate>2019-03-14</releaseDate>
<releaseDate>2019-03-19</releaseDate>
<version>5.3.3.dev</version>
<develStage>beta</develStage>
<compatibility>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment