Stripe is a popular payment processing service that makes online payments easy. In this tutorial series, we’ll guide you through how to use Stripe to process payments in Rails apps.
This is the first article in the “Process Payments with Stripe in Rails” tutorial series:
- Stripe Payments in Rails, Part 1 – Stripe Checkout (this tutorial)
- Stripe Payments in Rails, Part 2 – Use Custom Forms with Stripe.js
- Stripe Payments in Rails, Part 3 – Recurring Payments with Stripe
- Stripe Payments in Rails, Part 4 – Plan Discounts with Coupons
As an example we will have an imaginary “Cookery School” application to sell online cooking courses. For this tutorial, we will work on the checkout branch, to use Stripe Checkout to process payments:
1 2 3 4 5 6 7 8
Step 1. Create Stripe Account
This step is really easy, but very important. You just need an email account to sign up for Stripe.
Step 2. Configure project
Add Stripe gem to your Gemfile and run
1 2 3 4 5 6
You can find these keys at your Stripe console: Your account > Account Settings > API keys:
We will use testing keys for the development environment. For production environment you need to configure with live keys to receive real payments.
Add these keys at
1 2 3
For the development and test environments you can directly put your test keys in the
secrets.yml file. For the production environment you will not want to have those keys in your code. You want to use environment variables instead:
1 2 3
If you want to use more comprehensive solutions to manage your environment environments, check out our tutorial on How to manage configueration variables in Rails.
Step 3. Create Registration form with Stripe Checkout
We will create our registration form
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
We are configuring the very basic ‘stripe-button’, but you can parameterize much more of it with the Checkout data options.
checkout.js script creates a Submit button and a popup window to allow inputs for the customer payment data. When user submits this the form in this window,
checkout.js sends the info to Stripe, which validates the card and sends a token back if the card number is valid. Before submitting the form to our controller,
checkout.js inserts two hidden inputs in the form (
stripeToken value represents a credit card number stored on Stripe’s server, and we can use that to charge a card card.
The reason for this 2-step process is such that the customer’s credit card information will never flow through our server – in other words, Stripe is taking over the PCI Compliance burden away from our app.
Step 4. Processing payments and creating registration
In our controller we will build our registration and process payments:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
Here we are handling a possible error with the user’s registration and payment data. If any error raises in the process, it will be thrown back again to client as an error, and the payment/registration will not be created.
We need to add the
process_payment method in our model:
1 2 3 4 5 6 7 8 9 10
Note here we are passing
course.price * 100 as the amount to Stripe, because Stripe expects the amount to be in cents, not dollars.
We also need to create the
card_token column in the registrations table.
Test the features
At this point we will test our code. First select a course in our application and fill the registration form:
You can use the test card number: 4242 4242 4242 4242, with any 3-digit CVV.
If the process finished successfully you will see the registration confirmation:
You can find the payment in your Stripe console:
You can also use the special card number 4000 0000 0000 0002 to trigger a “Card Declined” transaction to see how the app handles it. The Stripe documentation on testing gives you more valid and invalid test cards to check more specific use cases.
This is the easiest way with Stripe to pay with credit cards, and you don’t have to do much work. In the next tutorial of this series, Stripe Payments in Rails – Use Custom Forms with Stripe.js we’ll cover how to use Stripe to process payments with your own forms so you can control the look and feel.