Skip to content

Instantly share code, notes, and snippets.

@k2052
Last active September 23, 2016 17:57
Show Gist options
  • Save k2052/9e0e8b0fe5d70a4501d653759fae3878 to your computer and use it in GitHub Desktop.
Save k2052/9e0e8b0fe5d70a4501d653759fae3878 to your computer and use it in GitHub Desktop.

First step, choose the authority. Decide if you are syncing from your app to stripe or from stripe to your app. Usually in most cases it works best to choose your own DB as the authority, then you update Stripe based on what is in your DB.

Store the plans in the DB, update those plans in response to events or hooks. The simplest hooks are just your auth flow, user registration, user subscribes etc. If you can, just manually add a call to stripe at every point where a plan changes. That will scale just fine for 90% of cases. But if you have a large app with a lot of contributors you will need to figure out a place/mental model where all the sync to Stripe code goes and how it works. Usually the best for it is some sort of events or job queue.

I found the best model for when you have to sync updates to another place is events. It scales a lot better. Otherwise eventually you end with all this spaghetti code and cron scripts keeping things in sync. You end up with all sorts of questions eventually like, if I change this code will that break Plans on the billing provider. Or you make a change to the flow on your site and forget to add code that updates plans. Easy to forget and write a codebase where things fall out of sync.

Use a DB with pub/sub and then listen for those events in a job worker/queue. Then when you need to sync data over to stripe you can just add a new event. For example:

db.subscribe('user:login', => {
  // update user details at stripe
})

db.subscribe('user:updatedAccount', => {
  // update user details at stripe
})

db.subscribe('plan:changed', => {
  // change plans at stripe
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment