Below is an outline of the Stripe payments module of video lessons in the Full Stack Rails Mastery course.
If you want to use Stripe to process payments in your Rails app, there are a number of steps you need to take.
(If you live in a country where Stripe is not available, you may use another payment processor like Paypal or Razorpay. The overall concepts in this tutorial remain the same, so read on.)
1. Create a Stripe account
Before you can start using Stripe for processing payments in your Rails app, you need to create a Stripe account on https://stripe.com.
The good news is that you can begin using your Stripe account in test mode as soon as you create it.
For using it on a live site (to take real payments), you will need to verify your account. See Stripe's checklists to go live (https://stripe.com/docs/payments/account/checklist
and https://stripe.com/docs/development/checklist)
2. Install the Stripe gem and command line interface (CLI)
The gem gives you convenient methods to call the Stripe API and the CLI is useful for local testing.
3. Set up a payment form
There are two main options here:
a. Use a form hosted by Stripe (called Stripe Checkout)
Or
b. Host a form on your own site
I recommend starting with a Stripe-hosted Checkout form. That way you don't need to handle any of the frontend parts for accepting payments.
Self-hosting the payment form has its benefits because you have full control over the design and user experience. However, I've found that the benefits of Stripe Checkout outweigh those of self-hosting. Stripe has been rapidly adding great new features to Stripe Checkout, including automatic tax handling, one-click pay, conversion rate optimisation and more. You get these improvements without having to do anything.
4. Create a Stripe Checkout session
This happens in your Rails backend (typically a controller action), triggered when a customer clicks on a buy or checkout button on your site.
If you want to use Stripe to process payments in your Rails app, there are a number of steps you need to take.
(If you live in a country where Stripe is not available, you may use another payment processor like Paypal or Razorpay. The overall concepts in this tutorial remain the same, so read on.)
1. Create a Stripe account
Before you can start using Stripe for processing payments in your Rails app, you need to create a Stripe account on https://stripe.com.
The good news is that you can begin using your Stripe account in test mode as soon as you create it.
For using it on a live site (to take real payments), you will need to verify your account. See Stripe's checklists to go live (https://stripe.com/docs/payments/account/checklist
and https://stripe.com/docs/development/checklist)
2. Install the Stripe gem and command line interface (CLI)
The gem gives you convenient methods to call the Stripe API and the CLI is useful for local testing.
3. Set up a payment form
There are two main options here:
a. Use a form hosted by Stripe (called Stripe Checkout)
Or
b. Host a form on your own site
I recommend starting with a Stripe-hosted Checkout form. That way you don't need to handle any of the frontend parts for accepting payments.
Self-hosting the payment form has its benefits because you have full control over the design and user experience. However, I've found that the benefits of Stripe Checkout outweigh those of self-hosting. Stripe has been rapidly adding great new features to Stripe Checkout, including automatic tax handling, one-click pay, conversion rate optimisation and more. You get these improvements without having to do anything.
4. Create a Stripe Checkout session
This happens in your Rails backend (typically a controller action), triggered when a customer clicks on a buy or checkout button on your site.
Stripe.api_key = 'sk_test_7kfiuehkddfuC8DvlxhddDewAsdf2' session = Stripe::Checkout::Session.create({ line_items: [{ price: 'price_0OdsKFdMzScYFKssw2ldsf3J', quantity: 1, }], customer_email: "[email protected]", mode: 'payment', success_url: 'http://example.com/products/box', cancel_url: 'http://example.com/products/box', automatic_tax: {enabled: true}, })
You initialise a Stripe Checkout session which encodes key information for Stripe to present the correct payment form to the customer. This information includes product information, charge amount, type of payment (one-time or subscription) and customer email (optional).
You'll need a Stripe API key to create a Stripe Checkout session.
Important note: Remember to never use your production keys in non-production environments including localhost and staging. Only use your test keys outside of production. And never hard-code your keys or check them into a git repository (not even test keys). Use Rails secrets or environment variables instead.
5. Present the payment form to the customer
Once a Stripe Checkout session is created, you redirect the user to the Stripe-hosted checkout form, where they enter their payment details.
redirect_to session.url, status: 303, allow_other_host: true
On successful payment, Stripe redirects the customer to your website.
You show them a confirmation message and fulfill the order.
But wait... how do you know if the payment was successful?
6. Confirm the Stripe payment on your server
When a customer makes a payment, Stripe sends your server a notification, called a webhook.
It contains information about the successful payment - what was purchased, the amount paid and the customer's email address.
You need to create an endpoint (controller action) where you can receive this webhook notification (for example, https://example.com/webhooks/stripe).
Once you've set this up in your Rails app, you need to let Stripe know by adding that url as a webhook endpoint in the Stripe dashboard.
You can use the information in the webhook to match the details with those of your customer's and then proceed to the last step.
7. Fulfil the order
Once you've confirmed that a customer has paid for a certain product or service, you can proceed to fulfil the order.
If you're selling a physical product, take the next steps to ship the product to the customer.
If you're selling a digital product, like a course or ebook, you can give the customer access to that content.
If you're selling a service, you may reach out to them manually or send them a link to schedule an appointment.
You'd also typically send an email confirmation.
These are roughly the main steps you need to take to sell a product online using Stripe. There are a number of other details that you also need to take care of, which I haven't covered yet.
Do you need to create products in Stripe beforehand?
What happens if the payment fails?
How does your server know Stripe sent the webhook notification (and not someone malicious)?
Do you handle a webhook asynchronously in a background job or synchronously?
How do you handle refunds?
You can learn how to do all of this in detail in the Stripe payments module of video lessons of the Full Stack Rails Mastery.