From 784b27739c74d214c137cf56a26f3190d416ad49 Mon Sep 17 00:00:00 2001 From: Frank Harris Date: Mon, 10 Nov 2025 13:02:10 -0500 Subject: [PATCH] delete order from cart --- modules/billing/cart.php | 125 +++++++- modules/billing/cart_old.php | 564 ----------------------------------- 2 files changed, 119 insertions(+), 570 deletions(-) delete mode 100644 modules/billing/cart_old.php diff --git a/modules/billing/cart.php b/modules/billing/cart.php index 3efa1d67..d8c803c8 100644 --- a/modules/billing/cart.php +++ b/modules/billing/cart.php @@ -209,6 +209,68 @@ if (empty($applied_coupon) && isset($_SESSION['cart_coupon_code'])) { } } +// AJAX remove invoice action (hard delete) - returns JSON when remove_invoice_ajax is set +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_invoice_ajax']) && isset($_POST['invoice_id'])) { + header('Content-Type: application/json'); + $remove_id = intval($_POST['invoice_id']); + if ($remove_id <= 0) { + echo json_encode(['success' => false, 'error' => 'Invalid invoice id.']); + exit; + } + + if (!$db) { + echo json_encode(['success' => false, 'error' => 'Database unavailable.']); + exit; + } + + // Verify ownership and that invoice is still due + $check_q = "SELECT invoice_id FROM {$table_prefix}billing_invoices WHERE invoice_id = " . intval($remove_id) . " AND user_id = " . intval($user_id) . " AND status = 'due' LIMIT 1"; + $check_r = mysqli_query($db, $check_q); + if (!($check_r && mysqli_num_rows($check_r) === 1)) { + echo json_encode(['success' => false, 'error' => 'Invoice not found or cannot be removed.']); + exit; + } + + // Hard-delete the invoice row + $del_q = "DELETE FROM {$table_prefix}billing_invoices WHERE invoice_id = " . intval($remove_id) . " AND user_id = " . intval($user_id) . " AND status = 'due' LIMIT 1"; + $ok = mysqli_query($db, $del_q); + if ($ok && mysqli_affected_rows($db) > 0) { + echo json_encode(['success' => true]); + } else { + echo json_encode(['success' => false, 'error' => 'Failed to delete invoice.']); + } + exit; +} + +// Handle non-AJAX remove invoice action (hard delete + redirect) +if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['remove_invoice']) && isset($_POST['invoice_id'])) { + $remove_id = intval($_POST['invoice_id']); + if ($remove_id <= 0) { + $error_message = 'Invalid invoice id.'; + } else { + if (!$db) { + $error_message = 'Unable to remove item: database unavailable.'; + } else { + // Verify ownership and that invoice is still due + $check_q = "SELECT invoice_id FROM {$table_prefix}billing_invoices WHERE invoice_id = " . intval($remove_id) . " AND user_id = " . intval($user_id) . " AND status = 'due' LIMIT 1"; + $check_r = mysqli_query($db, $check_q); + if ($check_r && mysqli_num_rows($check_r) === 1) { + // Hard-delete to remove from cart + $del_q = "DELETE FROM {$table_prefix}billing_invoices WHERE invoice_id = " . intval($remove_id) . " AND user_id = " . intval($user_id) . " AND status = 'due' LIMIT 1"; + if (mysqli_query($db, $del_q)) { + // Reload to avoid form re-submission and refresh invoice list + header('Location: /cart.php'); + exit; + } else { + $error_message = 'Failed to remove item from cart.'; + } + } else { + $error_message = 'Invoice not found or cannot be removed.'; + } + } + } +} + // Calculate discount if ($applied_coupon && $coupon_discount_percent > 0) { $discount_amount = $total_amount * ($coupon_discount_percent / 100); @@ -468,6 +530,8 @@ $siteBase = $protocol . $host; margin-top: 30px; } + + @@ -506,6 +570,7 @@ $siteBase = $protocol . $host; Quantity Status Price + Action @@ -518,12 +583,17 @@ $siteBase = $protocol . $host;
- - x - - - $ - + + x + + + $ + + + + @@ -666,6 +736,49 @@ $siteBase = $protocol . $host; } }).render('#paypal-button-container'); + diff --git a/modules/billing/cart_old.php b/modules/billing/cart_old.php deleted file mode 100644 index 6492221c..00000000 --- a/modules/billing/cart_old.php +++ /dev/null @@ -1,564 +0,0 @@ -= 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.

- -
- - -
- Continue Shopping - My Account -
-
- - - -
- - \ No newline at end of file