#rv_subscription-invoiceitems

1 messages Β· Page 1 of 1 (latest)

pallid sparrowBOT
#

πŸ‘‹ 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/1283470368283037801

πŸ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

native crescent
#

this is the process im trying to follow. using this:

woven star
#

Hello
When you say

It functions as expected when I manually make a draft invoice, but not when the subscription automatically does.
Are you referring to the initial subscription invoice or renewal invoice?

native crescent
#

When i create a draft invoice for the user, outside of the subscriptions t

#

the overage is checked and added correctly

woven star
#

and when you say

It functions as expected when I manually make a draft invoice, but not when the subscription automatically does.
you mean the invoice item isn't added to the subscription invoice automatically?

native crescent
#

Yes - when the subscription automatically creates a draft invoice the invoice items aren't added

#

but when I create an invoice manually for the same user it is added as expected

native crescent
woven star
#

Hmm intersting.. Can you share an example subscription ID and the invoice item ID you expected to see?

native crescent
#

Yes, one second

#

First.

Here is an example invoice I created outside the subscription where usage was properly called for and added:

in_1PxtzkHMO3TQomsM7oOLXeTl
added prod_QpMdE2cecanzpP

#

Here is an example subscription (same user and billing period) where I sped up the time and the draft invoice did not add the product

#

in_1Pxu1SHMO3TQomsMeT8YjFqy
sub_1PxtutHMO3TQomsMEtl2dmBe
prod_QpMdE2cecanzpP

woven star
#

I don't see a pending invoice item on the customer?

native crescent
#

Sorry - i accidentally deleted the successful invoice

#

see here: in_1Pxu7RHMO3TQomsMtMERnkBG

native crescent
#

should be two subscription drafts under the customer

#

the non-subscription one worked as expected but the subscription one did not

woven star
#

Sorry I think I'm confused.. Most likely you're misunderstanding the flow..

In your example, you're creating a separate invoice and adding the invoice item explicitly to that invoice..
https://dashboard.stripe.com/test/logs/req_B1SQs98EWuwCQ0

If you're trying to add an invoice item to a subscription draft invoice then you don't need to create a separate invoice..

If the subscription invoice hasn't been created yet then all you need to do is create an invoice item: https://docs.stripe.com/api/invoiceitems/create (without invoice or subscription parameter set).. Once the subscription invoice is drafted, it will automatically pull the invoice item..

If the subscription invoice exists, then you'd need to create an invoice item with invoice parameter set to subscription's draft invoice ID

pallid sparrowBOT
native crescent
#

If the subscription invoice exists, then you'd need to create an invoice item with invoice parameter set to subscription's draft invoice ID

This is how i think its setup currently?

toxic plinth
#

rv_subscription-invoiceitems

#

that's what you have to do yes but only if that Invoice was already issued from the Subscription and is already in draft

native crescent
#

Yeah. that's why i am trying to use the invoice.created webhook to trigger adding the invoice items

#

When i create a draft invoice for a customer (outside of the subscription auto-creating), the call is able to add the invoice items

#

sorry thank you for the help, confused

toxic plinth
#

yeah I'm trying to grasp your question but I don't get it. Are you solely talking about Invoices associated with a Subscription or something else?

native crescent
#

Yeah. Solely trying to edit invoices with a subscription. I was just testing it by manually creating

#

in both cases the invoice.created webhook should trigger was my thinking

toxic plinth
#

All good. Try again from scratch and show me the exact Invoice id and I'll figure out what's not working

native crescent
#

Kk

#

will be 5 or so minuites

toxic plinth
#

yep no rush!

native crescent
#

Just added a new customer:

cus_QpZXpDAurnD7He
sub_1PxuObHMO3TQomsM0Eupc1cC

#

Now I am going to add overage for them on my software.

#

Ok- so the user has a subscription and overage. What i want to happen is at the end of the billing cycle, when the subscription draft invoice is created it calls our software, which checks the overage and adds the products to their invoice, and then they get billed for the renewal and the months overage fees

