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

Extract getStripeCustomer into own function

parent 587e7407
Branches
Tags
1 merge request!217Implement Stripe Checkout (with support for SEPA and ACH)
......@@ -598,48 +598,16 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
// @fixme DO NOT SET ANYTHING ON $propertyBag or $params BELOW THIS LINE (we are reading from both)
$params = $this->getPropertyBagAsArray($propertyBag);
// @fixme: Check if we still need to call the getBillingEmail function - eg. how does it handle "email-Primary".
$email = $this->getBillingEmail($params, $propertyBag->getContactID());
$propertyBag->setEmail($email);
$stripeCustomer = $this->getStripeCustomer($propertyBag);
// See if we already have a stripe customer
$customerParams = [
'contact_id' => $propertyBag->getContactID(),
'processor_id' => $this->_paymentProcessor['id'],
'email' => $email,
// Include this to allow redirect within session on payment failure
'error_url' => $propertyBag->getCustomProperty('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);
}
else {
// Customer was found in civicrm database, fetch from Stripe.
try {
$stripeCustomer = $this->stripeClient->customers->retrieve($stripeCustomerId);
} catch (Exception $e) {
$err = self::parseStripeException('retrieve_customer', $e);
throw new PaymentProcessorException('Failed to retrieve Stripe Customer: ' . $err['code']);
}
if ($stripeCustomer->isDeleted()) {
// Customer doesn't exist, create a new one
CRM_Stripe_Customer::delete($customerParams);
try {
$stripeCustomer = CRM_Stripe_Customer::create($customerParams, $this);
} catch (Exception $e) {
// We still failed to create a customer
$err = self::parseStripeException('create_customer', $e);
throw new PaymentProcessorException('Failed to create Stripe Customer: ' . $err['code']);
}
}
}
// Attach the paymentMethod to the customer and set as default for new invoices
if (isset($paymentMethodID)) {
$paymentMethod = $this->stripeClient->paymentMethods->retrieve($paymentMethodID);
......@@ -719,6 +687,56 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
return $this->endDoPayment($params);
}
/**
* @param \Civi\Payment\PropertyBag $propertyBag
*
* @return \Stripe\Customer|PropertySpy
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
protected function getStripeCustomer(\Civi\Payment\PropertyBag $propertyBag) {
// See if we already have a stripe customer
$customerParams = [
'contact_id' => $propertyBag->getContactID(),
'processor_id' => $this->getPaymentProcessor()['id'],
'email' => $propertyBag->getEmail(),
// Include this to allow redirect within session on payment failure
'error_url' => $propertyBag->getCustomProperty('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);
}
else {
// Customer was found in civicrm database, fetch from Stripe.
try {
$stripeCustomer = $this->stripeClient->customers->retrieve($stripeCustomerID);
} catch (Exception $e) {
$err = self::parseStripeException('retrieve_customer', $e);
throw new PaymentProcessorException('Failed to retrieve Stripe Customer: ' . $err['code']);
}
if ($stripeCustomer->isDeleted()) {
// Customer doesn't exist, create a new one
CRM_Stripe_Customer::delete($customerParams);
try {
$stripeCustomer = CRM_Stripe_Customer::create($customerParams, $this);
} catch (Exception $e) {
// We still failed to create a customer
$err = self::parseStripeException('create_customer', $e);
throw new PaymentProcessorException('Failed to create Stripe Customer: ' . $err['code']);
}
}
}
return $stripeCustomer;
}
/**
* @param \Civi\Payment\PropertyBag $params
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment