Stripe
SaaSBold comes with Stripe integration to manage payment and subscriptions. This part of the documentation will show you how to integrate Stripe into your SaaS Boilerplate.
Overview
Stripe is a powerful payment processor that allows you to manage payment and subscriptions efficiently. This guide will show you how to set up Stripe, including creating subscription plans, generating required keys, and configuring webhooks.
Note: If you want to use another payment provider option for integration, skip this step. Instead, follow the LemonSqueezy guide or Paddle guide to integrate subscriptions.
Steps to Integrate Stripe
Getting the Stripe Secret Key
To begin, you need a Stripe account. If you donβt have one, go ahead and create one at Stripe.
For this integration, we are using the Test Mode.
Login to Stripe: Log in to your Stripe account.
Enable Test Mode: For this integration, we use Test Mode. Ensure to activate your Stripe account and switch to the real Secret Key before deploying.
Go to the Developers tab.
API Keys: Go to the API Keys tab, scroll down to the Secret Key, and copy it.
Update Environment Variable: Now, Add the Secret Key to your
.env
file:
Creating the Subscription plans
Now that we got the keys, letβs create a subscription plan.
Login to your Stripe account.
Navigate to Products: Go to the Products tab and click on the Add a Product button.
Fill Product Information: Complete the form, and make sure to choose "Recurring payment."
Save Product: Save the product and note the details.
Getting the Subscription data
Now that we have successfully created the subscription, we need to get the subscription plan data to use it on our app.
Open pricing data component: To update the data open up the pricing/pricingData.ts file.
Get Plan Details: From your Stripe Dashboard, navigate to Dashboard-> Product catalog, and open your product.
Now, get the Name
, Price
, and price_id
.
Update Data: Update the pricingData.ts
file with these details.
Note: For the price, make sure to multiply it by 100; itβs just a conventional 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 in the database.
First, we will see how to enable Webhook for testing locally, and then we will see how to enable it for production.
Webhook integration for local development
Add Local Listener: Go to Developers->Webhooks->Add local listener
Select Test Environment: Select Test in a local environment
Install Stripe CLI: Install Stripe CLI and login to stripe
Run Webhook Server: Run this command to forward the event to the webhook. Iβve added this script to the package.json file
It'll start the webhook server and give us the webhook secret.
Copy Webhook Secret: Copy the webhook secret (whsec_...) from the terminal and save it in the
.env
file:
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_p,rice_id stripe_subscript,ion_id
Webhook integration for production
Add Endpoint: Go to Developers->Webhooks->Add an endpoint
Add Webhook URL: Now add the webhook URL, which is yoursite.com/api/stripe/webhook.
Select API Version: Choose the Latest API version from the dropdown.
Click on the Select events button.
Select Events: Check the Select all events and then click on the Add events button.
Add Endpoint: 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.
Tips
Testing: Use Stripe's Test Mode to thoroughly test your integration before switching to live mode.
Documentation: Refer to Stripeβs official documentation for detailed guidance and troubleshooting.
Security: Ensure all API keys and secrets are stored securely.
Event Selection: Select only the webhook events relevant to your application to avoid unnecessary data processing.
User Experience: Make the payment and subscription process as seamless as possible for users to enhance their experience.
Summary
By following these steps, you will have integrated Stripe into your SaaSBold boilerplate, enabling robust payment and subscription management. Ensure to test the integration thoroughly in Test Mode before moving to the live environment.
Last updated