# PHASE 6: PREMIUM MONETIZATION - QUICK START ⚡

**Get AeThex's premium monetization running in under 10 minutes.**

---

## Prerequisites

- ✅ Phase 1-5 completed and running
- ✅ Stripe account (free at stripe.com)
- ✅ Node.js 18+ installed
- ✅ PostgreSQL database running

---

## 1. Database Setup (2 minutes)

### Run the migration:

```bash
cd /workspaces/AeThex-Connect

# Option A: Using the migrate script
npm run migrate

# Option B: Using psql directly
psql $DATABASE_URL -f src/backend/database/migrations/006_premium_monetization.sql
```

**Verify migration:**
```sql
psql $DATABASE_URL -c "SELECT * FROM feature_limits;"
```

You should see 3 rows (free, premium, enterprise) with default limits.

---

## 2. Stripe Setup (3 minutes)

### Create Stripe Account
1. Go to https://dashboard.stripe.com/register
2. Skip setup - go straight to test mode
3. Click "Developers" → "API keys"

### Copy Your Keys
```bash
# Add to .env file:
STRIPE_SECRET_KEY=sk_test_51...
STRIPE_PUBLISHABLE_KEY=pk_test_51...
```

### Create Products & Prices

**Quick Method - Use Stripe CLI:**
```bash
# Install Stripe CLI
brew install stripe/stripe-cli/stripe # macOS
# or download from: https://stripe.com/docs/stripe-cli

# Login
stripe login

# Create Premium Yearly
stripe prices create \
  --unit-amount=10000 \
  --currency=usd \
  --recurring="interval=year" \
  --product-data="name=AeThex Premium Yearly"
# Copy the price ID (price_...) to STRIPE_PREMIUM_YEARLY_PRICE_ID

# Create Premium Monthly
stripe prices create \
  --unit-amount=1000 \
  --currency=usd \
  --recurring="interval=month" \
  --product-data="name=AeThex Premium Monthly"
# Copy price ID to STRIPE_PREMIUM_MONTHLY_PRICE_ID

# Create Enterprise
stripe prices create \
  --unit-amount=50000 \
  --currency=usd \
  --recurring="interval=month" \
  --product-data="name=AeThex Enterprise"
# Copy price ID to STRIPE_ENTERPRISE_PRICE_ID
```

**Manual Method - Dashboard:**
1. Go to Products → Add Product
2. Name: "AeThex Premium Yearly"
3. Price: $100.00
4. Billing: Recurring, Yearly
5. Click "Save"
6. Copy the Price ID (starts with `price_`)
7. Repeat for Monthly ($10) and Enterprise ($500)

### Setup Webhook (for local testing)

```bash
# Forward webhooks to local server
stripe listen --forward-to localhost:5000/webhooks/stripe

# Copy the webhook signing secret (whsec_...) to .env
STRIPE_WEBHOOK_SECRET=whsec_...
```

---

## 3. Environment Variables (1 minute)

Update your `.env` file:

```bash
# Stripe (required)
STRIPE_SECRET_KEY=sk_test_51...
STRIPE_PUBLISHABLE_KEY=pk_test_51...
STRIPE_WEBHOOK_SECRET=whsec_...
STRIPE_PREMIUM_YEARLY_PRICE_ID=price_...
STRIPE_PREMIUM_MONTHLY_PRICE_ID=price_...
STRIPE_ENTERPRISE_PRICE_ID=price_...

# Platform (optional - has defaults)
PLATFORM_FEE_PERCENTAGE=10
FREE_MAX_FRIENDS=5
FREE_STORAGE_GB=0.1
PREMIUM_STORAGE_GB=10
ENTERPRISE_STORAGE_GB=-1
```

Frontend `.env` (in `src/frontend/`):
```bash
REACT_APP_STRIPE_PUBLISHABLE_KEY=pk_test_51...
```

---

## 4. Start the Server (1 minute)

```bash
# Backend
npm start

# Frontend (new terminal)
cd src/frontend
npm run dev
```

You should see:
```
✓ Premium routes loaded at /api/premium
✓ Stripe webhook listening at /webhooks/stripe
✓ Server running on port 5000
```

---

## 5. Test Premium Flow (3 minutes)

### Test Domain Availability

```bash
curl -X POST http://localhost:5000/api/premium/domains/check-availability \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"domain": "testuser.aethex"}'
```

**Expected response:**
```json
{
  "success": true,
  "available": true,
  "domain": "testuser.aethex",
  "price": 100.00
}
```

