Recurring Billing using Stripe API


To develop monthly subscription feature using Stripe API.


Step 1

Signup for Stripe Account. See your keys here Copy the API credentials:

Test Secret Key : sktestfIq8gFSbcYVu329
Test publishable Key : pktestI5yWcRN8OyFkr

Step 2

Install the stripe gem.

gem install stripe. 

If you are using bundler:

gem 'stripe', :git => ''

Step 3

I have all Stripe related logic in app/gateways/stripe folder in my Rails project.

Step 4

Create a Stripe plan. Playing in the irb:

require "stripe"
Stripe.api_key = "sk_test_BQokikJOvB2"

  :amount => 2000,
  :interval => 'month',
  :name => 'Amazing Gold Plan',
  :currency => 'usd',
  :id => 'gold'

The amount is in cents. The interval specifies frequency of billing for subscription. The id is a unique string that identifies this plan when subscribing a customer. It could be a string or a primary key from your own database. For more details on the fields, read the docs:

Delete a Plan

plan = Stripe::Plan.retrieve({PLAN_ID}) 

PLAN_ID is from your database or stored as a constant in your server side code.

Create a Customer

You need a token before you can create a customer. Create a token:

card = { :number => "4242424242424242", :exp_month => 8, :exp_year => 2015, :cvc => "314" }
response = Stripe::Token.create(:card => card)
puts response['id']

> tok_14Tj2w2eRoTz

Later, we will use Stripe.js in our web application to create token because it makes PCI compliance easy. Now, create a customer using the token generated in the above call as follows:

response = Stripe::Customer.create(:description => "Customer for",
                                   :card => "tok_14Tj2w2e3RoTz")
puts response
puts response.class
puts response['id']

The JSON response is :

  "id": "cus_4cz7I6SnCZIrYF",
  "object": "customer",
  "created": 1408579761,
  "livemode": false,
  "description": "Customer for",
  "email": null,
  "delinquent": false,
  "metadata": {},
at top level in "subscriptions": {"object":"list","total_count" at line 0
  "discount": null,
  "account_balance": 0,
  "currency": null,
at top level in "cards": {"object":"list","total_count" at line 1
  "default_card": "card_14Tj2w2eZvKYlCh"

Stripe::Customer id is cus_4cz7IYF. For writing tests, check this id is not nil.

Subscribe a Customer to a Plan

customer = Stripe::Customer.retrieve('cus_4cz7rYF')
subscription = customer.subscriptions.create({:plan => 'gold'})

puts subscription.class
puts subscription

The output is :

  "id": "sub_4dFhAPa595XZsN",
  "plan": {"id":"gold","interval":"month","name":"Basic","created" }
  "object": "subscription",
  "start": 1408641490,
  "status": "active",
  "customer": "cus_4cz7IYF",
  "cancel_at_period_end": false,
  "current_period_start": 1408641490,
  "current_period_end": 1411319890,
  "ended_at": null,
  "trial_start": null,
  "trial_end": null,
  "canceled_at": null,
  "quantity": 1,
  "application_fee_percent": null,
  "discount": null,
  "metadata": {}

For writing tests, check has some value. Later, we will get the Stripe token as a result of the form that is submitted to Stripe, the code will look something like this:

# Set your secret key: remember to change this to your live secret key in production
Stripe.api_key = "sk_test_BQogH4olfQ2"

# Get the credit card details submitted by the form
token = params[:stripeToken]

# Create a Customer
customer = Stripe::Customer.create(
  :card => token,
  :plan => "gold",
  :email => ""


In this article, we played in the irb to get familiar with Stripe API for charging a customer for a subscription. To make the PCI compliance easy, we need to use Stripe.js to create the Stripe token instead of creating it manually by providing the credit card value in the irb. We also need to handle form validation, notifying customers about their upcoming credit card expiry etc. We will discuss them in upcoming articles on Stripe.


  1. Stripe Examples
  2. Stripe Gem

Related Articles

Watch this Article as Screencast

You can watch this as a screencast Recurring Billing using Stripe API

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.