#

To test the overage call, I am manually creating a draft invoice outside of the subscription

toxic plinth
#

To test the overage call, I am manually creating a draft invoice outside of the subscription
yeahhhh that makes no sense (sorry)

native crescent
#

kk

toxic plinth
#

Like if you create your own Invoice, it's completely unrelated to the Subscription and doesn't really work that way.

native crescent
#

Yeah- in both scenarios its using the invoice.created action to call my webhook to check overage for the user, and add the overage to the invoice id in the webhook

#

shouldn't they behave the same ?

native crescent
#

testing now with the testclocks

#

in_1PxuWWHMO3TQomsMcTVH9YK5

#

invoice here. calls same webhook but doesn't add

toxic plinth
#

Hum sorry I'm struggling to follow. You post really short sentences with just a few words.

Can you please explain in details what you did, how you tested, what the exact API Request looks like, how you advance time, which Invoice you are trying to modify, what you see when you create the InvoiceItem, etc? All with clear code?

native crescent
#

Sure

native crescent
#

Ok. So on my webapp, I created a new customer for my SAAS. I then signed up for a subscription (sub_1PxuObHMO3TQomsM0Eupc1cC). I manually added overage to this customer on my software side. At the end of the billing cycle, what I am expecting to happen is the subscription automatically creates a draft invoice as outlined here

https://docs.stripe.com/billing/invoices/subscription#adding-draft-invoice-items

"You can create invoice items on that invoice. Make sure to provide the invoice parameter when you create these invoice items. Otherwise, they’re added as pending items and are included in the next subscription period."

I setup this webhook to trigger the overage checking on my side:

https://global-talents-test.bubbleapps.io/version-test/api/1.1/wf/invoice-created
Using the invoice.created action as a trigger.

So, what I did first to test my webhook is created a draft invoice outside of the subscription to trigger invoice.created. This sends the invoice ID to my webapp, and I can then return a webhook to stripe with the following call:

api request to
https://api.stripe.com/v1/invoiceitems

parameters included
customer
invoice
description
quantity
price

Testing it this way added the overage to the draft invoice i created.

So, then i went to test the timeclock (What I assumed was that subscription's create a draft invoice 1 hr before billing, and I can use the same api request as above.

When i run the simulation on the subscription for the same user, it triggers the webhook, but does not add the overage charges in the same way it does when I create a draft, even though it sends the same type of api request

api request to
https://api.stripe.com/v1/invoiceitems

parameters included
customer
invoice
description
quantity
price

#

Kind of the best I can explain it

toxic plinth
#

No that does really help. Now: please give me the exact Invoice id where you just tried to add the InvoiceItem. Also please share the exact InvoiceItem id ii_1234 that your code created

native crescent
#

**Invoice ID where I tried to add overage charges using the Testclocks
**
in_1PxuWWHMO3TQomsMcTVH9YK5

toxic plinth
#

that Invoice was from 27 minutes ago, that's expected?

native crescent
#

Invoice ID where I successfully added overage charges using a draft invoice

in_1PxuUbHMO3TQomsMJY4Yo8oz
and the item event it added
evt_1PxuUcHMO3TQomsM1pkkrXwD

native crescent
toxic plinth
#

yeah but you do a lot of things, and so I'm trying to find the exact API request with their exact order

#

But okay, that Event proves you created the InvoiceItem on that Invoice and the Invoice would have included it. So what's the issue?

native crescent
toxic plinth
#

πŸ˜…

#

I'm so sorry for being so dense. I feel like we are talking completely past each other and I don't understand what's going on

native crescent
#

ok i can rephrase my question

#

might help?

toxic plinth
#

I want you to ignore any Invoice you create yourself. I solely want you to focus on the exact draft Invoice created by the Subscription.

#

So can you please start fresh and do nothing else than try to add a new InvoiceItem to a brand new Subscription's draft Invoice in Test mode

native crescent
#

Ok - is there any additional parameters I would need to include?

#

I will link the api request for that scenario u are describing

toxic plinth
#

no there isn't. I really think we are just talking past each other right now

native crescent
#

evt_1PxuPdHMO3TQomsM6nWyiXvS

#

This is the event where it sends a webhook, and should have returned the additional products

#

on a subscription invoice

toxic plinth
#

I'm so sorry. This Invoice comes from you clicking somewhere in the Dashboard and not writing code or advancing a TestClock. You seem to have manually changed the Subscription in the Dashboard for some reason

native crescent
#

I removed the free trial and advanced the test clock

#

I can try again

toxic plinth
#

yeah let me say something: Not all Invoices are created in draft. The first Invoice of a Subscription is not in draft, it's synchrously closed/paid. The Invoice if you end a trial is not in draft, etc.

native crescent
#

yes that probably breaks the system

toxic plinth
#

yes though I expected to see a real API error where you basically try to create an InvoiceItem on the Invoice and it errors but that didn't seem to happen (that's why I had you retry each time to try and figure it out)

