Barry MSIH
Barry MSIH

Reputation: 3787

How do you retrieve inactive (cancelled) subscriptions from Stripe

I want to confirm that a customer subscription has been cancelled. The Stripe API documentation only refers to returning "active" subscription. How can I get a listing of all the subscriptions?

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}

Upvotes: 17

Views: 11019

Answers (3)

Avatar
Avatar

Reputation: 15166

For 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;
    // ...
}

With a canceled subscription, the most recent, unpaid invoice is closed, and no further invoices are generated. A customer.subscription.deleted event is triggered, following the charge.failed and invoice.payment_failed events that preceded it. You can see that a subscription was canceled automatically—as opposed to by your request—if the customer.subscription.deleted event’s request property is null.

Upvotes: -1

beth9watson
beth9watson

Reputation: 106

As of the 7/06/16 API update:

You can now view canceled subscriptions by specifying status=canceled or status=all when listing subscriptions. In addition, you can now retrieve a canceled subscription by its ID.

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

So if you're using the ruby API, for example, you should be able to fetch canceled subscriptions like this:

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

And get a list of all subscriptions, as you ask for above, like this:

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

Upvotes: 9

Larry Ullman
Larry Ullman

Reputation: 2271

Sorry for the confusion here! Stripe only returns active subscriptions; the API does not return canceled ones. However, you'll know that a subscription was canceled by watching for that event in your webhook URL. And, if the cancelation request is being made by your site (as opposed to an automatic cancelation due to payment failure), we'd throw an exception if that request failed.

Hope that helps, Larry

PS I work on Support at Stripe.

Upvotes: 4

Related Questions