![]() 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
/**
* Fix All Price IDs
* Finds existing prices, creates missing ones, fixes wrong ones
*/
session_start();
require_once 'config/database.php';
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
die("Admin access required");
}
$plans_config = require __DIR__ . '/config/subscription_plans.php';
$stripe_secret = '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';
$results = [];
$config_updates = [];
// Fetch all existing prices from Stripe
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/prices?limit=100&type=recurring');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$existing_prices = [];
$price_id_to_amount = []; // Map Price ID to amount for verification
$prices_data = json_decode($response, true);
if (isset($prices_data['data'])) {
foreach ($prices_data['data'] as $price) {
$amount = $price['unit_amount'] / 100;
$price_id = $price['id'];
$existing_prices[(string)$amount] = $price_id;
$price_id_to_amount[$price_id] = $amount;
}
}
// Process each plan
foreach ($plans_config as $plan_key => $plan) {
$current_price_id = $plan['stripe_price_id'] ?? '';
$expected_amount = (string)$plan['price'];
$is_placeholder = (
empty($current_price_id) ||
strpos($current_price_id, 'price_') !== 0 ||
strlen($current_price_id) < 20 ||
strpos($current_price_id, '_monthly') !== false
);
// Verify current Price ID if it exists
$current_price_amount = null;
if (!$is_placeholder && isset($price_id_to_amount[$current_price_id])) {
$current_price_amount = $price_id_to_amount[$current_price_id];
}
// Check if current Price ID is correct
$current_is_correct = ($current_price_amount !== null && abs($current_price_amount - $plan['price']) < 0.01);
// Check if correct price exists in Stripe
$correct_price_id = $existing_prices[$expected_amount] ?? null;
if ($current_is_correct && $current_price_id === $correct_price_id) {
// Already correct
$results[$plan_key] = ['status' => 'correct', 'price_id' => $current_price_id];
} elseif ($correct_price_id) {
// Correct price exists in Stripe, but current one is wrong
$results[$plan_key] = [
'status' => 'fixed',
'old_price_id' => $current_price_id,
'old_amount' => $current_price_amount,
'price_id' => $correct_price_id
];
$config_updates[$plan_key] = $correct_price_id;
} else {
// Need to create price
try {
// Create Product
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/products');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'name' => $plan['name'] . ' Plan',
'description' => $plan['tracks_per_month'] . ' tracks per month subscription',
'metadata[plan_key]' => $plan_key,
'metadata[tracks_per_month]' => (string)$plan['tracks_per_month']
]));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200) {
throw new Exception("Failed to create product: " . $response);
}
$product = json_decode($response, true);
$product_id = $product['id'];
// Create Price
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.stripe.com/v1/prices');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $stripe_secret]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'product' => $product_id,
'unit_amount' => (int)($plan['price'] * 100),
'currency' => 'usd',
'recurring[interval]' => 'month',
'metadata[plan_key]' => $plan_key,
'metadata[plan_name]' => $plan['name']
]));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($http_code !== 200) {
throw new Exception("Failed to create price: " . $response);
}
$price = json_decode($response, true);
$price_id = $price['id'];
$results[$plan_key] = [
'status' => 'created',
'product_id' => $product_id,
'price_id' => $price_id
];
$config_updates[$plan_key] = $price_id;
} catch (Exception $e) {
$results[$plan_key] = ['status' => 'error', 'error' => $e->getMessage()];
}
}
}
// Update config file
if (!empty($config_updates)) {
$config_file = __DIR__ . '/config/subscription_plans.php';
$config_content = file_get_contents($config_file);
foreach ($config_updates as $plan_key => $price_id) {
// Match the specific plan's stripe_price_id line
$pattern = "/(['\"]{$plan_key}['\"]\s*=>\s*\[[^\]]*'stripe_price_id'\s*=>\s*['\"])[^'\"]*(['\"])/s";
if (preg_match($pattern, $config_content)) {
$config_content = preg_replace($pattern, "$1{$price_id}$2", $config_content, 1);
} else {
// Fallback
$pattern2 = "/(['\"]stripe_price_id['\"]\s*=>\s*['\"])[^'\"]*(['\"])/";
$config_content = preg_replace($pattern2, "$1{$price_id}$2", $config_content, 1);
}
}
file_put_contents($config_file, $config_content);
$config_updated = true;
} else {
$config_updated = false;
}
$page_title = 'Fix All Price IDs';
include 'includes/header.php';
?>
<main style="max-width: 900px; margin: 40px auto; padding: 20px;">
<div style="background: #2a2a2a; border-radius: 12px; padding: 40px;">
<h1 style="color: white; margin-bottom: 20px;">🔧 Fix All Price IDs</h1>
<?php if ($config_updated): ?>
<div style="background: #2d5016; padding: 20px; border-radius: 8px; margin-bottom: 30px;">
<h2 style="color: #48bb78; margin-bottom: 15px;">✅ Success!</h2>
<p style="color: white; margin-bottom: 15px;">
Fixed <strong><?= count($config_updates) ?></strong> Price IDs and updated config file.
</p>
<a href="/verify_stripe_prices.php" style="display: inline-block; padding: 12px 24px; background: #48bb78; color: white; text-decoration: none; border-radius: 8px; margin-top: 10px;">
Verify All Prices
</a>
</div>
<?php endif; ?>
<table style="width: 100%; border-collapse: collapse; color: white;">
<thead>
<tr style="border-bottom: 2px solid #444; background: #1a1a1a;">
<th style="padding: 15px; text-align: left;">Plan</th>
<th style="padding: 15px; text-align: left;">Status</th>
<th style="padding: 15px; text-align: left;">Price ID</th>
</tr>
</thead>
<tbody>
<?php foreach ($results as $plan_key => $result):
$plan = $plans_config[$plan_key];
?>
<tr style="border-bottom: 1px solid #333;">
<td style="padding: 15px;">
<strong style="color: <?= $plan['color'] ?>;"><?= htmlspecialchars($plan['name']) ?></strong>
<br><small style="color: #a0aec0;">$<?= number_format($plan['price'], 2) ?>/month</small>
</td>
<td style="padding: 15px;">
<?php if ($result['status'] === 'correct'): ?>
<span style="color: #48bb78;">✅ Correct</span>
<?php elseif ($result['status'] === 'fixed'): ?>
<span style="color: #48bb78;">✅ Fixed</span>
<br><small style="color: #a0aec0;">Was: <?= htmlspecialchars(substr($result['old_price_id'], 0, 20)) ?>...</small>
<?php elseif ($result['status'] === 'created'): ?>
<span style="color: #48bb78;">✅ Created</span>
<?php else: ?>
<span style="color: #e53e3e;">❌ Error</span>
<br><small style="color: #e53e3e;"><?= htmlspecialchars($result['error'] ?? 'Unknown') ?></small>
<?php endif; ?>
</td>
<td style="padding: 15px; font-family: monospace; font-size: 0.9rem;">
<?php if (isset($result['price_id'])): ?>
<span style="color: #48bb78;"><?= htmlspecialchars($result['price_id']) ?></span>
<?php else: ?>
<span style="color: #e53e3e;">N/A</span>
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</main>
<?php include 'includes/footer.php'; ?>