#aaron_api

1 messages ยท Page 1 of 1 (latest)

oak stratusBOT
#

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

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

jovial shoal
finite timber
#

Hello
When you say

We are wondering how that amount is created?

Are you referring to invoice amount, due amount or something else?

#

I see the invoice you've attached but could you specify the amount you're asking about?

#

More context would help be on the same page ๐Ÿ™‚

jovial shoal
#

yup lemme get you the exact numbers

oak stratusBOT
jovial shoal
#

We are wanting to refund $60.00 + taxes on $60.00
The amount should be 6533 after all calculations are done which seems correct.

However the failure is happening on Stripes side with a error
```The sum of refunds, credit amount, and out of band amount ($65.33) must equal the credit note post_payment_amount ($65.32)


We previously asked about this undocumented field / response and your team got back to us saying that we needed to pass in the gross amount in the `lines[:amount]` param

Therefore, in the lines params we pass in

{
type: "invoice_line_item",
invoice_line_item: line_item.id,
amount: 7407 # this is the calculated gross amount of $60 before discounts.
tax_amounts: credit_note_tax_amounts,
net_amount_to_refund: to_stripe_integer(amount_to_refund)
}


When we hard code the amount to 7408 the preview works. 

We are wondering how that calculation is handled on your side? and if there is a recommended way to find out the pre discount amount for a specified amount?
proud mist
#

Hello, looking in to this. When you talked to our team before, was it on this channel or somewhere else? I am having trouble finding the other conversation

jovial shoal
#

the problem is that the calculation of getting the pre discount amount (7407) is one cent off from what stripe caculates as the pre discount amount

proud mist
#

Gotcha, so the question is how to get that original amount. Is this 7407 amount on the invoice that you shared the ID of? I don't see anything with a ~7400 amount on it

#

I am reaching out to my colleagues for help looking in to this and will get back with what we can find

oak stratusBOT
proud mist
#

Still working on this. Thought we had the calculation down but are double checking some of our math.

jovial shoal
#

thanks!

#

Gotcha, so the question is how to get that original amount. Is this 7407 amount on the invoice that you shared the ID of? I don't see anything with a ~7400 amount on it

No this is the amount that we want for a partial discount

#

but we need to derive the pre discount amount

#
  • we want to refund $60 out of the $508.73 on the line item.
  • there is a 10% subscription discount + 10% product level
  • We need to find the pre discount amount of $60.00 which according to our caclulation is $74.07 however it is failing validation on stripe's side because stripe believes it is $74.08 which causes the post_payment_amount to be $65.32in the error message above
#

if we hard code the amount as 7408 in the request, the request will be successful. however we do not know how to get to that number

proud mist
#

Gotcha, and how are you calculating this amount currently?

jovial shoal
#

net_amount_to_refund * line_item_full_amount / line_amount_after_discounts

  • this seems to work for like 90% of the cases but every once in a while we get a 1 cent error
proud mist
#

Thank you, and how are you calculating line_amount_after_discounts specifically?

jovial shoal
#

we grab the full amount of the line and substract it by the discount_amounts on the line

#

another thing we tried was getting the ceiling of the calculation above and it gives some sort of weird error as well.

We eventually debugged and found out that the amount we calculate above is 1 cent different than what stripe expects it to be.

slender skiff
#

๐Ÿ‘‹ taking over for my colleague who needs to step away. Give me a few minutes to catch up please!

jovial shoal
#

thanks!

slender skiff
#

Okay, I understand how we're calculating 65.33

#

The first Invoice line item has both a 10% line item discount and a 10% group discount. The second Invoice line item has just the 10% group discount.

#

This appears to be the formula we're using:
3704 - 3704 * 0.1 - (3704 - 3704 * 0.1) * 0.1 + 3333 - 3333 * 0.1 + 120 + 135 + 11 + 120 + 135 + 11

jovial shoal
#

yup that is what we calculated it as as well.

#

the error we get from stripe is The sum of refunds, credit amount, and out of band amount ($65.33) must equal the credit note post_payment_amount ($65.32)

#

post_payment_amount is coming from stripe and is calculating to $65.32 ๐Ÿค”

slender skiff
#

Nope, that's not it

jovial shoal
#

oh weird

slender skiff
#

65.33 is coming from Stripe

jovial shoal
#

hmm okay wait so lets go with your theory for now

jovial shoal
slender skiff
#

Ah, I think I see what's going on. We're immediately rounding the discount amounts after multiplying

#

So, using order of operations, we're ending up with:
3704 - 370 - 333 + 3333 - 333 + 120 + 135 + 11 + 120 + 135 + 11 = 6533

#

If you round until the end:
3704 - 370.4 - 333.6 + 3333 - 333 + 120 + 135 + 11 + 120 + 135 + 11 = 6532

jovial shoal
#

okay so I verified we are calculating 6533 correctly in our code.

#

we're still getting the same error ``` The sum of refunds, credit amount, and out of band amount ($65.33) must equal the credit note post_payment_amount ($65.32)

#

I think it means stripe is rounding at the end then?

slender skiff
#

Do you have a request ID for that credit note creation request?

