When calling Stripe::Customer.all(:limit => 100) there is a limit of 100 per call. We have a lot more customers than that, and I would like to get them all at once. Am I missing something, or is this only possible by writing a naive loop that checks on the has_more attribute and then makes a new call until has_more = false?
You are right, you must write a naive loop with a cursor per the stripe docs:
starting_after
optional
A cursor for use in pagination.
starting_afteris an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending withobj_foo, your subsequent call can includestarting_after=obj_fooin order to fetch the next page of the list.
Here's one in case anyone needs a quick copy-paste.
def self.all_stripe_customers
starting_after = nil
customers = []
loop
results = Stripe::Customer.all(limit: 100, starting_after: starting_after)
break if results.data.length == 0
customers = customers + results.data
starting_after = results.data.last.id
end
return customers
end
-
1Thanks for posting, was helpful! I did make one important change though, this code (almost) always makes one more query than it has to. Instead it should check
results.has_morein the while condition. – user12341234 May 25 '15 at 16:27 -
one addition - you probably want to rescue from
Stripe::RateLimitErrorand retry after waiting an appropriate amount of time – Phil Aug 16 '16 at 20:06 -
Probably a little late to the game here, but here's a yielding version of Doug's answer which loops through every customer, yielding them to a block:
def each_stripe_customer
starting_after = nil
loop
customers = Stripe::Customer.all(limit: 100, starting_after: starting_after)
break if customers.data.length == 0
customers.each do |customer|
yield customer
end
starting_after = customers.data.last.id
end
end
You can use this as follows:
each_stripe_customer do |customer|
puts customer.id
end
This abstracts away the fetching of customers from however you actually want to use them.
I would suggest to keep a local copy of customer data. so you get the data from Stripe API only once & use that local copy instead of asking that same data again-again.
For syncing changes, There are two main approaches you might use to make sure your database has the latest state:
In your code that makes API calls to Stripe, be sure you're also updating your local copy, or
Use webhooks to listen for events related to your customers (e.g.,
customer.created,customer.deletedetc). By doing this, you can be sure your local database always matches up with Stripe – even if you change your customer's plan via the dashboard, your webhook endpoint will receive the update.
The primary benefit to storing this data locally apart from getting around API limitations, is speed.
You will want to make sure you're storing the customer ID in your database so that you can match up the Stripe customer with your local user.
Hope it helps
-
2This may be good advice, but it doesn't answer the question - and you have no reason to think that the OP isn't already doing what you advise. Even if you have a local copy of the data, it's quite possible that you'll need to pull everything directly out of Stripe from time to time either to fix up bugs or gaps in recording your local copy, or for auditing purposes. Plus anyone who hasn't yet taken this advice needs an answer to the question that was asked in order to set up their local copy to start working from. This answer can't possibly help anybody on its own. – Mark Amery Sep 15 '15 at 16:24