How To Integrate Stripe Gem Into a Ruby on Rails App
More than half of all purchases are made on the Internet. Because of that, ensuring online payment support on a website has become a must for today’s business. Luckily, there is a plethora of payment systems allowing you to do that. In fact, there are so many of them that choosing the right one often becomes a struggle.
One of the most reliable payment systems is Stripe. It is a highly customizable and secure solution designed to suit most of the modern business requirements. Its robust API allows web developers to easily integrate this system into any business project.
In this article, we are going to show you how to implement the Stripe gem in a simple web application.
Creating a sample application
The first step is to create a new Rails app:
command line:
Stripe guides recommend using its library for developing web-applications. So, let’s add it to the gemfile of our new application (keep in mind that you need to run bundle afterwards):
Gemfile
Add your secret keys to secrets.yml for the for Stripe gem:
stripe:
You can find them on the Stripe account settings page:
For this application we’ll need users (you can find more detailed information about users here) and the products model.
Stripe saves users as customers, so we need to modify the user logic by engaging ActiveRecord callback “after_commit” (you will also need to add “has_many: credit cards”):
Next we create a customer on the Stripe’s side with the help of Stripe::Customer.create(email: email). After this we will be able to add credit cards for this user, so that one could use “one-step-payment” in future.
Now we need to create products. The simplest way to do this is to use scaffold generators.
command line:
Though scaffold is a great tool it doesn’t provide us with necessary means to implement some specific features.
Next, we set prices and names fields as required. We also need to include some method that would show prices in cents because Stripe sees prices only in cents. Keep this in mind! Otherwise, the system may simply take hundred times more than it was supposed to. So we modify the products model:
rails g scaffold credit_card digits:string month:integer year:integer
You can see that in order to save Stripe credit cards identification for one-step payments we involve quite a lot of validations. However, in real application, their number must be much bigger.
For payment logic our application requires a controller. Calling it charges controller as it is recommended in Stripe guidelines.
Create action:
We use Stripe::Charge.create for the payments, where:
- customer: user’s Stripe customer id,
- source: his credit card’s Stripe id (we need to use the one added earlier or ask Stripe to create a new one),
- amount: the price (in cents) that user should pay for product,
- currency: the currency used by the app (changeable for international applications).
The rest part of the code should be easy to understand.
We use Stripe::Token.create to add a new card to Stripe. It returns hash with information about the credit card in Stripe. We need its id for the last step — adding a new credit card.
Stripe::Customer.retrieve returns details about existing customer. There is some interesting information about user that you can send to Stripe when creating this customer in the user model.
To handle Stripe’s webhooks we need “stripe_event gem”, and “stripe_ruby_mock” gem to write tests for application.
Edit Stripe initializer with Stripe keys (you can find them in our secrets.yml) and a method that handles Stripe’s webbhoks (in this example we handle charge.success event).
You can find more information about webhooks in the Stripe’s documentation.
NB: Stripe webhooks can’t be used for local development, so we need to use the ultrahook service.
After you’ve done that your routes.rb should look like this:
“StripeEvent::Engine” is the path that receives Stripe’s webhooks.
We need two forms on the products#show page. The first one is for payment with a new credit card and the second one is for payment with an existing one.
Now users are able to pay with a new card or use a previously saved one:
Testing
For testing we will use the “Stripe-ruby-mock” gem. Here are the sample specs for “CreditCardService”:
and controller spec:
The full code of the application is in JetRuby’s E-Commerce repository.
Bottomline
Stripe is a great payment processing service with a good API and comprehensible documentation. With such flexible and user-friendly solution as Stripe, we can easily implement and improve any type of payment integrations — from ecommerce platforms to recurring billing and even more.
If you’d like to learn more about the technical side of things or looking to build an Ecommerce app, feel free to drop us a line at questions@jetruby.com. Cheers!