jovial shoal
#
  "expand": {
    "0": "tax_amounts.tax_rate"
  },
  "invoice": "in_1RdINiDAWTp2PXOww3zbsH1s",
  "lines": {
    "0": {
      "amount": "7407",
      "invoice_line_item": "il_1RdINiDAWTp2PXOw1WEqE1aB",
      "tax_amounts": {
        "0": {
          "amount": "240",
          "tax_rate": "txr_1ROjB9DAWTp2PXOwTgDRrY7E",
          "taxable_amount": "6000"
        },
        "1": {
          "amount": "270",
          "tax_rate": "txr_1ROjB9DAWTp2PXOwzXolntD7",
          "taxable_amount": "6000"
        },
        "2": {
          "amount": "23",
          "tax_rate": "txr_1ROjB9DAWTp2PXOwAqLqKR8j",
          "taxable_amount": "6000"
        }
      },
      "type": "invoice_line_item"
    }
  },
  "memo": "test",
  "metadata": {
    "tax_amount_1": "240",
    "tax_amount_2": "270",
    "tax_amount_3": "23",
    "tax_netsuite_id_1": "14956",
    "tax_netsuite_id_2": "14956",
    "tax_netsuite_id_3": "14956"
  },
  "refund_amount": "6533"
}
slender skiff
#

ah okay, this helps. Give me a min

jovial shoal
#

thank you! really appreciate the help! ๐Ÿ˜„

#

pls note that the 7407 is the PRE discount amount that I was advised to submit as params for the API to work.

I notice that if I hardcode 7408, the preview and creation will be successful which leads me to believe it has something to do with how stripe calculates the pre discounted amount

slender skiff
#

Okay so we're doing:
7407 - 7407 * 0.1 - (7407 * 0.1) * 0.1 + 240 + 270 + 23

We're rounding at the discount calculation so:
7407 - 741 - 667 + 240 + 270 + 23 = 6532

jovial shoal
#

oh interesting

oak stratusBOT
jovial shoal
#

hmm okay so I need to figure out how to get that amount then ๐Ÿค”

#

okay I'm really confused now

#

I figured out how to submit it as 6532

#

but the validation error changed again to

#

The sum of refunds, credit amount, and out of band amount ($65.32) must equal the credit note `post_payment_amount` ($65.31).

#

heres the new request

slender skiff
jovial shoal
#

ah okay
req_8XGZ40pDAfdxWj

#

this is the new one that I just made

slender skiff
#

Nope, that's the old one

jovial shoal
#

oh yeah my bad its this one
req_NG0hIdwARNcXez

#

yeah ๐Ÿ‘

slender skiff
#

ah, I see the difference

#

The old request used amount 23 for txr_1ROjB9DAWTp2PXOwAqLqKR8j. The new request used amount 22 for txr_1ROjB9DAWTp2PXOwAqLqKR8j

#

old request: 7407 - 741 - 667 + 240 + 270 + 23 = 6532
new request: 7407 - 741 - 667 + 240 + 270 + 22 = 6531

jovial shoal
#

hmm okay lemme check

#

okay so my question is -- how do we come to the value of 7407 - 741 - 667 if we want to refund 6000?

#

if you calculate that it ends up being 5999 not 6000

slender skiff
#

Taking a step back, why do you want to refund 6000 (+ tax)?

jovial shoal
#

one of the requirements is to be able to be creating credit notes partially

slender skiff
#

Got it, so not for the total amount of an invoice line item

jovial shoal
#

yup exactly.

#

it just happens that for some reason 60 gives this error

#

it seems like 99% of the time it works

#

except for 60, 51, 42 ๐Ÿคฏ

jovial shoal
slender skiff
#

If you want to refund 6000 + tax, you should start with 7408

#

7408 - 7408 * 0.1 - (7408 - 7408 * 0.1) * 0.1 + 240 + 270 + 23

This would be
7408 - 741 - 667 + 240 + 270 + 23 = 6533

jovial shoal
#

how would we get 7408 though?

#

our current implementation gets 7407

slender skiff
#

How are you calculating that?

jovial shoal
#

net_amount_to_refund * line_item_full_amount / line_amount_after_discounts

slender skiff
#

so in this case, does net_amount_to_refund = 60? can you plug in the numbers you're using here to get to 7407?

jovial shoal
#

yep

#

net_amount_to_refund = 6000

#

line_item_full_amount = 8900

#

line_amount_after_discounts = 7209

#

6000 * 8900 / 7209

#

= 7407

slender skiff
#

How are you calculating line_amount_after_discounts

#

Oh, I got it nevermind

#

It's 8900 - 890 - 801

jovial shoal
#

yeup

#

so I think I understand what the problem is now

#

We pass in the amount as 7407 and stripe does the automatic reduction of the discounts that ends up being 5999 + tax = 6532

#

however on our side , the user enters 6000 and we expect it to add the tax which ends up being 6533

#

so the question is:

  • how do we get 7408 from 6000 before discounts?
#

unless this is something on stripes end that can be fixed?

slender skiff
#

Likely not in the short term. I think what you're ultimately looking for here is the ability to pass a flat amount to refund while ignoring any discount amounts from the original Invoice

#

That way we don't do any additional calculation on our end for post_payment_amount

#

It might be clearer to see how net_amount_to_refund * line_item_full_amount / line_amount_after_discounts might not capture all scenarios if you try with some round numbers, e.g. an Invoice with a line item of 100, a line item discount of 10% and a group discount of 10%, then try to calculate a partial refund of $5

jovial shoal
#

hmm okay is there any recommended path forward then?

slender skiff
#

I think you can play around with this a bit, especially with rounder numbers. You might have to introduce some conditionals when using the result of net_amount_to_refund * line_item_full_amount / line_amount_after_discounts to calculate the amount pre tax. If the amount doesn't equal what the user entered, adjust

jovial shoal
#

alright sounds good! thank you so much for the help ๐Ÿ‘

slender skiff
#

Thank you for going through this together!