Updating Subscription status inside the database by using Stripe Webhooks

I am integrating the Stripe payment gateway with a subscription-based product website in ASP.NET Core 3.1.

I have a Subscription table in the SQL Server database. It has expiration_date (datetime) and is_active (bit) columns.

When the user subscribes to a product with a recurring price (per month), I add a current date + 1 month in the expiration_date column. I also insert 1 in is_active column to indicate that the subscription is active.

Once the first month of subscription has passed, I want the value of is_active flag of this subscription to become 0 in the database. Once the user has paid for the next month, the Current Period will update in stripe subscription and the value of is_active flag is to become 1 again.

For this purpose, I should use Stripe WebHooks. I want to get notified through webhook that a subscription period has ended. I am confused because, as per my understanding, webhook events for customer.subscription and price do not define something that gets triggered when the current period of subscription ends.

The image attached below indicates the current period and date of the next invoice. From documentation I have come to know that upcoming invoice events can be used. But I am not understanding how a webhook can be used with the upcoming invoice.

Updating Subscription status inside the database by using Stripe Webhooks

So, my questions are that which event fires at the end of the final day of one month of the subscription period and which one is fired when a new period is defined?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

See https://stripe.com/docs/billing/invoices/subscription#subscription-renewal and https://stripe.com/docs/billing/subscriptions/webhooks#tracking.

When a subscription period ends, an invoice is created(invoice.created) to charge for the upcoming one. So when the cycle ends, you get a customer.subscription.updated event (because current_period_start and current_period_end are updated to the new period) and also an invoice.created event for the invoice that gets created to charge for this new period.

The way I would do it is

You could also listen to customer.subscription.updated and check if the reason for it updating is the billing period changing(by looking for current_period_start in the previous_attributes of the Event ); or do the same as above but via invoice.paid instead of invoice.created if you only care to update your system when a successful payment is made(thats what the https://stripe.com/docs/billing/subscriptions/webhooks#tracking approach does)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x