Ruby on Rails

Stripe - a good payment option for Rails?

December 01, 2020

Author: Muhammad Hamza

Stripe is a tool that you can use to handle online payments, but there’s a lot more in it. This tool is so versatile, that you can use it for e-commerce platforms, several marketplaces, and subscription services. It is one of the topmost payment methods of the market today. Stripe account also supports recurring payments. It supports both the online processing and in-person processing as well so that you can set up both of them according to your needs.

Stripe accepts almost all the major credit cards, for example:

  • Visa
  • Master card
  • American Express
  • Discover
  • JCB (Japan)
  • UnionPay (China)

The integration of alternative payment options is also possible in stripe, such as:

  • American Express Checkout
  • Masterpass
  • Visa Checkout
  • Google Pay
  • Microsoft Pay
  • Apple Pay
  • ACH transfers

Furthermore, its pricing is simple and it has extensive technical and customer support. You can read more about it at stripe.

Stripe and Rails

Why it is a perfect option for Ruby on Rails, till now we have discussed some pros of Stripe as a general, now let’s talk about how it is a good fit for Ruby on Rails. Stripe is a perfect fit for Rails because of its extensive libraries and APIs that support Ruby, as we know Ruby on Rails is a Web Framework built on top of Ruby, and stripe supports Ruby integration, and a more powerful comment is that the Stripe backend language is also Ruby. This giant payment system is built on a language so why is it not good for it? You can find diverse documentation of stripe integration with Rails.

Moving towards its integration we can add Stripe in any rails app by including the respective gem in the Gemfile of the particular project { gem 'stripe' } You can check out this gem by navigating to this repo: There are some prerequisites that need to be fulfilled before using stripe in your application. You need to add the stripe keys to your project so that your project can communicate with stripe using the stripe API, as we are talking about Rails so we will provide a complete flow of a payment system in Rails using stripe and we will discuss:

  • One-time Payments
  • Subscriptions
  • And much more...

So, for some basic configurations that you need to do for your Rails project to communicate with stripe, First of all, you need a stripe account, in that account, there is a developers option in the panel, you need to get the API keys from there so you can use those keys in your project, and using those keys your project and stripe will start communicating. Let’s see how we can configure a Rails application with stripe, here I am assuming that anyone reading this will know the basics of Rails regarding how to set up a Rails project, etc. So, in a Rails framework, there is a folder inside the config folder named as initializers, I am not explaining what this folder is doing for the Rails app, but we need to add the stripe configurations here so that stripe can communicate with our application. The way for adding stripe configurations is to create a file named stripe.rb and put these configurations there,

# frozen_string_literal: true
# :Stripe Configuration:
Rails.configuration.stripe = {
 publishable_key: ENV['PUBLISHABLE_KEY'],
 secret_key: ENV['SECRET_KEY'],
 end_point_secret: ENV['ENDPOINT_SECRET']
Stripe.api_key = Rails.configuration.stripe[:secret_key]
Stripe.api_version = '2019-03-14'

Code Explanation

If you are a Rails developer for a while then you will know what the first line of the code is doing, 

# frozen_string_literal: true is a magic comment, supported for the first time in Ruby 2.3, that tells Ruby that all string literals in the file are implicitly frozen as if #freeze had been called on each of them. That is if a string literal is defined in a file with this comment, and you call a method on that string which modifies it, such as <<, you'll get RuntimeError: can't modify frozen String. If you want to learn more about it then you can go to this StackOverflow post or simply google it.

Moving towards the other lines, the second line is just the comment telling the reader or developer about what is happening inside this file, and after that, the major thing starts the configuration, here we are configuring the stripe with Rails by providing the keys, as we discussed above. Here publishable_key is the stripe publishable key and secret_key is the stripe secret key that stripe needs to communicate with the rails and the end_point_secret is the stripe key that validates that is request is coming from the known path, If this fails then stripe will not recognize the call and will fail, and the last thing is the stripe api_version, there are different types of versions that came with several changes or modifications, you can use them according to the needs. So, that was the pretty basic knowledge of stripe keys, you can read a lot more about them in detail in the Stripe documentation here, So, you can see it’s just a few simple steps that can get you up and running and you can use stripe with your Rails application, and by looking at the facts we can say that Stripe is a great option for Rails.

Here we are not giving any picture or code that you need to implement the payment scenario in stripe, but in the future, we will provide the complete flow. Till then stay tuned.