#-vineet_best-practices

1 messages · Page 1 of 1 (latest)

sinful daggerBOT
#

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

verbal mirage
#

hello! Is your main confusion here about the $0 invoice when creating a subscription with a metered price?

slow magnet
#

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 ?

verbal mirage
#

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

slow magnet
verbal mirage
slow magnet
#

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 ❤️

verbal mirage
#

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?

sinful daggerBOT
slow magnet
#

yeah, so if the user comes and pays as per my pay as you go model, is this method correct ?

#dev-help message

#

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

languid blaze
#

hi! I'm taking over this thread.

slow magnet
#

hi there, nice to meet you

languid blaze
#

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?

slow magnet
#

because as soon as user hits $0 on the credits i need to block his usage

slow magnet
#

please check these messages once @languid blaze, thank you !

languid blaze
#

because as soon as user hits $0 on the credits i need to block his usage
oh I see, looking into this.

slow magnet
#

thank you so much, gratefull for your help

languid blaze
#

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.

slow magnet
#

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

languid blaze
#

in your specific case you could only use alerts, and not threshold to avoid this.

slow magnet
#

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

languid blaze
slow magnet
#

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 ?

languid blaze
#

I think that's exactly what the link I pointed to does.

slow magnet
#

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 ?

languid blaze
#

maybe I misunderstood something, sorry. let me run some tests to double check on my end.

slow magnet
#

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 )

languid blaze
#

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.

slow magnet
languid blaze
#

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.

slow magnet
#

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 ?

sinful daggerBOT
sullen heath
slow magnet
#

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

sullen heath
#

I'm not sure what you mean

slow magnet
#

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 ?

sullen heath
#

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

slow magnet
#

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 )

sullen heath
#

Right, but I'm not sure what your question is. You're just describing what is laid out in the screenshot

slow magnet
#

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 ?

sullen heath
#

Yes

#

As I said, you can test it with a test clock

slow magnet
#

okay, but is this the actual expected way with usage based credits grants

#

i will check with test clock and come here again then

sullen heath
#

Is what the expected way?

slow magnet
#

my end goal is to keep the user aware of all transactions done on my platform

sullen heath
#

Yes a $0 invoice should be listed on the portal. They're just not emailed to the customer AFAIK

slow magnet
#

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

sullen heath
#

Why would you want to do that?

slow magnet
#

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

sullen heath
#

If you create a separate invoice for $10 for the same customer then it'd show in the portal, yes

slow magnet
#

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 )

sullen heath
#

But I don't understand why you'd want/need to do that?

slow magnet
#

so user's know that their payment was recorded and they can look at their transaction history ?

sullen heath
#

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?

slow magnet
#

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

sullen heath
#

OK, then yes any invoice regardless of amount will show in the portal invoice history

slow magnet
#

so if the customer pays $10

#

using payment intent

#

i should create an invoice for that using invoice_creation[enabled] = true right ?

sullen heath
sullen heath
slow magnet
#

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

sullen heath
#

As I said, should be easy enough for you to test all this

slow magnet
#

it will generate another invoice

slow magnet
sullen heath
#

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?

slow magnet
#

i m an inference provider

#

depending on amount of input and output tokens

#

i m charging them which is deducted against credit grant

sullen heath
#

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

slow magnet
#

yes

#

as i have pay as you go

#

but when the person pays or creates payment intent, upon succesfull i add a credit grant

sullen heath
#

OK?

#

Overall I don't know what you're asking me

sullen heath
slow magnet
#

can you check this once

sullen heath
#

I've read it, yeah

slow magnet
#
- 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

sullen heath
#

I've no idea what OpenRouter is, sorry

slow magnet
#

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

slow magnet
sullen heath
#

OK, but the billing credits (this) doesn't work with non-recurring payments. You need a subscription

#

This is why I'm fundamentally confused

slow magnet
sullen heath
#

But then why are you talking about mode:'payment' Checkout Sessions?

slow magnet
#

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

sullen heath
#

I'd recommend just building your flow and actually testing it end-to-end with a test clock

#

It seems fine on paper, yes

sinful daggerBOT
slow magnet
#

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

dusk coral
#

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 ?

slow magnet
#

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 ?

dusk coral
slow magnet
#
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 ?

slow magnet
#

does stripe expects me track the total balance added after every payment and keep on creating alerts everytime ?

dusk coral
#

Yeah I think so, let me double check if there is a native feature for this or not...

slow magnet
#

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

dusk coral
#

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

slow magnet
#

that means then i need to look at the balance available etc

#

and when it is past that just setup a alert

dusk coral
#

Yeah you need to update the alert threshold according to the balance available

slow magnet
#

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

dusk coral
#

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

slow magnet
#

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$

dusk coral
slow magnet
#

then query every single credit grant

dusk coral
slow magnet
#

exactly

#

but i need to stop before that

#

so i dont go negative

dusk coral
#

Once you receive the alert (webhook event) you stop proving the service to your Customer.