During the invoice.paid
event, maybe set the cancel_at_end_of_period
on the WCPay Subscription if this is the last payment
#2860
Labels
category: projects
For any issues which are part of any project, including bugs, enhancements, etc.
component: wcpay subscriptions
Issues related to Stripe Billing Subscriptions
focus: subscriptions
needs feedback
The issue/PR needs a response from any of the parties involved in the issue.
Description
At the moment, we handle cancelling the WCPay Subscription in two ways:
invoice.upcoming
webhook, if there's no next payment date we cancel/pause the subscription in Stripe as a last precautionThere are a few risks we're taking with this approach. Firstly, we're relying on the
invoice.upcoming
being handled successfully on every store, but that's not always the case (i.e. the store might be fataling at the time we need to process this incoming webhook). Stripe should retry these webhooks but the store could remain down for any amount of time.The second risk we're taking is we're relying on the Woo subscription to be set to cancelled/expired before Stripe processes the invoice payment which is not always the case.
The main thing we want to avoid here is customers being charged by Stripe when their subscription is cancelled in Woo.
One extra thing we could be doing is during when we process the
invoice.paid
event, after we have called$order->payment_complete()
check if there's a next payment date on the subscription.If there's no next payment we should be able to safely mark the WCPay subscription as
cancel_at_end_of_period
.This will mean no more payments will be taken and we reduce the risks from webhooks not being handled/delivered properly.
Example
I haven't reproduced this in my local environment but here's how I imagine an issue could happen:
invoice.upcoming
webhook event (we check if the subscription has a next payment date or isn't expiring)invoice.paid
event (we create the renewal in Woo)invoice.upcoming
invoice.paid
webhook.invoice.upcoming
. This is where our code notices the subscription in Woo doesn't have a next payment date and cancel the subscription in stripe before it processes paymentinvoice.paid
, if we didn't cancel the subscription in Stripe, another payment will be processed ❌Because the Subscription in Woo is set to expire/cancel, there is a chance that the subscription status in Woo gets updated to cancelled/expired before step 4 happens, which will cancel the subscription before stripe processes an incorrect payment. With that said, because Stripe controls the renewal cycle we can't rely on the Subscription in Woo always being cancelled before Stripe.
Therefore, one solution to this problem is to address it in step 3 ii., after calling
payment_completed()
, check if the subscription doesn't have a next payment date and then send a request to Stripe to set thecancel_at_end_of_period
param on the subscription.The text was updated successfully, but these errors were encountered: