-1

I am trying to write a ruby rails rake task that lists all the stripe customers whose subscriptions have expired? I have looked at the stripe api but do can't see how I can retrieve this information.

Kind Regards

Gurdipe

  • As far as I know you can't do this in stripe unless you loop through each customer and check the status for each subscription that this customer might have. In your case a customer has one subscription or more? – rmagnum2002 Jan 19 '15 at 12:06
  • Thank you for the quick update, the customers we have I think only have one subscription each. – Gurdipe Dosanjh Jan 19 '15 at 13:24

1 Answers1

0

The best thing I could think of is to get the customer subscription statuses from stripe for each customer:

2.1.1 :082 > a =Stripe::Customer.all.collect{|c| c.subscriptions.collect{|s| [c.id, s.status]}}.reject(&:empty?)
 => [[["cus_5VT4xC8uCoVIV5", "trialing"]], [["cus_5VT4Oh4AX74vqN", "trialing"]], [["cus_5VT4GFL6pPvsmH", "trialing"]], [["cus_5VT4uePgqT4Rrz", "trialing"]], [["cus_5VT4F8c8iK9cG8", "trialing"]], [["cus_5VT4zO0iMjoKsu", "trialing"]], [["cus_5VT4XQkAKCmRmg", "trialing"]]] 
2.1.1 :083 > 

In my case they are all demo customers so they all have trial status, next you should loop through this to save customers which subscription status is equal to active or trial and save into a variable, then using customer id from stripe you can find users in your app.

p.s. In case you need it as json (ugly, but... what can you do):

2.1.1 :111 > b = a.map{|r| "#{r[0][0]}, #{r[0][1]}"}.join(', ').split(', ')
 => ["cus_5VT4xC8uCoVIV5", "trialing", "cus_5VT4Oh4AX74vqN", "trialing", "cus_5VT4GFL6pPvsmH", "trialing", "cus_5VT4uePgqT4Rrz", "trialing", "cus_5VT4F8c8iK9cG8", "trialing", "cus_5VT4zO0iMjoKsu", "trialing", "cus_5VT4XQkAKCmRmg", "trialing"] 
2.1.1 :112 > h = Hash[*b]
 => {"cus_5VT4xC8uCoVIV5"=>"trialing", "cus_5VT4Oh4AX74vqN"=>"trialing", "cus_5VT4GFL6pPvsmH"=>"trialing", "cus_5VT4uePgqT4Rrz"=>"trialing", "cus_5VT4F8c8iK9cG8"=>"trialing", "cus_5VT4zO0iMjoKsu"=>"trialing", "cus_5VT4XQkAKCmRmg"=>"trialing"} 
2.1.1 :113 > 

update

https://stripe.com/docs/api#pagination use starting_after and ending_before params to navigate trough stripe customers records.

rmagnum2002
  • 11,341
  • 8
  • 49
  • 86
  • Thank you for the awesome update. I thought all I would need to do is find the customers with a status of "unpaid" and that the status of "unpaid" is set by stripe automatically when a payment fails is this correct? Also I have more than a 100 customers how do I use the Stripe::Customer.all to get more than a 100 customers? – Gurdipe Dosanjh Jan 19 '15 at 16:03
  • Actually to iterate thru all the customers I find this link http://stackoverflow.com/questions/19544897/is-it-possible-to-get-a-count-of-subscribers-for-a-plan-from-the-stripe-api – Gurdipe Dosanjh Jan 19 '15 at 16:34
  • See the update in the answer. Also there is no such thing as `paid` or `unaid` for customer, these attributes are for invoices, and after all attempts to get it paid the subscription becomes `inactive.` – rmagnum2002 Jan 19 '15 at 16:34
  • Again thanks for the awesome update, I am struggling to update the above commands to page thru using ther parameters Stripe::Customer.all(limit: limit, starting_after: last_customer), how can i find the last_customer id when using the collect – Gurdipe Dosanjh Jan 20 '15 at 12:49
  • When you load first 100 customers you will save them into a variable for example `customer_list`, so from this list you need to save the last one into `last_customer` variable with `customer_list.last` and use it for your next request. – rmagnum2002 Jan 20 '15 at 12:58