# 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://docs.lightningrails.com/features-setup/www.stripe.com)[ ](https://docs.lightningrails.com/features-setup/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="/files/6cYfEjtq9rJqJxBiFzZL" 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>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.lightningrails.com/features-setup/stripe-payment-gateway.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
