diff --git a/modules/billing/cart.php b/modules/billing/cart.php index 6492221c..7805cba3 100644 --- a/modules/billing/cart.php +++ b/modules/billing/cart.php @@ -1,146 +1,137 @@ = intval($coupon['max_uses'])) { - $max_uses_reached = 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) { - $coupon_error = 'This coupon has expired.'; - } elseif ($max_uses_reached) { - $coupon_error = '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) { - // Check if any invoice game is in allowed list - $has_valid_game = false; - foreach ($invoices as $inv) { - if (in_array($inv['game_key'], $allowed_games)) { - $has_valid_game = true; - break; - } - } - if (!$has_valid_game) { - $game_valid = false; + } + + 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) { + if (in_array($inv['game_key'], $allowed_games)) { + $has_valid_game = true; + break; } } - } - - if (!$game_valid) { - $coupon_error = 'This coupon is not valid for the items in your cart.'; - } else { - // Apply coupon (stored in session, applied at checkout) - $applied_coupon = $coupon; - $coupon_code = $submitted_code; - $coupon_discount_percent = floatval($coupon['discount_percent']); - $_SESSION['cart_coupon_code'] = $coupon_code; - $_SESSION['cart_coupon_id'] = $coupon['coupon_id']; - $coupon_success = 'Coupon "' . htmlspecialchars($coupon['name']) . '" applied! You save ' . $coupon_discount_percent . '%'; + if (!$has_valid_game) { + $game_valid = false; + } } } - } else { - $coupon_error = 'Invalid coupon code.'; + + 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.'; } } } @@ -149,16 +140,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['apply_coupon'])) { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_coupon'])) { unset($_SESSION['cart_coupon_code']); unset($_SESSION['cart_coupon_id']); - $coupon_code = ''; - $coupon_discount_percent = 0; $applied_coupon = null; + $coupon_discount_percent = 0; } -// Calculate discount if coupon is applied -// Calculate discount if coupon is applied -$discount_amount = 0; -if (!empty($coupon_code) && $coupon_discount_percent > 0 && $db) { - // Re-validate the coupon from session +// 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"; @@ -167,40 +155,48 @@ if (!empty($coupon_code) && $coupon_discount_percent > 0 && $db) { if ($coupon_result && mysqli_num_rows($coupon_result) === 1) { $applied_coupon = mysqli_fetch_assoc($coupon_result); $coupon_discount_percent = floatval($applied_coupon['discount_percent']); - $discount_amount = $total_amount * ($coupon_discount_percent / 100); + mysqli_free_result($coupon_result); + } else { + // Coupon no longer valid, clear from session + unset($_SESSION['cart_coupon_code']); + unset($_SESSION['cart_coupon_id']); } -} else { - // No DB or no coupon: ensure discount is zero - $discount_amount = 0; +} + +// 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; // Set to false for live PayPal +$sandbox = true; $client_id = 'AfvY_C2zA_hTHxHq7TIhtOeub4xBdySYrt_Hjj3d_WYQwjWI9NfOAVOTeResx2rgZ_nP5tOoxQSAHw8c'; -// Prepare PayPal items array +// 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' => intval($inv['qty']), + 'description' => $inv['description'] ?? '', + 'quantity' => $qty, 'unit_amount' => [ 'currency_code' => 'USD', - 'value' => number_format(floatval($inv['amount']) / intval($inv['qty']), 2, '.', '') + 'value' => number_format(floatval($inv['amount']) / $qty, 2, '.', '') ] ]; } -// Get site base URL dynamically +// Get site base URL $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://'; $host = $_SERVER['HTTP_HOST'] ?? 'localhost'; $siteBase = $protocol . $host; -if ($db) mysqli_close($db); +// Close database connection +mysqli_close($db); ?> @@ -211,14 +207,14 @@ if ($db) mysqli_close($db); @@ -356,15 +429,18 @@ if ($db) mysqli_close($db); -
- -
- Database error: -
The cart is read-only while the database is unavailable.
-
- + +

🛒 Shopping Cart

+ +
+ + + +
+ +

Your cart is empty

@@ -389,18 +465,12 @@ if ($db) mysqli_close($db);
-
- -
+
- x - - - - - + x + $ @@ -410,69 +480,58 @@ if ($db) mysqli_close($db); -
-

