Monetize your Side Hustle with Stripe’s API for Rails

Brittani Élan Taylor
2 min readNov 12, 2020

A guide to upgrading your digital lemonade stand

Maybe you think Etsy’s overrated? Or you just have quite the independent streak? Either way, if you’re reading this, you’re ready to take on the challenge of handling payments in your Rails API.

Luckily, Stripe makes it easy with this customizable API that saves you the trouble of storing user payment information on your website (which would leave you vulnerable to cyber attacks on even the best days).

Project setup

  1. Install the Stripe Ruby gem. After adding the following to your Gemfile, run ‘bundle’ in your terminal.

Server code

2. On the server side (aka your project’s backend), add an endpoint on your server that creates a one-time checkout session for whatever product(s) you have in mind. A Checkout Session controls what your customer sees in the Stripe-hosted payment page.

There are various areas for customization in the above code:

  • Stripe recognizes 7 different payment methods. Learn more about them here
  • Within the line_items object, you can define product information when you create the Checkout Session with price_data or alternatively use pre-defined prices and pass their IDs.
  • Checkout has three modes: payment, subscription, or setup. Payment is for one-time purchases, and what we’re demonstrating here.

Client code

  • Note that in the code above, one’s able to specify URLs for success and cancel pages, but this can also be handled within the same URL (examples below from Stripe for multiple URLs)

TLDR: Adds a success page — Creates a success page for the URL you provided as the Checkout Session success_url to display order confirmation messaging or order details to your customer.

TLDR: Adds a canceled page — Adds another page for cancel_url. Stripe redirects to this page when the customer clicks the back button in Checkout.

TLDR: Adds an order preview page — Allows the customer to review or modify their order — once a customer is sent to the Checkout page, the order is final.

Almost there!!!

Now, all that’s left is to start your server with ‘rails s’ in your terminal, and then and navigate to http://localhost:3000/checkout.html (or whatever your BASE_URL is)

And the testing begins. Stripe provides a number of card and account numbers to test depending on the payment method designated above. You can find them here. The most basic ones you can find below.

  • Payment succeeds — 4242 4242 4242 4242
  • Payment requires authentication — 4000 0025 0000 3155
  • Payment is declined — 4000 0000 0000 9995

Demo below to see the final product

Find a link to a demo here!!

--

--