Spaces:
Runtime error
Runtime error
File size: 7,833 Bytes
17847d4 f886e66 17847d4 f886e66 17847d4 f886e66 17847d4 f886e66 17847d4 f886e66 17847d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | # Stripe Subscription System Setup Guide
This guide will help you set up Stripe for the AutoForm subscription system.
## Overview
The AutoForm subscription system includes three tiers:
- **Free Tier**: $0/month, 25 credits
- **Pro Tier**: $20/month, 500 credits
- **Ultra Tier**: Custom pricing, 1000 credits
## Prerequisites
- A Stripe account (create one at https://stripe.com)
- Stripe CLI for webhook testing (optional but recommended)
## Step 1: Create Products and Prices in Stripe
### 1.1 Log in to Stripe Dashboard
Go to https://dashboard.stripe.com and log in to your account.
### 1.2 Create Products
Navigate to **Products** → **Add Product** and create three products:
#### Free Tier
- **Name**: AutoForm Free
- **Description**: Free tier with 25 credits per month
- **Pricing**:
- Price: $0.00
- Billing period: Monthly
- Note: This is for tracking only, users won't be charged
#### Pro Tier
- **Name**: AutoForm Pro
- **Description**: Professional tier with 500 credits per month
- **Pricing**:
- Price: $20.00
- Billing period: Monthly (recurring)
#### Ultra Tier
- **Name**: AutoForm Ultra
- **Description**: Ultra tier with 1000 credits per month
- **Pricing**:
- Price: $29.99 (or your preferred amount)
- Billing period: Monthly (recurring)
### 1.3 Get Price IDs
After creating each product, Stripe will assign a **Price ID** to each. They look like:
- `price_1234567890abcdef` (this is an example)
Copy these Price IDs - you'll need them for the environment variables.
### 1.4 Get Product IDs (Optional)
You can also copy the **Product IDs** if you want to store them. They look like:
- `prod_1234567890abcdef`
## Step 2: Get API Keys
### 2.1 Get Secret Key
1. Navigate to **Developers** → **API Keys**
2. Copy your **Secret key** (starts with `sk_test_` for test mode or `sk_live_` for live mode)
3. Keep this key secure - never commit it to version control!
### 2.2 Get Webhook Secret
1. Navigate to **Developers** → **Webhooks**
2. Click **Add endpoint**
3. Set the endpoint URL to: `https://your-domain.com/api/payment/webhook`
- For local testing, you can use the Stripe CLI (see Step 3)
4. Select the following events to listen for:
- `checkout.session.completed`
- `customer.subscription.updated`
- `customer.subscription.deleted`
- `invoice.payment_succeeded`
- `invoice.payment_failed`
5. After creating the endpoint, click to view it and copy the **Signing secret** (starts with `whsec_`)
## Step 3: Configure Environment Variables
Create or update your `backend/.env` file with the following variables:
```env
# Stripe Configuration
STRIPE_SECRET_KEY=sk_test_your_secret_key_here
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret_here
# Stripe Price IDs
STRIPE_FREE_PRICE_ID=price_free_tier_id
STRIPE_PRO_PRICE_ID=price_pro_tier_id
STRIPE_ULTRA_PRICE_ID=price_ultra_tier_id
# Stripe Product IDs (optional)
STRIPE_FREE_PRODUCT_ID=prod_free_tier_id
STRIPE_PRO_PRODUCT_ID=prod_pro_tier_id
STRIPE_ULTRA_PRODUCT_ID=prod_ultra_tier_id
# Frontend URL for redirects
FRONTEND_URL=http://localhost:5173
```
### Example format (replace with your actual values from Stripe Dashboard):
```env
STRIPE_SECRET_KEY=sk_test_YOUR_ACTUAL_SECRET_KEY_FROM_STRIPE_DASHBOARD
STRIPE_WEBHOOK_SECRET=whsec_YOUR_ACTUAL_WEBHOOK_SECRET_FROM_STRIPE_DASHBOARD
STRIPE_FREE_PRICE_ID=price_YOUR_FREE_PRICE_ID
STRIPE_PRO_PRICE_ID=price_YOUR_PRO_PRICE_ID
STRIPE_ULTRA_PRICE_ID=price_YOUR_ULTRA_PRICE_ID
FRONTEND_URL=http://localhost:5173
```
## Step 4: Initialize Database and Plans
### 4.1 Automatic Initialization (Recommended)
The application will automatically create database tables and initialize plans on startup if `AUTO_MIGRATE=1` in your `.env` file.
Just start your backend server:
```bash
cd backend
uvicorn app.main:app --reload
```
### 4.2 Manual Initialization
You can also manually initialize plans using the provided script:
```bash
cd backend
python -m app.scripts.init_plans --create-tables
```
Options:
- `--create-tables`: Create database tables if they don't exist
- `--force`: Force update of existing plans with new values
## Step 5: Test with Stripe CLI (Local Development)
For local testing, use the Stripe CLI to forward webhook events:
### 5.1 Install Stripe CLI
Follow instructions at: https://stripe.com/docs/stripe-cli
### 5.2 Login to Stripe CLI
```bash
stripe login
```
### 5.3 Forward Webhooks to Local Server
```bash
stripe listen --forward-to localhost:3001/api/payment/webhook
```
This will give you a webhook signing secret (starts with `whsec_`). Use this in your `.env` file for local testing.
### 5.4 Test a Payment
```bash
stripe trigger checkout.session.completed
```
## Step 6: Verify Setup
### 6.1 Check Plans Endpoint
Visit: `http://localhost:3001/api/plans`
You should see all three plans with their configurations.
### 6.2 Check Credit Balance (Authenticated)
Create a test user by logging in via Google OAuth, then check:
`GET http://localhost:3001/api/credits/balance`
Headers:
```
Authorization: Bearer YOUR_JWT_TOKEN
```
### 6.3 Test Checkout Flow
1. Log in to your application
2. Navigate to the subscription/pricing page
3. Click on a plan (Pro or Ultra)
4. Complete the checkout using Stripe's test card: `4242 4242 4242 4242`
- Use any future expiration date
- Use any 3-digit CVC
- Use any ZIP code
## Step 7: Go Live
When ready to go live:
1. Switch to **Live mode** in Stripe Dashboard
2. Create new products and prices in live mode
3. Get new live API keys (`sk_live_...`)
4. Create a new webhook endpoint for your production URL
5. Update your production `.env` with live keys
6. Test thoroughly before announcing!
## Troubleshooting
### Webhook Events Not Received
1. Check that your webhook endpoint is accessible from the internet
2. Verify the webhook signing secret in your `.env` file
3. Check Stripe Dashboard → Webhooks → Your endpoint for delivery logs
4. For local testing, ensure Stripe CLI is running
### Credits Not Resetting
- Check webhook logs for `invoice.payment_succeeded` events
- Verify the event is being handled correctly in logs
- Check credit transaction history via `/api/credits/history`
### Plans Not Showing
- Run the initialization script: `python -m app.scripts.init_plans`
- Check database for subscription_plans table
- Verify AUTO_MIGRATE is enabled
## Environment Variables Reference
| Variable | Required | Description | Example |
|----------|----------|-------------|---------|
| `STRIPE_SECRET_KEY` | Yes | Stripe API secret key | `sk_test_...` or `sk_live_...` |
| `STRIPE_WEBHOOK_SECRET` | Yes | Webhook signing secret | `whsec_...` |
| `STRIPE_FREE_PRICE_ID` | Yes | Price ID for Free tier | `price_...` |
| `STRIPE_PRO_PRICE_ID` | Yes | Price ID for Pro tier | `price_...` |
| `STRIPE_ULTRA_PRICE_ID` | Yes | Price ID for Ultra tier | `price_...` |
| `STRIPE_FREE_PRODUCT_ID` | No | Product ID for Free tier | `prod_...` |
| `STRIPE_PRO_PRODUCT_ID` | No | Product ID for Pro tier | `prod_...` |
| `STRIPE_ULTRA_PRODUCT_ID` | No | Product ID for Ultra tier | `prod_...` |
| `FRONTEND_URL` | Yes | Frontend URL for redirects | `http://localhost:5173` |
| `AUTO_MIGRATE` | No | Auto-create DB tables on startup | `1` (default) |
## Support
For issues or questions:
1. Check Stripe Dashboard logs
2. Check application logs
3. Review webhook event details in Stripe Dashboard
4. Consult Stripe documentation: https://stripe.com/docs
## Security Notes
⚠️ **Important Security Reminders:**
- Never commit API keys to version control
- Use environment variables for all sensitive data
- Always verify webhook signatures
- Use HTTPS in production
- Rotate keys if compromised
- Use different keys for test/live environments
- Implement rate limiting on payment endpoints
- Monitor webhook delivery for anomalies
|