Monetize your Side Hustle with Stripe’s API for Rails

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.

  • 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)

  • 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!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store