![]() 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/ |
<?php
/**
* Subscription Success Page
* Handles successful subscription signup
* CRITICAL: This page immediately records subscription info in database
* Webhooks are used as backup/sync, not primary source
*/
// Start output buffering BEFORE session_start to prevent any output
if (ob_get_level() === 0) {
ob_start();
}
session_start();
require_once 'config/database.php';
require_once __DIR__ . '/utils/subscription_helpers.php';
if (!isset($_SESSION['user_id'])) {
// Clean output buffer before redirect
while (ob_get_level() > 0) {
ob_end_clean();
}
header('Location: /auth/login.php');
exit;
}
$pdo = getDBConnection();
$session_id = $_GET['session_id'] ?? null;
$subscription_recorded = false;
$error_message = null;
if ($session_id) {
$stripe_secret = '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';
try {
// Step 1: Get checkout session from Stripe
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/checkout/sessions/' . urlencode($session_id) . '?expand[]=subscription');
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) {
$session = json_decode($response, true);
// Step 2: Get subscription ID from checkout session
$subscription_id = $session['subscription'] ?? null;
if ($subscription_id) {
// If subscription is expanded, use it directly, otherwise fetch it
if (is_array($subscription_id)) {
$subscription = $subscription_id;
} else {
// Fetch subscription details
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/subscriptions/' . urlencode($subscription_id));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$sub_response = curl_exec($ch);
$sub_http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($sub_http_code === 200) {
$subscription = json_decode($sub_response, true);
} else {
throw new Exception("Failed to fetch subscription from Stripe");
}
}
// Step 3: Get user ID from session metadata or current session
$user_id = $_SESSION['user_id'];
$customer_id = $subscription['customer'] ?? $session['customer'] ?? null;
$plan_name = $session['metadata']['plan'] ?? 'essential'; // Default to essential
// Determine plan from subscription price if not in metadata
if ($plan_name === 'essential' && !empty($subscription['items']['data'][0]['price']['id'])) {
require_once __DIR__ . '/config/subscription_plans.php';
$plans_config = require __DIR__ . '/config/subscription_plans.php';
$price_id = $subscription['items']['data'][0]['price']['id'];
foreach ($plans_config as $plan_key => $plan_data) {
if ($plan_data['stripe_price_id'] === $price_id) {
$plan_name = $plan_key;
break;
}
}
}
// Step 4: Record subscription in database IMMEDIATELY
$pdo->beginTransaction();
try {
// Update user's Stripe customer ID if not set
if ($customer_id) {
$stmt = $pdo->prepare("UPDATE users SET stripe_customer_id = COALESCE(stripe_customer_id, ?) WHERE id = ?");
$stmt->execute([$customer_id, $user_id]);
}
// Create or update subscription record
$period_start = date('Y-m-d H:i:s', $subscription['current_period_start']);
$period_end = date('Y-m-d H:i:s', $subscription['current_period_end']);
$status = $subscription['status'] ?? 'active';
$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),
stripe_customer_id = VALUES(stripe_customer_id),
updated_at = NOW()
");
$stmt->execute([
$user_id,
$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, $user_id]);
// Initialize monthly track usage
require_once __DIR__ . '/config/subscription_plans.php';
$plans_config = require __DIR__ . '/config/subscription_plans.php';
$track_limit = $plans_config[$plan_name]['tracks_per_month'] ?? 5;
// Get subscription ID from database
$sub_stmt = $pdo->prepare("SELECT id FROM user_subscriptions WHERE stripe_subscription_id = ?");
$sub_stmt->execute([$subscription['id']]);
$sub_record = $sub_stmt->fetch(PDO::FETCH_ASSOC);
$db_subscription_id = $sub_record['id'] ?? null;
$year_month = date('Y-m', $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([
$user_id,
$db_subscription_id,
$period_start,
$year_month,
$track_limit
]);
$pdo->commit();
$subscription_recorded = true;
// Log successful recording
error_log("Subscription recorded immediately at checkout success: User {$user_id}, Subscription {$subscription['id']}, Plan {$plan_name}");
} catch (Exception $e) {
$pdo->rollBack();
throw new Exception("Database error: " . $e->getMessage());
}
} else {
throw new Exception("No subscription found in checkout session");
}
} else {
throw new Exception("Failed to fetch checkout session from Stripe");
}
} catch (Exception $e) {
error_log("Error recording subscription at checkout success: " . $e->getMessage());
$error_message = "Subscription created but there was an error recording it. It should be synced via webhook shortly.";
// Don't fail completely - webhook will handle it
}
}
$page_title = 'Subscription Successful';
include 'includes/header.php';
?>
<main style="max-width: 600px; margin: 40px auto; padding: 20px; text-align: center;">
<div style="background: #2a2a2a; border-radius: 12px; padding: 40px;">
<div style="font-size: 4rem; margin-bottom: 20px;">✅</div>
<h1 style="color: white; margin-bottom: 10px;">Subscription Activated!</h1>
<p style="color: #a0aec0; font-size: 1.1rem; margin-bottom: 30px;">
Your subscription is now active! You can start creating tracks right away.
</p>
<?php if ($subscription_recorded): ?>
<div style="background: #2d5016; padding: 20px; border-radius: 8px; margin: 20px 0;">
<p style="color: #48bb78; margin: 10px 0;"><strong>✅ Subscription Recorded Successfully!</strong></p>
<p style="color: white;">Your subscription has been recorded in our database and is ready to use.</p>
</div>
<?php elseif ($error_message): ?>
<div style="background: #5a3a1a; padding: 20px; border-radius: 8px; margin: 20px 0;">
<p style="color: #ffc107; margin: 10px 0;"><strong>⚠️ Note:</strong></p>
<p style="color: white;"><?= htmlspecialchars($error_message) ?></p>
</div>
<?php endif; ?>
<div style="background: #1a1a1a; padding: 20px; border-radius: 8px; margin: 20px 0;">
<p style="color: white; margin: 10px 0;"><strong>What's Next?</strong></p>
<p style="color: #a0aec0;">Your subscription is active and your monthly limit will reset on your next billing date.</p>
</div>
<div style="background: linear-gradient(135deg, rgba(102, 126, 234, 0.1), rgba(118, 75, 162, 0.1)); border: 1px solid rgba(102, 126, 234, 0.3); padding: 20px; border-radius: 8px; margin: 20px 0;">
<p style="color: #667eea; margin: 10px 0; font-weight: 600;"><strong>💡 <?= t('subscribe.credits_available_title') ?></strong></p>
<p style="color: #a0aec0; margin: 0; font-size: 0.95rem;"><?= t('subscribe.credits_available_message') ?></p>
</div>
<div style="margin-top: 30px;">
<a href="/index.php#create" style="display: inline-block; padding: 12px 30px; background: #667eea; color: white; text-decoration: none; border-radius: 8px; margin: 5px;">
Start Creating
</a>
<a href="/manage_subscription.php" style="display: inline-block; padding: 12px 30px; background: #2a2a2a; color: white; text-decoration: none; border-radius: 8px; margin: 5px; border: 1px solid #444;">
Manage Subscription
</a>
</div>
</div>
</main>
<?php include 'includes/footer.php'; ?>