Apply Coupon Code

+
+

Coupon Code

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

Checkout with PayPal

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

- +
-
+ @@ -533,7 +592,6 @@ if ($db) mysqli_close($db); console.log('Capture result:', orderData); if (orderData.status === 'COMPLETED') { setStatus('Payment successful! Redirecting...'); - // Redirect to success page window.location.href = '/payment_success.php?order_id=' + data.orderID; } else { throw new Error('Unexpected payment status: ' + orderData.status); @@ -561,4 +619,4 @@ if ($db) mysqli_close($db);
- \ No newline at end of file + diff --git a/modules/billing/cart_old.php b/modules/billing/cart_old.php new file mode 100644 index 00000000..6492221c --- /dev/null +++ b/modules/billing/cart_old.php @@ -0,0 +1,564 @@ += intval($coupon['max_uses'])) { + $max_uses_reached = true; + } + } + + if ($expired) { + $coupon_error = 'This coupon has expired.'; + } elseif ($max_uses_reached) { + $coupon_error = '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) { + // Check if any invoice game is in allowed list + $has_valid_game = false; + foreach ($invoices as $inv) { + if (in_array($inv['game_key'], $allowed_games)) { + $has_valid_game = true; + break; + } + } + if (!$has_valid_game) { + $game_valid = false; + } + } + } + + if (!$game_valid) { + $coupon_error = 'This coupon is not valid for the items in your cart.'; + } else { + // Apply coupon (stored in session, applied at checkout) + $applied_coupon = $coupon; + $coupon_code = $submitted_code; + $coupon_discount_percent = floatval($coupon['discount_percent']); + $_SESSION['cart_coupon_code'] = $coupon_code; + $_SESSION['cart_coupon_id'] = $coupon['coupon_id']; + $coupon_success = 'Coupon "' . htmlspecialchars($coupon['name']) . '" applied! You save ' . $coupon_discount_percent . '%'; + } + } + } else { + $coupon_error = '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']); + $coupon_code = ''; + $coupon_discount_percent = 0; + $applied_coupon = null; +} + +// Calculate discount if coupon is applied +// Calculate discount if coupon is applied +$discount_amount = 0; +if (!empty($coupon_code) && $coupon_discount_percent > 0 && $db) { + // Re-validate the coupon from session + $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']); + $discount_amount = $total_amount * ($coupon_discount_percent / 100); + } +} else { + // No DB or no coupon: ensure discount is zero + $discount_amount = 0; +} + +$final_amount = $total_amount - $discount_amount; + +// PayPal configuration +$sandbox = true; // Set to false for live PayPal +$client_id = 'AfvY_C2zA_hTHxHq7TIhtOeub4xBdySYrt_Hjj3d_WYQwjWI9NfOAVOTeResx2rgZ_nP5tOoxQSAHw8c'; + +// Prepare PayPal items array +$paypal_items = []; +foreach ($invoices as $inv) { + $game_display = !empty($inv['game_name']) ? $inv['game_name'] : 'Game Server'; + $paypal_items[] = [ + 'name' => $inv['home_name'] . ' (' . $game_display . ')', + 'description' => $inv['description'], + 'quantity' => intval($inv['qty']), + 'unit_amount' => [ + 'currency_code' => 'USD', + 'value' => number_format(floatval($inv['amount']) / intval($inv['qty']), 2, '.', '') + ] + ]; +} + +// Get site base URL dynamically +$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://'; +$host = $_SERVER['HTTP_HOST'] ?? 'localhost'; +$siteBase = $protocol . $host; + +if ($db) mysqli_close($db); +?> + + + + + + Shopping Cart - Game Server Panel + + + + + + + + +
+ +
+ Database error: +
The cart is read-only while the database is unavailable.
+
+ +

🛒 Shopping Cart

+ + +
+

Your cart is empty

+

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

+ Browse Servers +
+ + + + + + + + + + + + + + + + + + + + + + +
Game ServerDurationQuantityStatusPrice
+
+
+ +
+ +
+ +
x + + + + + $ +
+ + +
+

Apply Coupon Code

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

Checkout with PayPal

+

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

+ +
+ + + +
+ + + +
+ + \ No newline at end of file