# Subscriptions (Stripe)

## Stripe

Before you start, you must create your account.

[**Stripe dashboard**](https://dashboard.stripe.com/register)

Then you need to setup Stripe CLI and configurate it within our project. Use the links below and follow the instructions there.

#### [Stripe docs](https://docs.stripe.com/stripe-cli)

Run stripe on your computer

```bash
stripe listen --forward-to localhost:8000/stripe/webhook
```

After your CLI starts working, it gives you a message with Stripe Webhook Secret, and this webhook secret you need to add to your `.env or IDEA configurations file` as "`STRIPE_WEBHOOK_SECRET`".

When you have done all the steps, enter next command in your terminal to populate your Stripe products (subscriptions):

```bash
stripe fixtures src/subscriptions/stripe.fixtures.json
```

#### Using your stripe credentials:

In your **React** project, open your UI `.env.development or IDEA configurations` and create (or replace) "`REACT_APP_STRIPE_PUBLIC_KEY`" value with your actual Public Stripe API Key (from Stripe Dashboard => Developers => API Keys => Publishable key).

In your **Nest.JS** project, open your API `.env or IDEA configurations` and create (or replace) variables called "`STRIPE_API_KEY`" with your actual Stripe API Key (from Stripe Dashboard => Developers => API Keys => Secret key) and "`STRIPE_WEBHOOK_SECRET`" with your actual Webhook Secret.\
\
This keys are unique for every developer and website deployment.&#x20;

{% hint style="danger" %}
N.B. You need to run Stripe in your terminal every time when you work with it!
{% endhint %}

### Library

Make notice, you must have next packages in your json file:

```json
"@golevelup/nestjs-stripe": "^0.6.5",
"stripe": "^14.20.0"
```

{% hint style="success" %}
N.B. Note the library versions. This is the version that was used at the time of writing the documentation.
{% endhint %}

### Stripe and Subscriptions Code

All the code responsive for the webhooks and subscriptions CRUD you can find in the payment module. CRUD here not means to "subscribe".&#x20;

**your\_project*****/api/src/payments/***\
\
But all the code in payments module are tied with subscriptions module so take a look there too.\
\
**your\_project*****/api/src/subscriptions/***\
\
In payment services you can find methods which are using @StripeWebhookHandler with console logs which can indicates what is goin on when you doing certain thing with stripe products and prices.\
\
Every time when you make some changes in subscriptions and subscription tiers you will see status code 200 in terminal or console logs depending on your actions.

{% hint style="success" %}
N.B. Due to behavior of the Stripe it will always "update" the product no matter what you do with your subscription. So no need in "create" method. Except of "delete" method, because we need some stuff to do there.
{% endhint %}

### No subscriptions in DB?

In common situation, you won't find Free subscription on the Stripe dashboard. It is already in DB. But if you cannot find the "not" free subscriptions in you DB, you need to update them. You can do it through [**Stripe Dashboard**](https://dashboard.stripe.com/)**.** In that dashboard go to Product Catalog and there you will find all the products (subscriptions). Just click on three dots on the product item and then edit button. Click "update product" at the bottom. This will update it and you will see status code in the terminal.\
\
Notice, you don't need to actually change the info inside.

{% hint style="danger" %}
N.B. The following metadata is required for the application to work correctly, they cannot be deleted in any case! Except for features. If you miss something - add it for correct work.
{% endhint %}

<figure><img src="/files/ajdfTM3NlmF0eWUcjKfM" alt=""><figcaption></figcaption></figure>

This and everything in product edit you can change as you wish, except of metadata:

<figure><img src="/files/kYNxs0EOadndm6ok8g6H" alt=""><figcaption></figcaption></figure>

The methods responsible for the "subscription" are found in the subscription module. Methods from there are closely related to methods from the payment module, please note. \
If someone trying to subscribe on certain subscription, request goes to subscription module first. After successful payment, Stripe make a call to the method inside payments service.\
\
All the subscription tiers you can find in your DB.

## UI

#### Subscriptions page

<http://your\\_website/subscriptions\\>
\
![](/files/bfpSNVFEfA9imYBygg2j)\
\
Here you will find all the subscription tiers, for example:

<figure><img src="/files/zwwLhYohXhJY2eqoNKBZ" alt=""><figcaption></figcaption></figure>

From here you can see all the tiers that you have in your DB. Please notice, they are going not from Stripe dashboard! In this page you can see your plan and other plans. What you can do here? Subscribe, cancel subscription, renew subscription.

For now, details in subscription tiers you can change in Stripe dashboard, such as name of tier, price features and what plan is more popular.

#### Who can manage the subscription?

For more details on permissions you can find in Permissions part on modules.


---

# 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://intercode.gitbook.io/intercode-saas-kit/features/subscriptions-stripe.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.
