This guide will help you set up Stripe payment gateway for testing orders, subscriptions, and analytics.
- Go to https://stripe.com
- Sign up for a free account
- Complete the account setup
- Log in to your Stripe Dashboard
- Navigate to Developers → API keys
- You'll see two keys:
- Publishable key (starts with
pk_test_for test mode) - Secret key (starts with
sk_test_for test mode)
- Publishable key (starts with
Add these to your .env.local file:
# Stripe Configuration
STRIPE_SECRET_KEY=sk_test_your_secret_key_here
NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_your_publishable_key_here
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here
# Required for payment redirects
NEXTAUTH_URL=http://localhost:3000-
Install Stripe CLI:
# macOS brew install stripe/stripe-cli/stripe # Windows # Download from https://github.com/stripe/stripe-cli/releases
-
Login to Stripe:
stripe login
-
Forward webhooks to your local server:
stripe listen --forward-to localhost:3000/api/stripe/webhook
-
Copy the webhook signing secret (starts with
whsec_) and add it to your.env.local:STRIPE_WEBHOOK_SECRET=whsec_...
- In Stripe Dashboard, go to Developers → Webhooks
- Click Add endpoint
- Enter your endpoint URL:
https://yourdomain.com/api/stripe/webhook - Select events to listen for:
checkout.session.completedpayment_intent.succeededpayment_intent.payment_failed
- Copy the webhook signing secret and add it to your production environment variables
Stripe provides test cards for testing different scenarios:
- Card Number:
4242 4242 4242 4242 - Expiry: Any future date (e.g.,
12/25) - CVC: Any 3 digits (e.g.,
123) - ZIP: Any 5 digits (e.g.,
12345)
- Card Number:
4000 0000 0000 0002 - Expiry: Any future date
- CVC: Any 3 digits
- ZIP: Any 5 digits
- Card Number:
4000 0025 0000 3155 - Expiry: Any future date
- CVC: Any 3 digits
- ZIP: Any 5 digits
- Add products to cart
- Go to checkout (
/checkout) - Fill in delivery information
- Click "Proceed to Payment"
- You'll be redirected to Stripe Checkout
- Use test card:
4242 4242 4242 4242 - Complete payment
- You'll be redirected back to
/payment/success - Order status will be updated to "processing" via webhook
- Checkout page integrated with Stripe
- Order creation before payment
- Payment processing via Stripe Checkout
- Webhook confirmation updates order status
- Subscription creation with Stripe
- Recurring billing setup
- Subscription management
- Orders are tracked in the database
- Payment status updates automatically
- Order history available in admin panel
- Check API Keys: Ensure
STRIPE_SECRET_KEYis set correctly - Check Webhook: Verify webhook secret is correct
- Check Console: Look for errors in browser console and server logs
- Local Development: Use Stripe CLI to forward webhooks
- Production: Ensure webhook endpoint is publicly accessible
- Verify Secret: Check
STRIPE_WEBHOOK_SECRETmatches Stripe dashboard
- Check NEXTAUTH_URL: Must match your domain
- Check Success URL: Should be
/payment/success - Check Cancel URL: Should be
/payment/cancelled
- Test Mode: All test transactions use
sk_test_andpk_test_keys - No Real Charges: Test mode never charges real cards
- Webhook Required: For automatic order status updates, webhook must be configured
- Currency: All payments are in ZAR (South African Rand)
When ready for production:
- Switch to Live mode in Stripe Dashboard
- Get your live API keys (start with
sk_live_andpk_live_) - Update environment variables with live keys
- Set up production webhook endpoint
- Test with real card (small amount first!)
Need Help? Check Stripe's documentation: https://stripe.com/docs