#joe_unexpected

1 messages Β· Page 1 of 1 (latest)

prisma tapirBOT
#

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

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

solemn raven
#

Hi! Can you share the ID of the subscription, and the request for your upcoming invoice API call?

rain mauve
#

Subscription ID: sub_1R4rvMK2Tf1SdvM0Lu0DXIMH

Upcoming Invoice API request:
Invoice.upcoming(
customer='cus_RuuaQaPUVwpBpk',
subscription='sub_1R4rvMK2Tf1SdvM0Lu0DXIMH',
subscription_details={
"items": [{'id': 'si_Rypa97OpYQYl5V', 'price': 'price_1R15B8K2Tf1SdvM00HPA0EkQ', 'quantity': 2, 'discounts': [{'coupon': '10_percent_discount'}]}, {'id': 'si_Rypapq79n9FM7w', 'price': 'price_1R15B7K2Tf1SdvM0FAF6B7Je', 'quantity': 1, 'discounts': [{'coupon': '10_percent_discount'}]}],
"proration_behavior": "always_invoice",
},
)

Response: attached

#

Actually, it's 2 upcoming invoice requests to 2 subscriptions, this was the first one, I'm sending the next one.

#

The issue is in the second one, my apologies, but they sometimes shuffle.

solemn raven
#

No worries. I'll wait until the second one. πŸ™‚

rain mauve
#

2nd Upcoming Invoice API request:
Invoice.upcoming(
customer='cus_RuuaQaPUVwpBpk',
subscription='sub_1R4ruoK2Tf1SdvM0eGnrca5i',
subscription_details={
"items": [{'id': 'si_RypZVkYpDZQkoM', 'price': 'price_1R15JZK2Tf1SdvM0KTEA7ynv', 'quantity': 2, 'discounts': [{'coupon': '10_percent_discount'}]}, {'id': 'si_RypZZp8bZiVGJV', 'price': 'price_1R15JZK2Tf1SdvM0Ou7299Bi', 'quantity': 1, 'discounts': [{'coupon': '10_percent_discount'}]}],
"proration_behavior": "always_invoice",
},
)

Response: attached

#

The amount due is 4759, not sure why it's less than the first upcoming invoice.

solemn raven
#

What do you mean 'less than the first ... invoice'?

rain mauve
#

The 2 subscriptions should be the same in terms of quantity and discount. So that when I alter them by adding an extra 1 quantity, the upcoming invoice for each should be the same.

solemn raven
#

Ah

rain mauve
#

I know this may sound confusing, but let me know how I can help you to understand it better.

solemn raven
#

No, it makes sense. I am helping a half dozen people but I'm digging into this for you. πŸ‘

rain mauve
#

Thanks mate, yeah take your time πŸ™‚

solemn raven
#

The difference is, I think, coming from the lack of discount on the proration adjustment on the first one.

#

I'm not sure why that's happening yet.

rain mauve
#

Yes, that’s what I concluded as well. Not sure why. Even though the request contains the discounts.. does it not expect to re-add the same discount?

solemn raven
#

It would add it; if it was there previously it would 're-add' it.

#

Which one is right?

#

The second one?

rain mauve
#

The first one

#

Amount due should be around 5355

#

Each

solemn raven
#

It doesn't expect anything. Still digging, sorry.

rain mauve
#

It’s okay take your time

solemn raven
#

When you created the 'incorrect' one, it was created with a discount.

When you created the 'correct' one, it was created without discounts, and then you updated it to add discounts at Mar 20, 2025 22:50 GMT

Your upcoming Invoice was run at Thu Mar 20 2025 22:52:42 GMT so it should have included those discounts.

#

You have a lot of identically named items in your test account; I'd suggest cleaning that up, and trying this again, and seeing how it goes - I think the second one was actually the one that seems to have rendered incorrectly as it should have also had the discount applied on the proration too.

#

I unfortunately have to run - well, I had to run 45 minutes ago πŸ€ͺ - but someone will be online again in about 7 1/4 hours.

#

Cheers!

prisma tapirBOT
rain mauve
#

Cheers mate, is there someone else over here?

