Subscriptions (Stripe)

Learn how to connect Stripe with website and use it with subscriptions.

Stripe

Before you start, you must create your account.

Stripe dashboard

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

Run stripe on your computer

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):

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.

N.B. You need to run Stripe in your terminal every time when you work with it!

Library

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

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

N.B. Note the library versions. This is the version that was used at the time of writing the documentation.

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".

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.

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.

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. 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.

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.

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

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

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.

Last updated