Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails, Stripe and Subscriptions - Model

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?

like image 576
Steve Wright Avatar asked Apr 17 '12 03:04

Steve Wright


2 Answers

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.

like image 78
eagspoo Avatar answered Sep 28 '22 06:09

eagspoo


"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.

like image 31
johnmcaliley Avatar answered Sep 28 '22 07:09

johnmcaliley