💳Stripe Payment Gateway

Stripe setup

The boilerplate comes by default with Single payments stripe checkout. We are working on adding subscriptions, in the meantime, you can modify the existing checkout controller with a few lines to activate subscriptions.

We use stripe checkout and pay gem for easy paywall setup and webhooks. To start using Stripe follow this guide:

Create account

Go to Stripe and create an account.

Install stripe CLI in your terminal

For us to listen to stripe webhooks and test in development, we need to install stripe CLI in our terminal:

For macOS:

brew install stripe/stripe-cli/stripe

Or for Ubuntu, check the official documentation.

Then login:

stripe login -i

This should ask you for an API key, you will find it in Test > Developers > API Keys > Secret Key

API keys and webhook secrets

Start listening to your webhook events by running in your terminal:

stripe listen --forward-to localhost:3000/pay/webhooks/stripe

This webhook comes by default with the pay gem. If you wish to create custom webhooks refer to the Pay Gem documentation.

Add the API Keys in the Rails credentials file. Open the file from the terminal with the following line:

EDITOR="code --wait" rails credentials:edit

Add the keys in the file with this exact wording for the keys and save:

Pay gem

All the Pay Gem setup is taken care of by LightningRails, the only setup needed from your side to get the gem to work is:

  • Go to application.rb and replace the example domain with your domain so Pay can generate links (for features like Stripe Checkout).

  • Create a product on the stripe product catalog, copy the product ID, and paste it on the checkouts_controller.rb action Show:

# controllers/checkouts_controller.rb

  def show
    current_user.set_payment_processor :stripe
    current_user.payment_processor.customer

    @checkout_session = current_user.payment_processor.checkout(
      mode: 'payment',
      line_items: 'price_1JZ9J3J9jgZ2Qj5vz1ZzZzZz',
      success_url: checkout_success_url
    )
  end

Set your pricing

Now that you have created your product on Stripe, ensure all the UI has the correct pricing, and modify the price on the _price_cards.rb component.

Premium Authorisation

Hide your premium features behind the paywall by adding the following line in the premium controllers/actions

class MyPremiumController < ApplicationController
before_action :verify_premium, only: [:some_action]

[...]

def verify_premium
   unless current_user.premium? # This is an instance method in the user.rb model
     flash[:notice] = "Go premium to access this feature"
     redirect_to checkout_path
   end
end

Last updated