File size: 7,177 Bytes
93f5fba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Monthly Subscriptions Setup Guide

This guide will help you set up monthly subscriptions for your 3DAI API backend.

## 1. Database Schema Updates

You'll need to create the following tables in your Supabase database:

### User_Subscriptions Table
```sql
CREATE TABLE "User_Subscriptions" (
    id SERIAL PRIMARY KEY,
    user_id UUID NOT NULL REFERENCES auth.users(id),
    stripe_subscription_id VARCHAR(255) UNIQUE NOT NULL,
    plan VARCHAR(50) NOT NULL,
    status VARCHAR(50) NOT NULL,
    current_period_start TIMESTAMP WITH TIME ZONE,
    current_period_end TIMESTAMP WITH TIME ZONE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    canceled_at TIMESTAMP WITH TIME ZONE
);

-- Add indexes for better performance
CREATE INDEX idx_user_subscriptions_user_id ON "User_Subscriptions"(user_id);
CREATE INDEX idx_user_subscriptions_status ON "User_Subscriptions"(status);
CREATE INDEX idx_user_subscriptions_stripe_id ON "User_Subscriptions"(stripe_subscription_id);
```

### Stripe_Customers Table
```sql
CREATE TABLE "Stripe_Customers" (
    id SERIAL PRIMARY KEY,
    user_id UUID NOT NULL REFERENCES auth.users(id),
    stripe_customer_id VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Add index
CREATE INDEX idx_stripe_customers_user_id ON "Stripe_Customers"(user_id);
```

### Subscription_Payment_History Table
```sql
CREATE TABLE "Subscription_Payment_History" (
    id SERIAL PRIMARY KEY,
    user_id UUID NOT NULL REFERENCES auth.users(id),
    subscription_id VARCHAR(255) NOT NULL,
    invoice_id VARCHAR(255) UNIQUE NOT NULL,
    amount DECIMAL(10,2) NOT NULL,
    credits_added INTEGER NOT NULL,
    payment_date TIMESTAMP WITH TIME ZONE NOT NULL,
    status VARCHAR(50) NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);

-- Add indexes
CREATE INDEX idx_subscription_payment_history_user_id ON "Subscription_Payment_History"(user_id);
CREATE INDEX idx_subscription_payment_history_subscription_id ON "Subscription_Payment_History"(subscription_id);
```

## 2. Stripe Configuration

### Create Products and Prices in Stripe

You'll need to create products and prices in your Stripe dashboard or via API:

```bash
# Using Stripe CLI (install from https://stripe.com/docs/stripe-cli)

# Create Basic Plan Product
stripe products create \
  --name="Basic Plan" \
  --description="50 credits per month"

# Create Basic Plan Price (replace prod_xxx with your product ID)
stripe prices create \
  --unit-amount=999 \
  --currency=gbp \
  --recurring-interval=month \
  --product=prod_xxx

# Create Pro Plan Product
stripe products create \
  --name="Pro Plan" \
  --description="150 credits per month"

# Create Pro Plan Price
stripe prices create \
  --unit-amount=2499 \
  --currency=gbp \
  --recurring-interval=month \
  --product=prod_xxx

# Create Premium Plan Product
stripe products create \
  --name="Premium Plan" \
  --description="300 credits per month"

# Create Premium Plan Price
stripe prices create \
  --unit-amount=3999 \
  --currency=gbp \
  --recurring-interval=month \
  --product=prod_xxx
```

### Update Price IDs

After creating the prices, update the `SUBSCRIPTION_PLANS` in `routers/payments.py` with your actual Stripe price IDs:

```python
SUBSCRIPTION_PLANS = {
    "basic": {
        "credits_per_month": 50,
        "price_gbp": 9.99,
        "stripe_price_id": "price_1234567890",  # Replace with actual price ID
        "name": "Basic Plan",
        "description": "50 credits per month"
    },
    # ... update other plans
}
```

## 3. Environment Variables

Add these new environment variables to your `.env` file:

```env
# Existing variables...
STRIPE_SECRET_KEY=sk_test_your_stripe_secret_key
STRIPE_PUBLISHABLE_KEY=pk_test_your_stripe_publishable_key

# New webhook secret (you'll get this when setting up the webhook)
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret
```

## 4. Webhook Configuration

### Set up Stripe Webhook

1. Go to your Stripe Dashboard → Developers → Webhooks
2. Click "Add endpoint"
3. Set the endpoint URL to: `https://your-domain.com/payment/webhook`
4. Select these events:
   - `invoice.payment_succeeded`
   - `invoice.payment_failed`
   - `customer.subscription.deleted`
   - `customer.subscription.updated`
5. Copy the webhook secret and add it to your environment variables

## 5. API Endpoints

Your subscription system now includes these new endpoints:

### Get Available Plans
```
GET /payment/subscription-plans
```

### Create Subscription
```
POST /payment/create-subscription
Content-Type: application/json
Authorization: Bearer <token>

{
    "plan": "basic"
}
```

### Get User's Subscription
```
GET /payment/subscription
Authorization: Bearer <token>
```

### Cancel Subscription
```
POST /payment/cancel-subscription
Content-Type: application/json
Authorization: Bearer <token>

{
    "subscription_id": "sub_1234567890"
}
```

### Webhook Endpoint
```
POST /payment/webhook
```

## 6. Frontend Integration Example

Here's a basic example of how to integrate subscriptions in your frontend:

```javascript
// Create subscription
const createSubscription = async (plan) => {
    const response = await fetch('/payment/create-subscription', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${userToken}`
        },
        body: JSON.stringify({ plan })
    });
    
    const { client_secret } = await response.json();
    
    // Use Stripe.js to confirm payment
    const { error } = await stripe.confirmPayment({
        elements,
        clientSecret: client_secret,
        confirmParams: {
            return_url: 'https://your-domain.com/subscription-success'
        }
    });
};

// Get user's subscription status
const getSubscription = async () => {
    const response = await fetch('/payment/subscription', {
        headers: {
            'Authorization': `Bearer ${userToken}`
        }
    });
    
    const { subscription } = await response.json();
    return subscription;
};
```

## 7. Testing

1. Use Stripe test mode for development
2. Use test card numbers from Stripe documentation
3. Test webhook events using Stripe CLI:
   ```bash
   stripe listen --forward-to localhost:8000/payment/webhook
   ```

## 8. Production Considerations

1. **Security**: Ensure webhook signature verification is enabled
2. **Monitoring**: Set up logging for subscription events
3. **Error Handling**: Implement retry logic for failed webhook processing
4. **Credits Management**: Consider implementing credit rollover policies
5. **Prorations**: Handle plan changes with appropriate prorations
6. **Tax Handling**: Implement tax calculation if required for your jurisdiction

## 9. Additional Features to Consider

- **Plan Upgrades/Downgrades**: Allow users to change subscription plans
- **Pause/Resume**: Allow temporary subscription pauses
- **Annual Plans**: Add yearly subscription options with discounts
- **Usage Limits**: Implement soft/hard limits based on subscription tier
- **Email Notifications**: Send emails for subscription events
- **Admin Dashboard**: Create admin interface for subscription management