#ironbeard_invoice-preview-discounts

1 messages ยท Page 1 of 1 (latest)

silk moonBOT
#

๐Ÿ‘‹ 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/1328382789568958559

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

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

simple rose
#

Thanks for the info, looking in to this

#

Apologies but the server got busy and I am just now looking in to this. Would you be able to paste the json output of one of those invoices here? Also if you do actually create the invoice have you seen the discounts apply?

ionic isle
#

No worries, one sec

#

Here are the invoice IDs, do they help?

il_tmp_123351CoTIfwbn281fbfaed2, il_tmp_143640CoTIfwbn2854be19af

#

I haven't tried actually creating the invoices, I'm using the preview feature to generate a shopping cart page to show lines / cost / discounts / prorations before they start the "actual" checkout flow

simple rose
#

Unfortunately I can't look them up. It looks like the items should have a discounts array that shows what discounts apply to them. Are those populated on your temporary items?

ionic isle
#

yeah one second

#

so neither of them have the discounts array populated, but they do have the discount_amounts array, and it's zero for the "mismatched" scenario. But I can't use discountable to detect discounts bc it's True for both situations

#

kind of as a related sitation, I'm kind of surprised Stripe doesn't provide a "line subtotal," it looks like I have to calculate it myself using amount / amount_excluding_tax and the values in discount_amounts.

#

(in the end, I suppose it's something I could prevent on my end by not requesting preview invoices containing promotion codes that don't apply to the "cart," which I'll fix. But was still surprised by this behavior and wasn't sure if it was a bug)

simple rose
#

Oh I found clarification. Will see if we can update the wording. This is meant to be a field that you set to true or false depending on whether discounts should apply to the item. So when we say "discounts will apply to this item" it more means "discounts can apply to this item". We will apply them if they line up, but we don't mean "will" as in "we found a specific discount that will apply to this item"

#

And yeah on the subtotals unfortunately. Can put in feedback about that

ionic isle
#

But if the promotion code's coupon doesn't apply the product, is it still true that the discount "can" apply to the item?

#

Or is "discountable" going to be True for every line in an Invoice that has a promotion code *regardless of whether or not the promotion code applies to that line?

simple rose
#

It means that a discount can apply to the invoice item, but not necessarily the discount(s) that are actually on the previewed invoice.

#

So yes it will be true whether or not the promotion code applies, unless you set it to false on the invoice item yourself

ionic isle
#

So the docs say that discountable is always False for proration. Are the following statements true?

  • If the preview invoice has no discount / promotion code / coupon, then every line will have discountable = False
  • If the preview invoice has a discount / promotion code / coupon, then every non-proration-related line will have discountable = True regardless of whether or not the discount applies to the line?
simple rose
#

From my understanding it would be true in the first bullet point as well (for non proration items). My understanding is that this is entirely the behavior of whether the item can in theory have a discount applied to it if you add one that is usable for its product and so on

ionic isle
#

Oh, strange I didn't catch that but yes it does seem like that's the case

#

btw I've noticed in all scenarios (e.g. whether or not discount_amounts is populated, and whether or not a discount applies to that line), that discounts is always an empty list. Do I need to use the expand parameter in order to get something there?

simple rose
#

I think so, I think our API ref is slightly out of sync there. We show "Expandable" on discounts on the actual invoice item reference, but not the one for the upcoming invoice's items. Can you test this out quickly? If they do start showing up, I can file something about that. That is strange that we return an empty array though, usually in this scenario we don't reaturn anything until the expand param is passed
https://docs.stripe.com/api/invoiceitems/object#invoiceitem_object-discounts

ionic isle
#
data = {
  'subscription_details': {
    'items': [{'price': 'price_1Qf5qKCoTIfwbn28OC1QgABk',
    'quantity': 1}]
  },
  'discounts': [{'coupon': 'RUMSnBKh'}],
  'expand': ['discounts']
}
i = stripe.Invoice.create_preview(**data)

Still had empty array for i.lines[0]['discounts']

#

(but discount_amounts had a positive value, e.g. the discount applied to the line)

simple rose
#

Interesting. I do not know why that would be. Consulting my colleagues

ionic isle
#

If you're able to figure out when I could expect a value for InvoiceLineItem.discounts (docs link), that'd be great, but I think I have the info needed to finish my current implementation.

But it'd be nice to be able to just loop through invoice.items in an HTML template and have all the information to show e.g. "$150.00 $0.00" (e.g., 1) whether the line actually has a discount applied and 2) what the final price of the line is, instead of having to do subtraction between the Stripe API request and rendering the template).

As a final question, can you offer any guidance for when I should use Invoice.create_preview vs Invoice.upcoming, or if one of them is a better fit for showing price information to a user in a checkout / shopping cart page?

silk moonBOT
eternal marten
#

Hi ๐Ÿ‘‹

I'm stepping in as my colleague needs to go soon. So, if I read this correctly, the issue is that while you do see discounts applied in the top level discoutns property, the line_item specific discount_amounts property is not being populated?

ionic isle
#

no worries ๐Ÿ™‚ For context, all my questions are currently with respect to Invoice.upcoming. I think this is a good summary of our findings and my remaining questions:

Findings:

  • Invoice.lines.discountable is False for prorations, but otherwise is basically always True, regardless of if the line has discounts actually applied to it

Questions:

  • Under what circumstances is Invocie.lines.discounts anything other than an empty list? I've used Invoice.upcoming with applied discounts and without them, even with expand=['discounts'] and I never get anything other than an empty list.
  • Is there any recommendations on when to use Invocie.create_preview vs Invoice.upcoming? My use case is displaying a shopping cart with prices, prorations, discounts, etc.

