mirror of
https://github.com/Oak-and-Sprout/sprout-track.git
synced 2026-02-08 08:38:37 -06:00
7.4 KiB
7.4 KiB
Stripe Payment Integration Setup Guide
This guide will help you set up Stripe payments for Sprout Track.
Overview
The Stripe integration allows users to:
- Subscribe to monthly or annual plans
- Purchase lifetime access with a one-time payment
- Manage their subscriptions (view status, cancel)
- View payment methods and renewal dates
Prerequisites
- A Stripe account (sign up at stripe.com)
- Node.js and npm installed
- Access to your
.envfile
Setup Steps
1. Get Your Stripe API Keys
- Log in to your Stripe Dashboard
- Click on Developers in the left sidebar
- Click on API keys
- Copy your Publishable key and Secret key
- For testing, use the test keys (they start with
pk_test_andsk_test_) - For production, use live keys (they start with
pk_live_andsk_live_)
- For testing, use the test keys (they start with
2. Create Products and Prices in Stripe
-
In the Stripe Dashboard, go to Products → Add product
-
Create three products:
Monthly Subscription:
- Name: "Monthly Subscription"
- Description: "Access to all Sprout Track features"
- Pricing: Recurring, $4.99/month
- Copy the Price ID (starts with
price_)
Annual Subscription:
- Name: "Annual Subscription"
- Description: "Best value - save 33%"
- Pricing: Recurring, $39.99/year
- Copy the Price ID (starts with
price_)
Lifetime Access:
- Name: "Lifetime Access"
- Description: "One-time payment for lifetime access"
- Pricing: One-time, $99.99
- Copy the Price ID (starts with
price_)
3. Set Up Webhook
- In the Stripe Dashboard, go to Developers → Webhooks
- Click Add endpoint
- Set the endpoint URL to:
https://yourdomain.com/api/accounts/payments/webhook- For local testing: Use Stripe CLI to forward webhooks
- Select these events to listen to:
checkout.session.completedcustomer.subscription.createdcustomer.subscription.updatedcustomer.subscription.deletedinvoice.payment_succeededinvoice.payment_failed
- Copy the Signing secret (starts with
whsec_)
4. Configure Environment Variables
Add the following to your .env 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"
# Stripe Price IDs
NEXT_PUBLIC_STRIPE_MONTHLY_PRICE_ID="price_your_monthly_price_id"
NEXT_PUBLIC_STRIPE_ANNUAL_PRICE_ID="price_your_annual_price_id"
NEXT_PUBLIC_STRIPE_LIFETIME_PRICE_ID="price_your_lifetime_price_id"
# Application URL (for Stripe redirects)
NEXT_PUBLIC_APP_URL="https://yourdomain.com"
# For local development:
# NEXT_PUBLIC_APP_URL="http://localhost:3000"
5. Test the Integration
Local Testing with Stripe CLI
- Install the Stripe CLI
- Log in to Stripe CLI:
stripe login - Forward webhooks to your local server:
stripe listen --forward-to localhost:3000/api/accounts/payments/webhook - Use the webhook signing secret provided by the CLI in your
.envfile
Test Cards
Use these test card numbers in Stripe Checkout:
- Success: 4242 4242 4242 4242
- Decline: 4000 0000 0000 0002
- Requires authentication: 4000 0025 0000 3155
Use any future expiration date, any 3-digit CVC, and any zip code.
6. Verify the Integration
- Start your Next.js application
- Log in to your account
- Navigate to Account Manager
- Click "Upgrade Plan" or "Manage Subscription"
- Select a plan and complete the checkout process
- Verify:
- Payment success page appears
- Account status updates in the Account Manager
- Subscription details are visible
- Webhook events are received in Stripe Dashboard
Features
For Users
- Payment Modal: Clean, modern UI for selecting plans
- Multiple Payment Options:
- Monthly subscription ($4.99/month)
- Annual subscription ($39.99/year) - best value
- Lifetime access ($99.99) - one-time payment
- Subscription Management:
- View current plan and expiration date
- See payment method details
- Cancel subscription (access until period end)
- Trial to Paid: Seamless upgrade from trial accounts
For Developers
- Secure Payment Processing: All payment data handled by Stripe
- Webhook Integration: Automatic subscription updates
- Account Status Tracking: Real-time subscription state
- Error Handling: Comprehensive error messages and retry logic
- Type Safety: Full TypeScript support
API Endpoints
Created Endpoints
-
POST /api/accounts/payments/create-checkout-session
- Creates Stripe Checkout session
- Requires:
priceId,planType - Returns:
sessionIdfor redirect
-
POST /api/accounts/payments/webhook
- Handles Stripe webhook events
- Updates account subscription status
- No authentication (uses webhook signature)
-
GET /api/accounts/payments/subscription-status
- Retrieves current subscription details
- Returns: plan info, renewal date, payment method
-
POST /api/accounts/payments/cancel-subscription
- Cancels active subscription
- Maintains access until period end
Database Schema
The integration uses existing Prisma schema fields:
model Account {
stripeCustomerId String? // Stripe customer ID
subscriptionId String? // Current subscription ID
planType String? // 'sub' or 'full'
planExpires DateTime? // Subscription end date
trialEnds DateTime? // Trial end date
}
Security Considerations
- Webhook Signature Verification: All webhooks are verified using Stripe signatures
- Account Owner Authentication: Payment endpoints require account owner access
- PCI Compliance: No card data stored or handled by your application
- Environment Variables: Sensitive keys stored in environment variables
Troubleshooting
Webhook Not Working
- Verify webhook URL is accessible publicly
- Check webhook signing secret is correct
- View webhook logs in Stripe Dashboard
- Use Stripe CLI for local testing
Payment Not Updating Account
- Check webhook events are being received
- View webhook event details in Stripe Dashboard
- Check server logs for webhook handler errors
- Verify account ID is in session metadata
Checkout Session Creation Fails
- Verify Price IDs are correct
- Check Stripe API keys are valid
- Ensure products are active in Stripe
- Check server logs for detailed error messages
Going to Production
- Switch from test keys to live keys in
.env - Update webhook endpoint to production URL
- Test with real payment methods
- Set up proper error monitoring
- Configure billing emails in Stripe
- Set up customer portal for self-service management (optional)
Support
For Stripe-specific issues:
For application-specific issues:
- Check server logs
- Review webhook event logs in Stripe Dashboard
- Contact development team
Future Enhancements
Potential improvements to consider:
- Customer portal integration for self-service
- Proration handling for plan changes
- Coupon/discount code support
- Multiple payment methods per customer
- Usage-based billing
- Team/family plan management