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
26256969
Commit
26256969
authored
2 years ago
by
mattwire
Browse files
Options
Downloads
Patches
Plain Diff
Fix charge.failed for StripeCheckout and add test
parent
78b4a5d1
No related branches found
No related tags found
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
Civi/Stripe/Webhook/Events.php
+4
-2
4 additions, 2 deletions
Civi/Stripe/Webhook/Events.php
tests/phpunit/CRM/Stripe/IpnTest.php
+85
-2
85 additions, 2 deletions
tests/phpunit/CRM/Stripe/IpnTest.php
with
89 additions
and
4 deletions
Civi/Stripe/Webhook/Events.php
+
4
−
2
View file @
26256969
...
...
@@ -481,10 +481,11 @@ class Events {
return
$return
;
}
$paymentIntentID
=
$this
->
getValueFromStripeObject
(
'payment_intent_id'
,
'String'
);
// Invoice ID is optional
$invoiceID
=
$this
->
getValueFromStripeObject
(
'invoice_id'
,
'String'
);
$contribution
=
$this
->
findContribution
(
$chargeID
,
$invoiceID
);
$contribution
=
$this
->
findContribution
(
$chargeID
,
$invoiceID
,
''
,
$paymentIntentID
);
if
(
empty
(
$contribution
))
{
$return
->
message
=
__FUNCTION__
.
' Contribution not found'
;
return
$return
;
...
...
@@ -492,10 +493,11 @@ class Events {
$failedContributionParams
=
[
'contribution_id'
=>
$contribution
[
'id'
],
'order_reference'
=>
$invoiceID
??
$chargeID
,
'cancel_date'
=>
$this
->
getValueFromStripeObject
(
'receive_date'
,
'String'
),
'cancel_reason'
=>
$this
->
getValueFromStripeObject
(
'failure_message'
,
'String'
),
];
// Fallback from invoiceID to chargeID. We can't use ?? because invoiceID might be empty string ie. '' and not NULL
$failedContributionParams
[
'order_reference'
]
=
empty
(
$invoiceID
)
?
$chargeID
:
$invoiceID
;
$this
->
updateContributionFailed
(
$failedContributionParams
);
$return
->
message
=
__FUNCTION__
.
' contributionID: '
.
$contribution
[
'id'
];
...
...
This diff is collapsed.
Click to expand it.
tests/phpunit/CRM/Stripe/IpnTest.php
+
85
−
2
View file @
26256969
...
...
@@ -373,6 +373,88 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
]);
}
/**
* Unlike charge succeeded, charge failed is processed.
*/
public
function
testNewOneOffStripeCheckoutChargeFailed
()
{
$this
->
setOrCreateStripeCheckoutPaymentProcessor
();
$this
->
getMocksForOneOffPayment
();
$contribution
=
$this
->
setupPendingContribution
([
'invoice_id'
=>
md5
(
uniqid
(
mt_rand
(),
TRUE
))]);
// Simulate payment
$this
->
assertInstanceOf
(
'CRM_Core_Payment_StripeCheckout'
,
$this
->
paymentObject
);
//
// Check the Contribution
// ...should be pending
// ...its transaction ID should be our Charge ID.
//
$this
->
checkContrib
([
'contribution_status_id'
=>
'Pending'
,
'trxn_id'
=>
''
,
'invoice_id'
=>
$contribution
[
'invoice_id'
]
]);
// Set the new contribution to have trxn_id=pi_mock
$success
=
$this
->
simulateEvent
([
'type'
=>
'checkout.session.completed'
,
'id'
=>
'evt_mock'
,
'object'
=>
'event'
,
// ?
'livemode'
=>
FALSE
,
'pending_webhooks'
=>
0
,
'request'
=>
[
'id'
=>
NULL
],
'data'
=>
[
'object'
=>
[
'id'
=>
'cs_mock'
,
'object'
=>
'checkout.session'
,
'customer'
=>
'cus_mock'
,
'payment_intent'
=>
'pi_mock'
,
'client_reference_id'
=>
$contribution
[
'invoice_id'
],
]
],
]);
$this
->
assertEquals
(
TRUE
,
$success
,
'IPN did not return OK'
);
$this
->
checkContrib
([
'contribution_status_id'
=>
'Pending'
,
'trxn_id'
=>
'pi_mock'
,
]);
$success
=
$this
->
simulateEvent
([
'type'
=>
'charge.failed'
,
'id'
=>
'evt_mock'
,
'object'
=>
'event'
,
'livemode'
=>
FALSE
,
'pending_webhooks'
=>
0
,
'request'
=>
[
'id'
=>
NULL
],
'data'
=>
[
'object'
=>
[
'id'
=>
'ch_mock'
,
'object'
=>
'charge'
,
'amount'
=>
$this
->
total
*
100
,
'amount_captured'
=>
$this
->
total
*
100
,
'captured'
=>
TRUE
,
'balance_transaction'
=>
'txn_mock'
,
'customer'
=>
'cus_mock'
,
'payment_intent'
=>
'pi_mock'
,
'created'
=>
time
(),
'failure_message'
=>
'Mocked failure'
,
]
],
]);
$this
->
assertEquals
(
TRUE
,
$success
,
'IPN did not return OK'
);
//
// Ensure Contribution is marked Failed, with the reason, and that the
// ContributionRecur is not changed from Pending.
//
$this
->
checkContrib
([
'contribution_status_id'
=>
'Failed'
,
'trxn_id'
=>
'ch_mock'
,
'cancel_reason'
=>
'Mocked failure'
,
]);
}
/**
* Test creating a recurring contribution and
* update it after creation. @todo The membership should also be updated.
...
...
@@ -487,11 +569,11 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
]);
$this
->
checkContribRecur
([
'contribution_status_id'
=>
'Pending'
]);
}
/**
* Unlike charge succeeded, charge failed is processed.
*/
public
function
testNewRecurringChargeFailed
()
{
$this
->
mockRecurringPaymentSetup
();
$success
=
$this
->
simulateEvent
([
...
...
@@ -527,8 +609,9 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
'trxn_id'
=>
'in_mock'
,
'cancel_reason'
=>
'Mocked failure'
,
]);
$this
->
checkContribRecur
([
'contribution_status_id'
=>
'Pending'
]);
$this
->
checkContribRecur
([
'contribution_status_id'
=>
'Pending'
]);
}
/**
*
* @see https://stripe.com/docs/billing/invoices/overview#invoice-status-transition-endpoints-and-webhooks
...
...
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