#-vineet_best-practices
1 messages · Page 1 of 1 (latest)
👋 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/1418501271911923833
📝 Have more to share? Add more details, code, screenshots, videos, etc. below.
hello! Is your main confusion here about the $0 invoice when creating a subscription with a metered price?
yes
i m confused if this is even the right way of doing this ?
If a customer purchases credits in multiple transactions (e.g. $10, $2, and $8 for a total of $20 in credit), and I’m using Stripe metered billing to deduct usage (by reporting usage events that get invoiced at the end of the cycle):
How can I retrieve the real-time remaining balance of credits after usage?
For example, if $13 worth of usage has already been reported, how do I get the updated available balance ($7) before the invoice is finalized?
I noticed that the balance_transaction API only reflects adjustments for a single credit grant, and it doesn’t update in real time with metered usage.
Is there any Stripe API that can give me the live available balance of prepaid credits, or do I need to maintain my own credits ledger outside of Stripe to log real time updates for user's dashboard ?
for context, a metered price only charges at the end of the month. When you first create a subscription, there wouldn't be anything to charge for at first since there's no usage reported.
If you don't want to receive a $0 invoice upon subscription creation, you can create a subscription with billing_mode: "flexible". It will not create a $0 invoice on subscription creation : https://docs.stripe.com/billing/subscriptions/billing-mode#suppress-zero-amount-line-items-when-adding-usage-based-items
give me a while to go through your other questions about credits
thank you so much, oki clear with this
hmmm, there is a credit balance summary which may be what you're looking for? https://docs.stripe.com/billing/subscriptions/usage-based/billing-credits#credit-balance-summaries-and-transactions
I can get the all credit grant summed up and returned like this for sure, but my concern is lets say right now i logged usage of $3, so if i query again my available balance should be $35-$3 = $32
but its not the case
for your context i has three credits grant on this customer
- $20
- $10
- $5
-
$35
i m concerned about real time usage, for that do i need to actually mantain an amount in my database now or not ?
as i dont see a way to get the real time value deduction from stripe tbh
glad you are helping on this, huge props to stripe team and you ❤️
It looks like there’s a bit of confusion between usage and credit grants. The credit balance only updates to reflect what you’ve already used from the credit grants and/or if you created new credit grants ; you haven't actually applied or utilized the credit grants yet since you're just reporting the usage.
Are you looking to check if you still have credit grants available to apply to an invoice?
yeah, so if the user comes and pays as per my pay as you go model, is this method correct ?
use payment intent -> upon successfull payment -> add credit grants -> use meter to sum per token usage -> billing cycle of weekly -> keeps on deducting
correct me please if this is the right approach to building pre-paid or pay-as-you go
hi! I'm taking over this thread.
hi there, nice to meet you
use payment intent -> upon successfull payment -> add credit grants -> use meter to sum per token usage -> billing cycle of weekly -> keeps on deducting
yes that looks correct to me. do you run into specific issues with this flow?
.
.
because as soon as user hits $0 on the credits i need to block his usage
.
.
please check these messages once @languid blaze, thank you !
because as soon as user hits $0 on the credits i need to block his usage
oh I see, looking into this.
you could use alerts for this: https://docs.stripe.com/billing/subscriptions/usage-based/monitor
thank you so much, gratefull for your help
you setup a threesold, and when its reached, you will receive an alert. and then its up to you to block that user.
everything is explained in details in the link I shared above.
so as soon as they are lets say below the threshold means, if they are less than $5 <- threshold the invoice will be generated and automatically and billing cycle can start from fresh again
in your specific case you could only use alerts, and not threshold to avoid this.
Okay got it. so that is how i can solve negative balance issue
and now for the real time balance left, do i need to use my db record for this ?
because i have to show the available balance - dollar value in the user's dashboard
and now for the real time balance left, do i need to use my db record for this ?
can't you use this as suggested earlier? https://docs.stripe.com/billing/subscriptions/usage-based/billing-credits#credit-balance-summaries-and-transactions
but this doesn't gets me the real time balance available, how can i get the real time balance even before the invoice was billed ?
lets say user used $3 right now, is there a way to get the left balance which is the result of deduction of $3 from the total credit grant ?
I think that's exactly what the link I pointed to does.
Correct me if i m wrong, but i dont get the realtime deducted / available credit grant left in the customer's account
i have used up 30.05$ already here, but still $35 is shown as available balance why ?
maybe I misunderstood something, sorry. let me run some tests to double check on my end.
so my available on this customer should be $4.98 approx
but i get $35 why ? i m sure i maybe doing something wrong here as i dont get the most real time available credit grant ( the invoice hasn't actually billed )
i have used up 30.05$ already here, but still $35 is shown as available balance why ?
I see the same thing on my end. sorry for the missunderstanding.
Please dont be sorry, its goood ... appreciate you helping on this
so looks like you need to use both the Balance Summary and the Meter Summary. then you subtract one by the other to get the available balance.
Oki got it, then there is no direct way to get this am i right ?
and for meters i have two meters, input and output token
so i to sum both of them up and then substract ?
Sounds good!
one more question
as the invoice is going to be generated at the end of billing cycle and amount that is gonna come up is most likely will be $0.00 in the invoice history as the credit grants will be applied again the subtotal.
wont that be misleading to the user, unless the person opens up the invoice
I'm not sure what you mean
at the end of billing cycle, the credit grants will be applied and the total will $0, which in the user's billing session page will come up as $0.00 because there was no left total
am i correct ?
Potentially? I'm not sure what the question is though. You can test the scenario you describe with a test clock though
If there's nothing due on the invoice, be that because of no reported usage or credits etc, then yes the invoice will be a zero-amount invoice. They're not sent to the user though
in this case there is usage right, the total was $30.02
but after credit grant was applied it was $0.00 ( amount remaining or to be paid )
Right, but I'm not sure what your question is. You're just describing what is laid out in the screenshot
so if the user's visists their payment history / billing session page
the amount that will come up here will be $0
am i correct ?
okay, but is this the actual expected way with usage based credits grants
i will check with test clock and come here again then
Is what the expected way?
to see $0.00 as amount in the invoice history
my end goal is to keep the user aware of all transactions done on my platform
Yes a $0 invoice should be listed on the portal. They're just not emailed to the customer AFAIK
if i create an invoice when paymentIntent succeeds, to just let the user be aware of the amount that they paid
wont this lead to duplicate invoices ?
as for the same amount then, my billing cycle will create an invoice
Why would you want to do that?
lets say i add $10
so now if i visit my invoice history will the $10 show up ?
but this was a pre paid thing
If you create a separate invoice for $10 for the same customer then it'd show in the portal, yes
thats why i m asking, i may not sound very correct to be honest, but please help as i want to clear this off to ( i m confused )
correct
But I don't understand why you'd want/need to do that?
so user's know that their payment was recorded and they can look at their transaction history ?
Sorry, you've completely lost me. What are you charging them $10 for?
You have a usage based model, right? So why do you need to add a random $10 invoice?
i have a pay-as-you go model
similar to how openai or openrouter has
to come, add amount and let that get used up
^ here
OK, then yes any invoice regardless of amount will show in the portal invoice history
so if the customer pays $10
using payment intent
i should create an invoice for that using invoice_creation[enabled] = true right ?
No, there needs to be an invoice – not just a Payment Intent
Well yes but that's a Checkout feature – not a Payment Intent feature
yeah my apologies
that only
so i should create an invoice when they checkout just for them to be aware
and when billing cycle ends
As I said, should be easy enough for you to test all this
it will generate another invoice
yeah, i just want to know my way is the preffered way or not
Not for mode: 'payment' no (which is where invoice_creation is supported)
In you pay-as-you-go model there's no recurring element, right? So why would there be a billing cycle?
i m an inference provider
depending on amount of input and output tokens
i m charging them which is deducted against credit grant
^ here is my roadmap of how it works
Right, but you just said you want to create a Payment Intent with an invoice – those are ad-hoc/one-time and don't automatically recur
yes
as i have pay as you go
but when the person pays or creates payment intent, upon succesfull i add a credit grant
There is no billing cycle for a Payment Intent/mode: 'payment' Checkout Session. That's why I was confused
I've read it, yeah
- Customer creation: Each project gets its own Stripe customer
- Payment collection: Using PaymentIntent/Checkout with mode: "payment"
- Credit management: Creating credit grants when payments are received
- Usage tracking: Recording meter events for input tokens ($3/100) and output tokens ($10/100)
- Subscription framework: Creating a subscription with both metered prices
- Daily billing cycle: Generating invoices daily to apply against credit grants.
this is the correct roadmap for achieving what open router has correct ?
basically pay as you kinda model
I've no idea what OpenRouter is, sorry
pay as you
go
hah
haha
you come, you add whatever you wish as amount, u use them up, when it hits $0 not usable anymore
please confirm that if this is the right way to achieve pay-as-you-go
as soma said yes it is
OK, but the billing credits (this) doesn't work with non-recurring payments. You need a subscription
This is why I'm fundamentally confused
yeah i do add subscription as soon as user is created in stipe
But then why are you talking about mode:'payment' Checkout Sessions?
cause that is how i am accepting the one time payment which gets added in the credit grant
so it can used up later by the customer
I'd recommend just building your flow and actually testing it end-to-end with a test clock
It seems fine on paper, yes
I just used test clocks and use more than the credit grant i had, it ended up creating an invoice for 20$ extra dollars that is no due for the user
how can i stop this solely on the stripe side, so that it the usage more than what total credit grant has is never used
Hey! Taking over for my colleague. Let me catch up.
how can i stop this solely on the stripe side, so that it the usage more than what total credit grant has is never used
You mean not allowing the customer to use your service above the credit grant ?
yes
exactly
so lets say the user added up $10 of credit grant
and now he used $15 worth of it
how to safeguard this so it doesn't go negative balance ?
I see, I think you need to monitor the usage and create alert threshold in order to disable the service for the customer:
https://docs.stripe.com/billing/subscriptions/usage-based/monitor
curl https://api.stripe.com/v1/billing/alerts \
-u "sk_test_51N42V8Kt5CJOb7A6Lilm0w1q2zpH5Yb1HzTIuLiLa4TAmP611D0UEV2U9qJyww6KfGiAD0J9ItTDH5Mx56ZJs2ia00py7kGmNH:" \
-d title="API Request usage alert" \
-d alert_type=usage_threshold \
-d "usage_threshold[gte]"=10000 \
-d "usage_threshold[meter]"=mtr_12345 \
-d "usage_threshold[recurrence]"=one_time
the "usage_threshold[gte]"=10000 is the amout in cents right ?
so if i have credit grant of lets say 10$, 20$, 5$ = 35$ in total
how can i setup usage_threshold so as soon as 35$ worth of usage is done, this gets fired ?
yes
does stripe expects me track the total balance added after every payment and keep on creating alerts everytime ?
Yeah I think so, let me double check if there is a native feature for this or not...
tbh i dont think is optimal solution
as alerts only works with meter
and i want to make sure i dont log up usage more than credit grants
Yeah, I just did a check and there is no native feature for this. The only way to achieve this is to calculate the total credit grant a customer has and then create/update the alert in order to get notified and stop the service
that means then i need to look at the balance available etc
and when it is past that just setup a alert
Yeah you need to update the alert threshold according to the balance available
the thing is stripe doesn't gives the balance available in real time
u have get the meter event, monitor every single usage and top up and then calculate which can have a lot of edge cases
Sorry, I mean the balance of credit grant available
You need to set a usage threshold according to the sum of all credit grants that a given customer has
Then you create/update the threshold/alert:
https://docs.stripe.com/billing/subscriptions/usage-based/monitor
there is no accumulated credit grant balance available that stripe gives
if you have 10$, 20$ and 5$ as credit grants
then your balance is 35$
but if you just logged the usage using meter events, then there's a deduction from the 35$
You can return/list all customer's credit grant using this API:
https://docs.stripe.com/api/billing/credit-grant/list#list_billing_credit_grants-customer
then query every single credit grant
No the deduction will happen only when the invoice is generated
Once you receive the alert (webhook event) you stop proving the service to your Customer.