native crescent
toxic plinth
#

yep just flagging.
But really we're stuck right now until you do a real reproduction without manual changes in the Dashboard and then share those exact new object ids so I can figure out the issue

native crescent
#

Okay yeah i'll give that a go. Although i suspect if it doesn't create a draft for the first invoice it will break, as the stripe docs say the invoice can only be editted in draft

#

And it only triggers my webhook when the invoice is created

toxic plinth
#

yeah but even if it broke, it should so something obvious that is visible in your logs

native crescent
#

theoretically if I tried on a future billing cycle adding overage it would create a draft?

#

testing not on the first period

native crescent
#

I can test both scenarios

toxic plinth
#

What you need to do is quite simple but hard to grasp

  1. Create a TestClock
  2. Create a Subscription
  3. Advance the TestClock time to after the current period. So if you have a monthly Price, you advance by exactly one month
  4. Make sure your webhook handler added the Invoice item
native crescent
#

Yes i've done that 4 times. it doesn't add the invoice item

#

i think i will have to rebuild to avoid the trigger being invoice.created and adding to draft

toxic plinth
#

Yes i've done that 4 times. it doesn't add the invoice item
but it must do something. That's the thing you need to track. The problem is you seem to use a no-code UI to do some of this instead of real end to end code you host where you can add clear logs to debug this in seconds

native crescent
#

Yeah. I found it. it's telling me that the condition i set to add the usage isnt passing, which just checks if overage is greater than 0.

#

It uses the billing cycle dates to check so I am guessing those might be resetting before it runs

toxic plinth
#

ah yeah if you have more logic that decides when to add the InvoiceItem in the first place, that would explain everything
and yeah that Invoice is for the new period

native crescent
#

but it checks the overage based on the current billing cycle

toxic plinth
#

yep that makes sense. Just summarizing that we spent all this time trying to debug why our API didn't work but you had logic that didn't call us.

native crescent
#

πŸ˜…

#

Ok so the invoice created action resets the billing cycle ?

#

I still need to figure out how to change my code :d

toxic plinth
#

Ok so the invoice created action resets the billing cycle ?
not really no
the Invoice is created because a new billing cycle started

native crescent
#

ah. Okay

#

That makes sense !

toxic plinth
#

Like the whole reason that Invoice happens is that say the September 1st - October 1st period ended, and it is now time to bill for the upcoming October 1st - November 1st period.
And we let you add extra line items to the Invoice for what you charge in "arrears"

#

Also the way you are framing it all, we usually recommend reporting usage to us during the period and we can charge that usage automatically for you. That's what our metered billing also called usage-based billing product is for: https://docs.stripe.com/billing/subscriptions/usage-based

pallid sparrowBOT
native crescent
#

Got it. thanks for the help. sorry it took a long time !!! new to building πŸ˜„