4
1 Comment

Can someone share their Stripe metered-billing business logic?

I'm trying to use Stripe metered billing (usage based billing) to my application. Using this guide and linked repo I'm able to get 90% of the way there.

Where I'm stuck is how to report the usage to Stripe. The code says:

// This code can be run on an interval (e.g., every 24 hours) for each active
// metered subscription.
// You need to write some of your own business logic before creating the
// usage record. Pull a record of a customer from your database
// and extract the customer's Stripe Subscription Item ID and
// usage for the day. If you aren't storing subscription item IDs,
// you can retrieve the subscription and check for subscription items
// The usage number you've been keeping track of in your own database for the last 24 hours.

So you're supposed to track your users usage in your own database then report the usage daily to Stripe. So my questions are:

  1. Do you just use a CRON/scheduler to run every 24 hours, grab all your users, and report the any usage event within the last 24 hour period?

  2. How accurate could that be, for instance if the CRON job fires even a few seconds late and grabs the last 24 hour window it could miss some events than fell within those few seconds outside the start of the window. Not that missing a few events is the biggest deal but seems like a poor choice.

  3. If you only report usage every 24 hours, what happens if a billing cycle ends in the middle of that day. You wouldn't have reported that last days usage so the customer wouldn't be charged for them. Do you just roll that usage over to the next month and start them out with some already used? What if that missed usage amount would have made the difference between going up a tier or not?

  4. Say I had 100,000 users when the CRON job runs and I try to report all their usage to Stripe. I'd quickly go over the 100/sec API limit. How do you avoid this, using something like node-rate-limiter to throttle your calls? That'd still take 1000 seconds to finish running. (not that I have 100k users but I want my solution to be scalable).

As you can see I have a lot of concerns with this, if anyone could share their code or business logic it would help a lot!

posted to Icon for group Developers
Developers
on August 31, 2021
  1. 1

    Stripe's documentation used to be straightforward. Now it's confusing as hell. This is way more complicated than it needs to be. When the user checks out, you use a SetupIntent to store the user's info for future billing. Then once a month you run a cron job to create PaymentIntents for all monthly usage. Read this: https://stripe.com/docs/payments/save-and-reuse?platform=web

    Feel free to add a delay between batches of customers to avoid API limits.

Trending on Indie Hackers
I shipped 3 features this weekend based entirely on community feedback. Here's what I built and why. User Avatar 155 comments I'm a lawyer who launched an AI contract tool on Product Hunt today — here's what building it as a non-technical founder actually felt like User Avatar 139 comments “This contract looked normal - but could cost millions” User Avatar 53 comments 👉 The most expensive contract mistakes don’t feel risky User Avatar 39 comments I realized showing problems isn’t enough — so I built this User Avatar 32 comments The indie maker's dilemma: 2 months in, 700 downloads, and I'm stuck User Avatar 30 comments