Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best practice to retrieve all customers from Stripe API into one list

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?

like image 630
user3590126 Avatar asked Apr 30 '14 16:04

user3590126


People also ask

How do I retrieve customer stripes?

Just use the following line of code you will get the customer id. $customer = $stripe->customers()->create([ 'email' => $_POST['stripeEmail'], 'source' => $_POST['stripeToken'], 'plan' => trim($plan) ]); echo $customer['id']; This will help you.

What API documentation tool does Stripe use?

By default, Stripe shows examples using the popular command line tool curl.

What makes Stripe API good?

It is built for its users, engineers Stripe's landing pages & online sandboxes play a key role at this. For each business area (Payment, Business operations, etc.), you can choose from integration guides, sample open source projects or no-code alternatives.

How does the Stripe API work?

Stripe integrations handle complicated processes. The API uses a single object to track each process. You create the object at the start of the process, and after every step you can check its status to see what needs to happen next. For instance, while completing a payment, a customer might try several payment methods.


1 Answers

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_after is an object ID that defines your place in the list. For instance, if you make a list request and receive 100 objects, ending with obj_foo, your subsequent call can include starting_after=obj_foo in 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 do
      results = Stripe::Customer.list(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
like image 91
Doug Avatar answered Oct 03 '22 14:10

Doug