Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
91 lines (74 sloc) 2.29 KB
# A simple Sinatra app showing how to use Stripe Checkout with Chargify
# To start: `ruby sinatra-checkout.rb` and visit http://localhost:4567
# References:
require 'sinatra'
require 'stripe'
require 'chargify_api_ares'
Chargify.configure do |c|
c.api_key = ENV['CHARGIFY_API_KEY']
set :publishable_key, ENV['STRIPE_PUBLISHABLE_KEY']
set :secret_key, ENV['STRIPE_SECRET_KEY']
Stripe.api_key = settings.secret_key
get '/' do
erb :index
post '/charge' do
# "Checkout doesn't actually create charges—it only creates tokens. You can use those tokens to create the actual charge on your server. Or you can save the card for charging later," (which is what we're doing here.)
customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:card => params[:stripeToken]
card =[0]
# Now use the Stripe Customer ID as the Chargify `vault_token` to set up a recurring subscription that will be charged to the saved card.
subscription = Chargify::Subscription.create(
product_handle: 'monthly-plan',
customer_attributes: {
first_name: "Valued",
last_name: "Customer",
email: params[:stripeEmail]
credit_card_attributes: {
current_vault: 'stripe',
first_name: "Valued",
last_name: "Customer",
card_type: card.brand.downcase,
last_four: card.last4,
expiration_month: card.exp_month,
expiration_year: card.exp_year
erb :success
error Stripe::CardError do
@@ layout
<!DOCTYPE html>
<%= yield %>
@@ index
<form action="/charge" method="post" class="payment">
<label class="amount">
<span>$30.00 per month</span>
<script src="" class="stripe-button"
data-key="<%= settings.publishable_key %>" data-label="Subscribe!">
@@ success
<h2>Thanks, you paid <strong>$30.00</strong> today and we'll charge you again next month!</h2>