![]() 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/.cursor-server/data/User/History/-3aa828e1/ |
<?php
session_start();
header('Content-Type: application/json');
// Enable error logging
error_log("add_payment_method.php called at " . date('Y-m-d H:i:s'));
if (!isset($_SESSION['user_id'])) {
error_log("add_payment_method.php: User not authenticated");
echo json_encode(['success' => false, 'error' => 'Not authenticated']);
exit;
}
require_once '../config/database.php';
$pdo = getDBConnection();
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = ?');
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
if (!$user) {
echo json_encode(['success' => false, 'error' => 'User not found']);
exit;
}
$stripe_secret = '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';
$customer_id = $user['stripe_customer_id'];
// Create Stripe customer if doesn't exist
if (!$customer_id) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/customers");
curl_setopt($ch, CURLOPT_USERPWD, $stripe_secret . ":");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'email' => $user['email'],
'name' => $user['name']
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$customer = json_decode($response, true);
curl_close($ch);
if (!empty($customer['id'])) {
// Save the new customer ID to database
$stmt = $pdo->prepare("UPDATE users SET stripe_customer_id = ? WHERE id = ?");
$stmt->execute([$customer['id'], $user['id']]);
$customer_id = $customer['id'];
error_log("add_payment_method.php: Created and saved new customer ID: " . $customer['id']);
} else {
error_log("add_payment_method.php: Failed to create Stripe customer. Response: " . $response);
echo json_encode(['success' => false, 'error' => 'Failed to create Stripe customer']);
exit;
}
}
$input = json_decode(file_get_contents('php://input'), true);
$payment_method_id = $input['payment_method_id'] ?? null;
if (!$payment_method_id) {
echo json_encode(['success' => false, 'error' => 'No payment method ID']);
exit;
}
// Attach payment method to customer
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/payment_methods/" . $payment_method_id . "/attach");
curl_setopt($ch, CURLOPT_USERPWD, $stripe_secret . ":");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['customer' => $customer_id]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$attach_result = json_decode($response, true);
curl_close($ch);
if ($http_code !== 200 || !empty($attach_result['error'])) {
echo json_encode(['success' => false, 'error' => $attach_result['error']['message'] ?? 'Stripe error']);
exit;
}
// Optionally set as default if first card (fetch all cards)
$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);
$resp = curl_exec($ch);
$pm_list = json_decode($resp, true);
curl_close($ch);
if (isset($pm_list['data']) && count($pm_list['data']) === 1) {
// Set as default
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com/v1/customers/$customer_id");
curl_setopt($ch, CURLOPT_USERPWD, $stripe_secret . ":");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'invoice_settings[default_payment_method]' => $payment_method_id
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
}
error_log("add_payment_method.php: Successfully added payment method " . $payment_method_id);
echo json_encode(['success' => true]);