# Testing Auto-Renewal in Stripe

Your system now has automatic renewal handlers implemented. Here's how to test them:

## Current Subscription
- **Subscription ID**: 34
- **User ID**: 17
- **Stripe Subscription ID**: sub_1SrvwFJjVkEBlEoG7EtTkFQc
- **Status**: active
- **Package**: ggggggggg ($200.00)

## Testing Methods

### Method 1: Trigger Renewal Event with Stripe CLI (Fastest - 30 seconds)

This simulates a successful renewal payment:

```powershell
cd C:\Users\User\Desktop\app
stripe trigger invoice.payment_succeeded
```

Then check the database:
```powershell
cd backend
php check_all_subscriptions.php
```

**What happens:**
- Stripe sends `invoice.payment_succeeded` webhook
- System updates subscription period dates
- Creates new invoice record for the renewal
- Creates transaction record
- Subscription period_end extends by 1 month/year

---

### Method 2: Manual Webhook with Real Subscription ID

```powershell
stripe trigger customer.subscription.updated --add subscription:subscription=sub_1SrvwFJjVkEBlEoG7EtTkFQc
```

---

### Method 3: Use Stripe Test Clocks (Most Realistic)

1. Go to Stripe Dashboard → Developers → Test clocks
2. Create a new test clock
3. Create a subscription with that test clock
4. Fast-forward time to trigger renewal

**Steps:**
```powershell
# Create test clock (set date to current time)
stripe test_clocks create --frozen_time=$(Get-Date -Format "yyyy-MM-ddTHH:mm:ssZ")

# Get the test clock ID (will be like: clock_xxx)
stripe test_clocks list

# Create a customer with test clock
stripe customers create --email="test@example.com" --test_clock="clock_xxx"

# Create subscription for that customer
# (Use your regular checkout flow but with this customer)

# Fast-forward time by 1 month
stripe test_clocks advance "clock_xxx" --frozen_time="2026-02-21T13:00:00Z"
```

---

### Method 4: Create Short-Period Test Subscription

Create a subscription with 1-day period for quick testing:

1. In SubscriptionController, temporarily change the interval
2. Create new subscription with 1-day period
3. Wait 24 hours or use test clock to advance

---

## What to Check After Renewal

```powershell
cd backend

# Check subscription details
php check_all_subscriptions.php

# Check logs for renewal
Get-Content storage\logs\laravel.log -Tail 50 | Select-String "Subscription renewed"

# Check invoices table
php -r "
require 'vendor/autoload.php';
\$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
\$dotenv->load();
\$pdo = new PDO('mysql:host=' . \$_ENV['DB_HOST'] . ';dbname=' . \$_ENV['DB_DATABASE'], \$_ENV['DB_USERNAME'], \$_ENV['DB_PASSWORD']);
\$stmt = \$pdo->query('SELECT * FROM invoices ORDER BY id DESC LIMIT 5');
while (\$row = \$stmt->fetch(PDO::FETCH_ASSOC)) {
    echo \"Invoice #{\$row['id']}: {\$row['invoice_number']} - ${\$row['amount']} {\$row['currency']} - {\$row['status']}\n\";
}
"

# Check transactions
php -r "
require 'vendor/autoload.php';
\$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
\$dotenv->load();
\$pdo = new PDO('mysql:host=' . \$_ENV['DB_HOST'] . ';dbname=' . \$_ENV['DB_DATABASE'], \$_ENV['DB_USERNAME'], \$_ENV['DB_PASSWORD']);
\$stmt = \$pdo->query('SELECT * FROM transactions ORDER BY id DESC LIMIT 5');
while (\$row = \$stmt->fetch(PDO::FETCH_ASSOC)) {
    echo \"Transaction #{\$row['id']}: {\$row['gateway_transaction_id']} - ${\$row['amount']} - {\$row['status']}\n\";
}
"
```

---

## Renewal Webhook Handlers Implemented

The system now handles these renewal events:

1. **invoice.payment_succeeded** - When renewal payment succeeds
2. **invoice.paid** - Alternative event for successful payment
3. **invoice.payment_failed** - When renewal payment fails
4. **customer.subscription.updated** - When subscription period updates

---

## Expected Behavior on Renewal

When a subscription renews automatically:

1. ✅ Stripe charges the customer
2. ✅ Webhook `invoice.payment_succeeded` sent
3. ✅ System updates `current_period_start` and `current_period_end`
4. ✅ New invoice created with renewal payment
5. ✅ Transaction record created
6. ✅ Subscription status remains "active"
7. ✅ User continues to have access

---

## Troubleshooting

If renewal doesn't work:

1. **Check Stripe CLI is running:**
   ```powershell
   Get-Process | Where-Object {$_.ProcessName -eq 'stripe'}
   ```

2. **Check Laravel server is running:**
   ```powershell
   Get-Process | Where-Object {$_.ProcessName -eq 'php'}
   ```

3. **Check webhook logs:**
   ```powershell
   Get-Content storage\logs\laravel.log -Tail 50 | Select-String "invoice"
   ```

4. **Check webhook secret:**
   ```powershell
   php check_webhook_secret.php
   ```

---

## Production Deployment Notes

For production:

1. Replace test webhook secret with production secret
2. Update Stripe API keys to production mode
3. Set up webhook endpoint in Stripe Dashboard
4. Subscribe to these events:
   - checkout.session.completed
   - invoice.payment_succeeded
   - invoice.paid
   - invoice.payment_failed
   - customer.subscription.updated
   - customer.subscription.deleted
5. Monitor renewal failures and send customer notifications
6. Implement grace period for failed payments
7. Add retry logic for failed webhooks
