# Stripe Payment Gateway

## Stripe setup

{% hint style="info" %}
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.
{% endhint %}

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](https://www.stripe.com)[ ](https://www.stripe.com)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.](https://docs.stripe.com/stripe-cli?install-method=apt)

**Then login:**

```
stripe login -i
```

This should ask you for an API key, you will find it in [Test > Developers > API Keys ](https://dashboard.stripe.com/apikeys)> 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](https://github.com/pay-rails/pay/tree/main) 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:

<figure><img src="https://2221554152-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPCf6vtFuZEPHXJd7JfIe%2Fuploads%2FrQby4jkNdXXAtsOwE4rZ%2FScreenshot%202024-08-19%20at%2020.44.59.png?alt=media&#x26;token=82cad6c3-c206-4246-8ac2-0feef045f5dd" alt=""><figcaption><p>Credentials Edit file</p></figcaption></figure>

## Pay gem

All the [Pay Gem](https://github.com/pay-rails/pay/tree/main) 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
```

<br>
