![]() 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/api/ |
<?php
session_start();
header('Content-Type: application/json');
require_once '../config/database.php';
require_once '../includes/translations.php';
const MAX_TICKETS_PER_PURCHASE = 10;
// 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['type']) || !isset($data['identifier']) || !isset($data['cart_key'])) {
echo json_encode(['success' => false, 'error' => 'Missing required parameters']);
exit;
}
$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 {
$pdo = getDBConnection();
// Initialize cart if it doesn't exist
if (!isset($_SESSION[$cartKey])) {
$_SESSION[$cartKey] = [];
}
// Find the item by identifier instead of using index
// This is more reliable since the merged cart index may not match the session array index
$itemIndex = null;
$item = null;
foreach ($_SESSION[$cartKey] as $idx => $cartItem) {
$matches = false;
if ($type === 'track') {
$itemId = $cartItem['track_id'] ?? null;
if ($itemId == $identifier) {
$matches = true;
}
} elseif ($type === 'credit') {
$itemPackage = $cartItem['package'] ?? null;
if ($itemPackage === $identifier) {
$matches = true;
}
} elseif ($type === 'ticket') {
$itemEventId = $cartItem['event_id'] ?? null;
if ($itemEventId == $identifier) {
$matches = true;
}
}
if ($matches) {
$itemIndex = $idx;
$item = $cartItem;
break;
}
}
// If item not found, try to find by checking if it's a duplicate (same track/package/event)
if ($itemIndex === null) {
// For tracks, check if there's a duplicate with same track_id
if ($type === 'track') {
foreach ($_SESSION[$cartKey] as $idx => $cartItem) {
if (isset($cartItem['track_id']) && $cartItem['track_id'] == $identifier) {
$itemIndex = $idx;
$item = $cartItem;
break;
}
}
} elseif ($type === 'credit') {
foreach ($_SESSION[$cartKey] as $idx => $cartItem) {
if (isset($cartItem['package']) && $cartItem['package'] === $identifier) {
$itemIndex = $idx;
$item = $cartItem;
break;
}
}
} elseif ($type === 'ticket') {
foreach ($_SESSION[$cartKey] as $idx => $cartItem) {
if (isset($cartItem['event_id']) && $cartItem['event_id'] == $identifier) {
$itemIndex = $idx;
$item = $cartItem;
break;
}
}
}
}
if ($itemIndex === null || $item === null) {
echo json_encode(['success' => false, 'error' => 'Item not found in cart']);
exit;
}
// Remove item if requested
if ($remove) {
array_splice($_SESSION[$cartKey], $itemIndex, 1);
// Reindex array
$_SESSION[$cartKey] = array_values($_SESSION[$cartKey]);
echo json_encode(['success' => true, 'message' => 'Item removed']);
exit;
}
// Track purchases are limited to a single copy
if ($type === 'track' && $change !== 0) {
echo json_encode(['success' => false, 'error' => t('cart.single_quantity_error') ?? 'Tracks can only be purchased once.']);
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;
}
if ($type === 'ticket') {
if ($newQuantity > MAX_TICKETS_PER_PURCHASE) {
echo json_encode(['success' => false, 'error' => 'You can only purchase up to ' . MAX_TICKETS_PER_PURCHASE . ' tickets per event.']);
exit;
}
$stmt = $pdo->prepare("
SELECT
e.id,
e.max_attendees,
COUNT(DISTINCT et.id) as tickets_sold
FROM events e
LEFT JOIN event_tickets et ON e.id = et.event_id AND et.status IN ('pending', 'confirmed')
WHERE e.id = ? AND e.status = 'published'
GROUP BY e.id
");
$stmt->execute([$identifier]);
$event = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$event) {
echo json_encode(['success' => false, 'error' => 'Event not available.']);
exit;
}
if (!empty($event['max_attendees'])) {
$remainingCapacity = max(0, $event['max_attendees'] - $event['tickets_sold']);
if ($change > 0 && $remainingCapacity <= 0) {
echo json_encode(['success' => false, 'error' => 'This event is sold out.']);
exit;
}
if ($change > 0 && $change > $remainingCapacity) {
echo json_encode(['success' => false, 'error' => "Only {$remainingCapacity} ticket(s) remain for this event."]);
exit;
}
}
}
// Update the quantity
$_SESSION[$cartKey][$itemIndex]['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()]);
}
?>