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
11f2b902
Commit
11f2b902
authored
2 years ago
by
mattwire
Browse files
Options
Downloads
Patches
Plain Diff
Fix #397 Refactor parsing stripe exceptions
parent
694d2c93
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CRM/Core/Payment/Stripe.php
+40
-13
40 additions, 13 deletions
CRM/Core/Payment/Stripe.php
CRM/Stripe/Customer.php
+0
-7
0 additions, 7 deletions
CRM/Stripe/Customer.php
api/v3/StripeCustomer.php
+0
-1
0 additions, 1 deletion
api/v3/StripeCustomer.php
with
40 additions
and
21 deletions
CRM/Core/Payment/Stripe.php
+
40
−
13
View file @
11f2b902
...
...
@@ -262,20 +262,50 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
}
/**
* Stripe exceptions contain a json object in the body "error". This function extracts and returns that as an array.
* @param String $op
* @param Exception $e
* @param Boolean $log
* This function parses, sanitizes and extracts useful information from the exception that was thrown.
* The goal is that it only returns information that is safe to show to the end-user.
*
* @see https://stripe.com/docs/api/errors/handling?lang=php
*
* @param string $op
* @param \Exception $e
*
* @return array $err
*/
public
static
function
parseStripeException
(
$op
,
$e
,
$log
=
FALSE
)
{
$body
=
$e
->
getJsonBody
();
if
(
$log
)
{
Civi
::
log
()
->
error
(
"Stripe_Error
{
$op
}
: "
.
print_r
(
$body
,
TRUE
));
public
static
function
parseStripeException
(
string
$op
,
\Exception
$e
)
:
array
{
$genericError
=
[
'code'
=>
9000
,
'message'
=>
E
::
ts
(
'An error occurred'
)];
switch
(
get_class
(
$e
))
{
case
'Stripe\Exception\CardException'
:
// Since it's a decline, \Stripe\Exception\CardException will be caught
\Civi
::
log
(
'stripe'
)
->
error
(
$op
.
': '
.
get_class
(
$e
)
.
': '
.
$e
->
getMessage
()
.
print_r
(
$e
->
getJsonBody
(),
TRUE
));
$error
[
'code'
]
=
$e
->
getError
()
->
code
;
$error
[
'message'
]
=
$e
->
getError
()
->
message
;
return
$error
;
case
'Stripe\Exception\RateLimitException'
:
// Too many requests made to the API too quickly
case
'Stripe\Exception\InvalidRequestException'
:
// Invalid parameters were supplied to Stripe's API
case
'Stripe\Exception\AuthenticationException'
:
// Authentication with Stripe's API failed
// (maybe you changed API keys recently)
case
'Stripe\Exception\ApiConnectionException'
:
// Network communication with Stripe failed
\Civi
::
log
(
'stripe'
)
->
error
(
$op
.
': '
.
get_class
(
$e
)
.
': '
.
$e
->
getMessage
());
return
$genericError
;
case
'Stripe\Exception\ApiErrorException'
:
// Display a very generic error to the user, and maybe send yourself an email
// Get the error array. Creat a "fake" error code if error is not set.
// The calling code will parse this further.
\Civi
::
log
(
'stripe'
)
->
error
(
$op
.
': '
.
get_class
(
$e
)
.
': '
.
$e
->
getMessage
()
.
print_r
(
$e
->
getJsonBody
(),
TRUE
));
return
$e
->
getJsonBody
()[
'error'
]
??
$genericError
;
default
:
// Something else happened, completely unrelated to Stripe
return
$genericError
;
}
// Get the error array. Creat a "fake" error code if error is not set.
return
$body
[
'error'
]
??
[
'code'
=>
9000
];
}
/**
...
...
@@ -578,7 +608,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
$stripeCustomer
=
$this
->
stripeClient
->
customers
->
retrieve
(
$stripeCustomerId
);
}
catch
(
Exception
$e
)
{
$err
=
self
::
parseStripeException
(
'retrieve_customer'
,
$e
);
\Civi
::
log
(
'stripe'
)
->
error
(
'Failed to retrieve Stripe Customer: '
.
$err
[
'message'
]
.
'; '
.
print_r
(
$err
,
TRUE
));
throw
new
PaymentProcessorException
(
'Failed to retrieve Stripe Customer: '
.
$err
[
'code'
]);
}
...
...
@@ -590,7 +619,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
}
catch
(
Exception
$e
)
{
// We still failed to create a customer
$err
=
self
::
parseStripeException
(
'create_customer'
,
$e
);
\Civi
::
log
(
'stripe'
)
->
error
(
'Failed to create Stripe Customer: '
.
$err
[
'message'
]
.
'; '
.
print_r
(
$err
,
TRUE
));
throw
new
PaymentProcessorException
(
'Failed to create Stripe Customer: '
.
$err
[
'code'
]);
}
}
...
...
@@ -869,7 +897,6 @@ class CRM_Core_Payment_Stripe extends CRM_Core_Payment {
}
catch
(
Exception
$e
)
{
$err
=
self
::
parseStripeException
(
'retrieve_balance_transaction'
,
$e
);
\Civi
::
log
(
'stripe'
)
->
error
(
'Failed to retrieve Stripe Balance Transaction: '
.
$err
[
'message'
]
.
'; '
.
print_r
(
$err
,
TRUE
));
throw
new
PaymentProcessorException
(
'Failed to retrieve Stripe Balance Transaction: '
.
$err
[
'code'
]);
}
if
((
$stripeCharge
[
'currency'
]
!==
$stripeBalanceTransaction
->
currency
)
...
...
This diff is collapsed.
Click to expand it.
CRM/Stripe/Customer.php
+
0
−
7
View file @
11f2b902
...
...
@@ -147,7 +147,6 @@ class CRM_Stripe_Customer {
}
catch
(
Exception
$e
)
{
$err
=
CRM_Core_Payment_Stripe
::
parseStripeException
(
'create_customer'
,
$e
,
FALSE
);
\Civi
::
log
(
'stripe'
)
->
error
(
'Failed to create Stripe Customer: '
.
$err
[
'message'
]
.
'; '
.
print_r
(
$err
,
TRUE
));
throw
new
PaymentProcessorException
(
'Failed to create Stripe Customer: '
.
$err
[
'code'
]);
}
...
...
@@ -186,7 +185,6 @@ class CRM_Stripe_Customer {
}
catch
(
Exception
$e
)
{
$err
=
CRM_Core_Payment_Stripe
::
parseStripeException
(
'create_customer'
,
$e
);
\Civi
::
log
(
'stripe'
)
->
error
(
'Failed to update Stripe Customer: '
.
$err
[
'message'
]
.
'; '
.
print_r
(
$err
,
TRUE
));
throw
new
PaymentProcessorException
(
'Failed to update Stripe Customer: '
.
$err
[
'code'
]);
}
return
$stripeCustomer
;
...
...
@@ -207,11 +205,6 @@ class CRM_Stripe_Customer {
->
execute
()
->
first
()[
'display_name'
];
$domainName
=
\Civi\Api4\Domain
::
get
(
FALSE
)
->
setCurrentDomain
(
TRUE
)
->
addSelect
(
'name'
)
->
execute
()
->
first
()[
'name'
];
$extVersion
=
civicrm_api3
(
'Extension'
,
'getvalue'
,
[
'return'
=>
'version'
,
'full_name'
=>
E
::
LONG_NAME
]);
$stripeCustomerParams
=
[
...
...
This diff is collapsed.
Click to expand it.
api/v3/StripeCustomer.php
+
0
−
1
View file @
11f2b902
...
...
@@ -257,7 +257,6 @@ function civicrm_api3_stripe_customer_updatestripemetadata($params) {
$paymentProcessor
->
stripeClient
->
customers
->
retrieve
(
$customerId
);
}
catch
(
Exception
$e
)
{
$err
=
CRM_Core_Payment_Stripe
::
parseStripeException
(
'retrieve_customer'
,
$e
);
\Civi
::
log
(
'stripe'
)
->
error
(
'Failed to retrieve Stripe Customer: '
.
$err
[
'message'
]
.
'; '
.
print_r
(
$err
,
TRUE
));
throw
new
PaymentProcessorException
(
'Failed to retrieve Stripe Customer: '
.
$err
[
'code'
]);
}
...
...
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