T.ME/BIBIL_0DAY
CasperSecurity


Server : Apache/2
System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64
User : gositeme ( 1004)
PHP Version : 8.2.29
Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname
Directory :  /home/gositeme/domains/soundstudiopro.com/private_html/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/gositeme/domains/soundstudiopro.com/private_html/fix_stephane_subscription.php
<?php
/**
 * Fix Stephane's Subscription
 * Checks Stripe for active subscription and updates database
 */

require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/utils/subscription_helpers.php';

$stripe_secret = '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';

// Stephane's info from logs
$stephane_email = 'stevenberg450@gmail.com';
$stephane_user_id = 5;

header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html>
<html>
<head>
    <title>Fix Stephane's Subscription</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background: #1a1a1a;
            color: #fff;
        }
        .section {
            background: #2a2a2a;
            padding: 20px;
            margin: 20px 0;
            border-radius: 8px;
        }
        .success { color: #48bb78; }
        .error { color: #f56565; }
        .info { color: #4299e1; }
        .warning { color: #ffc107; }
        pre {
            background: #1a1a1a;
            padding: 15px;
            border-radius: 5px;
            overflow-x: auto;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 10px 0;
        }
        th, td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #444;
        }
        th {
            background: #333;
        }
    </style>
</head>
<body>
    <h1>Fix Stephane's Subscription</h1>
    
    <?php
    $pdo = getDBConnection();
    
    // Step 1: Get user from database
    echo '<div class="section">';
    echo '<h2>Step 1: User Information</h2>';
    
    $stmt = $pdo->prepare("SELECT id, name, email, plan, stripe_customer_id FROM users WHERE id = ? OR email = ?");
    $stmt->execute([$stephane_user_id, $stephane_email]);
    $user = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if (!$user) {
        echo "<p class='error'>✗ User not found (ID: {$stephane_user_id}, Email: {$stephane_email})</p>";
        echo '</div></body></html>';
        exit;
    }
    
    echo "<p class='success'>✓ Found user: {$user['name']} (ID: {$user['id']}, Email: {$user['email']})</p>";
    echo "<p class='info'>Current plan in database: <strong>{$user['plan']}</strong></p>";
    echo "<p class='info'>Stripe Customer ID: " . ($user['stripe_customer_id'] ?? 'NOT SET') . "</p>";
    echo '</div>';
    
    // Step 2: Check Stripe for subscriptions
    echo '<div class="section">';
    echo '<h2>Step 2: Checking Stripe for Active Subscriptions</h2>';
    
    $customer_id = $user['stripe_customer_id'];
    
    // If no customer ID, try to find by email
    if (!$customer_id) {
        echo "<p class='warning'>⚠ No Stripe customer ID in database. Searching Stripe by email...</p>";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/customers?email=" . urlencode($stephane_email) . "&limit=1");
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        
        if ($http_code === 200) {
            $customers = json_decode($response, true);
            if (!empty($customers['data']) && count($customers['data']) > 0) {
                $customer_id = $customers['data'][0]['id'];
                echo "<p class='success'>✓ Found customer in Stripe: {$customer_id}</p>";
                
                // Update database
                $stmt = $pdo->prepare("UPDATE users SET stripe_customer_id = ? WHERE id = ?");
                $stmt->execute([$customer_id, $user['id']]);
                echo "<p class='success'>✓ Updated database with customer ID</p>";
            } else {
                echo "<p class='error'>✗ Customer not found in Stripe by email</p>";
                echo '</div></body></html>';
                exit;
            }
        } else {
            echo "<p class='error'>✗ Error searching Stripe: HTTP {$http_code}</p>";
            echo '</div></body></html>';
            exit;
        }
    }
    
    // Fetch subscriptions from Stripe
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/subscriptions?customer=" . urlencode($customer_id) . "&limit=10&status=all");
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    if ($http_code !== 200) {
        echo "<p class='error'>✗ Error fetching subscriptions: HTTP {$http_code}</p>";
        echo '<pre>' . htmlspecialchars($response) . '</pre>';
        echo '</div></body></html>';
        exit;
    }
    
    $subscriptions_data = json_decode($response, true);
    $subscriptions = $subscriptions_data['data'] ?? [];
    
    if (empty($subscriptions)) {
        echo "<p class='warning'>⚠ No subscriptions found in Stripe for customer {$customer_id}</p>";
        echo '</div></body></html>';
        exit;
    }
    
    echo "<p class='success'>✓ Found " . count($subscriptions) . " subscription(s) in Stripe</p>";
    
    // Find active subscriptions
    $active_subscriptions = array_filter($subscriptions, function($sub) {
        return in_array($sub['status'], ['active', 'trialing']) && 
               isset($sub['current_period_end']) && 
               $sub['current_period_end'] > time();
    });
    
    if (empty($active_subscriptions)) {
        echo "<p class='warning'>⚠ No active subscriptions found in Stripe</p>";
        echo "<p>All subscriptions:</p>";
        echo "<table>";
        echo "<tr><th>ID</th><th>Status</th><th>Period End</th></tr>";
        foreach ($subscriptions as $sub) {
            $period_end = isset($sub['current_period_end']) ? date('Y-m-d H:i:s', $sub['current_period_end']) : 'N/A';
            echo "<tr><td>{$sub['id']}</td><td>{$sub['status']}</td><td>{$period_end}</td></tr>";
        }
        echo "</table>";
        echo '</div></body></html>';
        exit;
    }
    
    echo "<p class='success'>✓ Found " . count($active_subscriptions) . " active subscription(s)</p>";
    echo '</div>';
    
    // Step 3: Determine plan and sync
    echo '<div class="section">';
    echo '<h2>Step 3: Syncing Subscription to Database</h2>';
    
    require_once __DIR__ . '/config/subscription_plans.php';
    $plans_config = require __DIR__ . '/config/subscription_plans.php';
    
    // Get the most recent active subscription
    $active_sub = reset($active_subscriptions);
    $stripe_sub_id = $active_sub['id'];
    $status = $active_sub['status'];
    $plan_name = 'essential'; // Default
    
    // Determine plan from price ID
    if (!empty($active_sub['items']['data'][0]['price']['id'])) {
        $price_id = $active_sub['items']['data'][0]['price']['id'];
        echo "<p class='info'>Price ID from Stripe: {$price_id}</p>";
        
        foreach ($plans_config as $plan_key => $plan_data) {
            if (isset($plan_data['stripe_price_id']) && $plan_data['stripe_price_id'] === $price_id) {
                $plan_name = $plan_key;
                echo "<p class='success'>✓ Matched plan: <strong>{$plan_name}</strong></p>";
                break;
            }
        }
        
        // If no match, try by price amount
        if ($plan_name === 'essential') {
            $price_amount = $active_sub['items']['data'][0]['price']['unit_amount'] ?? 0;
            $price_amount = $price_amount / 100;
            
            echo "<p class='warning'>⚠ Price ID not found in config. Price amount: \${$price_amount}</p>";
            
            foreach ($plans_config as $plan_key => $plan_data) {
                if (abs($plan_data['price'] - $price_amount) < 0.01) {
                    $plan_name = $plan_key;
                    echo "<p class='success'>✓ Matched plan by price: <strong>{$plan_name}</strong> (\${$plan_data['price']})</p>";
                    break;
                }
            }
        }
    }
    
    $period_start = date('Y-m-d H:i:s', $active_sub['current_period_start']);
    $period_end = date('Y-m-d H:i:s', $active_sub['current_period_end']);
    
    echo "<p class='info'>Subscription ID: {$stripe_sub_id}</p>";
    echo "<p class='info'>Status: {$status}</p>";
    echo "<p class='info'>Plan: <strong>{$plan_name}</strong></p>";
    echo "<p class='info'>Period: {$period_start} to {$period_end}</p>";
    
    // Check if subscription exists in database
    $stmt = $pdo->prepare("SELECT * FROM user_subscriptions WHERE stripe_subscription_id = ?");
    $stmt->execute([$stripe_sub_id]);
    $db_subscription = $stmt->fetch(PDO::FETCH_ASSOC);
    
    $pdo->beginTransaction();
    try {
        if (!$db_subscription) {
            // Create new subscription record
            echo "<p class='warning'>⚠ Subscription not found in database. Creating record...</p>";
            
            $track_limit = $plans_config[$plan_name]['tracks_per_month'] ?? 5;
            
            $stmt = $pdo->prepare("
                INSERT INTO user_subscriptions (
                    user_id, stripe_subscription_id, stripe_customer_id, plan_name, status,
                    current_period_start, current_period_end, created_at
                ) VALUES (?, ?, ?, ?, ?, ?, ?, NOW())
            ");
            $stmt->execute([
                $user['id'],
                $stripe_sub_id,
                $customer_id,
                $plan_name,
                $status,
                $period_start,
                $period_end
            ]);
            
            $subscription_id = $pdo->lastInsertId();
            
            // Initialize monthly track usage
            $year_month = date('Y-m', $active_sub['current_period_start']);
            $stmt = $pdo->prepare("
                INSERT INTO monthly_track_usage (
                    user_id, subscription_id, subscription_period_start, 
                    year_month, tracks_created, track_limit, reset_at
                )
                VALUES (?, ?, ?, ?, 0, ?, NOW())
                ON DUPLICATE KEY UPDATE 
                    track_limit = VALUES(track_limit),
                    reset_at = NOW()
            ");
            $stmt->execute([
                $user['id'], 
                $subscription_id, 
                $period_start, 
                $year_month, 
                $track_limit
            ]);
            
            echo "<p class='success'>✓ Created subscription record in database</p>";
        } else {
            // Update existing subscription
            echo "<p class='info'>ℹ Subscription exists in database. Updating...</p>";
            
            $stmt = $pdo->prepare("
                UPDATE user_subscriptions 
                SET status = ?,
                    plan_name = ?,
                    current_period_start = ?,
                    current_period_end = ?,
                    updated_at = NOW()
                WHERE stripe_subscription_id = ?
            ");
            $stmt->execute([
                $status,
                $plan_name,
                $period_start,
                $period_end,
                $stripe_sub_id
            ]);
            
            // Update track limit if needed
            $track_limit = $plans_config[$plan_name]['tracks_per_month'] ?? 5;
            $usage_stmt = $pdo->prepare("
                UPDATE monthly_track_usage
                SET track_limit = ?,
                    updated_at = NOW()
                WHERE user_id = ? AND subscription_id = ?
            ");
            $usage_stmt->execute([
                $track_limit,
                $user['id'],
                $db_subscription['id']
            ]);
            
            echo "<p class='success'>✓ Updated subscription record</p>";
        }
        
        // CRITICAL: Update user plan to match subscription
        $stmt = $pdo->prepare("UPDATE users SET plan = ? WHERE id = ?");
        $stmt->execute([$plan_name, $user['id']]);
        echo "<p class='success'>✓ <strong>Updated user plan to: {$plan_name}</strong></p>";
        
        $pdo->commit();
        
        echo "<p class='success' style='font-size: 1.2rem; padding: 15px; background: rgba(72, 187, 120, 0.2); border: 2px solid #48bb78; border-radius: 8px;'>";
        echo "✅ <strong>SUCCESS!</strong> Stephane's plan has been updated to <strong>{$plan_name}</strong> in the database.";
        echo "</p>";
        
    } catch (Exception $e) {
        $pdo->rollBack();
        echo "<p class='error'>✗ Error: " . htmlspecialchars($e->getMessage()) . "</p>";
        echo "<pre>" . htmlspecialchars($e->getTraceAsString()) . "</pre>";
    }
    
    echo '</div>';
    
    // Step 4: Verify
    echo '<div class="section">';
    echo '<h2>Step 4: Verification</h2>';
    
    $stmt = $pdo->prepare("SELECT plan FROM users WHERE id = ?");
    $stmt->execute([$user['id']]);
    $updated_user = $stmt->fetch(PDO::FETCH_ASSOC);
    
    $stmt = $pdo->prepare("SELECT plan_name, status FROM user_subscriptions WHERE user_id = ? ORDER BY created_at DESC LIMIT 1");
    $stmt->execute([$user['id']]);
    $updated_sub = $stmt->fetch(PDO::FETCH_ASSOC);
    
    echo "<table>";
    echo "<tr><th>Field</th><th>Value</th></tr>";
    echo "<tr><td>User Plan (users.plan)</td><td><strong>{$updated_user['plan']}</strong></td></tr>";
    if ($updated_sub) {
        echo "<tr><td>Subscription Plan (user_subscriptions.plan_name)</td><td><strong>{$updated_sub['plan_name']}</strong></td></tr>";
        echo "<tr><td>Subscription Status</td><td><strong>{$updated_sub['status']}</strong></td></tr>";
    }
    echo "</table>";
    
    // Check effective plan
    $effective_plan = getEffectivePlan($user['id']);
    echo "<p class='info'>Effective Plan (from getEffectivePlan()): <strong>{$effective_plan}</strong></p>";
    
    if ($effective_plan === $plan_name) {
        echo "<p class='success'>✅ Plan is correctly synced!</p>";
    } else {
        echo "<p class='warning'>⚠ Effective plan ({$effective_plan}) doesn't match Stripe plan ({$plan_name}). This may need investigation.</p>";
    }
    
    echo '</div>';
    ?>
    
    <div class="section">
        <h2>Next Steps</h2>
        <p><a href="manage_subscription.php" style="color: #4299e1;">View manage_subscription.php</a> to see the updated plan</p>
        <p><a href="account_settings.php?tab=subscription" style="color: #4299e1;">View account_settings.php?tab=subscription</a> to see subscription details</p>
    </div>
</body>
</html>

CasperSecurity Mini