From 48472b88a19d67b3c45976d984c6cbcb65af8cd4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 9 Nov 2025 20:51:15 +0000 Subject: [PATCH 1/7] Initial plan From 5dd60687fd53531dc86e8546cca02fa6ab476e3b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 9 Nov 2025 20:56:46 +0000 Subject: [PATCH 2/7] Rebuild cart.php from scratch for reliability Co-authored-by: iaretechnician <2749183+iaretechnician@users.noreply.github.com> --- modules/billing/cart.php | 516 ++++++++++++++++++-------------- modules/billing/cart_old.php | 564 +++++++++++++++++++++++++++++++++++ 2 files changed, 851 insertions(+), 229 deletions(-) create mode 100644 modules/billing/cart_old.php 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);
-