简体   繁体   English

如何使用条带API检索客户总数(没有“限制”)

[英]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. 我正在使用PHP条带API来检索特定条带帐户的所有客户的列表。 I just need the email address of the customer object. 我只需要客户对象的电子邮件地址。

The following function works well however it is only returning 10 customers. 以下功能运行良好,但它只返回10个客户。

  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 (ie \\Stripe\\Customer::all(array("limit" => 3)); ) is optional and the "default limit is 10". 这里阅读有关API之后,它告诉我“limit”参数(即\\Stripe\\Customer::all(array("limit" => 3)); )是可选的,“默认限制为10”。 So I guess this is why it is returning only 10 customers. 所以我想这就是为什么它只返回10个客户。

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. 为此,请在请求中指定include [] = total_count。

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. 谢谢您的帮助。

Stripe does not support getting the total count of objects anymore as the feature has been deprecated for a while. 由于该功能已被弃用一段时间,Stripe不再支持获取对象的总数。

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. 它不会立即将所有客户存储在$ customers中,只存储一页。 But when you reach the last one in that page the iterator will automatically fetch the next page for you. 但是当你到达该页面中的最后一个时,迭代器将自动为你提取下一页。

Not exactly for the Customer object, but I was working with the Event object and got it to retrieve all the Event (more than the 100 limit) by writing a recursive function in javascript. 不完全适用于Customer对象,但我正在使用Event对象并通过在javascript中编写递归函数来检索所有事件(超过100个限制)。 Notice how I am using the 'hasMore' field to pull in the next set of 100 Events until 'hasMore' == false. 注意我是如何使用'hasMore'字段来引入下一组100个事件,直到'hasMore'== false。

  const stripe = require('stripe')(process.env.STRIPE) module.exports = async function importCanceledSubscription ({$models}) { const {StripeEvent} = $models async function createEvents (last_id) { const {events, hasMore, nextId} = await getStripeEvents(last_id) let error = false for (const e of events) { const stripeObj = new StripeEvent stripeObj.id = e.id stripeObj.object = e.object stripeObj.api_version = e.api_version stripeObj.created = e.created stripeObj.type = e.type stripeObj.livemode = e.livemode stripeObj.pending_webhooks = e.pending_webhooks stripeObj.request = e.request stripeObj.data = e.data try { await stripeObj.save() } catch (e) { console.log('duplicate found') error = true break } } if (hasMore && !error) { await createEvents(nextId) } } await createEvents() } function getStripeEvents (last_id) { return new Promise((resolve) => { stripe.events.list({limit: 100, type: 'customer.subscription.deleted', starting_after: last_id}, (err, event) => { if (err) console.log(err) const events = event.data const nextId = event.data[event.data.length - 1].id resolve({nextId, events, hasMore: event.has_more}) }) }) } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM