#atlasdb

1 messages · Page 1 of 1 (latest)

tender otterBOT
willow stratus
#

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

low bridge
willow stratus
low bridge
willow stratus
#

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

low bridge
#

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.

willow stratus
#

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

low bridge
#

you're offerring a free trial, what are you charging for here?

willow stratus
#

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

low bridge
#

But if they are on a free pricing, what amount are you charging them for the usage if it's free?

willow stratus
#

I'm not charging anything

low bridge
#

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.

willow stratus
#

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?

low bridge
#

That is correct

willow stratus
#

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

low bridge
#

Which part are you confused on?

willow stratus
#

Everything, I've been explaining what I want and what Ive tried and I keep getting brick walled

low bridge
#

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

tender otterBOT
willow stratus
#

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)

timid turtle
#

👋 stepping in as pgskc needs to step away

willow stratus
#

Hello Bismarck

timid turtle
#

To clarify the above, the Jan 10th new Price is also metered, correct?

willow stratus
#

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    |
timid turtle
#

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.

willow stratus
#

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'
});
timid turtle
willow stratus
#

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);
timid turtle
#

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.

willow stratus
#

Can you confirm this is the field I would use to run:

const invoice = await stripe.invoices.pay(subscription_update.latest_invoice);
timid turtle
#

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

willow stratus
#

I see under collection_method

timid turtle
#

Yeah it is the default

#

You don't have to set it

willow stratus
#

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,
});```
willow stratus
timid turtle
#

Correct, the default is charge_automatically so you can omit that

willow stratus
#

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

timid turtle
#

Let me know

willow stratus
#

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

timid turtle
#

Invoice Items are for one-time charges.

#

Did you use a recurring Price ID for that?

willow stratus
#

That is correct

#

These are the sort of things I'm looking for guidance in, Should they all be one_time?

timid turtle
#

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?

willow stratus
#

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?

timid turtle
#

Yep

willow stratus
#

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)

timid turtle
#

Okay so this is a different scenario than we started with

willow stratus
#

Yeah completly

timid turtle
#

We were originally talking about an update mid-cycle

willow stratus
#

Thanks for sticking around this has been vary helpful

timid turtle
#

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.

willow stratus
#

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

timid turtle
#

Yep that's what I'd do

#

For what you described above

willow stratus
#

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)

timid turtle
#

Yep if you are using a fixed Price then when you update there will automatically be proration

willow stratus
#

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?

timid turtle
#

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

willow stratus
#

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?

timid turtle
#

Recurring

#

And yes it would charge automatically each month

willow stratus
timid turtle
#

No, you ran into an error becuase you tried to use a recurring Price for a one-time Invoice Item.

willow stratus
#

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

timid turtle
#

All looks good to me

#

In terms of omiting on your free -- that's up to your preference

#

No right/wrong or not

willow stratus
#

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

willow stratus
#

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)

low bridge
#

Hi, bismark had to step away. Let me look at your follow up questions here.

willow stratus
#

Hey Pgskc, thanks for joining

#

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

low bridge
#

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.