HomePage | RecentChanges | Preferences

Ruby, Rails & Stripe Checkout Guide

Stripe's [Rails Checkout Guide] assumes you already have a Ruby on Rails app and want to add Stripe for payment processing.

I didn't, so here's how I started a new Rails app from scratch and added a simple Stripe Checkout button to accept payments.

First things first: create a new Rails app called rcapp and put it under version control:

   $ rails new rcapp
   $ cd rcapp
   $ git init
   $ git add .
   $ git commit -m "Initial commit of new Rails app for Stripe Checkout"

(Your commands will be different if you're using a different version control system, or adding it to an existing repository. The important thing is to put it under version control SOMEWHERE.)

Next, add the stripe gem to your application's Gemfile

   gem 'stripe'

and run

   $ bundle install

If the stripe gem was not already installed, you'll see something like this in the output:

  Using sqlite3 1.3.9
  Installing stripe 1.15.0            <-
  Using turbolinks 2.4.0
  Using uglifier 2.5.3
  Your bundle is complete!
  Use `bundle show [gemname]` to see where a bundled gem is installed.

Note that the Stripe guide I linked to earlier suggests pointing directly at the github project. TODO: figure out if this is like using SNAPSHOTs that auto-update to the latest bleeding-edge code in the repository.

Now add the modified files and commit your changes:

  $ git add Gemfile
  $ git add Gemfile.lock 
  $ git commit -m "Add stripe gem to Gemfile"

From this point on, I will stop reminding you to commit your changes. You should do it. :)

Generate a controller for Charges

  $ rails g controller charges

and this happens:

      create  app/controllers/charges_controller.rb
      invoke  erb
      create    app/views/charges
      invoke  test_unit
      create    test/controllers/charges_controller_test.rb
      invoke  helper
      create    app/helpers/charges_helper.rb
      invoke    test_unit
      create      test/helpers/charges_helper_test.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/
      invoke    scss
      create      app/assets/stylesheets/charges.css.scss

Paste the content from the Stripe tutorial into charges_controller.rb or see [the file in my github repo].

Edit config/routes.rb as described or see -github link-.

Now you can run rake routes to see what that did:

 $ rake routes
      Prefix Verb   URI Pattern                 Controller#Action
     charges GET    /charges(.:format)          charges#index
             POST   /charges(.:format)          charges#create
 new_charge GET    /charges/new(.:format)      charges#new
  edit_charge GET    /charges/:id/edit(.:format) charges#edit
      charge GET    /charges/:id(.:format)      charges#show
             PATCH  /charges/:id(.:format)      charges#update
             PUT    /charges/:id(.:format)      charges#update
             DELETE /charges/:id(.:format)      charges#destroy

Once more with config/initializers/stripe.rb Note that I use environment variables prefixed with STRIPE_ here and below to launch the app.

And app/views/layouts/charges.html.erb at

And app/views/charges/new.html.erb

And app/views/charges/create.html.erb

Now launch the app. (At this point if you're going to do more development, it's worth adding those keys to your ~/.bash_profile so you don't have to specify them on the command line every time.)


And visit (this is important!) http://localhost:3000/charges/new

If you just visit http://localhost:3000 you will get the default home page with no mention of your checkout button.

The entire app is available here:

The Stripe tutorial then goes on to imply that this app can be trivially launched on Heroku.

Actually, that is not true, because a default Rails app uses Sqlite3, which Heroku does not support. Use Postgres instead and it will work.

HomePage | RecentChanges | Preferences
This page is read-only | View other revisions
Last edited October 23, 2014 12:57 pm by WendySmoak (diff)