How to Use Stripe in Ruby on Rails App

How to Use Stripe in Ruby on Rails App

Accepting payments directly on the website is necessary for any type of e-commerce business – internet shops, marketplace platforms, selling retail goods, and every other type. The modern market provides a wide range of payment systems, and the biggest challenge is to choose the one that will best fit your business.

In international business, one of the most popular payment systems is Stripe. After years of operating on the market, it has proved itself as a convenient and reliable service. It is loved by business owners and developers for the transparency in its functionality, easy of installation and maintenance.

In this article, we will tell you about the functionality of this payment system. You will learn about the specifics of working with Stripe and its benefits for any type of online business. We will also provide a brief guide on integrating Stripe into a Rails app.

Why Choose Stripe?

The modern market provides a wide variety of payment processing systems, and choosing the right one might seem like a real challenge. Let’s see which factors you should consider when choosing a system for your business.


The system guarantees stable performance and accepts payments without delays or errors.


The process of charging the commission is transparent, there are no hidden fees.


The system has successfully operated on the market for several years and provides a secure way of collecting and storing sensitive information.


The service’s interface is user-friendly. For developers and business owners, it is easy to integrate the system to the relevant platform. Clients will definitely appreciate a simple payment process.


The payment system accepts transactions from different cards, banks, and currencies. It can also be used for any type of online business – for example, we integrate Stripe payments for our marketplaces, such as TikkTalk.

While meeting all the above demands, Stripe has become one of the world’s most trusted payment processors. Let’s find out more about this system, and learn how to use Stripe in a Ruby on Rails application.

The core functionality of Stripe

Stripe is an international system for secured internet payments with credit cards. In other words, Stripe serves as a mediator between the buyer and the seller in performing secure transactions. The service handles all issues connected with credit card payments, including secure storage of data, regular payments, and the withdrawal of funds.

Accepting credit card payments

Stripe securely collects and stores credit card information. When the buyer enters sensitive information (card number, 3-digit CVV code and expiration date), it is validated and a one-time token is created. At this time, your server makes an API query for a one-time charge. The query may contain both the basic information (credit card details, currency, and amount) and additional information usually stored as metadata.

The token is valid for a few minutes. Upon its expiration, the user needs to re-enter his or her payment details. There is also an option to save the payment details of returning customers.

When integrating Stripe to your site, you may ask users to provide their billing addresses. Though this is optional, it will be an additional step to prevent fraudulent transactions.

Billing, Invoices, Subscriptions

It is easy to charge your customers using Stripe. You can send a one-time invoice to your customers or charge them on a regular basis. The process of charging is really simple: you need to create the customer’s profile in Stripe and create an invoice on the dashboard if it is a one-time charge. In order to charge the customer on a regular basis, you’ll need to create a service and set the pricing plan.

There are two types of Stripe invoices: one-time invoices and subscriptions. Subscriptions represent the amount of money that the customer owes you for using your product.

The payments can be charged automatically or can be sent to the customer’s email for him to pay later. An automatic payment can be performed if the customer has assigned a payment source and has enough funds to be charged. If the payment fails, the system tries again, and a notification is sent to the customer according to your settings.

For customers to review the charges or to select a convenient payment method, it is possible to send an invoice via email.

Stripe Sigma

Sigma is a customizable data analytics tool that is available in every Stripe account. It allows users to analyze sales data right in the dashboard and see real-time information without the need to install any additional software. The information is accessible to the authorized users with the permissions to view the reports. Each query can also be shared among team members.

By writing simple SQL queries, users can see detailed information about their business and download the result in CSV format. It is possible to view all the previously performed queries in the History tab and to run any of them again.

There are also a number of template queries that are based on frequently used reports that can serve as a base for creating your own queries. It is impossible to modify the templates, however you can to create a duplicate and edit it in any way you need.

Stripe Atlas

Stripe Atlas is a service that allows users to remotely register a company based in Delaware, USA. The cost of the service is $500, and for this sum, the company obtains all the necessary documents for operating a business in the United States, such as an Employer Identification Number, business bank account, Stripe account for receiving payments from clients all over the world, and more. Additionally, the company owner is provided with consulting services for legal and tax issues.

The service is best suited for US-based startups or foreign companies building a global business. Businesses that are not located in the USA but target the American audience may also consider this opportunity. Due to financial and governmental factors, Stripe has restricted the usage of Atlas in some countries. You can see the full list here.

The benefits of Stripe

Stripe is famous for its high speed of payment processing and security. For business owners and developers, Stripe provides the set of convenient tools for integration with websites. Let’s see the benefits of the system for developers:

Detailed Documentation

Since the product is focused on developers, its extensive documentation is one of its most valuable features. It is written in a simple language and even non-technical people will be able to understand the API, products, and features of Stripe. The docs contain lots of ready-made solutions for the different programming languages and provide vast possibilities for Stripe testing and customization. Since high-quality documentation is not common on the market, this feature sets Stripe apart from their competitors.

Custom Libraries

Stripe has created and is now supporting libraries for the majority of modern programming languages. This allows developers to cease reinventing the wheel and concentrate on the design of the main product. Using these libraries, it is no longer necessary to write the additional subprograms for integrating the API in their initial codebase. Stripe offers custom libraries for Ruby, Python, PHP, Java, Javascript and Node.js, making their tool efficient for each of these languages.

Testing Environment

Apart from the main documentation and the libraries, Stripe has created documents and bug-free functionality for testing. The system provides dozens of testing credit card numbers, including invalid ones that return specific error codes.

This allows developers to ensure the perfect performance of the product with thorough testing of the system before its integration into real business processes.

Guidelines: How to Integrate Stripe Checkout in a Rails App

Due to its convenient interface and extensive documentation, Stripe is an easy-to-use solution in any type of business. We have prepared a brief guide for you on how to add Stripe to a Ruby on Rails application.

