Skip to content

Commit ce74f66

Browse files
Clarified and resequenced setup instructions (vercel#203)
* Clarified and resequenced setup instructions * Added a note on reliability and reconciliation * Update README.md fixed typo * Update README.md Clarified which URL to use * Update README.md Added instructions on URL env variable * Update .env.local.example added public site url env var * Update .env.local.example --------- Co-authored-by: Thor 雷神 Schaeff <[email protected]>
1 parent c1a8214 commit ce74f66

File tree

2 files changed

+118
-75
lines changed

2 files changed

+118
-75
lines changed

.env.local.example

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@ SUPABASE_SERVICE_ROLE_KEY=
66
# Update these with your Stripe credentials from https://dashboard.stripe.com/apikeys
77
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_1234
88
STRIPE_SECRET_KEY=sk_test_1234
9-
STRIPE_WEBHOOK_SECRET=whsec_1234
9+
STRIPE_WEBHOOK_SECRET=whsec_1234
10+
11+
# Update this with your stable site URL only for the production environment.
12+
NEXT_PUBLIC_SITE_URL=https://your-deployment-url.vercel.app

README.md

Lines changed: 114 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,123 @@ The all-in-one starter kit for high-performance SaaS applications.
1919

2020
![Architecture diagram](./public/architecture_diagram.svg)
2121

22-
## Deploy with Vercel
22+
## Step-by-step setup
2323

24-
The Vercel deployment will guide you through creating a Supabase account and project. After installing the Supabase integration, you'll need to configure Stripe with a few simple steps.
24+
When deploying this template, the sequence of steps is important. Follow the steps below in order to get up and running.
25+
26+
The process is slightly different depending upon whether you start from Github or you start from Vercel, but it should work either way.
2527

2628
**Note:** We're working on our Stripe integration. We've documented the required steps below under "Configure Stripe" until the integration is ready.
2729

28-
To get started, click the "Deploy with Vercel" button below.
30+
### Initiate Deployment
31+
32+
#### Initiate Vercel Deployment
33+
34+
To get started from Github, click the "Deploy" button below. You may do this either from your own fork or from the [original template repo](https://github.com/vercel/nextjs-subscription-payments).
35+
36+
Vercel will prompt you to create a new Github repo. If you've used a fork, you may link your Vercel deployment to your existing fork by selecting "Import a different Git repository" from the left menu. If you've used the master template or started from the [Vercel website](https://vercel.com/new/promptly-technologies/templates/next.js/subscription-starter) instead of Github, you may allow Vercel to create a repo for you.
37+
38+
#### Open a Codespace or clone the repo
39+
40+
Before proceeding with your Vercel deployment, you'll need to set up a [Supabase](https://supabase.com) project. For this step, it will be helpful to either open a Github codespace from your fork or clone the repo to your local machine. To clone the repo, use `git clone https://github.com/[your_username]/[your_repo_name]` and then `cd [your_repo_name]`.
2941

30-
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnextjs-subscription-payments&project-name=nextjs-subscription-payments&repo-name=nextjs-subscription-payments&demo-title=Next.js%20Subscription%20Payments%20Starter&demo-description=Demo%20project%20on%20Vercel&demo-url=https%3A%2F%2Fsubscription-payments.vercel.app&demo-image=https%3A%2F%2Fsubscription-payments.vercel.app%2Fdemo.png&integration-ids=oac_jUduyjQgOyzev1fjrW83NYOv&external-id=nextjs-subscription-payments)
42+
### Set up Supabase
3143

32-
[![Screenshot of Vercel deployment](./public/vercel-deploy.png)](https://vercel.com/new/git/external?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnextjs-subscription-payments&project-name=nextjs-subscription-payments&repo-name=nextjs-subscription-payments&demo-title=Next.js%20Subscription%20Payments%20Starter&demo-description=Demo%20project%20on%20Vercel&demo-url=https%3A%2F%2Fsubscription-payments.vercel.app&demo-image=https%3A%2F%2Fsubscription-payments.vercel.app%2Fdemo.png&integration-ids=oac_jUduyjQgOyzev1fjrW83NYOv&external-id=nextjs-subscription-payments)
44+
#### Create a Supabase project
3345

34-
Once the project has deployed, continue with the configuration steps below.
46+
On Supabase, create a Supabase account and create a project, with any name you like. Also generate an access token from https://app.supabase.com/account/tokens. You will need this later.
3547

36-
The initial build will fail due to missing Stripe environment variables. After configuring Stripe, redeploy the application.
3748

38-
## Configure Supabase Auth
49+
#### Run `schema.sql`
3950

40-
#### Setup redirect wildcards for deploy previews
51+
From your Github fork, copy the code from `schema.sql`. In your Supabase project, navigate to the SQL editor, click `New Query`, paste the code, and run the code. This will create the necessary tables and RLS policies in your Supabase database.
4152

42-
For auth redirects (magic links, OAuth providers) to work correctly in deploy previews, navigate to the auth settings (i.e. `https://app.supabase.com/project/:project-id/auth/url-configuration`) and add the following wildcard URL to "Redirect URLs": `https://**vercel.app/*/*`.
53+
#### Set up redirect wildcards for deploy previews
4354

44-
You can read more about redirect wildcard patterns in the [docs](https://supabase.com/docs/guides/auth#redirect-urls-and-wildcards).
55+
For auth redirects (magic links, OAuth providers) to work correctly in deploy previews, navigate to the auth settings (i.e. `https://app.supabase.com/project/:project-id/auth/url-configuration`) and add the following wildcard URL to "Redirect URLs": `https://**vercel.app/*/*`. (You can read more about redirect wildcard patterns in the [docs](https://supabase.com/docs/guides/auth#redirect-urls-and-wildcards).)
4556

4657
#### [Optional] - Set up OAuth providers
4758

48-
You can use third-party login providers like GitHub or Google. Refer to the [docs](https://supabase.io/docs/guides/auth#third-party-logins) to learn how to configure these. Once configured you can add them to the `provider` array of the `Auth` component on the [`signin.tsx`](./pages/signin.tsx) page.
59+
You can use third-party login providers like GitHub or Google. Refer to the [docs](https://supabase.io/docs/guides/auth#third-party-logins) to learn how to configure these. Once configured, you can add them to the `provider` array of the `Auth` component on the [`signin.tsx`](./pages/signin.tsx) page.
60+
61+
#### Generate types from your Supabase database
62+
63+
Now open a terminal in your codespace or cloned repo. You can use the [Supabase CLI](https://supabase.com/docs/reference/cli/usage#supabase-gen-types-typescript) to generate types from your Database by running:
64+
65+
1. To install Supabase cli
66+
67+
```bash
68+
npm install supabase --save-dev
69+
yarn add supabase --dev
70+
```
71+
72+
2. Connect to Supabase
73+
74+
```bash
75+
npx supabase login
76+
```
77+
78+
3. Enter the access token you created earlier. (As mentioned above, you can generate an access token from https://app.supabase.com/account/tokens.)
79+
80+
4. Generate types
81+
82+
```bash
83+
npx supabase gen types typescript --project-id [YOUR-PROJECT-REF] --schema public > types_db.ts
84+
```
85+
86+
#### Set up Supabase environment variables
87+
88+
Next, we need to set up environment variables for our Supabase project. We can copy these from `Supabase > Project Settings > API` and paste them into the Vercel deployment interface. Copy project API keys and paste into the `NEXT_PUBLIC_SUPABASE_ANON_KEY` and `SUPABASE_SERVICE_ROLE_KEY` fields, and copy the project URL and paste to Vercel as `NEXT_PUBLIC_SUPABASE_URL`.
89+
90+
This completes Supabase setup.
4991

50-
## Configure Stripe
92+
### Configure Stripe
5193

52-
To start developing your SaaS application, we'll need to configure Stripe to handle test payments. For the following steps, make sure you have the ["Test Mode" toggle](https://stripe.com/docs/testing) switched on.
94+
Next, we'll need to configure [Stripe](https://stripe.com/) to handle test payments. If you don't already have a Stripe account, create one now.
5395

54-
### Configure webhook
96+
For the following steps, make sure you have the ["Test Mode" toggle](https://stripe.com/docs/testing) switched on.
5597

56-
We need to configure the webhook pictured in the architecture diagram above. This webhook is the piece that connects Stripe to your Vercel Serverless Functions.
98+
#### Create a webhook
99+
100+
We need to create a webhook in the `Developers` section of Stripe. Pictured in the architecture diagram above, this webhook is the piece that connects Stripe to your Vercel Serverless Functions.
57101

58102
1. Click the "Add Endpoint" button on the [test Endpoints page](https://dashboard.stripe.com/test/webhooks).
59-
1. Set the endpoint URL to `https://your-deployment-url.vercel.app/api/webhooks`.
103+
1. Enter any placeholder text for the endpoint URL. (We will return later and change this to `https://your-deployment-url.vercel.app/api/webhooks` once we complete deployment to Vercel.)
60104
1. Click `Select events` under the `Select events to listen to` heading.
61105
1. Click `Select all events` in the `Select events to send` section.
62106
1. Copy `Signing secret` as we'll need that in the next step.
63107

64-
### Set environment variables
108+
#### Set Stripe environment variables
65109

66-
To securely interact with Stripe, we need to add a few [Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables) in the Vercel dashboard.
110+
To securely interact with Stripe, we need to add a few more [Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables) in the Vercel deployment interface.
67111

68112
- `NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY`
69113
- `STRIPE_SECRET_KEY`
70114
- `STRIPE_WEBHOOK_SECRET_LIVE`
71115

72116
You can find the first two keys on the [API keys tab](https://dashboard.stripe.com/test/apikeys) in Stripe. The `STRIPE_WEBHOOK_SECRET_LIVE` is the `Signing secret` copied in the previous webhook configuration step.
73117

74-
### Redeploy
118+
### Complete deployment
119+
120+
#### Complete Vercel deployment
75121

76-
We need to redeploy the application so that the latest environment variables are present.
122+
Once you've set your environment variables in the Vercel deployment interface, complete your deployment. Vercel may take a few minutes to build your application. It will then provide you with a domain URL for your deployment. Copy this URL and add it to .env.local:
123+
124+
```
125+
NEXT_PUBLIC_SITE_URL=https://your-deployment-url.vercel.app
126+
```
77127

78-
Redeploy your application by going to the deployments tab, finding your deployment, and clicking "redeploy."
128+
Keep the url on your clipboard, because you will also need it for the next step.
79129

80-
### Create product and pricing information
130+
*NOTE:* Vercel assigns you a domain that is stable from deployment to redeployment (`https://your-deployment-url.vercel.app`) and a dynamic URL that changes every time you redploy (e.g., `https://your-deployment-url.vercel-12345678-your-organization.app`). You want to use the stable one, not the dynamic one!
81131

82-
For Stripe to automatically bill your users for recurring payments, you need to create your product and pricing information in the [Stripe Dashboard](https://dashboard.stripe.com/test/products). When you create or update your product and price information, the changes automatically sync with your Supabase database.
132+
#### Complete Stripe webhook configuration
133+
134+
Now that we have a deployment URL, we can complete our Stripe webhook configuration. Go back to the Stripe [test Webhooks page](https://dashboard.stripe.com/test/webhooks). Click your endpoint, and then click `... > Update Details`. In the `Endpoint URL` field, paste your deployment URL and add `/api/webhooks` to the end. For example, if your deployment URL is `https://your-deployment-url.vercel.app`, then your endpoint URL should be `https://your-deployment-url.vercel.app/api/webhooks`. Click `Update endpoint`.
135+
136+
#### Create product and pricing information
137+
138+
Your application's webhook listens for product updates on Stripe and automatically propagates them to your Supabase database. So with your webhook listener running, you can now create your product and pricing information in the [Stripe Dashboard](https://dashboard.stripe.com/test/products).
83139

84140
Stripe Checkout currently supports pricing that bills a predefined amount at a specific interval. More complex plans (e.g., different pricing tiers or seats) are not yet supported.
85141

@@ -92,13 +148,11 @@ For example, you can create business models with different pricing tiers, e.g.:
92148
- Price 1: 20 USD per month
93149
- Price 2: 200 USD per year
94150

95-
#### Generate test data with the Stripe CLI
151+
Optionally, to speed up the setup, we have added a [fixtures file](fixtures/stripe-fixtures.json) to bootstrap test product and pricing data in your Stripe account. The [Stripe CLI](https://stripe.com/docs/stripe-cli#install) `fixtures` command executes a series of API requests defined in this JSON file. Simply run `stripe fixtures fixtures/stripe-fixtures.json`.
96152

97-
The [Stripe CLI](https://stripe.com/docs/stripe-cli#install) `fixtures` command executes a series of API requests defined in a JSON file. To speed up the setup, we have added a [fixtures file](fixtures/stripe-fixtures.json) to bootstrap test product and pricing data in your Stripe account. Simply run `stripe fixtures fixtures/stripe-fixtures.json`.
153+
**Important:** Be sure webhook forwarding is active when you create your products, or the products created will not be imported into your database.
98154

99-
**Important:** Be sure to start the webhook forwarding (see below) so that the products created by the fixtures command above are imported into your database.
100-
101-
### Configure the Stripe customer portal
155+
#### Configure the Stripe customer portal
102156

103157
1. Set your custom branding in the [settings](https://dashboard.stripe.com/settings/branding)
104158
1. Configure the Customer Portal [settings](https://dashboard.stripe.com/test/settings/billing/portal)
@@ -108,53 +162,13 @@ The [Stripe CLI](https://stripe.com/docs/stripe-cli#install) `fixtures` command
108162
1. Add the products and prices that you want
109163
1. Set up the required business information and links
110164

111-
### Generate types from your Supabase database
112-
113-
You can use the [Supabase CLI](https://supabase.com/docs/reference/cli/usage#supabase-gen-types-typescript) to generate types from your Database by running
114-
115-
1. To install supabase cli
116-
117-
```bash
118-
npm install supabase --save-dev
119-
yarn add supabase --dev
120-
```
121-
122-
2. Connect to supabase
123-
124-
```bash
125-
npx supabase login
126-
```
127-
128-
3. Enter your access token. You can generate an access token from https://app.supabase.com/account/tokens
129-
4. Generate types
130-
131-
```bash
132-
npx supabase gen types typescript --project-id [YOUR-PROJECT-REF] --schema public > types_db.ts
133-
```
134-
135165
### That's it
136166

137-
That's it. Now you're ready to earn recurring revenue from your customers 🥳
138-
139-
## Going live
140-
141-
### Archive testing products
142-
143-
Archive all test mode Stripe products before going live. Before creating your live mode products, make sure to follow the steps below to set up your live mode env vars and webhooks.
144-
145-
### Configure production environment variables
146-
147-
To run the project in live mode and process payments with Stripe, modify the environment variables from Stripe "test mode" to "production mode." After switching the variables, be sure to redeploy the application.
148-
149-
To verify you are running in production mode, test checking out with the [Stripe test card](https://stripe.com/docs/testing). The test card should not work.
150-
151-
### Redeploy
152-
153-
Afterward, you will need to rebuild your production deployment for the changes to take effect. Within your project Dashboard, navigate to the "Deployments" tab, select the most recent deployment, click the overflow menu button (next to the "Visit" button) and select "Redeploy."
167+
That's it. Now you're ready to earn recurring revenue from your customers. 🥳
154168

155169
## Develop locally
156170

157-
Deploying with Vercel will create a repository for you, which you can clone to your local machine.
171+
If you haven't already done so, clone your Github repository to your local machine.
158172

159173
Next, use the [Vercel CLI](https://vercel.com/download) to link your project:
160174

@@ -171,13 +185,13 @@ Use the Vercel CLI to download the development env vars:
171185
vercel env pull .env.local
172186
```
173187

174-
Running this command will create a new `.env.local` file in your project folder. For security purposes, you will need to set the `SUPABASE_SERVICE_ROLE_KEY` manually from your [Supabase dashboard](https://app.supabase.io/) (Settings > API). Lastly, the webhook secret differs for local testing vs. when deployed to Vercel. Follow the instructions below to get the corresponding webhook secret.
188+
Running this command will create a new `.env.local` file in your project folder. For security purposes, you will need to set the `SUPABASE_SERVICE_ROLE_KEY` manually from your [Supabase dashboard](https://app.supabase.io/) (`Settings > API`).
175189

176190
### Use the Stripe CLI to test webhooks
177191

178-
First [install the CLI](https://stripe.com/docs/stripe-cli) and [link your Stripe account](https://stripe.com/docs/stripe-cli#login-account).
192+
[Install the Stripe CLI](https://stripe.com/docs/stripe-cli) and [link your Stripe account](https://stripe.com/docs/stripe-cli#login-account).
179193

180-
Next, start the webhook forwarding:
194+
Next, start local webhook forwarding:
181195

182196
```bash
183197
stripe listen --forward-to=localhost:3000/api/webhooks
@@ -187,10 +201,36 @@ Running this Stripe command will print a webhook secret (such as, `whsec_***`) t
187201

188202
### Install dependencies and run the Next.js client
189203

204+
In a separate terminal, run the following commands to install dependencies and start the development server:
205+
190206
```bash
191207
npm install
192208
npm run dev
193209
# or
194210
yarn
195211
yarn dev
196212
```
213+
214+
Note that webhook forwarding and the development server must be running concurrently in two separate terminals for the application to work correctly.
215+
216+
Finally, navigate to [http://localhost:3000](http://localhost:3000) in your browser to see the application rendered.
217+
218+
## Going live
219+
220+
### Archive testing products
221+
222+
Archive all test mode Stripe products before going live. Before creating your live mode products, make sure to follow the steps below to set up your live mode env vars and webhooks.
223+
224+
### Configure production environment variables
225+
226+
To run the project in live mode and process payments with Stripe, switch Stripe from "test mode" to "production mode." Your Stripe API keys will be different in production mode, and you will have to create a separate production mode webhook. Copy these values and paste them into Vercel, replacing the test mode values.
227+
228+
### Redeploy
229+
230+
Afterward, you will need to rebuild your production deployment for the changes to take effect. Within your project Dashboard, navigate to the "Deployments" tab, select the most recent deployment, click the overflow menu button (next to the "Visit" button) and select "Redeploy."
231+
232+
To verify you are running in production mode, test checking out with the [Stripe test card](https://stripe.com/docs/testing). The test card should not work.
233+
234+
## A note on reliability
235+
236+
This template mirrors completed Stripe transactions to the Supabase database. This means that if the Supabase database is unavailable, the Stripe transaction will still succeed, but the Supabase database will not be updated, and the application will pass an error code back to Stripe. [By default](https://stripe.com/docs/webhooks/best-practices), Stripe will retry sending its response to the webhook for up to three days, or until the database update succeeds. This means that the Stripe transaction will eventually be reflected in the Supabase database as long as the database comes back online within three days. You may want to implement a process to automatically reconcile the Supabase database with Stripe in case of a prolonged outage.

0 commit comments

Comments
 (0)