Prerequisites
Before starting, ensure you have a Stripe account and Node.js 16+ installed.
Step 1: Install Dependencies
npm install stripe @stripe/react-stripe-js
Step 2: Set Up Environment Variables
Add your Stripe keys to .env.local: STRIPE_SECRET_KEY, NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY
Step 3: Create Checkout Session
Build an API route that creates a Stripe Checkout session with your pricing plans.
Step 4: Handle Webhooks
Set up webhook handling at /api/stripe/webhook to listen for checkout.session.completed events.
Step 5: Update Database
On successful payment, update your user database with their subscription status and plan.
Best Practices
Always use the Stripe client library for security. Never store card details. Use webhooks for asynchronous updates.