This guide explains how to set up Stripe subscriptions for the Workouts application.
- A Stripe account (can be a test account for development)
- A Supabase project with the database migrations applied
Set up the Stripe environment variables in your .env file (based on .env.example if needed.)
- Log in to your Stripe Dashboard: https://dashboard.stripe.com/
- Go to Product Catalogue > Create Product
- Create the following products and prices:
- Name: "Workouts Pro - Monthly"
- Description: "Full access to all workout features with cloud sync"
- Price:
- Currency: USD
- Amount: $8.99
- Recurring: Monthly
- Save the price ID (starts with "price_") and update in
pricing-plans.ts
- Name: "Workouts Pro - Annual"
- Description: "Full access with 2 months free"
- Price:
- Currency: USD
- Amount: $89.99
- Recurring: Annual
- Save the price ID (starts with "price_") and update in
pricing-plans.ts
- In the Stripe Dashboard, go to Developers > Webhooks
- Add an endpoint with your application's URL:
- Local development: Use Stripe CLI or a tunneling service like ngrok
- Production: https://your-domain.com/api/subscriptions/webhook
- Add these events to listen for:
- customer.subscription.created
- customer.subscription.updated
- customer.subscription.deleted
- checkout.session.completed
- Copy the Webhook Secret and add it to your
.envfile asSTRIPE_WEBHOOK_SECRET
Open /src/lib/subscription/pricing-plans.ts and update the Stripe product and price IDs with your actual values from Stripe:
export const pricingPlans: PricingPlan[] = [
// ... free plan ...
{
id: 'monthly',
// ... other properties ...
stripe_product_id: 'prod_your_monthly_product_id',
stripe_price_id: 'price_your_monthly_price_id',
},
{
id: 'yearly',
// ... other properties ...
stripe_product_id: 'prod_your_yearly_product_id',
stripe_price_id: 'price_your_yearly_price_id',
}
];- Use Stripe's test credit card numbers for testing:
- Successful payment: 4242 4242 4242 4242
- Failed payment: 4000 0000 0000 0002
- Set an expiration date in the future, any CVC, and any billing address
- Ensure your server is running and can receive webhook events
- Use the Stripe CLI to forward events to your local server:
stripe listen --forward-to localhost:5173/api/subscriptions/webhook
- Copy the webhook signing secret from the CLI output and add it to your
.envfile asSTRIPE_WEBHOOK_SECRET - Test the webhook by triggering events from the Stripe Dashboard or using the CLI:
stripe trigger customer.subscription.created
- Check your server logs to confirm that the webhook was received and processed correctly
- Verify that the subscription status is updated in your Supabase database
- Check the Stripe Dashboard for the event logs to see if the webhook was successful
- Ensure that the subscription status is updated in your Supabase database
Before going live:
- Switch your Stripe keys from test to production
- Update your webhook endpoint to your production URL
- Test the full subscription flow in your production environment
- Check webhook logs in Stripe Dashboard
- Ensure your webhook secret is correctly configured
- Verify that your server can receive POST requests at the webhook endpoint
- Validate that the product and price IDs are correct
- Check the Stripe dashboard for subscription status
- Look for error logs in your application server