#Mickey

1 messages ยท Page 1 of 1 (latest)

normal fossilBOT
languid patio
#

I can help you out! What's your question?

earnest bramble
#

but then I gotta out for a while

languid patio
#

gotcha

#

did you have the request ID for the failing credit note creation request?

earnest bramble
#

how to get that?

languid patio
earnest bramble
#

I think this one: req_IU6XBLdEhTrY4i

#

I was trying to create that credit notes then cancel the subscription after that

#

Per requirements, We would not allow to use credit balance for cancelling or downgrading

languid patio
#

And how'd you get the value for 56.27 that you passed in for the credit note?

earnest bramble
#

I use stripe.invoices.retrieveUpcoming()

languid patio
#

With which values? and can you share the full response you get back from that request?

earnest bramble
#

a moment please

languid patio
#

And which attribute are you looking at to get the 56.27?

earnest bramble
#

// Calculate the partial refund amount
let remainCents = 0;
for (let i = 0; i < invoices.lines.data.length; i++) {
const invoiceItem = invoices.lines.data[i];
if (invoiceItem.period.start == prorationDate) {
currentProrations.push(invoiceItem);
remainCents += invoiceItem.amount;
if (invoiceItem.tax_amounts)
remainCents += invoiceItem.tax_amounts[0].amount;
}
}
const refundAmount = (remainCents < 0) ? remainCents * -1 : remainCents;

#

I got them from the lines items with the prorated moment

languid patio
#

Gotcha - just to make sure I have this right, you're trying to generate a credit note to refund some amount for an immediate cancellation?

earnest bramble
#

I would say sometimes after creation may be days

#

for hours

languid patio
#

got it

#

so I believe the issue here is with how you're creating the credit note - you're passing in an invoice_line_item , which we interpret as you want to credit the ENTIRE invoice line item amount (which is 56.44 in the request that you sent over).

#

You need to be creating a custom_line_item credit note instead

earnest bramble
#

says: I have to items that make up the total refund

#

How should it be for this case?

#

const creditNote = await stripe.creditNotes.create({
invoice: paidInvoices.data[0].id,
lines: [
{ type: 'custom_line_item', description: "Unused times for item A", quantity: 1, amount: 4965 }
{ type: 'custom_line_item', description: "Taxes for Unused times for item A", quantity: 1, amount: 645}
]
})

#

Do you think this will work?

#

Will it create a corresponding refund and will the invoice includes the tax as a separated line as well?

languid patio
#

I don't think you'd need the separate line item for the tax - what you'd want to do is tweak your code calculating the refund amount to not include tax like this

// Calculate the partial refund amount
  let remainCents = 0;
  for (let i = 0; i < invoices.lines.data.length; i++) {
    const invoiceItem = invoices.lines.data[i];
    if (invoiceItem.period.start == prorationDate) {
      currentProrations.push(invoiceItem);
      remainCents += invoiceItem.amount;
    }
  }
  const refundAmount = (remainCents < 0) ? remainCents * -1 : remainCents;

and then generate the credit note for that amount

#

The credit note should automatically incorporate the tax for you

earnest bramble
#

OK, I'll try right now

#

Unluckily I got this error: message:
'Custom line items are not supported for invoices with automatic_tax.enabled = true.'

#

const line1 = paidInvoices.data[0].lines.data[0];
const creditNote = await stripe.creditNotes.create({
invoice: paidInvoices.data[0].id,
lines: [
{ type: 'custom_line_item', description: "Unused times for item A", quantity: 1, unit_amount: 4965 }
],
});

#

req_r6W3lV2kjE7Wko

languid patio
#

dang - I was hoping that would work

#

GIve me a few minutes to check some things

earnest bramble
#

Sure, Thanks karbi

languid patio
#

Sorry that took a while - had to try a few things out

#

So I'd go back to using invoice_line_item (not custom line items) but set amount instead of quantity. And the amount that you set for the line item should be equal to the amount you calculate w/o tax

normal fossilBOT
earnest bramble
#

Ok

#

Thanks for your finding.

#

Will it still require the refund_amount param or just within the line item?

#

const line1 = paidInvoices.data[0].lines.data[0];
const creditNote = await stripe.creditNotes.create({
invoice: paidInvoices.data[0].id,
lines: [
{ type: 'invoice_line_item', invoice_line_item: line1.id, quantity: line1.quantity, unit_amount: line1.amount }
],
});

languid patio
#

