![]() 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/.cursor-server/data/User/History/-4066c3f2/ |
<?php
/**
* Fix Stephane Bergeron's Subscription
* Directly updates the database with his subscription from Stripe
*/
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/utils/subscription_helpers.php';
$stripe_secret = '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';
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: 1000px;
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 Bergeron's Subscription</h1>
<?php
$pdo = getDBConnection();
// Step 1: Find Stephane's user account
echo '<div class="section">';
echo '<h2>Step 1: Finding Stephane Bergeron</h2>';
$stmt = $pdo->prepare("SELECT id, name, email, stripe_customer_id, plan FROM users WHERE email = ? OR name LIKE ?");
$stmt->execute(['stevenberg450@gmail.com', '%Stephane%']);
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($users)) {
echo "<p class='error'>✗ User not found. Searching by email: stevenberg450@gmail.com</p>";
$stmt = $pdo->prepare("SELECT id, name, email, stripe_customer_id, plan FROM users WHERE email LIKE ?");
$stmt->execute(['%stevenberg450%']);
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
if (empty($users)) {
echo "<p class='error'>✗ Could not find Stephane Bergeron in database</p>";
echo '</div></body></html>';
exit;
}
// Find the right user (prefer exact email match)
$stephane = null;
foreach ($users as $user) {
if (stripos($user['email'], 'stevenberg450') !== false) {
$stephane = $user;
break;
}
}
if (!$stephane) {
$stephane = $users[0]; // Use first match
}
echo "<p class='success'>✓ Found user: {$stephane['name']} ({$stephane['email']})</p>";
echo "<p class='info'>User ID: {$stephane['id']}</p>";
echo "<p class='info'>Current Plan: {$stephane['plan']}</p>";
echo "<p class='info'>Stripe Customer ID: " . ($stephane['stripe_customer_id'] ?: 'None') . "</p>";
echo '</div>';
// Step 2: Get customer ID from Stripe if not in database
$customer_id = $stephane['stripe_customer_id'];
if (!$customer_id) {
echo '<div class="section">';
echo '<h2>Step 2: Finding Customer in Stripe</h2>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/customers?email=" . urlencode($stephane['email']) . "&limit=1");
curl_setopt($ch, CURLOPT_USERPWD, $stripe_secret . ":");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$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, $stephane['id']]);
echo "<p class='success'>✓ Updated customer ID in database</p>";
} else {
// Try the known customer ID from screenshot
$customer_id = 'cus_TU1piJi9qLbFyS';
echo "<p class='warning'>⚠ Customer not found by email. Using known customer ID from screenshot: {$customer_id}</p>";
// Verify this customer exists
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/customers/{$customer_id}");
curl_setopt($ch, CURLOPT_USERPWD, $stripe_secret . ":");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code === 200) {
$customer = json_decode($response, true);
echo "<p class='success'>✓ Verified customer ID exists in Stripe</p>";
// Update database
$stmt = $pdo->prepare("UPDATE users SET stripe_customer_id = ? WHERE id = ?");
$stmt->execute([$customer_id, $stephane['id']]);
echo "<p class='success'>✓ Updated customer ID in database</p>";
} else {
echo "<p class='error'>✗ Customer ID not found in Stripe</p>";
echo '</div></body></html>';
exit;
}
}
} else {
// Use known customer ID
$customer_id = 'cus_TU1piJi9qLbFyS';
echo "<p class='info'>Using known customer ID: {$customer_id}</p>";
$stmt = $pdo->prepare("UPDATE users SET stripe_customer_id = ? WHERE id = ?");
$stmt->execute([$customer_id, $stephane['id']]);
}
echo '</div>';
} else {
echo '<div class="section">';
echo '<h2>Step 2: Customer ID Already in Database</h2>';
echo "<p class='success'>✓ Customer ID: {$customer_id}</p>";
echo '</div>';
}
// Step 3: Fetch subscription from Stripe
echo '<div class="section">';
echo '<h2>Step 3: Fetching Subscription from Stripe</h2>';
$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_USERPWD, $stripe_secret . ":");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$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='error'>✗ No subscriptions found in Stripe</p>";
echo '</div></body></html>';
exit;
}
// Find active subscription
$active_subscription = null;
foreach ($subscriptions as $sub) {
if ($sub['status'] === 'active' || $sub['status'] === 'trialing') {
$active_subscription = $sub;
break;
}
}
if (!$active_subscription) {
$active_subscription = $subscriptions[0]; // Use most recent
}
echo "<p class='success'>✓ Found subscription: {$active_subscription['id']}</p>";
echo "<p class='info'>Status: {$active_subscription['status']}</p>";
echo '</div>';
// Step 4: Determine plan name
echo '<div class="section">';
echo '<h2>Step 4: Determining Plan</h2>';
require_once __DIR__ . '/config/subscription_plans.php';
$plans_config = require __DIR__ . '/config/subscription_plans.php';
$plan_name = 'essential'; // Default
// Try to match by price ID
if (!empty($active_subscription['items']['data'][0]['price']['id'])) {
$price_id = $active_subscription['items']['data'][0]['price']['id'];
echo "<p class='info'>Price ID: {$price_id}</p>";
foreach ($plans_config as $plan_key => $plan_data) {
if ($plan_data['stripe_price_id'] === $price_id) {
$plan_name = $plan_key;
echo "<p class='success'>✓ Matched plan by price ID: {$plan_name}</p>";
break;
}
}
// If no match, try by price amount
if ($plan_name === 'essential') {
$price_amount = $active_subscription['items']['data'][0]['price']['unit_amount'] ?? 0;
$price_amount = $price_amount / 100; // Convert from cents
echo "<p class='info'>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: {$plan_name} (\${$plan_data['price']})</p>";
break;
}
}
}
}
$track_limit = $plans_config[$plan_name]['tracks_per_month'] ?? 5;
echo "<p class='success'>✓ Final plan: {$plan_name} ({$track_limit} tracks/month)</p>";
echo '</div>';
// Step 5: Update database
echo '<div class="section">';
echo '<h2>Step 5: Updating Database</h2>';
$period_start = date('Y-m-d H:i:s', $active_subscription['current_period_start']);
$period_end = date('Y-m-d H:i:s', $active_subscription['current_period_end']);
$status = $active_subscription['status'];
$pdo->beginTransaction();
try {
// Create or update subscription record
$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())
ON DUPLICATE KEY UPDATE
status = VALUES(status),
current_period_start = VALUES(current_period_start),
current_period_end = VALUES(current_period_end),
plan_name = VALUES(plan_name),
stripe_customer_id = VALUES(stripe_customer_id),
updated_at = NOW()
");
$stmt->execute([
$stephane['id'],
$active_subscription['id'],
$customer_id,
$plan_name,
$status,
$period_start,
$period_end
]);
// Update user plan
$stmt = $pdo->prepare("UPDATE users SET plan = ?, stripe_customer_id = COALESCE(stripe_customer_id, ?) WHERE id = ?");
$stmt->execute([$plan_name, $customer_id, $stephane['id']]);
// Get subscription ID
$sub_stmt = $pdo->prepare("SELECT id FROM user_subscriptions WHERE stripe_subscription_id = ?");
$sub_stmt->execute([$active_subscription['id']]);
$sub_record = $sub_stmt->fetch(PDO::FETCH_ASSOC);
$subscription_id = $sub_record['id'] ?? null;
// Initialize monthly track usage
$year_month = date('Y-m', $active_subscription['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([
$stephane['id'],
$subscription_id,
$period_start,
$year_month,
$track_limit
]);
$pdo->commit();
echo "<p class='success'>✓ Successfully updated subscription in database!</p>";
echo "<table>";
echo "<tr><th>Field</th><th>Value</th></tr>";
echo "<tr><td>User ID</td><td>{$stephane['id']}</td></tr>";
echo "<tr><td>Plan</td><td>{$plan_name}</td></tr>";
echo "<tr><td>Status</td><td>{$status}</td></tr>";
echo "<tr><td>Period Start</td><td>{$period_start}</td></tr>";
echo "<tr><td>Period End</td><td>{$period_end}</td></tr>";
echo "<tr><td>Stripe Subscription ID</td><td>{$active_subscription['id']}</td></tr>";
echo "<tr><td>Track Limit</td><td>{$track_limit}</td></tr>";
echo "</table>";
} catch (Exception $e) {
$pdo->rollBack();
echo "<p class='error'>✗ Error updating database: " . htmlspecialchars($e->getMessage()) . "</p>";
echo '</div></body></html>';
exit;
}
echo '</div>';
// Step 6: Verify
echo '<div class="section">';
echo '<h2>Step 6: Verification</h2>';
$stmt = $pdo->prepare("SELECT * FROM user_subscriptions WHERE user_id = ? ORDER BY created_at DESC LIMIT 1");
$stmt->execute([$stephane['id']]);
$db_subscription = $stmt->fetch(PDO::FETCH_ASSOC);
if ($db_subscription) {
echo "<p class='success'>✓ Subscription verified in database!</p>";
echo "<table>";
echo "<tr><th>Field</th><th>Database Value</th></tr>";
echo "<tr><td>Plan</td><td>{$db_subscription['plan_name']}</td></tr>";
echo "<tr><td>Status</td><td>{$db_subscription['status']}</td></tr>";
echo "<tr><td>Period End</td><td>{$db_subscription['current_period_end']}</td></tr>";
echo "</table>";
// Check user plan
$stmt = $pdo->prepare("SELECT plan FROM users WHERE id = ?");
$stmt->execute([$stephane['id']]);
$user_plan = $stmt->fetchColumn();
echo "<p class='info'>User's plan field: {$user_plan}</p>";
} else {
echo "<p class='error'>✗ Subscription not found in database after update</p>";
}
echo '</div>';
echo '<div class="section">';
echo '<h2>✅ Done!</h2>';
echo '<p>Stephane Bergeron\'s subscription has been updated in the database.</p>';
echo '<p>It should now appear in:</p>';
echo '<ul>';
echo '<li><a href="manage_subscription.php" style="color: #4299e1;">manage_subscription.php</a> (when Stephane logs in)</li>';
echo '<li><a href="account_settings.php?tab=credits" style="color: #4299e1;">account_settings.php?tab=credits</a></li>';
echo '<li><a href="account_settings.php?tab=subscription" style="color: #4299e1;">account_settings.php?tab=subscription</a></li>';
echo '<li><a href="profile.php" style="color: #4299e1;">profile.php</a></li>';
echo '</ul>';
echo '</div>';
?>
</body>
</html>