'; echo "Shutdown error:\n" . htmlspecialchars(print_r($err, true)); echo ''; } }); /** * Shopping Cart - Rebuilt from scratch for reliability * Displays unpaid invoices and provides PayPal checkout * Standalone billing module - uses only standard PHP mysqli */ // Start session with website session name if (session_status() === PHP_SESSION_NONE) { session_name("gameservers_website"); session_start(); } // Load configuration require_once(__DIR__ . '/includes/config.inc.php'); // Variables from config.inc.php (helps IDEs understand scope) /** @var string $db_host Database host */ /** @var string $db_user Database user */ /** @var string $db_pass Database password */ /** @var string $db_name Database name */ /** @var string $table_prefix Table prefix for database tables */ /** @var string $SITE_BASE_URL Site base URL */ /** @var string $SITE_DATA_DIR Data directory path */ // Check if user is logged in $user_id = 0; if (isset($_SESSION['website_user_id']) && !empty($_SESSION['website_user_id'])) { $user_id = intval($_SESSION['website_user_id']); } elseif (isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])) { $user_id = intval($_SESSION['user_id']); } // Redirect to login if not authenticated if ($user_id <= 0) { $return_to = urlencode($_SERVER['REQUEST_URI'] ?? '/cart.php'); header('Location: /login.php?return_to=' . $return_to); exit; } // Connect to database (non-fatal) $db = @mysqli_connect($db_host, $db_user, $db_pass, $db_name); $db_error = ''; // Initialize variables $invoices = []; $total_amount = 0.00; $discount_amount = 0.00; $coupon_discount_percent = 0; $applied_coupon = null; $error_message = ''; $success_message = ''; if (!$db) { // record error for UI/debugging but do not die here $db_error = 'Database connection failed: ' . mysqli_connect_error(); $cart_empty = true; } else { // Fetch unpaid invoices for this user. Select only invoice fields to avoid referencing // columns that may not exist in all deployments (some schemas differ). $query = "SELECT i.* FROM {$table_prefix}billing_invoices i WHERE i.user_id = " . intval($user_id) . " AND i.status = 'due' ORDER BY i.invoice_date ASC"; $result = mysqli_query($db, $query); if ($result) { while ($row = mysqli_fetch_assoc($result)) { $invoices[] = $row; $total_amount += floatval($row['amount']); } mysqli_free_result($result); } $cart_empty = (count($invoices) === 0); } // Handle coupon application if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['apply_coupon'])) { $coupon_code = trim($_POST['coupon_code'] ?? ''); if (empty($coupon_code)) { $error_message = 'Please enter a coupon code.'; } else { // Validate coupon if (!$db) { $error_message = 'Coupon system unavailable: database connection failed.'; } else { $safe_code = mysqli_real_escape_string($db, $coupon_code); $coupon_query = "SELECT * FROM {$table_prefix}billing_coupons WHERE code = '$safe_code' AND is_active = 1"; $coupon_result = mysqli_query($db, $coupon_query); if ($coupon_result && mysqli_num_rows($coupon_result) === 1) { $coupon = mysqli_fetch_assoc($coupon_result); // Check if expired $expired = false; if (!empty($coupon['expires'])) { $expires_time = strtotime($coupon['expires']); if ($expires_time && $expires_time < time()) { $expired = true; } } // Check usage limit $max_uses_reached = false; if (!empty($coupon['max_uses'])) { if (intval($coupon['current_uses']) >= intval($coupon['max_uses'])) { $max_uses_reached = true; } } if ($expired) { $error_message = 'This coupon has expired.'; } elseif ($max_uses_reached) { $error_message = 'This coupon has reached its maximum usage limit.'; } else { // Check game filter $game_valid = true; if ($coupon['game_filter_type'] === 'specific_games' && !empty($coupon['game_filter_list'])) { $allowed_games = json_decode($coupon['game_filter_list'], true); if (is_array($allowed_games) && count($allowed_games) > 0) { $has_valid_game = false; foreach ($invoices as $inv) { $inv_game_key = isset($inv['game_key']) ? $inv['game_key'] : null; if ($inv_game_key !== null && in_array($inv_game_key, $allowed_games)) { $has_valid_game = true; break; } } if (!$has_valid_game) { $game_valid = false; } } } if (!$game_valid) { $error_message = 'This coupon is not valid for the items in your cart.'; } else { // Apply coupon $applied_coupon = $coupon; $coupon_discount_percent = floatval($coupon['discount_percent']); $_SESSION['cart_coupon_code'] = $coupon_code; $_SESSION['cart_coupon_id'] = $coupon['coupon_id']; $success_message = 'Coupon "' . htmlspecialchars($coupon['name']) . '" applied! You save ' . $coupon_discount_percent . '%'; } } mysqli_free_result($coupon_result); } else { $error_message = 'Invalid coupon code.'; } } } } // Handle coupon removal if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_coupon'])) { unset($_SESSION['cart_coupon_code']); unset($_SESSION['cart_coupon_id']); $applied_coupon = null; $coupon_discount_percent = 0; } // Re-validate coupon from session if present if (empty($applied_coupon) && isset($_SESSION['cart_coupon_code'])) { $coupon_code = $_SESSION['cart_coupon_code']; $safe_code = mysqli_real_escape_string($db, $coupon_code); $coupon_query = "SELECT * FROM {$table_prefix}billing_coupons WHERE code = '$safe_code' AND is_active = 1"; $coupon_result = mysqli_query($db, $coupon_query); if ($coupon_result && mysqli_num_rows($coupon_result) === 1) { $applied_coupon = mysqli_fetch_assoc($coupon_result); $coupon_discount_percent = floatval($applied_coupon['discount_percent']); mysqli_free_result($coupon_result); } else { // Coupon no longer valid, clear from session unset($_SESSION['cart_coupon_code']); unset($_SESSION['cart_coupon_id']); } } // Calculate discount if ($applied_coupon && $coupon_discount_percent > 0) { $discount_amount = $total_amount * ($coupon_discount_percent / 100); } $final_amount = $total_amount - $discount_amount; // PayPal configuration $sandbox = true; $client_id = 'AfvY_C2zA_hTHxHq7TIhtOeub4xBdySYrt_Hjj3d_WYQwjWI9NfOAVOTeResx2rgZ_nP5tOoxQSAHw8c'; // Prepare PayPal items $paypal_items = []; foreach ($invoices as $inv) { $game_display = !empty($inv['game_name']) ? $inv['game_name'] : 'Game Server'; $qty = max(1, intval($inv['qty'])); $paypal_items[] = [ 'name' => $inv['home_name'] . ' (' . $game_display . ')', 'description' => $inv['description'] ?? '', 'quantity' => $qty, 'unit_amount' => [ 'currency_code' => 'USD', 'value' => number_format(floatval($inv['amount']) / $qty, 2, '.', '') ] ]; } // Get site base URL $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://'; $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; $siteBase = $protocol . $host; // Close database connection mysqli_close($db); ?> Shopping Cart - Game Server Panel
Database error:

🛒 Shopping Cart

Your cart is empty

Browse our game servers and add them to your cart to get started!

Browse Servers
Game Server Duration Quantity Status Price
x $

Coupon Code

Coupon Applied: (% off)
0): ?>
Subtotal: $
Discount (%): -$
Total: $

Checkout with PayPal

Click the button below to complete your purchase securely through PayPal.