|
| 1 | +# Stripe Subscription Setup Guide |
| 2 | + |
| 3 | +This guide explains how to set up Stripe subscriptions for the Workouts application. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. A Stripe account (can be a test account for development) |
| 8 | +2. A Supabase project with the database migrations applied |
| 9 | + |
| 10 | +## Environment Variables |
| 11 | + |
| 12 | +Add the following environment variables to your `.env` file in the app directory: |
| 13 | + |
| 14 | +```bash |
| 15 | +# Stripe API keys |
| 16 | +STRIPE_PUBLISHABLE_KEY=pk_test_your_publishable_key |
| 17 | +STRIPE_SECRET_KEY=sk_test_your_secret_key |
| 18 | +STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret |
| 19 | + |
| 20 | +# Supabase Service Role Key (for webhook operations) |
| 21 | +SUPABASE_SERVICE_ROLE_KEY=your_supabase_service_role_key |
| 22 | +SUPABASE_URL=your_supabase_url |
| 23 | +``` |
| 24 | + |
| 25 | +> Note: For backward compatibility, the application also supports the legacy `VITE_` prefixed versions of these variables, but the non-prefixed versions are recommended. |
| 26 | +
|
| 27 | +## Stripe Product and Price Setup |
| 28 | + |
| 29 | +1. Log in to your Stripe Dashboard: https://dashboard.stripe.com/ |
| 30 | +2. Go to Product Catalogue > Create Product |
| 31 | +3. Create the following products and prices: |
| 32 | + |
| 33 | +### Monthly Subscription |
| 34 | +- Name: "Workouts Pro - Monthly" |
| 35 | +- Description: "Full access to all workout features with cloud sync" |
| 36 | +- Price: |
| 37 | + - Currency: USD |
| 38 | + - Amount: $8.99 |
| 39 | + - Recurring: Monthly |
| 40 | + - Save the price ID (starts with "price_") and update in `pricing-plans.ts` |
| 41 | + |
| 42 | +### Annual Subscription |
| 43 | +- Name: "Workouts Pro - Annual" |
| 44 | +- Description: "Full access with 2 months free" |
| 45 | +- Price: |
| 46 | + - Currency: USD |
| 47 | + - Amount: $89.99 |
| 48 | + - Recurring: Annual |
| 49 | + - Save the price ID (starts with "price_") and update in `pricing-plans.ts` |
| 50 | + |
| 51 | +## Webhook Setup |
| 52 | + |
| 53 | +1. In the Stripe Dashboard, go to Developers > Webhooks |
| 54 | +2. Add an endpoint with your application's URL: |
| 55 | + - Local development: Use Stripe CLI or a tunneling service like ngrok |
| 56 | + - Production: https://your-domain.com/api/subscriptions/webhook |
| 57 | +3. Add these events to listen for: |
| 58 | + - customer.subscription.created |
| 59 | + - customer.subscription.updated |
| 60 | + - customer.subscription.deleted |
| 61 | + - checkout.session.completed |
| 62 | +4. Copy the Webhook Secret and add it to your `.env` file as `STRIPE_WEBHOOK_SECRET` |
| 63 | + |
| 64 | +## Update Price IDs in the Application |
| 65 | + |
| 66 | +Open `/src/lib/subscription/pricing-plans.ts` and update the Stripe product and price IDs with your actual values from Stripe: |
| 67 | + |
| 68 | +```typescript |
| 69 | +export const pricingPlans: PricingPlan[] = [ |
| 70 | + // ... free plan ... |
| 71 | + { |
| 72 | + id: 'monthly', |
| 73 | + // ... other properties ... |
| 74 | + stripe_product_id: 'prod_your_monthly_product_id', |
| 75 | + stripe_price_id: 'price_your_monthly_price_id', |
| 76 | + }, |
| 77 | + { |
| 78 | + id: 'yearly', |
| 79 | + // ... other properties ... |
| 80 | + stripe_product_id: 'prod_your_yearly_product_id', |
| 81 | + stripe_price_id: 'price_your_yearly_price_id', |
| 82 | + } |
| 83 | +]; |
| 84 | +``` |
| 85 | + |
| 86 | +## Testing Subscriptions |
| 87 | + |
| 88 | +1. Use Stripe's test credit card numbers for testing: |
| 89 | + - Successful payment: 4242 4242 4242 4242 |
| 90 | + - Failed payment: 4000 0000 0000 0002 |
| 91 | +2. Set an expiration date in the future, any CVC, and any billing address |
| 92 | + |
| 93 | +### Listening to Webhook Events |
| 94 | + |
| 95 | +- Ensure your server is running and can receive webhook events |
| 96 | +- Use the Stripe CLI to forward events to your local server: |
| 97 | + ```bash |
| 98 | + stripe listen --forward-to localhost:5173/api/subscriptions/webhook |
| 99 | + ``` |
| 100 | +- Copy the webhook signing secret from the CLI output and add it to your `.env` file as `STRIPE_WEBHOOK_SECRET` |
| 101 | +- Test the webhook by triggering events from the Stripe Dashboard or using the CLI: |
| 102 | + ```bash |
| 103 | + stripe trigger customer.subscription.created |
| 104 | + ``` |
| 105 | +- Check your server logs to confirm that the webhook was received and processed correctly |
| 106 | +- Verify that the subscription status is updated in your Supabase database |
| 107 | +- Check the Stripe Dashboard for the event logs to see if the webhook was successful |
| 108 | +- Ensure that the subscription status is updated in your Supabase database |
| 109 | + |
| 110 | + |
| 111 | +## Going Live |
| 112 | + |
| 113 | +Before going live: |
| 114 | + |
| 115 | +1. Switch your Stripe keys from test to production |
| 116 | +2. Update your webhook endpoint to your production URL |
| 117 | +3. Test the full subscription flow in your production environment |
| 118 | + |
| 119 | +## Troubleshooting |
| 120 | + |
| 121 | +### Webhook Issues |
| 122 | +- Check webhook logs in Stripe Dashboard |
| 123 | +- Ensure your webhook secret is correctly configured |
| 124 | +- Verify that your server can receive POST requests at the webhook endpoint |
| 125 | + |
| 126 | +### Subscription Issues |
| 127 | +- Validate that the product and price IDs are correct |
| 128 | +- Check the Stripe dashboard for subscription status |
| 129 | +- Look for error logs in your application server |
0 commit comments