Retrieve a Customer's Subscription ID from Stripe
Asked Answered
E

3

12

In Rails, I'm attempting to implement a controller action which will allow a customer to change the plan they are subscribed to. Per the Stripe documentation, this can be done as follows:

customer = Stripe::Customer.retrieve("CUSTOMER_ID")
subscription = customer.subscriptions.retrieve("SUBSCRIPTION_ID")
subscription.plan = "premium_monthly"
subscription.save

I have each customer's ID saved in the user table in my Database, but I'm stuck on how to retrieve a customer's subscription ID given their customer ID. Each customer in this project will only have one subscription, how could I go about pulling this given their customer ID?

Externalize answered 15/12, 2014 at 20:31 Comment(0)
P
17

The relevant part of the Stripe API reference suggests that upon retrieving a customer you will have access to a subscriptions attribute containing a url at which you can access a list of subscriptions.

However, on creating a subscription, Stripe will return the subscription to you with the subscription id. I would suggest storing this in your database as an attribute on the customer model (if they will only have one subscription) or, and the method I use, is to have a Subscription model in my database which stores some of the frequently accessed data and avoids pinging the Stripe API too much.

Poplar answered 15/12, 2014 at 20:42 Comment(3)
Thanks, I took a look at the API and found the URL you mentioned. I'm still very new to working with Stripe though (and APIs in general), and am not sure how to use that URL to get the subscription ID saved to a variable.Externalize
They work just like Ruby objects. Bear in mind that the subscriptions attribute returns an array but calling customer.subscriptions.first.id will retrieve the id of the first subscription (and the only according to your question).Poplar
"Changes since API version 2019-08-14 → 2020-08-27 The subscriptions property on Customers is no longer included by default. You can expand the list but for performance reasons we recommended against doing so unless needed." stripe.com/docs/api/expanding_objectsSingley
S
11

"retrieving a customer you will have access to a subscriptions"

Was the case with the API from 2019.

Changes since API version 2019-08-14 → 2020-08-27 The subscriptions property on Customers is no longer included by default. You can expand the list but for performance reasons we recommended against doing so unless needed.

See https://stripe.com/docs/api/expanding_objects

Example code (PHP):

$stripe = new \Stripe\StripeClient(YOURSTRIPE_SECRETKEY);
$stripe_customer = $stripe->customers->retrieve($payerid, [ 'expand' => ['subscriptions'] ]);
$stripe_status = $stripe_customer->subscriptions->data[0]->status;

I am still trying to figure out the "recommended way" and will hopefully report back.

Singley answered 22/2, 2021 at 8:10 Comment(0)
E
-1

You dont need to fetch the Customer object to retrieve Subscriptions. Just use the static Subscription.search API like this :

final SubscriptionSearchResult searchResult 
    = Subscription.search(SubscriptionSearchParams.builder()
                            .setQuery("customer:'PUT_CUSTOMER_ID_HERE'")
                            .build());
...

final List<Subscription> subscriptions = searchResult.getData();
Elison answered 16/4 at 19:36 Comment(1)
Field customer is an unsupported search field for resource subscriptions. See stripe.com/docs/search#query-fields-for-subscriptions for a list of supported fields.Atory

© 2022 - 2024 — McMap. All rights reserved.