Mukesh Godara
Mukesh Godara

Reputation: 15

Stripe: Get total successfully paid invoices count

I'm using stripe for subscription. Where i need to fire an event after 3 successfully charges. For this i am using invoice.payment_succeeded webhook. But there is no key which specify the number of this recurring payment means whether it is first or second or nth charge. So how could i get the number of successfully payment made on a subscription.

Upvotes: 1

Views: 2388

Answers (3)

tg44
tg44

Reputation: 850

FYI there is an expand to it, like with the current node api;

const ret = await stripe.invoices.list(
    {
      expand: ["total_count"],
      limit: 0,
  });
console.log(ret.total_count)

It can be called with ${url}?expand[]=total_count&limit=0 too, if you need to write it by hand.

I did not found this in the documentation, but some libs using it, and it is indeed works. (But be careful, if it is not documented it could stop working too.)

Here is the docs; https://stripe.com/docs/api/pagination/search#search_pagination-total_count

Upvotes: 1

bkrnetic
bkrnetic

Reputation: 31

Yes, as it is stated in the answer by D Malan, you can use the https://api.stripe.com/v1/invoices endpoint.

Though there is one thing to keep in mind when retrieving data from Stripe.

You can only get 100 items per call. Therefore, if you had recurring payment that generates more than 100 invoices after some time, you wouldn't be able to get the total count in one call. I suggest storing invoices/payment intents to your database and then using your DB engine to count the results.

Keep in mind that you should listen to invoice.payment_succeeded event in order to keep your database in sync, if you store the invoice or payment intent beforehand, while its status is not paid or succeeded, respectively.

Upvotes: 0

D Malan
D Malan

Reputation: 11414

You can call the https://api.stripe.com/v1/invoices API endpoint with the customer's ID, the status parameter set to paid and optionally, the subscription parameter and then count how many invoices were returned.

There are some other parameters, like limit, starting_after, etc. that you can send it too.

The invoice.payment_succeeded webhook sends the invoice object in the data.object field so you should be able to get the customer and subscription values from it.

I'd recommend doing the invoices call asynchronously to ensure that the webhook call doesn't time out.

Upvotes: 2

Related Questions