Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check failed recurring subscription Stripe

How should I design an on-login middleware that checks if the recurring subscription has failed ? I know that Stripe fires events when things happen, and that the best practice is webhooks. The problem is, I can't use webhooks in the current implementation, so I have to check when the user logs in.

like image 374
litenull Avatar asked Dec 04 '22 09:12

litenull


2 Answers

The Right Answer:

As you're already aware, webhooks.

I'm not sure what you're doing that webhooks aren't an option in the current implementation: they're just a POST to a publicly-available URL, the same as any end-user request. If you can implement anything else in Node, you can implement webhook support.

Implementing webhooks is not an all-or-nothing proposition; if you only want to track delinquent payments, you only have to implement processing for one webhook event.

The This Has To Work Right Now, Customer Experience Be Damned Answer:

A retrieved Stripe Customer object contains a delinquent field. This field will be set to true if the latest invoice charge has failed.

N.B. This call may take several seconds—sometimes into the double digits—to complete, during which time your site will appear to have ceased functioning to your users. If you have a large userbase or short login sessions, you may also exceed your Stripe API rate limit.

like image 110
colinm Avatar answered Dec 23 '22 18:12

colinm


I actually wrote the Stripe support team an email complaining about this issue (the need to loop through every invoice or customer if you're trying to pull out delinquent entries) and it appears that you can actually do this without webhooks or wasteful loops... it's just that the filtering functionality is undocumented. The current documentation shows that you can only modify queries of customers or invoices by count, created (date), and offset... but if you pass in other parameters the Stripe API will actually try to understand the query, so the cURL request:

https://api.stripe.com/v1/invoices?closed=false&count=100&offset=0

will look for only open invoices.... you can also pass a delinquent=true parameter in when looking for delinquent customers. I've only tested this in PHP, so returning delinquent customers looks like this:

Stripe_Customer::all(array(
  "delinquent" => true
));

But I believe this should work in Node.js:

 stripe.customers.list(
       {delinquent:true},
      function(err, customers) {
      // asynchronously called
    });

The big caveat here is that because this filtering is undocumented it could be changed without notice... but given how obvious the approach is, I'd guess that it's pretty safe.

like image 22
Ben D Avatar answered Dec 23 '22 18:12

Ben D