0) { $_SESSION['website_user_id'] = $user_id; } } } // Always accept order_id from GET (link-based) or POST (form) $order_id = intval($_REQUEST['order_id'] ?? 0); // Allow optional duration override via ?duration=year $duration = (isset($_REQUEST['duration']) && $_REQUEST['duration'] === 'year') ? 'year' : 'month'; $redirect_to = 'cart.php'; if ($order_id <= 0 || $user_id <= 0) { header('Location: ' . $redirect_to); exit; } // Fetch order and verify ownership $stmt = $db->prepare('SELECT order_id, user_id, service_id, qty, invoice_duration, price, home_id FROM ogp_billing_orders WHERE order_id = ? LIMIT 1'); if (!$stmt) { header('Location: ' . $redirect_to); exit; } $stmt->bind_param('i', $order_id); $stmt->execute(); $res = $stmt->get_result(); if (!$res || $res->num_rows === 0) { $stmt->close(); header('Location: ' . $redirect_to); exit; } $order = $res->fetch_assoc(); $stmt->close(); if (intval($order['user_id']) !== intval($user_id)) { // Not the owner — silently redirect header('Location: ' . $redirect_to); exit; } // Determine price for selected duration by looking up service prices $service_id = intval($order['service_id'] ?? 0); $price_val = floatval($order['price'] ?? 0.0); if ($service_id > 0) { $sstmt = $db->prepare('SELECT price_monthly, price_year FROM ogp_billing_services WHERE service_id = ? LIMIT 1'); if ($sstmt) { $sstmt->bind_param('i', $service_id); $sstmt->execute(); $sres = $sstmt->get_result(); if ($sres && $sres->num_rows > 0) { $srow = $sres->fetch_assoc(); if ($duration === 'year' && !empty($srow['price_year']) && floatval($srow['price_year']) > 0) { $price_val = floatval($srow['price_year']); } else { $price_val = floatval($srow['price_monthly']); } } $sstmt->close(); } } // Update order: set status='renew', invoice_duration, qty, price $new_status = 'renew'; $qty = 1; $price_formatted = number_format($price_val, 2, '.', ''); $upd = $db->prepare('UPDATE ogp_billing_orders SET status = ?, invoice_duration = ?, qty = ?, price = ? WHERE order_id = ? AND user_id = ? LIMIT 1'); if ($upd) { // types: status (s), invoice_duration (s), qty (i), price (d), order_id (i), user_id (i) $upd->bind_param('ssiddi', $new_status, $duration, $qty, $price_formatted, $order_id, $user_id); $ok = $upd->execute(); $affected = $upd->affected_rows; $upd->close(); if ($ok && $affected > 0) { // Insert a row into ogp_logger if table exists (best-effort) $client_ip = $_SERVER['REMOTE_ADDR'] ?? ''; $msg = "USER-RENEW: User {$user_id} marked order {$order_id} as renew"; // Try insert into ogp_logger table $escaped_msg = mysqli_real_escape_string($db, $msg); $escaped_ip = mysqli_real_escape_string($db, $client_ip); // Determine logger table name (best-effort). Try standard name then any table that ends with 'logger'. $logger_table = null; $check = mysqli_query($db, "SHOW TABLES LIKE 'ogp_logger'"); if ($check && mysqli_num_rows($check) > 0) { $logger_table = 'ogp_logger'; } else { $reslt = mysqli_query($db, "SHOW TABLES LIKE '%logger'"); if ($reslt && mysqli_num_rows($reslt) > 0) { $row = mysqli_fetch_row($reslt); $logger_table = $row[0]; } } if ($logger_table) { $dt = date('Y-m-d H:i:s'); $ins = "INSERT INTO `" . $logger_table . "` (`date`, `user_id`, `ip`, `message`) VALUES ('{$dt}', " . intval($user_id) . ", '{$escaped_ip}', '{$escaped_msg}')"; @mysqli_query($db, $ins); } } } // Done — redirect back silently header('Location: ' . $redirect_to); exit; ?>