Stripe Payments in Rails

Will Holcomb

12 June 2013

Background

Stripe is an online credit card processing service. Stripe charges 2.9% + 30ยข per charge. One of the primary advantages of Stripe is card verification takes place in the browser so the only information stored on the server is authentication tokens.

Quick Start

A sample of the application is running at http://synaptian-stripe-example.heroku.com.

Setup

First, sign up for a Stripe account. Under the "Your Account" menu in the upper right hand corner select "Account Settings." From there choose "API Keys" and this will give you the keys for accessing your account. Save these in an initializer:

config/initializers/stripe.rb

Using environment variables for your keys allows you to avoid recording them on disk. To specify them when starting a server do:

STRIPE_SECRET_KEY=sk_test_… rails s

Next, include the javascript in your application and add error notifications.

app/views/layouts/application.html.erb

Next add the appropriate gems for authentication and Stripe.

Gemfile

After running bundle install the gems are installed. Next we run the following commands to set up the payments model.

This generates the product views. We begin by editing the payment form. This payment form includes saved card info.

app/views/payments/_form.html.erb

Next we add javascript tp the page to handle authenticating cards.

app/assets/javascripts/payments.js

Finally is the controller to handle the charge creation logic.

app/controllers/payments_controller.rb