Stripe configuration in web app
How to configure Stripe in web app in development and production modes.
Payment is the most important feature for the web app. We use Stripe for the payment processing and we want to be sure that all payments are processed correctly not to loose any money. The following are the environment variables that are required for the Stripe payment processing.
We have .env.development.local
and .env.production.local
for the development and production environment.
Development
To test Stripe payments on local we need to save two keys: STRIPE_API_KEY
and STRIPE_WEBHOOK_SECRET
.
Create API keys
- Go to Stripe Dev Dashboard
- Select the API keys tab.
- In the Standard keys list, in the Secret key row, click Reveal test key.
- Copy the key value by clicking it.
- Save the key value in the
.env.development.local
file asSTRIPE_API_KEY
.
Recommended to check Stripe Key generation guide in the official Stripe documentation
- Return to Stripe Dev Dashboard
- Select the Webhooks keys tab.
- Press
+ Add local listener
button. - Find in Sample endpoint the key in code row
const endpointSecret = "whsec_SUPER_SECRET_KEY";
. Copywhsec_SUPER_SECRET_KEY
value. - Save the key value in the
.env.development.local
file asSTRIPE_WEBHOOK_SECRET
.
Install Stripe CLI
We need to install Stripe CLI to test the webhook locally.
- Install Stripe CLI with preferred method from the official Stripe CLI
- For examples for macOS we can use Homebrew
- Login in to Stripe CLI. More detailed steps in the official Stripe Log in to the CLI
- We're done! Now we can test the webhook locally.
Testing purchase webhook and user registration locally
- Make sure that local Supabase docker containers are running. Check Supabase section in docs.
- Make sure that you logged-in to Stripe CLI.
- Run the following command to listen the webhook locally.
where localhost:3000
is the address where our NextJS app is running.
You should see similar to next output:
- Now go to
sign-up
page on local localhost:3000/sign-up and make a purchase. - For test purchase enter any email and for
Card Information
use the following default test Stripe card details:
- Card number (strict): 4242 4242 4242 4242
- Expiration date (any date in future): 01/27
- CVC (any 3 digits): 123
More info about Simulate payments to test your integration you can find in the official Stripe documentation
- After successful purchase you should see the next output in the terminal
charge.succeeded
: