Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the total number of customers (without a "limit") with the stripe API

I'm working with the PHP stripe API to retrieve a list of all customers for a particular stripe account. I just need the email address of the customer object.

The following function works well however it is only returning 10 customers.

  function getListOfCustomers($stripe){
        \Stripe\Stripe::setApiKey($stripe['secret_key']);
        $list_of_customers = \Stripe\Customer::all(array());
        return $list_of_customers['data']; 

    }

After reading here about the API it tells me that the "limit" parameter (i.e \Stripe\Customer::all(array("limit" => 3)); ) is optional and the "default limit is 10". So I guess this is why it is returning only 10 customers.

I would like to return an unlimited amount of customers. I was wondering does anybody know exactly how to do this?

I read also the following on the same page:

You can optionally request that the response include the total count of all customers that match your filters. To do so, specify include[]=total_count in your request.

However it doesnt tell me exactly how to "include this in my request". I tried the following however I am receiving syntax errors.

$list_of_customers = \Stripe\Customer::all(array(), include[]=total_count);

and I also tried:

$list_of_customers = \Stripe\Customer::all(array(include[]=total_count));

Thanks for the help.

like image 604
Sarah Avatar asked Dec 23 '22 20:12

Sarah


1 Answers

Stripe does not support getting the total count of objects anymore as the feature has been deprecated for a while.

Instead, they recommend looping over all the customers to count them or find a specific one you want. This is really easy with auto-pagination Your code would look like this:

$customers = \Stripe\Customer::all(array("limit" => 100));
foreach ($customers->autoPagingIterator() as $customer){
     echo "Current customer: $customer";
}     

It won't store all the customers at once in $customers, only a page. But when you reach the last one in that page the iterator will automatically fetch the next page for you.

like image 146
koopajah Avatar answered Dec 26 '22 16:12

koopajah