Build a Subscription Service for your NextJS & Prisma App with QStash
The most common business model in building a Saas is a subscription model where users pay a certain fee to use your service in a specified time interval. In this article, we will go through the process of adding a subscription service to a NextJS project and the best practices to follow once going into production.
Our Saas will be helping our users to get a daily dose of motivation quotes every morning on their email at a fee of say, $1 per month.
To create your project, paste this command on your terminal and choose Prisma as your ORM; you can opt-out of the rest of the tools. In case you're not familiar with create-t3-app, it is one of the easiest ways to start a nextJS app and add tools like Drizzle, Prisma, trpc, and tailwind. I will be using the Pages router in this article.
pnpm create t3-app@latest
Data Modelling
A good data modeling will make your application grow easier and managing your database even more seamless. Let's start by first adding the data modelling for our quotes to be sent to our users. Open the schema.prisma file located in your prisma directory and add the following. I will be using postgres but implementation in other databases will be fairly the same.
On our users' side, we are recording their name, email, every payment they have ever paid, and their subscription status. Let's go ahead and add the last two
We are recording the payment details that we get from our payment provider to track when a
particular user paid for a service in case they ask for a refund or whatnot. The subscription table
is where we will check whether this user is currently subscribed to our service. The daysWithService
column
is what we will be using to count how many days (or any time interval) this user has used our service.
To deploy your database, you must configure your database URL depending on where you host your database. I won't go into the details of hosting your database in this article.
Recording Payments Details
For us to be able to receive money from our users, we need to set up our payment provider. There are different options depending on your location. If you're in US, UK, or Canada you can go ahead with Stripe, as it is fairly easy to integrate with different components and setting up webhooks is relatively easy. If you're in Africa I would recommend choosing DPO Payments as it is available in many countries as compared to Flutterwave and other providers. The choice you make here depends on your business requirements and where you are located.
All providers will allow you to configure a webhook that they can ping every time someone makes a payment
to your account, so go ahead and implement that. Let's say your webhook URL is https://www/yourDomain.com/api/payments
,
go ahead and create a payment.ts
file at the API directory of your application.
What we did here is we took the response we got back from our payment provider, extracted the userId from the email that we got back and recorded the payments in our table as well as doing the subscription logic in our table. We also handled the case where the user has made multiple payments in a row on how it will affect the subscription. We also add daysWithService table as 31 meaning this user is subscribed to the service for the next month. Ensure you check what data shape your payment provider returns and adjust the above code accordingly.
Adding CRON Jobs with Upstash
The last piece of the puzzle is adding CRON jobs to our saas. The main point of adding the CRON jobs is to unsubscribe the user from getting our service once the subscription ends.
To achieve this, we will be using upstash Qstash Service. Go ahead and create an account and head over
to Qstash in your dashboard. You will be prompted with UI to publish your messages.
On the Endpoint section, add your URL https://www/yourDomain.com/api/updateSubscription
, type Choose Once and specify every midnight, and click schedule. This will hit your specified endpoint every midnight and allow us to trigger a database update.
Go ahead and create a updateSubscription.ts
file at the API folder.
Here, we go and update our subscription table according to the remaining days of service. If the daysOfService is less than or equal to zero, we set isSubscribed field to false meaning the user subscription has expired, otherwise we reduce the daysOfService by 1.
And those are all the pieces required to add a subscription service to a saas.
Hope you enjoyed it.