Using Checkout and Sinatra

This tutorial will help you integrate Stripe and Ruby's Sinatra framework.

We’re going to create a simple application using Sinatra and Stripe’s Checkout to accept payments in a few lines of code. Sinatra and Stripe are extremely well suited to each other, and the end result of this tutorial is elegant and compact.

You’ll need to have the following prerequisites before we go any further:

We’re creating two Sinatra settings, :publishable_key and :secret_key which are being pulled out of the current environmental variables. We’re not hardcoding these keys, since it’s often bad practice to put sensitive data into source control.

Next, let’s create some Sinatra methods, to first display our payment form, and then to create charges.

In our /charge method, we’re creating a Stripe::Charge object with various POST parameters. Stripe expects charges to be in cents, so we’re converting the amount parameter by multiplying it by a hundred.

Stripe Charges also take an optional :description parameter, which in this case is Sinatra Charge. This is not something that’ll display in people’s statements, but just in Stripe’s management interface.

Finally, we’re setting the :source property to the stripeToken parameter. This is something that is automatically created for us by Checkout, which we’re going to cover next.

Some payments could fail, for a variety of reasons such as an invalid CVC, card number or failed charge. We can cater for this in a basic manner for the time being, and catch any CardError exceptions.

Next, let’s create our :index template, which is going to have the credit card form, amount and description. Notice we’re using Checkout, which will display a credit card overlay which includes validation and error handling.

So that’s it, complete Stripe and Sinatra integration in about 60 lines of Ruby and HTML! You can see the full example here. Let’s start the server, making sure to set the environmental variables we used earlier to Stripe’s publishable and secret keys. For now, let’s use the test keys, rather than the live ones.

Now, if we navigate to http://localhost:4567, we should see our payment form ready to use. If you’re using Stripe’s test keys, then we can test it with some dummy data. Enter the test number 4242 4242 4242 4242, a three digit CVC and any expiry date in the future. Submitting the form should bring up our successful charge page.

Deploying to Heroku

So now we’ve created our basic payment page, let’s get it deployed somewhere so people can use it! Heroku is an awesome way to do this, and integrates seamlessly with Sinatra and Rack. Firstly, get a Heroku account, and install the Heroku Toolbelt. We then need to setup a few things, so Heroku knows what gems we’re using. Firstly, we’ll to create a Gemfile for Bundler:

source 'http://rubygems.org'
gem 'sinatra'
gem 'stripe'

Remember to run bundle install to create the Gemfile.lock. Next, let’s add a file called config.ru for Rack:

require './app'
run Sinatra::Application

That’s all the setup required. Now we’ll push our code to Heroku and tell it to deploy our application.