如何从Stripe中检索无效(已取消)的订阅?

17

我希望确认客户的订阅已被取消。 Stripe API文档 只涉及返回“活动”订阅。我该如何获取所有订阅的列表?

Listing subscriptions

You can see a list of the customer's active subscriptions.

GET https://api.stripe.com/v1/customers/{CUSTOMER_ID}/subscriptions

Retrieving a customer's subscription

By default, you can see the 10 most recent active subscriptions stored on a customer directly on the customer object, but you can also retrieve details about a specific active subscription for a customer.

GET https://api.stripe.com/v1/customers/{CUSTOMER_ID}/subscriptions/{SUBSCRIPTION_ID}
3个回答

8
截至 7/06/16 API 更新:
您现在可以通过指定状态为 canceled 或 all 来列出已取消的订阅,并且还可以通过其 ID 检索已取消的订阅。

https://stripe.com/docs/upgrades#api-changelog

如果您正在使用Ruby API,例如,您应该能够像这样获取已取消的订阅:
Stripe::Subscription.all(status: 'canceled')

像您上面所要求的那样,获取所有订阅的列表,如下所示:

Stripe::Subscription.all(status: 'all')

3
当使用“canceled”或“all”作为状态时,仍会收到“Invalid status: must be one of trialing, active, past_due, or unpaid”的错误提示。 - Jude Calimbas
5
我们的大部分代码使用较旧版本的API,因此我们的Stripe账户仍设置为该版本。因此,为了在新项目中使用更新的API版本,在能够获取取消订阅信息之前,我需要调用stripe.setApiVersion(versionString)函数来设置API版本。参考链接:https://stripe.com/docs/api#versioning - Alex Egli

4
抱歉这里可能有些混淆!Stripe仅返回活动订阅;API不会返回已取消的订阅。但是,通过在您的Webhook URL中观察该事件,您将知道订阅已被取消。而且,如果取消请求是由您的网站发起的(而不是由于付款失败而自动取消),如果该请求失败,我们会抛出异常。
希望这可以帮助您。 Larry
PS:我在Stripe的支持部门工作。

22
这似乎是糟糕的设计 - 如果我们直接通过ID查询,至少应该能够通过API检索被取消的订阅。 - Mark Amery
2
在Python库中,返回的异常是:stripe.error.InvalidRequestError。拥有一个更明确的异常,比如stripe.error.CancelledSubscription会更有帮助。 - François Constant
2
我相信这个说法在2016年已经不再适用了。请参考:https://stripe.com/docs/upgrades#2016-07-06。 - hirowatari
这对我来说也很困惑。我能够通过仪表板检索订阅,但无法通过控制台检索... - knagode

-1

对于PHP:

require_once('stripe/init.php');

\Stripe\Stripe::setApiKey(YOUR_STRIPE_SECRETKEY);

// Retrieve the request's body and parse it as JSON
$input = @file_get_contents("php://input");
$event_json = json_decode($input);

if($event_json->type=='customer.subscription.deleted')
{
    $stripe_customerid = $event_json->data->object->customer; // cus_123456
    $customer = \Stripe\Customer::retrieve($stripe_customerid);
    $usermail = $customer->email;
    // ...
}

在取消订阅后,最近的未付款发票将被关闭,并且不会生成任何进一步的发票。在 charge.failed 和 invoice.payment_failed 事件之后,将触发 customer.subscription.deleted 事件。如果 customer.subscription.deleted 事件的 request 属性为 null,则可以看到订阅是自动取消的,而不是由您的请求取消的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接