![]() 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/public_html/ |
<?php
/**
* CLI Version - Fix Stephane Bergeron's Subscription
* Run from command line: php fix_stephane_cli.php
*/
require_once __DIR__ . '/config/database.php';
require_once __DIR__ . '/utils/subscription_helpers.php';
$stripe_secret = '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';
$pdo = getDBConnection();
echo "=== Fixing Stephane Bergeron's Subscription ===\n\n";
// Step 1: Find user
echo "Step 1: Finding user...\n";
$stmt = $pdo->prepare("SELECT id, name, email, stripe_customer_id, plan FROM users WHERE email = ? OR (name LIKE ? AND email LIKE ?)");
$stmt->execute(['stevenberg450@gmail.com', '%Stephane%', '%stevenberg450%']);
$stephane = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$stephane) {
die("ERROR: User not found!\n");
}
echo "✓ Found: {$stephane['name']} (ID: {$stephane['id']}, Email: {$stephane['email']})\n";
echo " Current Plan: {$stephane['plan']}\n";
echo " Stripe Customer ID: " . ($stephane['stripe_customer_id'] ?: 'None') . "\n\n";
// Step 2: Get/Set customer ID
$customer_id = $stephane['stripe_customer_id'];
if (!$customer_id) {
echo "Step 2: Finding customer in Stripe...\n";
// Try by email first
$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'])) {
$customer_id = $customers['data'][0]['id'];
echo "✓ Found customer by email: {$customer_id}\n";
}
}
// If not found, use known ID from screenshot
if (!$customer_id) {
$customer_id = 'cus_TU1piJi9qLbFyS';
echo "✓ Using known customer ID from screenshot: {$customer_id}\n";
}
// Update database
$stmt = $pdo->prepare("UPDATE users SET stripe_customer_id = ? WHERE id = ?");
$stmt->execute([$customer_id, $stephane['id']]);
echo "✓ Updated customer ID in database\n\n";
} else {
echo "Step 2: Customer ID already set: {$customer_id}\n\n";
}
// Step 3: Fetch subscription
echo "Step 3: Fetching subscription from Stripe...\n";
$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) {
die("ERROR: Failed to fetch subscriptions (HTTP {$http_code})\n");
}
$subscriptions_data = json_decode($response, true);
$subscriptions = $subscriptions_data['data'] ?? [];
if (empty($subscriptions)) {
die("ERROR: No subscriptions found in Stripe\n");
}
// 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];
}
echo "✓ Found subscription: {$active_subscription['id']}\n";
echo " Status: {$active_subscription['status']}\n\n";
// Step 4: Determine plan
echo "Step 4: Determining plan...\n";
require_once __DIR__ . '/config/subscription_plans.php';
$plans_config = require __DIR__ . '/config/subscription_plans.php';
$plan_name = 'essential';
$price_id = $active_subscription['items']['data'][0]['price']['id'] ?? null;
if ($price_id) {
foreach ($plans_config as $plan_key => $plan_data) {
if ($plan_data['stripe_price_id'] === $price_id) {
$plan_name = $plan_key;
break;
}
}
// If no match, try by price
if ($plan_name === 'essential') {
$price_amount = ($active_subscription['items']['data'][0]['price']['unit_amount'] ?? 0) / 100;
foreach ($plans_config as $plan_key => $plan_data) {
if (abs($plan_data['price'] - $price_amount) < 0.01) {
$plan_name = $plan_key;
break;
}
}
}
}
$track_limit = $plans_config[$plan_name]['tracks_per_month'] ?? 5;
echo "✓ Plan: {$plan_name} ({$track_limit} tracks/month)\n\n";
// Step 5: Update database
echo "Step 5: Updating database...\n";
$period_start = isset($active_subscription['current_period_start']) ? date('Y-m-d H:i:s', $active_subscription['current_period_start']) : date('Y-m-d H:i:s');
$period_end = isset($active_subscription['current_period_end']) ? date('Y-m-d H:i:s', $active_subscription['current_period_end']) : date('Y-m-d H:i:s', strtotime('+1 month'));
$status = $active_subscription['status'] ?? 'active';
echo " Period Start: {$period_start}\n";
echo " Period End: {$period_end}\n";
echo " Status: {$status}\n\n";
$pdo->beginTransaction();
try {
// Create/update subscription
$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 = ? WHERE id = ?");
$stmt->execute([$plan_name, $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', isset($active_subscription['current_period_start']) ? $active_subscription['current_period_start'] : time());
// Check if usage record exists
$check_usage = $pdo->prepare("SELECT id FROM monthly_track_usage WHERE user_id = ? AND subscription_period_start = ?");
$check_usage->execute([$stephane['id'], $period_start]);
$existing_usage = $check_usage->fetch(PDO::FETCH_ASSOC);
if ($existing_usage) {
// Update existing record
$stmt = $pdo->prepare("
UPDATE monthly_track_usage
SET subscription_id = ?, track_limit = ?, reset_at = NOW(), `year_month` = ?
WHERE user_id = ? AND subscription_period_start = ?
");
$stmt->execute([$subscription_id, $track_limit, $year_month, $stephane['id'], $period_start]);
echo "✓ Updated existing monthly usage record\n";
} else {
// Insert new record - use backticks for year_month
$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())
");
$stmt->execute([
$stephane['id'],
$subscription_id,
$period_start,
$year_month,
$track_limit
]);
echo "✓ Created new monthly usage record\n";
}
$pdo->commit();
echo "✓ Subscription recorded successfully!\n";
echo "✓ User plan updated to: {$plan_name}\n";
echo "✓ Monthly usage initialized\n\n";
echo "=== SUCCESS ===\n";
echo "Stephane's subscription is now in the database.\n";
echo "It should appear in manage_subscription.php when he logs in.\n";
} catch (Exception $e) {
$pdo->rollBack();
die("ERROR: " . $e->getMessage() . "\n");
}