Stripe
For payments, we use Stripe. Follow these steps to get all the necessary API Keys, add the product and price in Stripe, set up the Stripe CLI, etc.
Create a Product and Price
-
Log in to your existing [Stripe] account or create a new one.
-
In the Stripe Dashboard, go to the
PRODUCT CATALOG
section. -
Click on
CREATE PRODUCT
. -
Fill in the required fields such as
product name
,description
,price
, etc. and click onCreate product
.
Update the Product Price ID in the Database
-
In the Stripe Dashboard, go to the
PRODUCT CATALOG
section. -
Find the product you want to obtain the price ID of, and click on it.
-
Then, inside the pricing section, click on the three dots (…) next to the price.
-
Then, copy the price ID.
-
In the Supabase Dashboard, go to the
TABLE EDITOR
section. -
In the
PRODUCTS
table, find thestripe_price_id
column and paste the price Id into it.
If you haven’t created a PRODUCTS
table yet, please go back to the Database of this documentation and follow the steps to create it. Then come back here.
Locate the Stripe API Keys
-
Inside the Stripe Dashboard, go to the developers section and then click on
API KEYS
. -
Copy the
PUBLISHABLE KEY
andSECRET KEY
. -
Inside the codebase, go to the
.env.local
file in the root folder and add them to the variables shown below.
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEy=
NEXT_PUBLIC_STRIPE_SECRET_KEy=
Setting up the Stripe CLI
-
Inside the codebase, install the Stripe CLI:
brew install stripe/stripe-cli/stripe
-
Log in to your Stripe account via the CLI:
stripe login
-
Start the Stripe webhook listener:
stripe listen --forward-to localhost:3000/api/webhooks/stripe
-
You will receive a webhook signing secret from the Stripe CLI. Update the
.env.local
file with it:.env.localNEXT_PUBLIC_STRIPE_WEBHOOK_SECRET=
How To Use Stripe In Development
-
Turn ON Test Mode in your Stripe Dashboard.
-
Start the Stripe webhook listener:
stripe listen --forward-to localhost:3000/api/webhooks/stripe
-
Open
http://localhost:3000/choose-pricing-plan
in your browser. You need to be logged in to see the page. -
Select a pricing plan and click on
UPGRADE NOW
. You should have completed a free trial or are in one before performing this step. -
Use the following credit card details
4242 4242 4242 4242
with any future date (e.g. 02/28) and any CVC number (e.g. 728).
The stripe webhook api/webhook/stripe/route.ts
will listen to Stripe events and receive the checkout.session.completed
event.
Then, the checkout.session.completed
event will trigger the handleCheckoutSessionCompleted
function which updates the subscription_tier
column in the purchased_subscriptions
table to the selected pricing plan and many more.
const { error } = await supabase.from("purchased_subscriptions").insert({
user_id: userId,
created_at: moment().toISOString(),
updated_at: moment().toISOString(),
stripe_price_id: stripePriceId,
status: SubscriptionStatus.ACTIVE,
subscription_tier: subscriptionTier,
});
How To Use Stripe In Production
-
Turn OFF Test Mode in your Stripe Dashboard.
-
Head to the
DEVELOPERS
tab and copy thePUBLISHABLE KEY
andSECRET KEY
. -
Update the
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
andNEXT_PUBLIC_STRIPE_SECRET_KEY
variables in your production environment. For instance, in Vercel, go toSETTINGS
→ENVIRONMENT VARIABLES
. -
In the Stripe Dashboard, go to
DEVELOPERS
→WEBHOOKS
and add the webhook URL as a new endpointhttps://example-domain.com/api/webhooks/stripe
. -
Select the
checkout.session.completed
event (or more if needed).
Further Configuration (Optional)
-
In
SETTINGS
→BUSINESS
→BANK ACCOUNTS AND CURRENCIES
→PAYOUT SCHEDULE
, set a specific date of the month to receive your payouts (I use the 20th of each month). -
In
SETTINGS
→BUSINESS
→CUSTOMER EMAILS
→PAYMENTS
, activate emails for successful payments and refunds.