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

We can derive contactID from stripe customer

parent 14d25e3b
No related branches found
No related tags found
No related merge requests found
......@@ -104,6 +104,24 @@ class Charge extends Base {
$this->setDescription('Stripe: Manual import via API');
}
if (empty($this->getContactID())) {
// Derive contact ID from stripe customer
if (empty(\CRM_Stripe_Api::getObjectParam('customer_id', $stripeCharge))) {
throw new \Exception('Missing customer_id in Stripe Charge. Cannot derive contact ID');
}
$stripeCustomer = \Civi\Api4\StripeCustomer::get(FALSE)
->addWhere('customer_id', '=', \CRM_Stripe_Api::getObjectParam('customer_id', $stripeCharge))
->addWhere('processor_id', '=', $this->getPaymentProcessorID())
->addClause('OR', ['currency', 'IS EMPTY'], ['currency', '=', \CRM_Stripe_Api::getObjectParam('customer_id', $stripeCharge)])
->addOrderBy('currency', 'DESC')
->execute()
->first();
if (empty($stripeCustomer)) {
throw new \Exception('Stripe Customer: ' . \CRM_Stripe_Api::getObjectParam('customer_id', $stripeCharge) . ' not found in CiviCRM. Cannot derive contact ID');
}
$this->setContactID($stripeCustomer['contact_id']);
}
// Check for a subscription.
if (isset($stripeInvoice)) {
$subscriptionID = \CRM_Stripe_Api::getObjectParam('subscription_id', $stripeInvoice);
......
......@@ -21,8 +21,9 @@ function _civicrm_api3_stripe_importcharge_spec(&$spec) {
$spec['ppid']['type'] = CRM_Utils_Type::T_INT;
$spec['ppid']['api.required'] = TRUE;
$spec['contact_id']['title'] = E::ts('Contact ID');
$spec['contact_id']['description'] = E::ts('If not specified this will be determined from the "customer" on the charge - a StripeCustomer record must exist in CiviCRM');
$spec['contact_id']['type'] = CRM_Utils_Type::T_INT;
$spec['contact_id']['api.required'] = TRUE;
$spec['contact_id']['api.required'] = FALSE;
$spec['charge']['title'] = E::ts('Stripe Charge ID');
$spec['charge']['description'] = E::ts('The Charge ID to import');
$spec['charge']['type'] = CRM_Utils_Type::T_STRING;
......
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