#rv_subscription-invoiceitems
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/1283470368283037801
π Have more to share? Add more details, code, screenshots, videos, etc. below.
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?
When i create a draft invoice for the user, outside of the subscriptions t
the overage is checked and added correctly
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?
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
In my testing by speeding up the subscription time
Hmm intersting.. Can you share an example subscription ID and the invoice item ID you expected to see?
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
I don't see a pending invoice item on the customer?
Sorry - i accidentally deleted the successful invoice
see here: in_1Pxu7RHMO3TQomsMtMERnkBG
I don't want it to add as a pending invoice item, but directly to the subscription invoice
should be two subscription drafts under the customer
the non-subscription one worked as expected but the subscription one did not
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
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?
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
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
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?
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
All good. Try again from scratch and show me the exact Invoice id and I'll figure out what's not working
yep no rush!
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
To test the overage call, I am manually creating a draft invoice outside of the subscription
yeahhhh that makes no sense (sorry)
kk
Like if you create your own Invoice, it's completely unrelated to the Subscription and doesn't really work that way.
What you want is to use TestClocks to simulate the time advancing and get to the next period: https://docs.stripe.com/billing/testing/test-clocks
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 ?
yes trying this
regardless, that invoice is here.
in_1PxuUbHMO3TQomsMJY4Yo8oz
calls webhook and adds the invoice items
testing now with the testclocks
in_1PxuWWHMO3TQomsMcTVH9YK5
invoice here. calls same webhook but doesn't add
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?
Sure
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
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
**Invoice ID where I tried to add overage charges using the Testclocks
**
in_1PxuWWHMO3TQomsMcTVH9YK5
that Invoice was from 27 minutes ago, that's expected?
Invoice ID where I successfully added overage charges using a draft invoice
in_1PxuUbHMO3TQomsMJY4Yo8oz
and the item event it added
evt_1PxuUcHMO3TQomsM1pkkrXwD
Yes - referring to the initial one i tested
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?
That is a draft. it doesn't work when the Subscription makes the draft invoice.
π
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
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
Ok - is there any additional parameters I would need to include?
I will link the api request for that scenario u are describing
no there isn't. I really think we are just talking past each other right now
evt_1PxuPdHMO3TQomsM6nWyiXvS
This is the event where it sends a webhook, and should have returned the additional products
on a subscription invoice
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
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.
yes that probably breaks the system
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)
Also right now look at https://dashboard.stripe.com/test/logs/req_M1UAn22TyhHtP9. That's your own code trying to create an InvoiceItem with a negative quantity as you passed quantity: -15 which is not allowed
yeah was trying to adjust somethings on my end . typically wouldnt send that
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
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
yeah but even if it broke, it should so something obvious that is visible in your logs
theoretically if I tried on a future billing cycle adding overage it would create a draft?
testing not on the first period
or would this just apply to a free trial period ending
I can test both scenarios
What you need to do is quite simple but hard to grasp
- Create a TestClock
- Create a Subscription
- Advance the TestClock time to after the current period. So if you have a monthly Price, you advance by exactly one month
- Make sure your webhook handler added the Invoice item
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
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
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
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
it's just simply
invoice.created
-> check if overage if greater than 0, if so trigger the add invoice item api call with the number of overage to the invoice id recieved
but it checks the overage based on the current billing cycle
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.
π
Ok so the invoice created action resets the billing cycle ?
I still need to figure out how to change my code :d
Ok so the invoice created action resets the billing cycle ?
not really no
the Invoice is created because a new billing cycle started
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