Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Stripe
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Extensions
Stripe
Commits
c667eacc
Commit
c667eacc
authored
2 years ago
by
mattwire
Browse files
Options
Downloads
Patches
Plain Diff
Extract getStripeCustomer into own function
parent
587e7407
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!217
Implement Stripe Checkout (with support for SEPA and ACH)
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CRM/Core/Payment/Stripe.php
+54
-36
54 additions, 36 deletions
CRM/Core/Payment/Stripe.php
CRM/Core/Payment/StripeCheckout.php
+0
-0
0 additions, 0 deletions
CRM/Core/Payment/StripeCheckout.php
with
54 additions
and
36 deletions
CRM/Core/Payment/Stripe.php
+
54
−
36
View file @
c667eacc
...
...
@@ -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
*
...
...
This diff is collapsed.
Click to expand it.
CRM/Core/Payment/StripeCheckout.php
0 → 100644
+
0
−
0
View file @
c667eacc
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment