Prerequisites
- A Stripe account (sign up here)
- Your ShipFree application running locally or deployed
Setup Instructions
Step 1: Get Your API Keys
- Log in to your Stripe Dashboard
- Navigate to Developers → API keys
- Copy your Secret key (starts with
sk_test_for test mode) - You’ll also need the Publishable key later (starts with
pk_test_)
Step 2: Create Products and Prices
You need to create products in Stripe for each pricing plan.- Go to Products in your Stripe Dashboard
- Click Add product
- Create products matching your plans (Starter, Pro, Enterprise)
- Set the name (e.g., “Starter Plan”)
- Set the description
- Add pricing:
- Monthly: $9.90/month (recurring)
- Yearly: $99/year (recurring)
- Set the billing period
- Save and copy the Price ID (starts with
price_)
Step 3: Configure Environment Variables
Add these variables to your.env file:
The
NEXT_PUBLIC_ prefix makes these variables accessible in the browser. Only use it for non-sensitive data like Price IDs.Step 4: Update Payment Configuration
The price IDs are automatically read from environment variables insrc/config/payments.ts. Verify your configuration:
Step 5: Set Up Webhooks
Stripe uses webhooks to notify your application about events (payments, subscription changes, etc.).For Local Development
-
Install the Stripe CLI:
-
Log in to Stripe CLI:
-
Forward webhooks to your local server:
-
The CLI will output a webhook signing secret (starts with
whsec_). Add it to your.env:
For Production
- Go to Developers → Webhooks in your Stripe Dashboard
- Click Add endpoint
-
Set the endpoint URL:
https://yourdomain.com/api/webhooks/payments -
Select events to listen for:
customer.createdcustomer.updatedcustomer.deletedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.payment_succeededinvoice.payment_failedcheckout.session.completed
- Click Add endpoint
- Copy the Signing secret and add it to your production environment variables
Testing in Development
Test Mode
Stripe provides test mode by default. Use test API keys (starting withsk_test_ and pk_test_).
Test Cards
Use these test card numbers for testing:| Card Number | Scenario |
|---|---|
4242 4242 4242 4242 | Successful payment |
4000 0025 0000 3155 | Requires authentication (3D Secure) |
4000 0000 0000 9995 | Declined (insufficient funds) |
4000 0000 0000 0341 | Declined (incorrect CVC) |
- Use any future expiration date
- Use any 3-digit CVC
- Use any valid ZIP code
Testing Subscriptions
-
Start your development server:
-
In another terminal, start Stripe webhook forwarding:
- Navigate to your pricing page and create a checkout session
- Use a test card to complete the payment
- Verify the webhook events are received and processed
Testing the Customer Portal
The Stripe Customer Portal allows customers to manage their subscriptions:- Update payment methods
- View invoices
- Cancel subscriptions
- Update billing information
Stripe Implementation Details
The Stripe adapter is implemented insrc/lib/payments/providers/stripe.ts.
Key Features
Checkout Sessions
Creates Stripe Checkout sessions with:- Support for one-time and recurring payments
- Trial periods
- Promotional codes
- Automatic tax calculation (if configured)
Customer Management
- Automatically creates or retrieves customers
- Links customers to your user records
- Stores customer metadata
Subscription Handling
- Retrieves subscription details
- Maps Stripe status to unified status
- Handles seat-based billing
- Supports immediate or end-of-period cancellation
Webhook Processing
The adapter processes these Stripe webhook events:customer.created,customer.updated,customer.deletedcustomer.subscription.created,customer.subscription.updated,customer.subscription.deletedinvoice.payment_succeeded,invoice.payment_failedcheckout.session.completed
Going Live
Pre-Launch Checklist
- Replace test API keys with live keys (
sk_live_,pk_live_) - Create live products and prices in Stripe
- Update environment variables with live price IDs
- Configure production webhook endpoint
- Set up webhook signing secret for production
- Enable Stripe Radar for fraud prevention
- Configure email receipts in Stripe Dashboard
- Set up tax settings (if applicable)
- Test the complete checkout flow in production
Security Best Practices
- Never expose secret keys: Keep
STRIPE_SECRET_KEYserver-side only - Validate webhooks: Always verify webhook signatures
- Use HTTPS: Stripe requires HTTPS for production webhooks
- Handle errors gracefully: Don’t expose Stripe errors to users
- Log securely: Don’t log sensitive payment information
Troubleshooting
Webhook Not Received
- Check that Stripe CLI is running (for local dev)
- Verify webhook endpoint is accessible
- Check webhook logs in Stripe Dashboard
- Ensure
STRIPE_WEBHOOK_SECRETis set correctly
Checkout Session Fails
- Verify
STRIPE_SECRET_KEYis set - Check that price IDs exist and are correct
- Ensure products are active in Stripe Dashboard
- Check Stripe Dashboard logs for detailed errors
Customer Portal Not Working
- Ensure customer exists in Stripe
- Verify customer ID format (remove
stripe_prefix if present) - Check that customer has an active subscription
Additional Resources
- Stripe API Documentation
- Stripe Testing Guide
- Stripe Webhook Best Practices
- Stripe Customer Portal
- Stripe Checkout
Support
For Stripe-specific issues: For ShipFree integration issues:- Check the source code in
src/lib/payments/providers/stripe.ts - Review webhook handling in
src/app/api/webhooks/payments/route.ts