![]() 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/-7d074efb/ |
<?php
session_start();
header('Content-Type: application/json');
// Check if it's a POST request
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
echo json_encode(['success' => false, 'error' => 'Invalid request method']);
exit;
}
$data = json_decode(file_get_contents('php://input'), true);
if (!isset($data['index']) || !isset($data['type']) || !isset($data['identifier']) || !isset($data['cart_key'])) {
echo json_encode(['success' => false, 'error' => 'Missing required parameters']);
exit;
}
$index = (int)$data['index'];
$type = $data['type'];
$identifier = $data['identifier'];
$cartKey = $data['cart_key'];
$change = isset($data['change']) ? (int)$data['change'] : 0;
$remove = isset($data['remove']) && $data['remove'] === true;
try {
// Initialize cart if it doesn't exist
if (!isset($_SESSION[$cartKey])) {
$_SESSION[$cartKey] = [];
}
// Validate index
if ($index < 0 || $index >= count($_SESSION[$cartKey])) {
echo json_encode(['success' => false, 'error' => 'Invalid item index']);
exit;
}
// Get the item
$item = $_SESSION[$cartKey][$index];
// Verify the item matches
if ($type === 'track') {
$itemId = $item['track_id'] ?? null;
if ($itemId != $identifier) {
echo json_encode(['success' => false, 'error' => 'Item identifier mismatch']);
exit;
}
} elseif ($type === 'credit') {
$itemPackage = $item['package'] ?? null;
if ($itemPackage !== $identifier) {
echo json_encode(['success' => false, 'error' => 'Item identifier mismatch']);
exit;
}
}
// Remove item if requested
if ($remove) {
array_splice($_SESSION[$cartKey], $index, 1);
// Reindex array
$_SESSION[$cartKey] = array_values($_SESSION[$cartKey]);
echo json_encode(['success' => true, 'message' => 'Item removed']);
exit;
}
// Update quantity
$currentQuantity = $item['quantity'] ?? 1;
$newQuantity = $currentQuantity + $change;
// Don't allow quantity below 1
if ($newQuantity < 1) {
echo json_encode(['success' => false, 'error' => 'Quantity cannot be less than 1. Use remove button to delete item.']);
exit;
}
// Update the quantity
$_SESSION[$cartKey][$index]['quantity'] = $newQuantity;
echo json_encode([
'success' => true,
'message' => 'Quantity updated',
'new_quantity' => $newQuantity
]);
} catch (Exception $e) {
error_log('Error updating cart quantity: ' . $e->getMessage());
echo json_encode(['success' => false, 'error' => 'Server error: ' . $e->getMessage()]);
}
?>