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

Description and Customer fields in Stripe backend - fixes #78

parent 841e8d90
Branches
Tags
No related merge requests found
......@@ -409,8 +409,11 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
'stripe_error_url' => $params['stripe_error_url'],
];
// Get the Stripe Customer:
// 1. Look for an existing customer.
// 2. If no customer (or a deleted customer found), create a new one.
// 3. If existing customer found, update the metadata that Stripe holds for this customer.
$stripeCustomerId = CRM_Stripe_Customer::find($customerParams);
// Customer not in civicrm database. Create a new Customer in Stripe.
if (!isset($stripeCustomerId)) {
$stripeCustomer = CRM_Stripe_Customer::create($customerParams, $this);
......@@ -436,23 +439,37 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
throw new \Civi\Payment\Exception\PaymentProcessorException('Failed to create Stripe Customer: ' . $errorMessage);
}
}
else {
CRM_Stripe_Customer::updateMetadata($customerParams, $this, $stripeCustomer->id);
}
}
// Prepare the charge array, minus Customer/Card details.
if (empty($params['description'])) {
$params['description'] = E::ts('Backend Stripe contribution');
$params['description'] = E::ts('Contribution: %1', [1 => $this->getPaymentProcessorLabel()]);
}
$contactContribution = $this->getContactId($params) . '-' . ($this->getContributionId($params) ?: 'XX');
$intentParams = [
'customer' => $stripeCustomer->id,
'description' => "{$params['description']} {$contactContribution} #" . CRM_Utils_Array::value('invoiceID', $params),
'statement_descriptor_suffix' => "{$contactContribution} " . substr($params['description'],0,7),
];
$intentParams['statement_descriptor'] = substr("{$contactContribution} " . $params['description'], 0, 22);
$intentMetadata = [
'amount_to_capture' => $this->getAmount($params),
];
// This is where we actually charge the customer
try {
\Stripe\PaymentIntent::update($paymentIntentID, $intentParams);
$intent = \Stripe\PaymentIntent::retrieve($paymentIntentID);
$intent->description = $params['description'] . ' # Invoice ID: ' . CRM_Utils_Array::value('invoiceID', $params);
$intent->customer = $stripeCustomer->id;
switch ($intent->status) {
case 'requires_confirmation':
$intent->confirm();
case 'requires_capture':
$intent->capture(['amount_to_capture' => $this->getAmount($params)]);
$intent->capture($intentMetadata);
break;
}
}
......
......@@ -117,23 +117,13 @@ class CRM_Stripe_Customer {
*/
public static function create($params, $stripe) {
$requiredParams = ['contact_id', 'processor_id'];
// $optionalParams = ['email'];
foreach ($requiredParams as $required) {
if (empty($params[$required])) {
throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (create): Missing required parameter: ' . $required);
}
}
$contactDisplayName = civicrm_api3('Contact', 'getvalue', [
'return' => 'display_name',
'id' => $params['contact_id'],
]);
$stripeCustomerParams = [
'description' => $contactDisplayName . ' (CiviCRM)',
'email' => CRM_Utils_Array::value('email', $params),
'metadata' => ['civicrm_contact_id' => $params['contact_id']],
];
$stripeCustomerParams = self::getStripeCustomerMetadata($params);
try {
$stripeCustomer = \Stripe\Customer::create($stripeCustomerParams);
......@@ -155,6 +145,61 @@ class CRM_Stripe_Customer {
return $stripeCustomer;
}
/**
* @param array $params
* @param \CRM_Core_Payment_Stripe $stripe
* @param string $stripeCustomerID
*
* @return \Stripe\Customer
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public static function updateMetadata($params, $stripe, $stripeCustomerID) {
$requiredParams = ['contact_id', 'processor_id'];
foreach ($requiredParams as $required) {
if (empty($params[$required])) {
throw new \Civi\Payment\Exception\PaymentProcessorException('Stripe Customer (updateMetadata): Missing required parameter: ' . $required);
}
}
$stripeCustomerParams = self::getStripeCustomerMetadata($params);
try {
$stripeCustomer = \Stripe\Customer::update($stripeCustomerID, $stripeCustomerParams);
}
catch (Exception $e) {
$err = CRM_Core_Payment_Stripe::parseStripeException('create_customer', $e, FALSE);
$errorMessage = $stripe->handleErrorNotification($err, $params['stripe_error_url']);
throw new \Civi\Payment\Exception\PaymentProcessorException('Failed to update Stripe Customer: ' . $errorMessage);
}
return $stripeCustomer;
}
/**
* @param array $params
* Required: contact_id; Optional: email
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
private static function getStripeCustomerMetadata($params) {
$contactDisplayName = civicrm_api3('Contact', 'getvalue', [
'return' => 'display_name',
'id' => $params['contact_id'],
]);
$stripeCustomerParams = [
'name' => $contactDisplayName,
'description' => 'CiviCRM: ' . civicrm_api3('Domain', 'getvalue', ['current_domain' => 1, 'return' => 'name']),
'email' => CRM_Utils_Array::value('email', $params),
'metadata' => [
'CiviCRM Contact ID' => $params['contact_id'],
'CiviCRM URL' => CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$params['contact_id']}", TRUE),
],
];
return $stripeCustomerParams;
}
/**
* Delete a Stripe customer from the CiviCRM database
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment