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
df3827f8
Commit
df3827f8
authored
1 year ago
by
mattwire
Browse files
Options
Downloads
Patches
Plain Diff
Move customer.subscription IPN functions to events processor
parent
848172f0
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/StripeIPN.php
+10
-33
10 additions, 33 deletions
CRM/Core/Payment/StripeIPN.php
Civi/Stripe/Webhook/Events.php
+69
-5
69 additions, 5 deletions
Civi/Stripe/Webhook/Events.php
with
79 additions
and
38 deletions
CRM/Core/Payment/StripeIPN.php
+
10
−
33
View file @
df3827f8
...
...
@@ -434,8 +434,17 @@ class CRM_Core_Payment_StripeIPN {
$return
=
$webhookEventProcessor
->
doInvoicePaid
();
break
;
case
'customer.subscription.updated'
:
$return
=
$webhookEventProcessor
->
doCustomerSubscriptionUpdated
();
break
;
case
'customer.subscription.deleted'
:
$return
=
$webhookEventProcessor
->
doCustomerSubscriptionDeleted
();
break
;
default
:
$return
->
ok
=
$this
->
processEventType
();
$return
->
message
=
$this
->
eventType
.
' - not implemented'
;
$return
->
ok
=
TRUE
;
}
}
...
...
@@ -466,38 +475,6 @@ class CRM_Core_Payment_StripeIPN {
return
$return
;
}
/**
* Process the received event in CiviCRM
*
* @return bool
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
private
function
processEventType
()
{
// NOTE: If you add an event here make sure you add it to the webhook or it will never be received!
switch
(
$this
->
eventType
)
{
case
'customer.subscription.updated'
:
// Subscription is updated. This used to be "implemented" but didn't work
return
TRUE
;
case
'customer.subscription.deleted'
:
// Subscription is cancelled
if
(
!
$this
->
getSubscriptionDetails
())
{
// Subscription was not found in CiviCRM
CRM_Mjwshared_Hook
::
webhookEventNotMatched
(
'stripe'
,
$this
,
'subscription_not_found'
);
return
TRUE
;
}
// Cancel the recurring contribution
$this
->
updateRecurCancelled
([
'id'
=>
$this
->
contribution_recur_id
,
'cancel_date'
=>
$this
->
retrieve
(
'cancel_date'
,
'String'
,
FALSE
)]);
return
TRUE
;
}
// Unhandled event
return
TRUE
;
}
/**
* Create the next contribution for a recurring contribution
* This happens when Stripe generates a new invoice and notifies us (normally by invoice.finalized but
...
...
This diff is collapsed.
Click to expand it.
Civi/Stripe/Webhook/Events.php
+
69
−
5
View file @
df3827f8
...
...
@@ -456,7 +456,7 @@ class Events {
if
(
isset
(
$contribution
[
'payments'
]))
{
foreach
(
$contribution
[
'payments'
]
as
$payment
)
{
if
(
$payment
[
'trxn_id'
]
===
$refund
->
id
)
{
$return
->
message
=
'
Refund '
.
$refund
->
id
.
' already recorded in CiviCRM'
;
$return
->
message
=
__FUNCTION__
.
'
Refund '
.
$refund
->
id
.
' already recorded in CiviCRM'
;
$return
->
ok
=
TRUE
;
return
$return
;
}
...
...
@@ -665,7 +665,7 @@ class Events {
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public
function
doInvoicePaid
()
{
public
function
doInvoicePaid
()
:
\stdClass
{
$return
=
$this
->
getResultObject
();
// Check we have the right data object for this event
...
...
@@ -737,7 +737,7 @@ class Events {
])[
'count'
]
>
0
)
{
// Payment already recorded
$return
->
ok
=
TRUE
;
$return
->
message
=
E
::
ts
(
'Payment already recorded'
);
$return
->
message
=
__FUNCTION__
.
': '
.
E
::
ts
(
'Payment already recorded'
);
return
$return
;
}
...
...
@@ -776,7 +776,7 @@ class Events {
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
public
function
doInvoiceFinalized
()
{
public
function
doInvoiceFinalized
()
:
\stdClass
{
$return
=
$this
->
getResultObject
();
// Check we have the right data object for this event
...
...
@@ -836,7 +836,7 @@ class Events {
* @throws \Civi\Payment\Exception\PaymentProcessorException
* @throws \Stripe\Exception\ApiErrorException
*/
public
function
doInvoicePaymentFailed
()
{
public
function
doInvoicePaymentFailed
()
:
\stdClass
{
$return
=
$this
->
getResultObject
();
// Check we have the right data object for this event
...
...
@@ -889,4 +889,68 @@ class Events {
return
$return
;
}
/**
* Subscription is cancelled.
*
* @return \stdClass
* @throws \CiviCRM_API3_Exception
* @throws \Civi\Payment\Exception\PaymentProcessorException
*/
public
function
doCustomerSubscriptionDeleted
():
\stdClass
{
$return
=
$this
->
getResultObject
();
// Check we have the right data object for this event
if
((
$this
->
getData
()
->
object
[
'object'
]
??
''
)
!==
'subscription'
)
{
$return
->
message
=
__FUNCTION__
.
' Invalid object type'
;
return
$return
;
}
$subscriptionID
=
$this
->
getValueFromStripeObject
(
'subscription_id'
,
'String'
);
if
(
!
$subscriptionID
)
{
$return
->
message
=
__FUNCTION__
.
' Missing subscription_id'
;
return
$return
;
}
$contributionRecur
=
$this
->
getRecurFromSubscriptionID
(
$subscriptionID
);
if
(
empty
(
$contributionRecur
))
{
// Subscription was not found in CiviCRM
$result
=
[];
\CRM_Mjwshared_Hook
::
webhookEventNotMatched
(
'stripe'
,
$this
,
'subscription_not_found'
,
$result
);
if
(
empty
(
$result
[
'contributionRecur'
]))
{
$return
->
message
=
__FUNCTION__
.
': '
.
E
::
ts
(
'No contributionRecur record found in CiviCRM. Ignored.'
);
$return
->
ok
=
TRUE
;
return
$return
;
}
else
{
$contributionRecur
=
$result
[
'contributionRecur'
];
}
}
// Cancel the recurring contribution
$this
->
updateRecurCancelled
([
'id'
=>
$contributionRecur
[
'id'
],
'cancel_date'
=>
$this
->
getValueFromStripeObject
(
'cancel_date'
,
'String'
)]);
$return
->
message
=
__FUNCTION__
.
' contributionRecurID: '
.
$contributionRecur
[
'id'
]
.
' cancelled'
;
$return
->
ok
=
TRUE
;
return
$return
;
}
/**
* Subscription is updated. We don't currently do anything with this
*
* @return \stdClass
*/
public
function
doCustomerSubscriptionUpdated
():
\stdClass
{
$return
=
$this
->
getResultObject
();
// Check we have the right data object for this event
if
((
$this
->
getData
()
->
object
[
'object'
]
??
''
)
!==
'subscription'
)
{
$return
->
message
=
__FUNCTION__
.
' Invalid object type'
;
return
$return
;
}
$return
->
message
=
__FUNCTION__
.
' ignoring - not implemented'
;
$return
->
ok
=
TRUE
;
return
$return
;
}
}
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