prisma tapirBOT
tacit magnet
#

Hi, yes. How can I help?

rain mauve
#

Hi there, umm do you see the previous chat? or do I re ask the question?

tacit magnet
#

Can you summarize the outstanding question please?

rain mauve
#

Yes sure. I'm using the upcoming invoice API, and it's inaccurate, not sure what caused this.

So I have 2 different subscriptions, with Subscription Items and discounts on the Items.

What I am doing is I want to anticipate the upcoming invoice if I increase the quantity of the subscription items in both.

I'm calling upcoming invoice API twice (once for each). and the amount due is inaccurate in one of them.

#

I have the proration set as "always_invoice", from what I understand, the items are removed (credited) then re-added. The issue is in the crediting or removing.

#

The amount credited is the original price, not the discounted price.

tacit magnet
#

How are the discounts applied? Are you able to review the above?

rain mauve
#

The discount is applied on the Subscription Item level, and in the upcoming invoice API, the method is called like this:

Invoice.upcoming(
customer='cus_RuuaQaPUVwpBpk',
subscription='sub_1R4ruoK2Tf1SdvM0eGnrca5i',
subscription_details={
"items": [{'id': 'si_RypZVkYpDZQkoM', 'price': 'price_1R15JZK2Tf1SdvM0KTEA7ynv', 'quantity': 2, 'discounts': [{'coupon': '10_percent_discount'}]}, {'id': 'si_RypZZp8bZiVGJV', 'price': 'price_1R15JZK2Tf1SdvM0Ou7299Bi', 'quantity': 1, 'discounts': [{'coupon': '10_percent_discount'}]}],
"proration_behavior": "always_invoice",
},
)

#

The thing is, this issue occurs for 1 of the subscriptions, and not on the other

#

While both have the same settings in terms of quantity and discounts, just different prices.

tacit magnet
rain mauve
#

This is an example:
Subscription.create(
customer=customer_id,
items=[
{
"price": price.stripe_id,
"discounts": [{"coupon": item_discount}]

                }
            ]
            
        )
#

It's on the Subscription Item level.

#

The discount itself is created as a coupon.
Coupon.create(
id=payload["id"],
name=payload["name"],
percent_off=payload["percent_off"],
amount_off=payload["amount_off"],
duration=payload["duration"],
duration_in_months=payload["duration_in_months"],
)

tacit magnet
#

I highly recommend that you try creating this scanario from scratch by following identical steps.

rain mauve
#

Okay sure, I can retry, but is the issue in the current subscription items? or the upcoming invoice call?

#
tacit magnet
#

These are current, they were created on 3/20. However, you said the upcoming incoices showed different results... and I'm saying since you passed a discount on one and not the other when you created the subscription, you're not comparing the exact same scenarios

#

No, sub_1R4ruoK2Tf1SdvM0eGnrca5i does not have the discounts

rain mauve
tacit magnet
rain mauve
#

I see what you mean.

#

But what I do is

#

once the second subscription is created (with a discount), I modify all existing subscriptions and add a discount to them through the webhook.

#

It's like a promo for the users, if they subscribe to more items, they get discounts on all existing.

tacit magnet
#

So you'd want to create that scenario exactly and then compare the two

rain mauve
#

Yes exactly

tacit magnet
#

I'll be here while you test

rain mauve
#

It's not actually a comparison, I just want to figure what the user has to pay

#

This is the current scenario πŸ˜„

tacit magnet
#

Yeah - on the same page

rain mauve
#

I'm sorry if I'm confusing you.

tacit magnet
#

Your main question is 'why is this behaving different on the discount on these two subscriptions?' What I'm saying is that you're not comparing the exact same scenarios so they would not be identical.

As a next step, you'd want to test this from scratch, create the subscription, the update subscription with the discount, then use the upcoming invoice API to attain the amount you're seeking for.

#

I'll be here while you test

rain mauve
#

Alright, I'll create them from scratch

#

And in the webhook, it updated the previous subscription and added a discount.

#

Everything is fine until now.

#

I then added another subscription item to each of them (indicates a team member) - and this is correct amount.

#