Enhancements:

  • It would be nice if Invoice.lines would have an indicator of whether a discount was actually applied to the line
  • It would be nice if i didn't have to calculate the line's subtotal using e.g. amount - discount_amounts[0]['amount']
eternal marten
ionic isle
#

๐Ÿค”

#

Invoice.upcoming should be the first, I'm using the python lib

eternal marten
#

Ack. Just wanted to confirm because we added a new API that allows users to persist the Invoice preview

ionic isle
#

So both create_preview and upcoming seem to have empty discounts arrays. can you see the requests if I give you their ids?

eternal marten
#

Yes but for the GET request I can only see the parameters you pass, not the data returned

ionic isle
#
  • req_Z1bBHHhsQyMiNt (POST create_invoice)
  • req_M8jMtCo6OsYpxI (GET upcoming)
eternal marten
#

Okay taking the first request first, I do see the discount_amounts populated with data

ionic isle
#

Corrct, discount_amounts has data, but discounts is just an empty list. Is it maybe being depreciated? It doesn't really matter to me, I just was curious what it should respond, and it it would help me detect if 1) a discount applies to the line (since discountable seems to basically always be True for non-prorations) or 2) would provide me the "final" price of the line

eternal marten
#

My colleague has suggested that the Invoice.lines.discounts is only populated when you apply the discount to specific line items. In your case, you are applying the discount at the Invoice level.

The amount is deducted from each line item (populated discount_amounts) but since the Discount object is applied at the Invoice level, that is where you can find the data.

ionic isle
#

Oh, interesting. So currently i'm doing e.g.

stripe.Invoice.upcoming({
  'subscription_items': [{'price': 'price_1Qf5qKCoTIfwbn28OC1QgABk', 'quantity': 1}],
  'discounts': [{'coupon': 'RUMSnBKh'}],
}]

and letting Stripe figure out how/where/if to apply the discount (I'm just passing on the coupon value from a form in the ShoppingCart.

But, if I did the logic to determine if the promocode/coupon applies on my end, and specify it in e.g. subscription_items['discounts'], I'd probably get something back.

#

Yup, seems to be the case.

eternal marten
#

Sorry but I think I'm missing a key part here. Is there a specific reason the details in discount_amounts is not sufficient?

ionic isle
#

Well, the details in discount_amounts would be non-empty and "0" for a line in my invoice that the coupon doesn't apply to

#

I didn't know if there was something else I could check. And I didn't know if discounts would tell me the "final" amount, not just the discount amount. (e.g., for an item that cost $100 and a discount of $20, it seems like if i want to display "$100 $80" in my HTML template I have to do the calculation myself).

eternal marten
#

True, and include logic to handle whether situations where you use either percent_off or amount_off

ionic isle
#

Yeah, but my understanding is that discount_amounts is a calculated value in cents, regardless of percent_off or amount_off. So that's nice. It would just be nice if Stripe was able to send the "$80" number instead of "$100" and "$20", but no worries ๐Ÿ™‚

One sec I'll show a final example

#

In this one, I specified the discount on the invoice, not the item/line: req_4dIWXwyf6zF1o8. So the response has a discount_amounts for both items, one of which is 0 (since the coupon only applies to one item).

In this one, I specify the discount on the subscription_item: req_U7z5jmatDuu41x, so the response only has discount_amounts for the relevant line.

I suppose this all makes sense. But it turns out that I either need to:

  1. Validate the coupon on my end and only send it in the appropriate subscription_item or
  2. Send the coupon on the invoice as a whole (and let Stripe validate it), but then I'd have to check if discount_amounts[i]['amount'] is nonzero.
eternal marten
#

Yes that all matches what we would expect. I realize it makes your integration a little more complicated (since you have to figure out if your coupons apply for each line item). But on our side, we are trying to do exactly what you tell us. If you apply a discount at the level of the Invoice, that is where we store the data. If you apply it at the item level, then we put it there.

ionic isle
#

Yeah, that all makes sense, I just had to walk myself through it. Thanks!

#

On an unrelated note, you've been helping me for years and I really appreciate it! One last question: any guidance on when to use .upcoming vs .create_preview?

eternal marten
#

Yeah, that all makes sense, I just had to walk myself through it. Thanks!

Honestly, if I don't talk through our products when I'm integrating them with my colleagues I find I don't really understand them either.

#

One last question: any guidance on when to use .upcoming vs .create_preview?

That depends on the use case. The create_preview method corresponds to our newer API that creates an actual record that can persist on your account. This could allow you to create a preview once and refer to it multiple times.

the upcoming method dynamically calculates the data for each request and you cannot persist the data unless you store it locally

ionic isle
#

Hmm, gotcha. Good to know! So if I wanted to view a persisted preview invoice could I still just use e.g. stripe.Invoice.retrieve('upcoming_in_1QgrPlCoTIfwbn28jXUK3XDU')

#

lol it looks like Invoice.create_preview IDs start with upcoming, not to be confused with Invocie.upcoming which has no IDs?

eternal marten
#

Correct!

#

Invoice.upcoming does not create a data objecton your account so you cannot retrieve it again

#

Invoice.create_preview creates a data object with it's own ID for you to retrieve again

ionic isle
#

Okay, thanks so much ๐Ÿ™‚

eternal marten
#

Sure thing! It's why we're here. ๐Ÿ™‚

And we appreciate that you are always willing to be patient and test out different approaches when you ask complex questions here. It makes digging into these issues easier.

ionic isle
#

Of course, it's nice to be able to actually ask technical questions to tech support! Y'all are amazing. Have a wonderful day!

eternal marten
#

You too ๐Ÿ‘‹