![]() 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
/**
* Stripe Price Setup Helper
* Creates products and prices in Stripe for all subscription tiers
* Run this ONCE to set up all subscription prices
*/
session_start();
require_once 'config/database.php';
// Check admin access
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 = [];
$errors = [];
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['create_prices'])) {
// Only create prices that don't have valid Price IDs yet
foreach ($plans_config as $plan_key => $plan) {
$current_price_id = $plan['stripe_price_id'] ?? '';
$has_valid_price = (
strpos($current_price_id, 'price_') === 0 &&
strlen($current_price_id) > 20 &&
strpos($current_price_id, '_monthly') === false // Skip placeholders
);
// Skip if already has a valid price ID (unless force recreate)
if ($has_valid_price && !isset($_POST['force_recreate'])) {
$results[$plan_key] = [
'status' => 'skipped',
'price_id' => $current_price_id,
'message' => 'Already has valid Price ID'
];
continue;
}
try {
// Step 1: 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'];
// Step 2: 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), // Convert to cents
'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] = [
'product_id' => $product_id,
'price_id' => $price_id,
'status' => 'success'
];
} catch (Exception $e) {
$errors[$plan_key] = $e->getMessage();
}
}
// Auto-update config file if prices were created successfully
if (!empty($results)) {
$config_file = __DIR__ . '/config/subscription_plans.php';
$config_content = file_get_contents($config_file);
// Update each successfully created price
foreach ($results as $plan_key => $result) {
// More specific pattern to match the exact plan's stripe_price_id line
$pattern = "/(['\"]stripe_price_id['\"]\s*=>\s*['\"])[^'\"]*(['\"])/";
$replacement = "$1{$result['price_id']}$2";
// Find the plan block and replace within it
$plan_pattern = "/('{$plan_key}'\s*=>\s*\[[^\]]*'stripe_price_id'\s*=>\s*['\"])[^'\"]*(['\"])/s";
if (preg_match($plan_pattern, $config_content)) {
$config_content = preg_replace($plan_pattern, "$1{$result['price_id']}$2", $config_content, 1);
} else {
// Fallback: replace any stripe_price_id for this plan
$config_content = preg_replace($pattern, $replacement, $config_content, 1);
}
}
file_put_contents($config_file, $config_content);
$config_updated = (empty($errors));
} else {
$config_updated = false;
}
}
$page_title = 'Stripe Price Setup';
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;">đ§ Stripe Price Setup</h1>
<p style="color: #a0aec0; margin-bottom: 30px;">
This tool will create all subscription products and prices in your Stripe account automatically.
</p>
<?php if (!empty($results)): ?>
<div style="background: #2d5016; padding: 20px; border-radius: 8px; margin-bottom: 30px;">
<h2 style="color: #48bb78; margin-bottom: 15px;">
<?php
$created_count = count(array_filter($results, function($r) { return ($r['status'] ?? '') === 'success'; }));
$skipped_count = count(array_filter($results, function($r) { return ($r['status'] ?? '') === 'skipped'; }));
if ($created_count > 0) {
echo "â
Prices Created Successfully!";
} elseif ($skipped_count > 0) {
echo "âšī¸ Prices Already Exist";
} else {
echo "â ī¸ Price Creation Results";
}
?>
</h2>
<?php if (isset($config_updated) && $config_updated): ?>
<div style="background: #1a1a1a; padding: 15px; border-radius: 8px; margin-bottom: 20px; border: 2px solid #48bb78;">
<p style="color: #48bb78; font-size: 1.2rem; margin: 0;">
<strong>đ Config File Auto-Updated!</strong> The Price IDs have been automatically saved to <code>config/subscription_plans.php</code>
</p>
</div>
<?php else: ?>
<div style="background: #1a1a1a; padding: 15px; border-radius: 8px; margin-bottom: 20px;">
<p style="color: #ffc107; margin-bottom: 10px;"><strong>â ī¸ Note:</strong> Some prices failed. Please check errors below and update config manually if needed.</p>
</div>
<?php endif; ?>
<div style="background: #1a1a1a; padding: 20px; border-radius: 8px; font-family: monospace;">
<?php foreach ($results as $plan_key => $result):
$plan = $plans_config[$plan_key];
$status = $result['status'] ?? 'unknown';
?>
<div style="color: white; margin-bottom: 15px; padding: 10px; background: #2a2a2a; border-radius: 5px; border-left: 4px solid <?= $status === 'success' ? '#48bb78' : ($status === 'skipped' ? '#ffc107' : '#e53e3e') ?>;">
<strong style="color: <?= $plan['color'] ?>;"><?= htmlspecialchars($plan['name']) ?> (<?= ucfirst($plan_key) ?>):</strong>
<?php if ($status === 'success'): ?>
<br>Product ID: <span style="color: #48bb78;"><?= htmlspecialchars($result['product_id']) ?></span>
<br><strong>Price ID: <span style="color: #48bb78;"><?= htmlspecialchars($result['price_id']) ?></span></strong>
<?php elseif ($status === 'skipped'): ?>
<br><span style="color: #ffc107;">âī¸ Skipped: <?= htmlspecialchars($result['message'] ?? 'Already configured') ?></span>
<br>Current Price ID: <span style="color: #a0aec0;"><?= htmlspecialchars($result['price_id']) ?></span>
<?php else: ?>
<br><span style="color: #e53e3e;">â Error: <?= htmlspecialchars($result['error'] ?? 'Unknown error') ?></span>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php if (isset($config_updated) && $config_updated): ?>
<div style="margin-top: 20px; padding: 15px; background: #1a1a1a; border-radius: 8px; text-align: center;">
<p style="color: #48bb78; font-size: 1.1rem; margin-bottom: 15px;">
<strong>â
All Done! Subscriptions are now ready to use.</strong>
</p>
<a href="/subscribe.php?plan=essential" style="display: inline-block; padding: 12px 24px; background: #667eea; color: white; text-decoration: none; border-radius: 8px; margin-right: 10px;">
Test Essential Plan
</a>
<a href="/pricing.php" style="display: inline-block; padding: 12px 24px; background: #48bb78; color: white; text-decoration: none; border-radius: 8px;">
View Pricing Page
</a>
</div>
<?php endif; ?>
</div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div style="background: #5a1a1a; padding: 20px; border-radius: 8px; margin-bottom: 30px;">
<h2 style="color: #e53e3e; margin-bottom: 15px;">â Errors</h2>
<?php foreach ($errors as $plan_key => $error): ?>
<p style="color: #e53e3e; margin-bottom: 10px;">
<strong><?= ucfirst($plan_key) ?>:</strong> <?= htmlspecialchars($error) ?>
</p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<div style="background: #1a1a1a; padding: 30px; border-radius: 8px; margin-bottom: 30px;">
<h2 style="color: white; margin-bottom: 20px;">Plans to Create</h2>
<table style="width: 100%; color: white; border-collapse: collapse;">
<thead>
<tr style="border-bottom: 1px solid #333;">
<th style="padding: 10px; text-align: left;">Plan</th>
<th style="padding: 10px; text-align: left;">Price</th>
<th style="padding: 10px; text-align: left;">Tracks/Month</th>
<th style="padding: 10px; text-align: left;">Current Price ID</th>
</tr>
</thead>
<tbody>
<?php foreach ($plans_config as $plan_key => $plan): ?>
<tr style="border-bottom: 1px solid #333;">
<td style="padding: 10px;"><?= htmlspecialchars($plan['name']) ?></td>
<td style="padding: 10px;">$<?= number_format($plan['price'], 2) ?>/month</td>
<td style="padding: 10px;"><?= $plan['tracks_per_month'] ?></td>
<td style="padding: 10px; color: <?= (strpos($plan['stripe_price_id'], 'price_') === 0 && strlen($plan['stripe_price_id']) > 20) ? '#48bb78' : '#e53e3e' ?>;">
<?= htmlspecialchars($plan['stripe_price_id']) ?>
<?php if (strpos($plan['stripe_price_id'], 'price_') === 0 && strlen($plan['stripe_price_id']) > 20): ?>
â
<?php else: ?>
â ī¸ Not configured
<?php endif; ?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<form method="POST" style="text-align: center;">
<button type="submit" name="create_prices" value="1" style="padding: 15px 40px; background: #667eea; color: white; border: none; border-radius: 8px; font-size: 1.2rem; cursor: pointer;">
đ Create All Prices in Stripe
</button>
</form>
<div style="margin-top: 30px; padding: 20px; background: #1a1a1a; border-radius: 8px;">
<h3 style="color: white; margin-bottom: 15px;">â ī¸ Important Notes</h3>
<ul style="color: #a0aec0; line-height: 1.8;">
<li>This will create products and prices in your <strong>LIVE</strong> Stripe account</li>
<li>Make sure you're using the correct Stripe API key (live vs test)</li>
<li>After creating, you MUST update <code>config/subscription_plans.php</code> with the Price IDs</li>
<li>You only need to run this once</li>
<li>If prices already exist, this will create duplicates (that's okay, just use the new ones)</li>
</ul>
</div>
</div>
</main>
<?php include 'includes/footer.php'; ?>