Creating the developer’s profile

In order to receive access to Stripe’s API, you need to create a developer’s profile. On the Dashboard, go to Account Settings => API keys. There you will see the test secret key and the publishable one. The keys are used for deployment of the pre-created integration.


There are three types of keys, including test and live versions. While integrating Stripe, you should only use the test keys in order to avoid charging real customers accidentally. The live keys are only used when you are ready to deploy your application to production.

Installing Stripe to the Project

In order to use Stripe in your application, you need to add the Stripe Ruby gem:
gem 'stripe'

Then, add it to the project by launching the $ bundle command and create the controller via the command line $ rails g controller charges.
Upon adding the controller, create the Customer object. Although you can perform the charges directly, creating the customer will allow you to use repetitive billing.

def new

def create
# Amount in cents
@amount = 500

customer = Stripe::Customer.create(
:email => params[:stripeEmail],
:source => params[:stripeToken]

charge = Stripe::Charge.create(
:customer =>,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'

rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to new_charge_path

Configuring Stripe Keys

Return to your Stripe profile and open the API keys tab. Save your test and live keys in the file secrets.yml and store it in the config folder. The contents of the file should look like this (we used the keys from our test account):

stripe_secret_key: 'sk_test_nWjL9fQH8v23uSiEPJT5d1Xs'
stripe_publishable_key: 'pk_test_NxfJqAJ1j1w8p0K3959byw1P'


stripe_secret_key: 'sk_live_wHzOtGqRSHVCCh1eaJiVd3M1'
stripe_publishable_key: 'pk_live_iGxbYHVwYXZlQBZAKeuhzYEW'

Then go to the stripe.rb file located in the directory config/initializers/stripe.rb and add the following code there:

Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']

Stripe.api_key = Rails.configuration.stripe[:secret_key]

Remember that in order to avoid compromising the application affecting real customers, you should only add the test keys.

Creating the Page Views

The next step is to set up how the payment form will be viewed on the page: to create a credit card payment form and the page that is viewed after the charge is processed.

The payment form is necessary for you to charge your customer. The easiest way to integrate it is to use Stripe Checkout – a tool to easily accept payments. It automatically generates a simple, yet functional payment form. In order to get it, include this code on your payment page.

Now create a charges.html.erb layout under the app/views/layouts directory:

<!DOCTYPE html>
<%= yield %>

The next step is to create a checkout page new.html.erb in the app/views/charges folder in order to display the payment form, validate the credit card details and report errors:

<%= form_tag charges_path do %>
<% if flash[:error].present? %>
<div id="error_explanation">
<p><%= flash[:error] %></p>
<% end %>
<label class="amount">
<span>Amount: $5.00</span>

<script src="" class="stripe-button"
data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
data-description="Monthly subscription"
<% end %>

Than add a create.html.erb view in the same directory in order to show the user a message about a successful transaction:

<h2>Great! Your payment of $100 was successful!</h2>

We have added the payment form validating the details provided by the customer, and generated a one-time token that will be submitted directly to your server. By default, the snippet will look like this, but it can be customized in order to match your brand’s colors. Learn how to do this here.

We have successfully implemented Stripe into a Ruby on Rails app. Now it’s time to test what we’ve done and deploy it.

Testing the performance

To test the functionality of the integrated payment system, launch the Rails server and set the variables for your test publishable and private keys:

SECRET_KEY=sk_test_nWjL9fQH8v23uSiEPJT5d1Xs rails s

Now, go to http://localhost:3000/charges/new to see the usable payment form. Enter the Stripe test card number, a random three-digit CVC and any future expiration date. If the charge is successful, it will display on your dashboard.

Deploy to Heroku

Having successfully performed the transaction, you are ready to make it publicly available for your users. To deploy the application, you need a Heroku account and an installed Heroku Toolbelt. Launch the following code:

git init
git add
git commit -m 'My simple Stripe application'
heroku create
heroku config:set PUBLISHABLE_KEY=pk_test_NxfJqAJ1j1w8p0K3959byw1P SECRET_KEY=sk_test_nWjL9fQH8v23uSiEPJT5d1Xs
git push heroku master
heroku open

Since Stripe is now ready to be used, replace your test keys with the live ones. Congratulations! You have successfully set up Stripe for your Ruby on Rails site.


In this article, we have considered Stripe as a potential payment solution for your business. You are now aware of its major benefits:

  • Stability – the system will work perfectly and accept payments without delays or errors
  • Transparency of payments – Stripe does not charge hidden fees from businesses nor customers
  • Reliable reputation – Stripe has successfully operated on the market for several years and has proven itself as a trustworthy payment processor
  • Usability of the interface – the system is easy-to-use and has extensive documentation for developers
  • Universality – it works with all type of currencies, banks and credit cards and can be introduced in any type of online platform

We have also spoken about the core functionality of the payment processor:

  • Accepting payments

By generating a one-time token, Stripe quickly and securely validates the sensitive details of the credit cards (such as expiration date and CVV-code) and handles the transactions.

  • Creating the subscriptions

With Stripe, you can charge your customers on a regular basis – just create a service in the dashboard and set the price.

  • Stripe Sigma

By sending simple SQL queries, you can obtain the recent statistics of your business and export it to a CSV file. The system stores all previously performed queries, so you can run each of them at any time.

  • Stripe Atlas

With Stripe, it is possible to remotely register a US-based company located in Delaware. The newly created legal entity will have all the necessary documentation, an account in a US bank and access to the services of consultants.

Lastly, we provided a brief guide on integrating Stripe payments in a Ruby on Rails application. Should you have any questions, feel free to contact our team.

Olga Iakubovska

Olga Iakubovska

Project Manager

Join our Newsletter