Rails Stripe Configuration


  1. Using Rails for API only
  2. Stripe Ruby Github
  3. Dotenv Ruby Github
  4. Scaffolding routes
  5. HTTP Requests in Rails Apps
  6. Action Controlller Overview

Getting Started

Assuming you have Rails installed, run the following:

rails new ruby-rails-stripe cd ruby-rails-stripe

Add the following to the top of your Gemfile for us to read local dotenv values and bundle Stripe.

gem 'dotenv-rails', groups: [:development, :test] gem 'stripe'

On the console, run bundle.

Scaffolding the Charges Route

From the console run:

rails generate controller Charges create

This will scaffold our app/controllers/charges_controller.rb controller.

Inside that, let's update the code:

require 'stripe' class ChargesController < ApplicationController # POST /charge # POST /charge.json def create # `source` is obtained with Stripe.js; see https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token charge = Stripe::Charge.create({ amount: params[:amount], currency: 'aud', source: 'tok_amex', receipt_email: params[:receipt_email], description: 'My First Test Charge (created for API docs)', }) render json: charge end end

This code will make a charge to Stripe using the JSON body params amount and receipt_email.

If the charge is successful, it will return the charge information as JSON.

Updating config/routes.rb

Ensure routes has the following for POST:

Rails.application.routes.draw do # ... the rest is omitted for brevity post 'charges/create' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end

This ensures that we can send a POST request to http://localhost:PORT/charges/create when we run the server.

Running the code

Run rails server to get our server up and running (defaulting to 3000), then call http POST http://localhost:3000/charges/create amount:=1700 receipt_email=hello_rails@example.com (using HTTPie) and we will get back our charge results sent as JSON. Hooray!