【问题标题】:How to retrieve the total number of customers (without a "limit") with the stripe API如何使用条带 API 检索客户总数(没有“限制”)
【发布时间】:2017-07-10 06:01:12
【问题描述】:

我正在使用 PHP 条带 API 来检索特定条带帐户的所有客户的列表。我只需要客户对象的电子邮件地址。

以下函数运行良好,但仅返回 10 个客户。

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

    }

在阅读here 关于 API 后,它告诉我“限制”参数(即 \Stripe\Customer::all(array("limit" => 3));)是可选的,“默认限制为 10”。 所以我想这就是它只返回 10 个客户的原因。

我想退货不限数量的客户。 我想知道有人知道具体怎么做吗?

我还在same page 上阅读了以下内容:

您可以选择请求响应包括与您的过滤器匹配的所有客户的总数。为此,请指定 在您的请求中包含[]=total_count。

但是它并没有确切地告诉我如何“将其包含在我的请求中”。 我尝试了以下方法,但收到语法错误。

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

我也试过了:

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

感谢您的帮助。

【问题讨论】:

    标签: php api stripe-payments


    【解决方案1】:

    Stripe 不再支持获取对象总数,因为该功能已被弃用一段时间。

    相反,他们建议遍历所有客户以计算他们或找到您想要的特定客户。使用auto-pagination 真的很容易您的代码如下所示:

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

    它不会一次将所有客户存储在 $customers 中,只会存储一个页面。但是当您到达该页面中的最后一个页面时,迭代器会自动为您获取下一页。

    【讨论】:

    • 谢谢。我会试一试,让你知道我的进展如何:)
    • 我使用了您关于自动分页的建议。非常感谢:)
    • @Sarah 那么您可能必须删除total_count,因为它必须计算您的所有客户并且最终可能会超时。
    • @sarah 很难说没有更多细节。我建议写信给 Stripe 支持:support.stripe.com/email
    • @DexterBengil 谢谢。 autoPagingIterator() 最终运行良好。我决定使用 webcron 作业(定期运行以更新我的数据库)发出 Stripe API 请求,而不是在用户注册期间发出请求。
    【解决方案2】:

    不完全适用于 Customer 对象,但我正在使用 Event 对象并通过在 javascript 中编写递归函数来检索所有 Event(超过 100 个限制)。请注意我如何使用“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})
        })
      })
    
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2019-01-04
      • 2015-12-07
      • 2021-03-24
      • 1970-01-01
      相关资源
      最近更新 更多