Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve a Customer's Subscription ID from Stripe

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?

like image 595
Arw50452 Avatar asked Dec 15 '14 20:12

Arw50452


1 Answers

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.

like image 77
Robin Fisher Avatar answered Oct 02 '22 22:10

Robin Fisher