#jacob_best-practices

1 messages ¡ Page 1 of 1 (latest)

gray prismBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1326290304290259046

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

whole patio
#

Hello
The answer to the first question is mostly on you. You can choose the formula based on how you want the behavior to be - https://docs.stripe.com/billing/subscriptions/usage-based/recording-usage#configure-meter

Sum: Bill customers based on the sum of all usage values for the billing period.
Count: Bill customers based on the count of all usage for the billing period.

For the second question, based on - https://docs.stripe.com/billing/subscriptions/usage-based/configure-grace-period#add-usage-to-draft-invoices
You can add the usage to a draft invoice too but make sure the usage timepstamp is prior to draft invoice creation .

buoyant swan
#

I'm not sure this fully clarifies what I'm asking.

For the first question:
What is the best practice for a storage in gigabytes meter? Say I am uploading files, using 80gb, then delete 20gb of files. How would I make the proper calls in a Sum approach? How would I make the proper calls in a Count approach?

#

--
For the second question:
Instead of adding usage to the draft, could I just trigger a meter update/(sync) once the draft invoice is created? (Setting the transition period to something reasonable for my system).

And then once it transitions to a finalized invoice, the meter will be in sync since I ran my internal check / update?

whole patio
#

For 1/
Have you tried using both aggregate functions in in test mode yet? It'd be super quick to test via dashboard.

2/ What do you mean by meter sync exactly?

buoyant swan
#

Yes, I tried both.

My initial impression was that Count accepted any value and that became the updated metered amount. But some testing shows that it simply iterates the count by 1, on each call.

#

Proper was the wrong choice of words

#

I was more asking, which makes more sense for the scenario of uploading 80gb, then deleting 20gb.

It seems that calling a count meter only iterates the meter by 1 value. While sum accepts any value.

So I could do a meter of +80, then a meter of -20.

#

But wanted to check in case I was missing something. Because it would be nice DX if I could simply send a total value for all metering.

As in. A meter call of 80, then a meter call of 60

whole patio
#

count aggregate function is pretty much "hey I did something so add +1 to my usage" - for example: you subscribe to a service that charges per API call. So a record in that case would be 1 API request.

In your use case sum seems to be the right choice. However, I don't know if you can pass negative usage to the meter.

Let me check something

#

Checking with a colleague if this is a known limitation of Meters API. I know with legacy usage records, we did allow set action which would just use the quantity you provide in the usage record - https://docs.stripe.com/api/usage_records/create

buoyant swan
#

I did a quick test and it seemed the meter system did accept a negative integer. To reduce usage. Unless you know of another way to manage the meter api.

I don't think I can use usage records for billing right? Isn't that a deprecated system?

whole patio
#

HUH interesting. Based on my understanding we only allowed whole numbers as payload. Can you share an example request ID where you passed a negative value?
https://support.stripe.com/questions/finding-the-id-for-an-api-request

I don't think I can use usage records for billing right? Isn't that a deprecated system?
It is legacy, yes. That's how stripe handled usage-based billing prior to Meters API

buoyant swan
#

Yes! Let me mock one up right now

whole patio
#

the request ID should start with req_xx

buoyant swan
#

req_9GBNLwdjQv1kOQ

whole patio
#

Thanks. looking into it..

#

It looks like we do support passing negative usage value after all. The docs I was referring to are just wrong.

But yeah basically, when your customer deletes a file - you can report that as negative usage to keep the meter upto date

buoyant swan
#

Awesome!

#

Yeah some of the docs were a bit confusing, so I just started testing calls in the shell

#

Ok then I guess Sum with +/- values will handle that

#

on to: 2

#

2/ What do you mean by meter sync exactly?

  1. I track the storage usage internally (source of truth). Then call a meter update to Stripe system. These values needs to be in sync between the Stripe billing system and my internal storage tracking.

So one ideas was occasionally (preferably right before in invoice finalization) call a function on my server that gets the latest meter from stripe and checks if it === the internal storage usage value.

If it doesn't, send a new meter event to correct the difference.

This way the source of truth stays as my system, but I keep the two in sync

whole patio
#

Makes sense

gray prismBOT
buoyant swan
#

Is there a webhook event for invoice drafted? Or reasonable way to trigger this sync function I will build?

whole patio
#

invoice.created

buoyant swan
#

Excellent! I think this answers all my questions for now.

Shame there isn't a SET function for the new metering api, but the diff math should be easy enough