【发布时间】:2013-11-01 22:03:03
【问题描述】:
我正在使用 Stripe.net 库来调用 Stripe API。
我想获得各种计划的订阅者总数,但我不确定当前的 API 和/或 Stripe.NET 库是否可以做到这一点。
谁能提供任何关于这是否可能的见解?
【问题讨论】:
标签: c# .net stripe-payments stripe.net
我正在使用 Stripe.net 库来调用 Stripe API。
我想获得各种计划的订阅者总数,但我不确定当前的 API 和/或 Stripe.NET 库是否可以做到这一点。
谁能提供任何关于这是否可能的见解?
【问题讨论】:
标签: c# .net stripe-payments stripe.net
我发现这行得通:(对不起,这是 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 的List() method in Stripe.Net)返回每个客户的完整 JSON 对象,包括他们的订阅和计划信息。您可以轻松地遍历它并建立您的订阅者数量列表。
请注意,如果您有很多用户,则必须分块检索客户列表。 API 调用的上限为 100 条记录(默认为 10 条)并接受偏移量。为了方便遍历列表,Stripe 的 JSON 响应中的 count 属性是客户记录的总数。
所以对于一个基本的大纲,你的策略是:
List()请求100条记录
List()请求100条记录,偏移100*次迭代【讨论】:
我了解您正在为 .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
【讨论】: