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
b06d7e90
Commit
b06d7e90
authored
5 years ago
by
mattwire
Browse files
Options
Downloads
Patches
Plain Diff
Description and Customer fields in Stripe backend - fixes
#78
parent
841e8d90
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
CRM/Core/Payment/Stripe.php
+21
-4
21 additions, 4 deletions
CRM/Core/Payment/Stripe.php
CRM/Stripe/Customer.php
+56
-11
56 additions, 11 deletions
CRM/Stripe/Customer.php
with
77 additions
and
15 deletions
CRM/Core/Payment/Stripe.php
+
21
−
4
View file @
b06d7e90
...
...
@@ -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
;
}
}
...
...
This diff is collapsed.
Click to expand it.
CRM/Stripe/Customer.php
+
56
−
11
View file @
b06d7e90
...
...
@@ -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
*
...
...
This diff is collapsed.
Click to expand it.
davej
@davej
mentioned in issue
#332 (closed)
·
3 years ago
mentioned in issue
#332 (closed)
mentioned in issue #332
Toggle commit list
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