Question

In my site, I have all the Stripe integration working, but I am still trying to wrap my head around how to determine if a subscription is active. I thought about keeping everything in the local DB, but then i'd have duplicate data from Stripe. However, if Stripe is the master record, what if they are down and I can't determine if a user has an active subscription? Seems to me like there should be a way to sync everything together. Should I keep some sort of date of current subscription expiration with the account/user?

What subscription information should be stored with the model? Should this be a part of the User model or as part of a separate "Subscription" model?

Was it helpful?

Solution

Just store a flag indicating whether subscription is active or not which is set to true by default when you subscribe.

Then set up stripe callback hooks to notify your app if the subscription is expired/cancelled/whatever. Listen for customer.subscription.updated and look at the status field which can either by active, past_due, cancelled or unpaid.

You will be notified immediately by stripe so there is no need incur the overhead of checking constantly. If stripe for some reason cannot make the callback, it will retry several times with exponential backoff so it is very robust.

Don't implement subscriptions yourself and don't run a job periodically to sync with stripe. With stripes callback hooks both strategies are totally unnecessary.

OTHER TIPS

"What subscription information should be stored with the model? Should this be a part of the User model or as part of a separate "Subscription" model?"

Store all the info in the Subscription model and associate that model to the User. Since subscriptions have their own meta data and they are really a different entity, it makes sense to store them separately.

"However, if Stripe is the master record, what if they are down and I can't determine if a user has an active subscription? Seems to me like there should be a way to sync everything together. Should I keep some sort of date of current subscription expiration with the account/user?"

If you are checking the subscription on every request or login, it probably does not make sense to hit the stripe api every time... that is alot of overhead. It all really depends on your use case, but it may make sense to run a daily (or hourly) cron job that hits the stripe api to check for subscription expiration and then update the local Subscription store.

The way I approach it with the SaaS Rails Kit is to keep the info in the Subscription model, including a next_renewal_at field, and not leaving it up to Stripe to do the billing. Instead, I have a daily cron job that bills all subscriptions that have a new_renewal_at = today, so you know immediately if a charge failed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top