是否可以从 Stripe API 获取计划的订阅者数量

本文关键字:计划 获取 Stripe API 是否 | 更新日期: 2023-09-27 18:36:45

我正在使用 Stripe.net 库来调用 Stripe API。

我想获得各种计划的订阅者总数,但我不确定当前的 API 和/或 Stripe.NET 库是否可以做到这一点。

谁能提供任何关于这是否可能的见解?

是否可以从 Stripe API 获取计划的订阅者数量

我发现这有效:(对不起,这是PHP)

$subscriptions = 'Stripe'Subscription::all(array('limit' => 1, 'plan' => 'plan-name-here', 'status' => 'trialing|active|past_due|unpaid|all', 'include[]' => 'total_count'));
echo $subscriptions->total_count;

没有直接要求,但很容易完成。

"列出所有客户"API 调用(StripeCustomerService Stripe.Net 中的List()方法)返回每个客户的完整 JSON 对象,包括其订阅和计划信息。您可以轻松迭代并构建订阅者计数列表。

请注意,如果您有很多用户,则必须以块为单位检索客户列表。API 调用上限为 100 条记录(默认值为 10),并接受偏移量。为了便于遍历列表,Stripe JSON 响应中的count属性是客户记录的总数

因此,对于基本大纲,您的策略是:

  1. 通过List()请求 100 条记录
  2. 计算所需的额外请求数
  3. 处理最初的 100 条记录
  4. 通过 List() 请求 100 条记录,偏移 100 * 迭代
  5. 处理当前 100 条记录
  6. 重复4和5,直到记录用尽
我知道

您正在为 .NET 实现这一点,但这里有一个示例 Ruby 实现:

limit = 100
last_customer = nil
while limit == 100 do 
  customers = Stripe::Customer.list(limit: limit, starting_after: last_customer)
    # Do stuff to with customers
    # the list is in customers.data
    # save the last customer to know the offset
    last_customer = customers.data.last.id 
  end
end

如果您使用的是Stripe.net包,则可以使用 StripeSubscriptionService 获取计划的订阅列表。因此,您无需遍历所有客户。

var planService = new StripePlanService();
var planItems = planService.List(new StripeListOptions()
{
  Limit = 10 // maximum plans to be returned
});
foreach(var planItem in planItems)
{
  var subscriptionService = new StripeSubscriptionService();
  var stripeSubscriptions = subscriptionService.List(new StripeSubscriptionListOptions
  {
    PlanId = planItem.Id
  });
  // Do your calculation here
}

他们现在在他们的网站中有更好的 .NET 文档。您可以在此处找到完整的信息 https://stripe.com/docs/api/dotnet