![]() 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
session_start();
require_once 'config/database.php';
if (!isset($_SESSION['user_id'])) {
die('Not logged in');
}
$pdo = getDBConnection();
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
if (empty($user['stripe_customer_id'])) {
die('No Stripe customer ID found');
}
$stripe_secret = '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';
$customer_id = $user['stripe_customer_id'];
echo "<h1>Cleanup Duplicate Payment Methods</h1>";
echo "<p><strong>Customer ID:</strong> $customer_id</p>";
// Fetch all payment methods
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/payment_methods?customer=$customer_id&type=card");
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("Failed to fetch payment methods. HTTP: $http_code");
}
$payment_data = json_decode($response, true);
$payment_methods = $payment_data['data'] ?? [];
echo "<h2>Found " . count($payment_methods) . " payment methods</h2>";
// Group by card details to find duplicates
$card_groups = [];
foreach ($payment_methods as $pm) {
$card_key = $pm['card']['brand'] . '_' . $pm['card']['last4'] . '_' . $pm['card']['exp_month'] . '_' . $pm['card']['exp_year'];
if (!isset($card_groups[$card_key])) {
$card_groups[$card_key] = [];
}
$card_groups[$card_key][] = $pm;
}
$duplicates_removed = 0;
foreach ($card_groups as $card_key => $cards) {
if (count($cards) > 1) {
echo "<h3>Duplicate cards found for: $card_key</h3>";
echo "<ul>";
// Keep the first one, remove the rest
for ($i = 0; $i < count($cards); $i++) {
$card = $cards[$i];
echo "<li>";
echo "<strong>ID:</strong> " . $card['id'] . " - ";
echo "<strong>Brand:</strong> " . $card['card']['brand'] . " - ";
echo "<strong>Last 4:</strong> " . $card['card']['last4'] . " - ";
echo "<strong>Exp:</strong> " . $card['card']['exp_month'] . "/" . $card['card']['exp_year'];
if ($i === 0) {
echo " <strong>(KEEPING)</strong>";
} else {
echo " <strong>(REMOVING)</strong>";
// Detach the duplicate payment method
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/payment_methods/" . $card['id'] . "/detach");
curl_setopt($ch, CURLOPT_USERPWD, $stripe_secret . ":");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$detach_response = curl_exec($ch);
$detach_http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($detach_http === 200) {
echo " <span style='color: green;'>✓ Removed</span>";
$duplicates_removed++;
} else {
echo " <span style='color: red;'>✗ Failed to remove</span>";
}
}
echo "</li>";
}
echo "</ul>";
}
}
echo "<h2>Cleanup Complete</h2>";
echo "<p><strong>Duplicates removed:</strong> $duplicates_removed</p>";
echo "<p><strong>Remaining cards:</strong> " . count($payment_methods) . "</p>";
// Show remaining cards
if (!empty($payment_methods)) {
echo "<h3>Remaining Cards:</h3>";
echo "<ul>";
foreach ($payment_methods as $pm) {
echo "<li>";
echo "<strong>Brand:</strong> " . $pm['card']['brand'] . " - ";
echo "<strong>Last 4:</strong> " . $pm['card']['last4'] . " - ";
echo "<strong>Exp:</strong> " . $pm['card']['exp_month'] . "/" . $pm['card']['exp_year'];
echo "</li>";
}
echo "</ul>";
}
echo "<p><a href='/account_settings.php?tab=payment'>Back to Account Settings</a></p>";
echo "<script>setTimeout(function() { window.location.href = '/account_settings.php?tab=payment'; }, 3000);</script>";
?>