Now, when I need to increase the quantity, the upcoming invoice is inaccurate.

tacit magnet
#

What amount did you expect vs what you see with what changes?

rain mauve
#

Current: 95.20
Expected: 107.10

#

Should I paste the upcoming invoice params?

#

I have the responses as well.

#

Btw the amount is the sum of 2 upcoming invoice calls (one for each subscription).

tacit magnet
#

Hmm, why would you want the sum of 2 upcoming invoice calls?

#

Yes please, if you can share those it would be helpful

rain mauve
#

Yes sure, I want the user to pay once, and I need to show them what they will pay, and since it's 2 subscriptions, it's going to be 1 for each. So I create 1 bulk invoice with the invoice items generated from the upcoming invoices.

tacit magnet
#

Why do you have two separate subscriptions? What is the use case?

rain mauve
#

Request:
InvoiceService(user.site.id).upcoming_invoice(
customer_id='cus_RuuaQaPUVwpBpk',
subscription_id='sub_1R55lPK2Tf1SdvM0JowPSFuM',
subscription_items=[{'id': 'si_Rz3weh6uDDRP80', 'price': 'price_1R15JZK2Tf1SdvM0KTEA7ynv', 'quantity': 2, 'discounts': [{'coupon': '10_percent_discount'}]}, {'id': 'si_Rz3ttwhMFBTkKo', 'price': 'price_1R15JZK2Tf1SdvM0Ou7299Bi', 'quantity': 1, 'discounts': [{'coupon': '10_percent_discount'}]}],
)

Response:

#

The user subscribes to a product, then later they subscribe to another one.

#

The user can add team members to their subscriptions with a discounted price.

#

If the user is subscribed to 2 or more products they get 10% discount on the subscription items in any active subscription.

#

The data I sent you was for one of the invoices, which is the one that fails, it shows 4759 as the amount due.

#

While it should be 5355

tacit magnet
#

To simplify this, I think you could have one subscription and just update the subscription with adiditonal products as subscriptions can have multiple products: https://docs.stripe.com/billing/subscriptions/multiple-products

This might make managing things a bit easier on your end and you would not need to do any manual calculations between the two subscriptions to surface the upcoming invoice amount.

rain mauve
#

😦

#

I can't modify the implementation now, it would be great if I can just understand why this is happening.

tacit magnet
#

Fair, looking at the response, hang tight

rain mauve
#

Thank you mate

#

Why should it be 5355? because I'm increasing the quantity of a price that costs 59.5 that has 10% discount = 53.55.

tacit magnet
#

From looking at that response, it looks like prorations are working as expected. I do not think you're accounting for the 'Unused time' and 'Remaining time' from the previous invoice. I think you'd want to pass 'proration:none': https://docs.stripe.com/api/invoices/upcoming#upcoming_invoice-subscription_proration_behavior

You can confirm this by looking at the description under lines.data

rain mauve
#

The thing is the Unused time doesn't consider the discount.

#

So it credits the original price not the discounted price.

#

I'm okay with the proration as it is, the user only has to pay what's left in the subscription.

#

But not sure why the Unused time is crediting the original price not the discounted price.

#

I think I undertand now, it credits the paid amount. When I created these items originally they didn't have a discount? is this the case?

#

I'm not sure how I'm supposed to solve it, it's an issue.

tacit magnet
rain mauve
#

I see, how can I set them to True? and at which stage?

tacit magnet
rain mauve
#

I cannot, because some of the items in my use case may not be discountable.

tacit magnet
#

In that case, you'd want to create prices that are already discounted

rain mauve
#

I see, it's unfortunately getting so much more complex.

#

It will be harder to track them, but I will give it a go.

#

Is there anything else you can advice me to go for?

tacit magnet
#

You can add metadata to these objects, https://docs.stripe.com/metadata to make tracking better.

I truly think your flow would be much easier if you create one subscription and then just update them when they want to add more products. It might be something you test out first and ensure that it fits your business use case fully.

#

This way, you would not need to add the total amount from the two invoices to show the full amount

rain mauve
#

Thanks mate, thanks for your time and effort.

#

Have a good day.