#florian-ruen_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/1275356384162091031
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.
- florian-ruen_api, 5 days ago, 20 messages
Hello there,
The previous thread was closed, even if the question is related to it, I think not required to read all previous messages.
In the previous case, you suggest to not use subscription for this complex pricing model, but use only invoices when I want to bill a specific customer.
My question now is: where i can store the price of the product (default is 3% on transaction price with minimun of 0.25 / TiB), but sometime I want to apply better pricing for some customers (big clients, partners ...) ?
If I store in my database, the financial departements need to ask developers to change the price every time, so I prefer storing this in Stripe, but without using subscription I don't know what is the best practise for this (maybe using customer metadata)
Thanks for your help
Hmm didn't the Price API work for you? (Create Price API etc)
What do you means by Price API ?
The problem here, it's that I can't really set on a product the price "3% on transaction price, with a minimun of 0.25 $ /TiB"
And the second problem is how to define a "global" pricing, but something a pricing per customer
Stripe doesn't have anything built-in like this, but you can always calculate it yourself and set the amount to the resulting value.
What Stripe integration are you using?
Understood, how to create price.
Is it possible to define two price for a single "Price" for example normal price is 0.03 (3%) and minimun price is 0.25, so when I fetch the price, I can get both of them to create invoice ?
Another thing: I don't see how to link a Price to a customer (without a subscription), to apply custom price
I'm using Golang SDK for Stripe requests
3% on transaction price
What is the "transaction price" exactly?
I understand you're using Stripe Billing - Subscriptions, right?
No, because I can't implement this kind of pricing model with Subscription (can't record usage with float, only integer, and with a not very clear invoice with pre-computed price)
So in the previous thread, it was suggested to keep usage on my side, and generate a invoice directly using stripe without subscription
Yes, this is one way.
float, only integer
You can use smaller units, e.g. 1.01 unit is101, just like currency in Stripe, where $1.5 is150.
Yes, but my amount are more likely 0.0000589, and sometimes can be 1.5, depends on customer usage
Another workaround could be:
Keep a Subscription with a $0 Price, to automatically create Invoices on regular basis, and maybe even recording usage with it. Then, when you get a invoice.created webhook event (https://docs.stripe.com/api/events/types#event_types-invoice.created) you can add an InvoiceItem to it, with the custom amount that you calculate yourself: https://docs.stripe.com/api/invoiceitems/create#create_invoiceitem-amount
Then the amount is 589 or 15000000, same principle.
There must be some kind of minimal discrete unit in your system.
Yes, I can handle this way, but on the invoice, it's not very clear for the customer to understand the amount
He will see an amount, without the associated usage, so to recalculate, he will ask our support, and we can have a lot of requests for this
My idea is to create 30 line item on invoice (one usage recorded per day, invoiced each month), with as description a clear text like "You have received X transaction, for a total size of 350 GiB, for the 2024/08/20. Because the 3% on XX amount is under the minimun requirements, this second one apply"
So the customer can easily understand the total amount of the invoice, and to verify using the description data. do you see what i mean?
Maybe I can do with this:
- my customer click on a "Subscribe" button, I create a checkout session with setup mode (only to collect credit card and billing address) ;
- the backend will listen checkout.session.completed to link a Subscription to the customer with $0 price ;
- the backend record on my own database the usage (with a lot of details), each day at midnight;
- when my backend receive invoice.created, fetch associated usage from my database and add InvoiceItem for each record to the invoice ;
- remove usage from my own database for the next periode ;
Do you think this process can work ?
The problem here will be the same: if the subscription price is set to $0, how to handle a custom price for this customer ? and the default price ?
(i want to store prices in Stripe rather in my database, my database will only store usage recorded each day)
You can remove the dummy $0 InvoiceItem and create all the items you want. You will just use Stripe Subscriptions to schedule Invoices.
This will work.
You won't be able to use Stripe Prices, you will just need to calculate the total amount yourself, and create bespoke Invoice Items for each Invoice.
This means, I need to store the "unit price" on my own database ? For example if the normal is 3%, and for a specific customer, I want to apply 1.5%
where it's annoying is that the finance or sales department that wants to adapt the prices must necessarily go through a developer ...
Maybe if I can modify an invoice with the webhook, I can store the price in the subscription (and not set it to $0)
So I create 2 products:
- one called "Percentage price" of $0.03
- one called "Minimum price" of $0.25
By default, in the subscription after the checkout.session.completed I put these prices there
At the time of the webhook event invoice.created, I retrieve the prices associated with the subscription to calculate the invoice
And we will be able to apply price exceptions directly on Stripe dashboard to put 0.015 or 0.00 for each customer
Can this work?
At the time of the webhook event invoice.created, I retrieve the prices associated with the subscription to calculate the invoice
At this moment you can just delete one of the Invoice Items.
๐ taking over for my colleague. Let me know if there's any follow-up Qs I can answer!
@shrewd bay do you think the workflow can work, If i can delete the Invoice Item in invoice.created event, and add my items based on subscription prices ?
can you please summarize the ask?
I've a pricing model which is 3 % on transaction price, with a minimun of 0.25$ / TiB (each transaction have a price, and a size)
Because it's not really easy to use Billing meters (because I can record only integers not float, the invoice is not clear and easily understandable by the customer) I can't use this
In the first thread, a suggestion was to keep usage on my own, and create a invoice with Stripe each month (with custom line items, description ...)
Now, another suggestion is to keep subscription with $0 price only to schedule invoices, and listen to invoice.created to add/remove InvoiceItems
But my original question was: how to have a "default price" for all customers, and sometime attach a custom price for specific customer (partner with free price, or something like this)
Using subscription with $0 price not handle this.
So my suggestion/workaround with this workflow can I think handle this
Because it's not really easy to use Billing meters (because I can record only integers not float, the invoice is not clear and easily understandable by the customer) I can't use this
you can always make the meter count for the x10^n of the fraction
I think we have an issue with the price modeling that we need to fix before talking about exceptions
do you agree?
Yes I can use 10^n, but the problem with pre-computing the price, is that on the invoice, the amount will be very "strange" for the customer
If the customer want to re-calculate on his own to check the invoice, he can't do it easily. And I guess, it will contact our support to ask for details each time (with a lot of tickets maybe)
My idea is to create one LineItem per day (one usage record per day, billed each month)
With a very clear description like "2024/08/20: X transaction received (total size: 350 TiB, total price: $0). Price applied: 0.25 * 350 = 87.5 $"
Using this, it can easily understand the invoice without contact our support
Do you see what i means ?
Yes I do, so your problem is basically with the Invoice PDF rather than the subscription calculating your customer's invoice amount
correct?
Yes, the values recorded I don't really care, what I what is a comprehensible invoice for the customer (and a way to list recorded values on my app for the customer to track their usage day by day, which seems not possible using billing meter)
This is what billing meter, I think is not the best solution here.
(and a way to list recorded values on my app for the customer to track their usage day by day, which seems not possible using billing meter)
that's possible with Meter Event Summaries https://docs.stripe.com/api/billing/meter-event_summary/list
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
and you can choose whether you want to show them this aggregated by month or by day
This is what billing meter, I think is not the best solution here.
It is the best solution in your use case, you just need to use that data to either provide a detailed invoice PDF that you create on your own, or generate a link to your app where it explains the details based on the meter event summaries that I suggested earlier
Yes, but only got the pre-computed amount, not possible to add details on how the amount was calculated to add to the invoice. (I tried by adding keys to payload, works, but I can't retrived all of them, only value is present in response for list billing meter event summaries)
Recorded data
{
"identifier": "f6718c05-bfff-4977-bd0f-f1273d4cdafa",
"timestamp": 1723542623,
"event_name": "deals_sent_price_to_bill",
"payload": {
"1_total_deal_price_over_last_24h_in_attofil": "0",
"2_total_deal_size_over_last_24h_in_bytes": "1683627180032",
"3_total_deal_size_over_last_24h_in_tib": "0.15312",
"4_minimun_required_price_on_deal_size": "0.038281",
"5_three_percent_on_deals_price": "0",
"6_multiplied_by_100_and_ceil_minimun_price": "4",
"7_multiplied_by_100_and_rounded_three_percent_price": "0",
"client_address_id": "f02347452",
"stripe_customer_id": "cus_QbgF3mRoLcvdjA",
"value": "4"
}
}
Only the value or aggregated value is present in the response
So not very easy to generate a detailled invoice using this
I'm not sure I understand what you mean
When my app send a new billing meter record (for example a customer have sent 5 transactions, total price is 0, total size is 32 GiB = 0,03125 TiB, amount will be 0,03125 * 0.25 = 0.00078125 $)
I will record 78125 (x 100.000.000), so to bill the right price, the product price will be 0.0000001 / unit right ?
So on the invoice (if I record only this single value), the customer will be 78125 units at 0.0000001 / unit = 0.00078125 $
Not really easy to understand, that this amount means 5 transactions for a total of 32 GiB ...
And I can't, on my side create a detailled invoice PDF, using only the billing meter value of 78125 (I need to keep track on my own of how this amount was calculated)
My first idea was to store more info with the value (as you can see in JSON above), but only value is provided when I fetch billing meters events summaries
This is what billing meter is hard to use in that case, and I am trying to tind another solution
you shouldn't be storing the amount but rather the size
so basically instead of sending 0,03125 you can send 3125
and then the unit price changes to accommodate for this
the unit should be what you choose to count for your customer
Yes but I got 2 price here: a normal price, and a minimun price, and it can't works
either transaction numbers or transaction size
you can always have both
with 2 different billing events
Yes, let's say, I've:
- a biling meter called total_price to record the price (in my example 0)
- a billing meter called total_size to record the size (in my example 0.03125, will record 3125)
On a product, I can setup only one billing meter to use, which one ?
If I create two product (one for size, one for price), add both of them to the subscription, the final price will take into account two product (here it's 0 for total_price, but if it's 50, the total amount will be 50*0.03 + 3125 * 0.000001 = 1.53125, whereas, I want to take the max amount, not both of them!)
you want max(50 *0.03, 3125*0.000001)
Yes, this is how I apply 3% of price, or 0.25 / TiB as minimum
that's not really possible then! it's way to complex to be taken care of by our subscription system
This is what I say billing meters can't work in this case ๐
in that case the best thing is to go back to my colleague's suggestion about creating one invoices each time
Yes, this can works perfectly. But my question today is, how to store the price.
The normal price is 3% and 0.25 / TiB (for all customers), but sometimes (for partners, best client), I want to apply another price (for example 0% and 0.10 / TiB)
I don't want to store this in my database, because the business department will need a developper to change the price
My question was: where can i store this price on Stripe (linked on a customer) easily
It seems a solution can be:
- create two product: one called product_1 with price 0.03, and another called product_2 with price 0.25
- each time a customer subscribe, attach a subscription with those two products
- on webhook event invoice.created, use my own record data and subscription prices to create the invoice amount (here 0.03 and 0.25)
(for example here max(50 * 0.03, 0.03125 * 0.25))
but if the business department, want to change: go to Stripe dashboard, specific customer subscription, apply exceptions (for example 0% and 0.10 / TiB)
And my app, will see the new prices when invoice.created arrive with subscription specific prices (for example here max(50 * 0, 0.03125 * 0.1))
So the subscription is only used to schedule invoice correctly (based on when the customer subscribe etc...), the price is stored in Stripe
And i'm in charge of generating the invoice correctly
My question was: where can i store this price on Stripe (linked on a customer) easily
maybe asmetadataon a Customer object?
that's what I thought at first, but a client can have multiple tenants, and I would ideally like to be able to configure this for each of them.
If I want to do it like this, it requires me to store a JSON in metadata
yes we usually recommend not storing complex stuctures in metadata, it's better to store an ID that then lets you look up context in your own database.
The Customer model as it exists currently doesn't have good support for "this Customer is a business that has multiple users under it" yes, if that's what you're referring to. You'd probably want multiple Customer objects(but then the complication is they all have a separate payment methods and subscriptions).
This is pretty much what I have.
And the solution I referred to above, couldn't it work?
not sure(it's hard for me to follow), should be possible to try it in test mode and see what happens
will do this afternoon. but there is no way to keep this open, at least for today (with the same contacts), to avoid having to start all over again while finding a solution / testing ?
Because the case is quite complicated, and re-explaining it each time does not simplify it, on the contrary
no sorry, better to have a support ticket for long-running conversations, we do Discord in shifts and cross-region so can't really keep all the context
Understood, will try this this afternoon, let you known if I need more help
Just one more question: the webhook event invoice.created is fired before or after invoice creation ?
My question is, is it possible to add/remove InvoiceItem, only by using the invoice ID, and the invoice update api call at this time ?
depends if it's the first invoice of a subscription(in which case it's finalized immediately and can't be added to) or not(in which case you can add items when handling the invoice.created event, as mentioned on https://docs.stripe.com/billing/invoices/subscription#subscription-renewal )
So, If I'm using the checkout.session.completed to create the subscription, the first invoice will be $0, finalized immediatly, but no the other one
I'll try using this
Based on what I can read here: https://docs.stripe.com/api/invoiceitems
I see that invoice items can be added before the invoice is ready
It means, each day I can add an invoice item to an upcoming invoice right ? Maybe I can use this to replace billing meters in my case
Each day at midnight, at an invoice item with the usage for the last 24h and so one each day
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
(or I can listen invoice.created and add events at the same, both are the same I think)
you can add invoice items whenever, and they get 'sucked into' the next invoice created for the subscription; and you can also directly add items to an Invoice that is not yet finalized , which is the flow https://docs.stripe.com/billing/invoices/subscription#adding-draft-invoice-items
Is there a better approach ?
Between:
- add invoice items each day to an invoice
- add all at once at invoice.created
the options are these , I would say.
It seems using this https://docs.stripe.com/api/invoice-line_item/bulk-add
The amount field is an int64, how to bill 0.0034 $ for specific line for example ? I can't see a param to specify the decimal part
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
hi there!
you could use a Price object or directly price_data: https://docs.stripe.com/api/invoice-line_item/bulk-add#bulk_add_lines-lines-price_data-unit_amount_decimal
and the amount is not required, if I provided price_data right ?
the doc say price or price_data, but nothing about amount
and the amount is not required, if I provided price_data right ?
correct