5

I'm having a problem to adding invoice item in monthly invoice. Here is my billing implementation.

After getting an invoice.created webhook from the Stripe, I calculate the extra payment amount and trying to update the current invoice, by adding invoice item. Here is the code:

stripe.invoiceItems.create({
   'customer': 'cus_00000000',
   'amount': 2000,
   'currency': 'usd',
   'description': 'additional fee'
}, function (error, invoice) {
   if (error) {
     throw new Error('Error creating invoice item');
   }
   return Promise.resolve(invoice);
});

As the result Stripe creates an Invoice item, and adds it to the next upcoming invoice. The issue is, that I need to add the extra payment line to the current created invoice. My question is, is there any way to update pending invoice, not the upcoming.

Danis
  • 1,978
  • 3
  • 16
  • 27

1 Answers1

9

When you create the invoice item via the API, you can pass the optional invoice parameter which tells Stripe which invoice to attach the invoice item to. If you don't pass one, it will simply stay pending until the future invoice.

Since you want to add it to the invoice that was just created, make sure to pass that invoice's id in the invoice parameter:

stripe.invoiceItems.create({
   'customer': 'cus_00000000',
   'amount': 2000,
   'currency': 'usd',
   'description': 'additional fee',
   'invoice': 'in_1234'
}, function (error, invoice) {
   if (error) {
     throw new Error('Error creating invoice item');
   }
   return Promise.resolve(invoice);
});
koopajah
  • 23,792
  • 9
  • 78
  • 104
  • I have like 50 invoiceItems by Invoice… It's a lot of requests to Stripe. Is there a way with Stripe to create the Invoice and the 50 InvoiceItems in just one HTTP request? – Dam Fa Jul 12 '22 at 03:17
  • No that is not something Stripe supports at the moment and you'll have to create those one by one. Alternatively, charge for the total amount and design your own invoice/receipt instead. – koopajah Jul 17 '22 at 18:08