Stripe

SaaSBold comes with Stripe integration for managing payment and subscriptions. This part of the documentation will show you how to integrate Stripe into your SaaS Boilerplate.

Note: If you want to use LemonSqueezy for integration skip this. Follow the LemonSqueezy guide for integrating subscriptions.

Getting the Stripe Secret Key

To get the Stripe secret key you have to have a Stripe account. If you don’t have one go ahead and create it.

For this integration, we are using the Test Mode. Before you deploy your site, Activate your Stripe account and integrate it with a real Secret Key.

  1. Go to the Developers tab.

  1. Then go to the API Keys tab, after that scroll down to the Secret Key and copy it.

Now add it to the .env file.

STRIPE_SECRET_KEY=YOUR_STRIPE_SECRET_KEY

Creating the Subscription plans

Now that we got the keys, let’s create a subscription plan.

  1. Login to your stripe account.

  2. Go to Products and click on the Add a product button.

Fill out all the information. And make sure to choose Recurring payment.

Getting the Subscription data

Now that we created the subscription successfully, we have to get the subscription plan data to use it on our app.

To update the data open up the pricing/pricingData.ts file.

Go to your stripe Dashboard-> Product catalog and open up your product.

Get the Name, Price, and price_id

Note: For the price make sure to multiply it by 100, it’s just a convention stripe use. For example, $5 becomes 500

Webhook Integration

We've integrated Stripe, created subscription plans, and loaded the data on the pricingData.ts file. Now, we will have to integrate stripe webhook to save the subscription data on the Database.

First, we are going to see how to enable Webhook for testing locally, then we are going to see how we can enable it for production.

Webhook integration for local development

  1. Go to Developers->Webhooks->Add local listener

  1. Select Test in a local environment

  1. Install Stripe CLI and login to stripe

stripe login
  1. Run this command to forward the event to the webhook. I’ve added this script to the package.json file

npm run stripe:listen

It'll start the webhook server and give us the webhook secret. Copy the secret (whsec_...) from the terminal and save it on the env file.

STRIPE_WEBHOOK_SECRET=

Now when you make a purchase it'll update the user's table on the Database.

You'll see these entries got updated with the appropriate data.

stripe_current_period_end stripe_customer_id stripe_price_id stripe_subscription_id

Webhook integration for production

  1. Go to Developers->Webhooks->Add an endpoint

  2. Now add the webhook URL, which is yoursite.com/api/stripe/webhook. After that select the Latest API version from the dropdown.

  1. Click on the Select events button.

  1. Check the Select all events and then click on the Add events button.

After that scroll down and click on the Add endpoint button.

We are almost done. One last thing we have to do is get the Webhook secret for the live site and add that to the .env file.

After you’ve added the endpoint you’ll see something like this. Click on Reveal it'll give you a new webhook secret for the live site.

Update the env with the new secret.

STRIPE_WEBHOOK_SECRET=

Last updated