#atlasdb
1 messages · Page 1 of 1 (latest)
At the moment this code only updates the subscription, which is good, but it wont charge the customer until they update the subscription again or wait till the subscription cycle ends
Hi, you can learn more about it here: https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#immediate-payment. With metered billing, it will be charged after the usage is reported: https://stripe.com/docs/billing/subscriptions/upgrade-downgrade#metered-billing.
"With metered billing the customer is charged after the usage is reported"
I'm sorry but where on this page can I find what you just mentioned?
Sorry, wrong link. This is what I meant to send: https://stripe.com/docs/billing/subscriptions/usage-based#sample-integration. You'd need to record usage at the end of the month to ochange the customer.
I'm sorry but this documentation doesn't make sense with what you are telling me.
Based on your instructions, after running stripe.subscriptions.update I should report the usage to stripe to induce a charge.
I just implemented that to the process and and now I'm receiving this error when trying to report the usage after updating the subscription :
"Cannot create the usage record with this timestamp because timestamps must be before the subscription's current period end time."
You need to report the usage before the end of the current period. Can you outline the exact business flow you're looking for? I think there is a disconnect here and I'd like to understand fully the steps you're taking here.
Absolutely
I have the following Product with the Prices listed bellow
### Usage Based Plan
| Price | Usage is Metered | FIRST UNIT | LAST UNIT | FLAT FEE |
| ---------- | ---------------- | ---------- | --------- | -------- |
| Free | `true` | 0 | 10k | -- |
| Standard | `true` | 0 | 50k | XX.XX |
| Business | `true` | 0 | 100k | XX.XX |
| Enterprise | `true` | 0 | 500k | XX.XX |
First step: When a user registers to the site a customer is created on stripe and it is registered to a free monthly subscription.
stripe.subscriptions.create({
customer: customer_id,
items: [{ price: free_price_id }],
})```
Later a user tries to update from the **free** to the **standard** plan.
When a user updates their subscription, on my service the user will get access to the higher limitations immediately (assuming the customer successfully pays and it goes through).
So the intended behavior would be to clear the usage, bill the customer, and the updated plan starts on the same day the subscription is updated.
Let me know if I left any information out
you're offerring a free trial, what are you charging for here?
No free trial, just a free pricing tier.
I don't plan on charging the user for over usage on a free plan, it simply stops their service.
So no card is required
usage is reported all the same no matter the subscription price
But if they are on a free pricing, what amount are you charging them for the usage if it's free?
Right, so when you change the price there is nothing to charge. Then, after you change the price, you record the usage before the billing cycle ends on the new price. Then, you charge the customer.
That is how this works.
So in this scenario we just outline, the customer is always charged at the end of the cycle? There is no way of changing the price and receiving payment up front?
That is correct
My friend, I've been at this for three weeks now. Docs don't help, and every time I get on discord all I get are links back to the same docs or magic eight ball responses. Can you give me some sort of actual guidance as to how to acheive a metered bases monthly subscriptions? Is my product planning wrong? Ive re-written my codebase each time I get on a dev chat and each time i get further and further from getting anything done
Which part are you confused on?
Everything, I've been explaining what I want and what Ive tried and I keep getting brick walled
1/ You create a free price with metered billing and create a Sub.
2/ You update the Sub. with the new price
3/ At the end of the month, you record the usage and then charge the customer
I understand that.
What do I have to do so that flow looks like this:
1/ You create a free price with metered billing and create a Sub. (Jan 3rd)
2/ You update the Sub. with the new price (Jan 10th)
2a/ Bill the customer with new price ($20) of Plan (Jan 10th)
3/ Report usage, charging fee if customer goes over limit (Jan 10th - Feb 10th)
4/ Bill customer ($20) again (Feb 10th)
👋 stepping in as pgskc needs to step away
Hello Bismarck
To clarify the above, the Jan 10th new Price is also metered, correct?
Correct
### Usage Based Plan
| Price | Usage is Metered | FIRST UNIT | LAST UNIT | FLAT FEE |
| ---------- | ---------------- | ---------- | --------- | -------- |
| Free | `true` | 0 | 10k | -- |
| Standard | `true` | 0 | 50k | XX.XX |
| Business | `true` | 0 | 100k | XX.XX |
| Enterprise | `true` | 0 | 500k | XX.XX |
Gotcha, in that case you are going to need to create an Invoice Item and an Invoice on Jan 10th and associate it to the Subscription when you perform that update.
So after the subscription item is updated with the new price, I would have to manually call stripe.invoiceItems.create passing through the Customer ID, New Price ID, and Subscription ID?
For example
const invoiceItem = await stripe.invoiceItems.create({
customer: 'cus_NeZei8imSbMVvi',
price: 'price_1MtGUsLkdIwHu7ix1be5Ljaj',
subscription: 'sub_u7ix1be5Ljaj1MtGUsLkdIw'
});
You can actually do this right on the Sub update request: https://stripe.com/docs/api/subscriptions/update#update_subscription-add_invoice_items
Then you would create an Invoice and pass the Sub ID: https://stripe.com/docs/api/invoices/create#create_invoice-subscription
And finally you call the /pay endpoint to charge that Invoice: https://stripe.com/docs/api/invoices/pay
I see, this makes sense.
So it would look something like this
Update the subscription (Create Invoice)
const subscription_update = await stripe.subscriptions.update(current_subscription_id, {
billing_cycle_anchor: 'now',
items: [{
id: current_subscription_id,
price: new_price_id
}],
default_payment_method: payment_id,
add_invoice_items: [{
price: new_price_id
}]
});
Then we would Create the invoice with the Sub ID:
const invoiceItem = await stripe.invoiceItems.create({
subscription: current_subscription_id
});
And finally:
const invoice = await stripe.invoices.pay(invoiceItem.id);
Yeah that looks right to me. What I don't actually remember here is whether that update will create an Invoice itself since you are resetting the billing_cycle_anchor. I think it actually will, in which case you can just skip over the creating/paying Invoice step and it will automatically pick up that Invoice Item. Best thing to do at this point is test it out.
Yeah i see here: https://stripe.com/docs/api/subscriptions/update
they return an ID under latest_invoice
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Can you confirm this is the field I would use to run:
const invoice = await stripe.invoices.pay(subscription_update.latest_invoice);
If you are using charge_automatically, which I assume you are, then you don't have to call that at all.
It will finalize and charge itself
Best thing to do now is to test this out and use a test clock to do so: https://stripe.com/docs/billing/testing/test-clocks
Where would i set charge_automatically? I dont see that on the https://stripe.com/docs/api/subscriptions/update docs
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
I see under collection_method
So how does this look? Its assuming I can update the subscription , create the invoice and charge it automatically:
const subscription_update = await stripe.subscriptions.update(current_subscription_id, {
billing_cycle_anchor: 'now',
proration_behavior: 'always_invoice',
collection_method: 'charge_automatically',
add_invoice_items: [{ price: new_price_id }],
default_payment_method: payment_id,
items: new_subscription_items,
});```
I dont have to set it?
Correct, the default is charge_automatically so you can omit that
Okay I have made th e update to the Subscription Update, and added an extra step to pay the invoice using the latest_invoice I'm about to give it a test
Let me know
So I ran the pipeline with the new step, but im getting an error now when updating the subscription:
The price specified is set to `type=recurring` but this field only accepts prices with `type=one_time`.
That is correct
These are the sort of things I'm looking for guidance in, Should they all be one_time?
Let's pause
I thought you wanted to charge them one time on the 10th when they update?
And then you are going to charge them again the next month via recording usage, right?
That sounds about right...
Just to make sure we're on the same page when you say 'recording usage' you are referring to Metered usage?
Yep
Okay I see the flaw in my thinking
The way understood it to work was like this:
1\ A customer subscribes to a paid plan to get gets 5 requests. (ie Jan 4th)
2\ First they pay the Monthly Fee ($20) and then they are given access to the 5 Requests.
3\ Within the month the user now uses up all 5 Requests. Each Request after the initial 5 are now going to be charged to the user (ie $0.25 per request after limit is reached)
4\ At the end of the month We charge the Monthly Fee again and start over.
I guess what I never thought about was, at what point do I charge the customer the extra fees they incurred
The Next month I would have to add those fees into the Monthly Bill so
M = Monthly Fee
R = Extra Requests
Rf = Request Fee
Next Month Bill = M + (R * Rf)
Okay so this is a different scenario than we started with
Yeah completly
We were originally talking about an update mid-cycle
Thanks for sticking around this has been vary helpful
With what you are talking about now, you just want two different Prices -- one that is a fixed Price that will charge at the beginning of the cycle and one that is metered to track the amount of usage over the course of the period.
So is that how I should treat the subscription?
For example have two Items under the subscription:
(Price 1) Monthly Fixed Price, and (Price 2) Metered Price 0.25/unit
Okay this is making sense
So in the case of a Mid Cycle Subscription update, I would remove both items and add two new items
then same as before after the update, submit payment to that invoice
Or since the Fixed Price is now Fixed will stripe automatically charge the customer (skipping the payment submit step)
Yep if you are using a fixed Price then when you update there will automatically be proration
Okay Makes sense.
Before I start making changes, what are the best practices when creating products and prices?
For example now I have to have two prices types: Fixed and Metered.
Should I create two products
(Prod 1) Fixed Product - Free, Stnd, Bsns, Ent
(Prod 2) Metered Product - Free, Stnd, Bsns, Ent
Or should all Prices be grouped into one Product?
Or does it even matter?
That is totally up to you
But things like what the line items show is based on Product's name
So it mostly depends on your business model
Okay that makes sense as well
The new Fixed price is referring to a Standard Pricing Model, correct?
If Yes, Does this price need to be 'Recurring' or 'One Time' ?
I ask because with this new model having two Prices (1xStandard/OneTime 1xMetered) what happens after a Month, does the the Fixed Price automatically get charged, or do I need to manually submit the payment?
Wouldn't this cause the same error I encountered above when the user decides to Upgrade or Downgrade the price?
No, you ran into an error becuase you tried to use a recurring Price for a one-time Invoice Item.
I see
Does the following example of product models make sense to you?
### Free Plan
| Model | Fee | Type | Metered |
| -------- | ----- | --------- | ------- |
| Standard | $0.00 | Recurring | `false` |
| -- | -- | -- | -- |
### Standard Plan
| Model | Fee | Type | Metered |
| --------- | ------ | --------- | ------- |
| Standard | $20.00 | Recurring | `false` |
| Graduated | $0.005 | Recurring | `true` |
### Business Plan
| Model | Fee | Type | Metered |
| --------- | ------ | --------- | ------- |
| Standard | $45.00 | Recurring | `false` |
| Graduated | $0.005 | Recurring | `true` |
I omit the metered plan from the Free Product, because I don't think its necessary. But if you recommend to keep it for consistency thats no problem
All looks good to me
In terms of omiting on your free -- that's up to your preference
No right/wrong or not
Rodger that
This is what I have so far for the metered price
I mostly followed the setup the docs show, not sure if this applies to my scenario as well
Yeah you want to follow https://stripe.com/docs/billing/subscriptions/usage-based/pricing-models#flat-rate-metered-usage
Wow this is fantastic
So I'm trying to translate step 3 from the Doc to the Stripe Client, Does this make sense?
It looks like I set a standard price, with monthly and metered billing, and If I Understand correctly, every time I create a Usage report for the customer, lets say sum up 12, the user will get charged 12 x Standard Metered Price (ie. 0.005)
Hi, bismark had to step away. Let me look at your follow up questions here.
Hey Pgskc, thanks for joining
To catch you up, I'm following this Doc at the moment:
https://stripe.com/docs/billing/subscriptions/usage-based/pricing-models#flat-rate-metered-usage
My question is in regards to step 3 "Create the Meeting per minute usage price."
I am trying to translate this snippet of code:
const price = await stripe.prices.create({
nickname: 'Metered Monthly Plan',
product: '{{PRODUCT_ID}}',
unit_amount: 700,
currency: 'usd',
recurring: {
interval: 'month',
usage_type: 'metered',
},
});
Into the Stripe Dashboard > Create Product > Add Price
I recommend that you test this and see how it reflects on the Dashboard. The Dashboard and the API do not have the same functionalities at times. From the above summary and the screenshot, it does look like yoiu have it right.