Yeah you should still set the refund_amount param (which you'll want to set to the amount WITH tax)

earnest bramble
#

Ok

#

req_Cgy4dY3XoftYke

#

I went back to the original error unfortunately

#

const line1 = paidInvoices.data[0].lines.data[0];
const creditNote = await stripe.creditNotes.create({
invoice: paidInvoices.data[0].id,
lines: [
{ type: 'invoice_line_item', invoice_line_item: line1.id, amount: line1.amount } //quantity: line1.quantity,
],
refund_amount: refundWTax
});

#

This is confusing

#

Is there any other way to use refund API for this requirements of partial refund with tax?

languid patio
#

This line is the issue:
{ type: 'invoice_line_item', invoice_line_item: line1.id, amount: line1.amount } //quantity: line1.quantity,
],
Are you pulling line1.amount from the latest Invoice or the upcoming Invoice? Your request passed in 4995 (which is the cost of the original line item) instead of 4559 (which would be the refund w/o tax)

earnest bramble
#

oh my bad. Let's me check it again

#

req_wKiKzg4Wp7SFCw

#

I tried the amount:4954 (total refund without tax) but still error

#

It was because of 1 cent different

#

They are all in integer of number of cents. Do you how to avoid this?

languid patio
#

Yeah it's a rounding issue at this point - give me a bit to type this all out:

#

So if you really wanted to solve this I'd do something like this:

  • Use the upcoming Invoices API to sum up all the prorations (Without tax) you want to refund for
  • Use the preview credit note API (https://stripe.com/docs/api/credit_notes/preview) with that proration amount as an invoice line item which will calculate the automatic tax and do the rounding for you so you know the exact total amount
  • Generate the real credit note using the same proration amount and the calculated total you got from previewing the credit note
earnest bramble
#

Thanks karbi. I will try right now

#

It seems working now

#

const creditNotePreview = await stripe.creditNotes.preview({
invoice: paidInvoices.data[0].id,
lines: [
{ type: 'invoice_line_item', invoice_line_item: line1.id, amount: refundAmount } //quantity: line1.quantity,
],
});

const creditNote = await stripe.creditNotes.create({
  invoice: paidInvoices.data[0].id,
  lines: [
    { type: 'invoice_line_item', invoice_line_item: line1.id, amount: refundAmount } //quantity: line1.quantity,
  ],
  refund_amount: creditNotePreview.amount
});
languid patio
#

awesome! (and sorry this was so difficult to get working)

earnest bramble
#

cool. I really appreciate your help

#

pi_3MrhkeJxyI1MWs2T1cMW7aBQ

#

I will need to check for the refund invoice now

#

I could not find the corresponding refund invoice for this transaction

#

Will it send the receipt in production mode without manually send it from the Dashboard?

languid patio
earnest bramble
#

Ok. Got it

languid patio
#

I deleted that last message since it has your email in it and this is a public channel

#

but what questions do you have?

earnest bramble
#

Thanks for that. What does Adjusted invoice total of $0.50 means?

languid patio
#

That's just the original invoice total - the credited amount

earnest bramble
languid patio
#

Yeah so it's Amount paid (56.44) - Credited total (55.94)

#

does that make sense?

earnest bramble
#

yes, got it now

#

the used cost ๐Ÿ™‚

#

I've got two emails: 1 for the credit notes, 1 for the receipt the I manually did

#

So to confirm, Is credit notes the only way to handle refund with tax?

languid patio
#

You're welcome to refund the Payment Intent directly without a credit note, but the only way to link the refund directly to the Invoice and get a new PDF is with the credit note

earnest bramble
#

Do you mean the new PDF is the Refund file or the credit note file?

#

What we want to achieve is to refund (partially most of the cases) and clearly mentioned the tax amount for reporting liability

#

When using the refund API it only do a fixed total refund amount

languid patio
#

If you need the tax amount to be clearly separated like that then definitely use credit notes

earnest bramble
#

Please hang on with me for couple minutes as I will discuss this with our accountant as well

languid patio
#

@earnest bramble I need to head out, but if you have any other questions codename_ducchess is around and can help

earnest bramble
#

I am back. Thanks for holding on with me

languid patio
#

๐Ÿ‘ Did you need anything else?

earnest bramble
#

I think we are OK now. Great thanks for your help and patient. ๐Ÿ‘

#

I hope you have a great weekend

languid patio
#

๐Ÿ‘‹ glad I could help!