### Test Subscription via Frontend

1. Open http://localhost:5173/premium/upgrade
2. Click "Choose Premium"
3. Enter domain name: `yourname.aethex`
4. Click "Check Availability"
5. Enter test card: `4242 4242 4242 4242`
6. Expiry: Any future date (e.g., 12/25)
7. CVC: Any 3 digits (e.g., 123)
8. Click "Subscribe"
9. Should redirect to success page

### Verify in Database

```sql
-- Check subscription
SELECT * FROM premium_subscriptions WHERE user_id = 'your-user-id';

-- Check domain
SELECT * FROM blockchain_domains WHERE owner_user_id = 'your-user-id';

-- Check user tier upgraded
SELECT id, email, premium_tier FROM users WHERE id = 'your-user-id';
```

---

## 6. Test Webhook (2 minutes)

With Stripe CLI running (`stripe listen --forward-to...`):

```bash
# Trigger test webhook
stripe trigger customer.subscription.updated

# Check your server logs - should see:
# ✅ Webhook received: customer.subscription.updated
# ✅ Subscription updated successfully
```

**Verify webhook events:**
```sql
SELECT * FROM payment_transactions ORDER BY created_at DESC LIMIT 5;
```

---

## Quick Troubleshooting

### "Migration failed"
```bash
# Check database connection
psql $DATABASE_URL -c "SELECT version();"

# Check if migration already ran
psql $DATABASE_URL -c "\dt premium_*"

# Force re-run
psql $DATABASE_URL -c "DROP TABLE IF EXISTS premium_subscriptions CASCADE;"
npm run migrate
```

### "Stripe key invalid"
- Make sure you copied the FULL key (starts with `sk_test_` or `pk_test_`)
- Check for extra spaces
- Verify in Stripe dashboard it's from test mode

### "Webhook signature verification failed"
```bash
# Get new webhook secret
stripe listen --forward-to localhost:5000/webhooks/stripe

# Copy the new whsec_... to .env
# Restart server
```

### "Domain registration hangs"
- Check Stripe keys are set
- Verify price IDs are correct
- Check server logs for errors
- Try test card: 4242 4242 4242 4242

---

## Test Cards

### Successful Payment
```
Card: 4242 4242 4242 4242
Expiry: Any future date
CVC: Any 3 digits
```

### Declined Payment
```
Card: 4000 0000 0000 0002
```

### Requires Authentication (3D Secure)
```
Card: 4000 0025 0000 3155
```

---

## Next Steps

### Test Full Flow
1. ✅ Create free account
2. ✅ Hit friend limit (5 friends)
3. ✅ Upgrade to premium
4. ✅ Register domain
5. ✅ Check unlimited friends works
6. ✅ View analytics dashboard
7. ✅ Cancel subscription
8. ✅ Verify downgrade at period end

### Production Deployment
See [PHASE6-COMPLETE.md](PHASE6-COMPLETE.md) for:
- Production webhook setup
- Live Stripe keys
- Security checklist
- Monitoring setup

---

## Common Commands

```bash
# Check subscription status
curl http://localhost:5000/api/premium/subscription \
  -H "Authorization: Bearer YOUR_TOKEN"

# Get analytics
curl http://localhost:5000/api/premium/analytics?period=7d \
  -H "Authorization: Bearer YOUR_TOKEN"

# List domains
curl http://localhost:5000/api/premium/domains \
  -H "Authorization: Bearer YOUR_TOKEN"

# Check feature limits
curl http://localhost:5000/api/premium/features \
  -H "Authorization: Bearer YOUR_TOKEN"

# Browse marketplace
curl http://localhost:5000/api/premium/marketplace
```

---

## Support

**Stripe docs:** https://stripe.com/docs  
**Test mode:** https://dashboard.stripe.com/test  
**Webhook testing:** https://stripe.com/docs/webhooks/test

**Issues?**
1. Check server logs
2. Check Stripe dashboard logs
3. Verify environment variables
4. Check database migrations ran
5. Test with curl first, then frontend

---

## Success Checklist

- [x] Database migration completed
- [x] Stripe keys configured
- [x] Products & prices created
- [x] Webhook listening (local)
- [x] Server starts without errors
- [x] Domain availability check works
- [x] Test payment succeeds
- [x] User tier upgraded in database
- [x] Subscription visible in Stripe
- [x] Webhook events logged

**✨ You're ready to monetize! ✨**

---

**Phase 6 Quick Start Complete**  
**Time to Revenue: 10 minutes** 🚀
