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
fda8a1e2
Commit
fda8a1e2
authored
2 years ago
by
mattwire
Browse files
Options
Downloads
Patches
Plain Diff
Add tests for charge.succeeded and charge.captured
parent
346f1d4a
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
CRM/Core/Payment/StripeIPN.php
+2
-1
2 additions, 1 deletion
CRM/Core/Payment/StripeIPN.php
tests/phpunit/CRM/Stripe/BaseTest.php
+10
-1
10 additions, 1 deletion
tests/phpunit/CRM/Stripe/BaseTest.php
tests/phpunit/CRM/Stripe/IpnTest.php
+205
-0
205 additions, 0 deletions
tests/phpunit/CRM/Stripe/IpnTest.php
with
217 additions
and
2 deletions
CRM/Core/Payment/StripeIPN.php
+
2
−
1
View file @
fda8a1e2
...
@@ -554,6 +554,7 @@ class CRM_Core_Payment_StripeIPN {
...
@@ -554,6 +554,7 @@ class CRM_Core_Payment_StripeIPN {
if
(
empty
(
CRM_Stripe_Api
::
getObjectParam
(
'customer_id'
,
$this
->
getData
()
->
object
)))
{
if
(
empty
(
CRM_Stripe_Api
::
getObjectParam
(
'customer_id'
,
$this
->
getData
()
->
object
)))
{
return
TRUE
;
return
TRUE
;
}
}
// Deliberately missing break here because we process charge.succeeded per charge.captured
// Deliberately missing break here because we process charge.succeeded per charge.captured
case
'charge.captured'
:
case
'charge.captured'
:
// For a single contribution we have to use charge.captured because it has the customer_id.
// For a single contribution we have to use charge.captured because it has the customer_id.
...
@@ -569,7 +570,7 @@ class CRM_Core_Payment_StripeIPN {
...
@@ -569,7 +570,7 @@ class CRM_Core_Payment_StripeIPN {
// We only process charge.captured for one-off contributions
// We only process charge.captured for one-off contributions
if
(
empty
(
CRM_Stripe_Api
::
getObjectParam
(
'captured'
,
$this
->
getData
()
->
object
)))
{
if
(
empty
(
CRM_Stripe_Api
::
getObjectParam
(
'captured'
,
$this
->
getData
()
->
object
)))
{
return
TRUE
;
return
TRUE
;
}
;
}
// If contribution is in Pending or Failed state record payment and transition to Completed
// If contribution is in Pending or Failed state record payment and transition to Completed
if
(
in_array
(
$this
->
contribution
[
'contribution_status_id'
],
$statusesAllowedToComplete
))
{
if
(
in_array
(
$this
->
contribution
[
'contribution_status_id'
],
$statusesAllowedToComplete
))
{
...
...
This diff is collapsed.
Click to expand it.
tests/phpunit/CRM/Stripe/BaseTest.php
+
10
−
1
View file @
fda8a1e2
...
@@ -149,7 +149,7 @@ abstract class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implement
...
@@ -149,7 +149,7 @@ abstract class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implement
'payment_method_id'
=>
$paymentMethod
->
id
,
'payment_method_id'
=>
$paymentMethod
->
id
,
'amount'
=>
$this
->
total
,
'amount'
=>
$this
->
total
,
'payment_processor_id'
=>
$this
->
paymentProcessorID
,
'payment_processor_id'
=>
$this
->
paymentProcessorID
,
'payment_intent_id'
=>
NULL
,
'payment_intent_id'
=>
$params
[
'paymentIntentID'
]
??
NULL
,
'description'
=>
NULL
,
'description'
=>
NULL
,
];
];
$result
=
civicrm_api3
(
'StripePaymentintent'
,
'process'
,
$paymentIntentParams
);
$result
=
civicrm_api3
(
'StripePaymentintent'
,
'process'
,
$paymentIntentParams
);
...
@@ -178,6 +178,15 @@ abstract class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implement
...
@@ -178,6 +178,15 @@ abstract class CRM_Stripe_BaseTest extends \PHPUnit\Framework\TestCase implement
$ret
=
$this
->
paymentObject
->
doPayment
(
$params
);
$ret
=
$this
->
paymentObject
->
doPayment
(
$params
);
/*if ($ret['payment_status'] === 'Completed') {
civicrm_api3('Payment', 'create', [
'trxn_id' => $ret['trxn_id'],
'total_amount' => $params['amount'],
'fee_amount' => $ret['fee_amount'],
'order_reference' => $ret['order_reference'],
'contribution_id' => $params['contributionID'],
]);
}*/
if
(
array_key_exists
(
'trxn_id'
,
$ret
))
{
if
(
array_key_exists
(
'trxn_id'
,
$ret
))
{
$this
->
trxn_id
=
$ret
[
'trxn_id'
];
$this
->
trxn_id
=
$ret
[
'trxn_id'
];
$contribution
=
new
CRM_Contribute_BAO_Contribution
();
$contribution
=
new
CRM_Contribute_BAO_Contribution
();
...
...
This diff is collapsed.
Click to expand it.
tests/phpunit/CRM/Stripe/IpnTest.php
+
205
−
0
View file @
fda8a1e2
...
@@ -45,6 +45,74 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
...
@@ -45,6 +45,74 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
'installments'
=>
5
,
'installments'
=>
5
,
];
];
/**
* Test creating a one-off contribution and
* update it after creation.
*/
public
function
testNewOneOffChargeSucceeded
()
{
$this
->
mockOneOffPaymentSetup
();
$this
->
simulateEvent
([
'type'
=>
'charge.succeeded'
,
'id'
=>
'evt_mock'
,
'object'
=>
'event'
,
// ?
'livemode'
=>
FALSE
,
'pending_webhooks'
=>
0
,
'request'
=>
[
'id'
=>
NULL
],
'data'
=>
[
'object'
=>
[
'id'
=>
'ch_mock'
,
'object'
=>
'charge'
,
'customer'
=>
'cus_mock'
,
'charge'
=>
'ch_mock'
,
'created'
=>
time
(),
'amount'
=>
$this
->
total
*
100
,
'status'
=>
'succeeded'
,
"captured"
=>
FALSE
,
]
],
]);
// Ensure Contribution status is updated to complete and that we now have both invoice ID and charge ID as the transaction ID.
$this
->
checkContrib
([
'contribution_status_id'
=>
'Pending'
,
'trxn_id'
=>
'ch_mock'
,
]);
}
/**
* Test creating a one-off contribution and
* update it after creation.
*/
public
function
testNewOneOffChargeCaptured
()
{
$this
->
mockOneOffPaymentSetup
();
$this
->
simulateEvent
([
'type'
=>
'charge.captured'
,
'id'
=>
'evt_mock'
,
'object'
=>
'event'
,
// ?
'livemode'
=>
FALSE
,
'pending_webhooks'
=>
0
,
'request'
=>
[
'id'
=>
NULL
],
'data'
=>
[
'object'
=>
[
'id'
=>
'ch_mock'
,
'object'
=>
'charge'
,
'customer'
=>
'cus_mock'
,
'charge'
=>
'ch_mock'
,
'created'
=>
time
(),
'amount'
=>
$this
->
total
*
100
,
'status'
=>
'succeeded'
,
"captured"
=>
TRUE
,
]
],
]);
// Ensure Contribution status is updated to complete and that we now have both invoice ID and charge ID as the transaction ID.
$this
->
checkContrib
([
'contribution_status_id'
=>
'Completed'
,
'trxn_id'
=>
'ch_mock'
,
]);
}
/**
/**
* Test creating a recurring contribution and
* Test creating a recurring contribution and
* update it after creation. @todo The membership should also be updated.
* update it after creation. @todo The membership should also be updated.
...
@@ -79,6 +147,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
...
@@ -79,6 +147,7 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
]);
]);
$this
->
checkContribRecur
([
'contribution_status_id'
=>
'In Progress'
]);
$this
->
checkContribRecur
([
'contribution_status_id'
=>
'In Progress'
]);
}
}
/**
/**
* Test creating a recurring contribution and
* Test creating a recurring contribution and
* the handling of charge.succeeded
* the handling of charge.succeeded
...
@@ -751,6 +820,142 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
...
@@ -751,6 +820,142 @@ class CRM_Stripe_IpnTest extends CRM_Stripe_BaseTest {
return
[
$mockCharge1
,
$mockCharge2
,
$mockInvoice2
,
$balanceTransaction2
];
return
[
$mockCharge1
,
$mockCharge2
,
$mockInvoice2
,
$balanceTransaction2
];
}
}
/**
* DRY code. Sets up the database as it would be after a recurring contrib
* has been set up with Stripe.
*
* Results in a pending ContributionRecur and a pending Contribution record.
*
* The following mock Stripe IDs strings are used:
*
* - pm_mock PaymentMethod
* - pi_mock PaymentIntent
* - cus_mock Customer
* - ch_mock Charge
* - txn_mock Balance transaction
* - sub_mock Subscription
*/
protected
function
mockOneOffPaymentSetup
()
{
PropertySpy
::
$buffer
=
'none'
;
// Set this to 'print' or 'log' maybe more helpful in debugging but for
// generally running tests 'exception' suits as we don't expect any output.
PropertySpy
::
$outputMode
=
'exception'
;
$this
->
assertInstanceOf
(
'CRM_Core_Payment_Stripe'
,
$this
->
paymentObject
);
// Create a mock stripe client.
$stripeClient
=
$this
->
createMock
(
'Stripe\\StripeClient'
);
// Update our CRM_Core_Payment_Stripe object and ensure any others
// instantiated separately will also use it.
$this
->
paymentObject
->
setMockStripeClient
(
$stripeClient
);
// Mock the payment methods service.
$mockPaymentMethod
=
$this
->
createMock
(
'Stripe\\PaymentMethod'
);
$mockPaymentMethod
->
method
(
'__get'
)
->
will
(
$this
->
returnValueMap
([
[
'id'
,
'pm_mock'
]
]));
$stripeClient
->
paymentMethods
=
$this
->
createMock
(
'Stripe\\Service\\PaymentMethodService'
);
$stripeClient
->
paymentMethods
->
method
(
'create'
)
->
willReturn
(
$mockPaymentMethod
);
$stripeClient
->
paymentMethods
->
method
(
'retrieve'
)
->
with
(
$this
->
equalTo
(
'pm_mock'
))
->
willReturn
(
$mockPaymentMethod
);
// Mock the Customers service
$stripeClient
->
customers
=
$this
->
createMock
(
'Stripe\\Service\\CustomerService'
);
$stripeClient
->
customers
->
method
(
'create'
)
->
willReturn
(
new
PropertySpy
(
'customers.create'
,
[
'id'
=>
'cus_mock'
])
);
$stripeClient
->
customers
->
method
(
'retrieve'
)
->
with
(
$this
->
equalTo
(
'cus_mock'
))
->
willReturn
(
new
PropertySpy
(
'customers.retrieve'
,
[
'id'
=>
'cus_mock'
])
);
// Need a mock intent with id and status
$mockCharge
=
$this
->
createMock
(
'Stripe\\Charge'
);
$mockCharge
->
method
(
'__get'
)
->
will
(
$this
->
returnValueMap
([
[
'id'
,
'ch_mock'
],
[
'captured'
,
TRUE
],
[
'status'
,
'succeeded'
],
[
'balance_transaction'
,
'txn_mock'
],
]));
$mockChargesCollection
=
new
\Stripe\Collection
();
$mockChargesCollection
->
data
=
[
$mockCharge
];
$mockCharge
=
new
PropertySpy
(
'Charge'
,
[
'id'
=>
'ch_mock'
,
'object'
=>
'charge'
,
'captured'
=>
TRUE
,
'status'
=>
'succeeded'
,
'balance_transaction'
=>
'txn_mock'
,
'amount'
=>
$this
->
total
*
100
,
]);
$stripeClient
->
charges
=
$this
->
createMock
(
'Stripe\\Service\\ChargeService'
);
$stripeClient
->
charges
->
method
(
'retrieve'
)
->
with
(
$this
->
equalTo
(
'ch_mock'
))
->
willReturn
(
$mockCharge
);
$mockPaymentIntent
=
$this
->
createMock
(
'Stripe\\PaymentIntent'
);
$mockPaymentIntent
->
method
(
'__get'
)
->
will
(
$this
->
returnValueMap
([
[
'id'
,
'pi_mock'
],
[
'status'
,
'succeeded'
],
[
'charges'
,
$mockChargesCollection
]
]));
$stripeClient
->
paymentIntents
=
$this
->
createMock
(
'Stripe\\Service\\PaymentIntentService'
);
$stripeClient
->
paymentIntents
->
method
(
'retrieve'
)
->
with
(
$this
->
equalTo
(
'pi_mock'
))
->
willReturn
(
$mockPaymentIntent
);
$stripeClient
->
paymentIntents
->
method
(
'update'
)
->
willReturn
(
$mockPaymentIntent
);
$stripeClient
->
balanceTransactions
=
$this
->
createMock
(
'Stripe\\Service\\BalanceTransactionService'
);
$stripeClient
->
balanceTransactions
->
method
(
'retrieve'
)
->
with
(
$this
->
equalTo
(
'txn_mock'
))
->
willReturn
(
new
PropertySpy
(
'balanceTransaction'
,
[
'id'
=>
'txn_mock'
,
'fee'
=>
1190
,
/* means $11.90 */
'currency'
=>
'usd'
,
'exchange_rate'
=>
NULL
,
'object'
=>
'balance_transaction'
,
]));
$this
->
setupTransaction
();
// Submit the payment.
$payment_extra_params
=
[
'contributionID'
=>
$this
->
contributionID
,
'paymentIntentID'
=>
'pi_mock'
,
];
$this
->
doPayment
(
$payment_extra_params
);
//
// Check the Contribution
// ...should be pending
// ...its transaction ID should be our Charge ID.
//
$this
->
checkContrib
([
'contribution_status_id'
=>
'Pending'
,
'trxn_id'
=>
'ch_mock'
,
]);
}
/**
/**
* DRY code. Sets up the database as it would be after a recurring contrib
* DRY code. Sets up the database as it would be after a recurring contrib
* has been set up with Stripe.
* has been set up with Stripe.
...
...
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