
Stripe Setup
Overview
This guide will help you set up Stripe integration in your application for handling payments and subscriptions.
Prerequisites
- A Stripe account (create one at stripe.com)
- Stripe CLI installed for webhook testing
- Basic understanding of Stripe concepts
Environment Variables
Add the following environment variables to your .env.local
file:
# Stripe API KeysSTRIPE_SECRET_KEY=sk_test_...NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_...STRIPE_WEBHOOK_SECRET=whsec_...# Your application URLNEXT_PUBLIC_SITE_URL=http://localhost:3000
Stripe Client Setup
The Stripe client is initialized in src/lib/stripe/client.ts
:
import Stripe from "stripe";export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {apiVersion: "2023-10-16",typescript: true,});
Testing Stripe Integration
Use test card numbers for development:
Test Card Success: 4242 4242 4242 4242
Test Card Failure: 4000 0000 0000 0002
Test 3D Secure: 4000 0000 0000 3220
Webhook Development
Use the Stripe CLI to test webhooks locally:
# Login to Stripe CLIstripe login# Forward webhooks to your local serverstripe listen --forward-to localhost:3000/api/webhooks/stripe