0) { $upd_coupon = "UPDATE `" . $TABLE_PREFIX . "billing_coupons` SET current_uses = current_uses + 1 WHERE coupon_id = ?"; if ($stmt = mysqli_prepare($db, $upd_coupon)) { mysqli_stmt_bind_param($stmt, 'i', $coupon_id); mysqli_stmt_execute($stmt); mysqli_stmt_close($stmt); } } // If this invoice already has an order -> treat as renewal if ($order_id > 0) { // compute months $months = (stripos($duration, 'year') !== false) ? ($qty * 12) : $qty; // fetch current end_date $get = "SELECT end_date FROM `" . $TABLE_PREFIX . "billing_orders` WHERE order_id = ? LIMIT 1"; if ($stmt = mysqli_prepare($db, $get)) { mysqli_stmt_bind_param($stmt, 'i', $order_id); mysqli_stmt_execute($stmt); $res = mysqli_stmt_get_result($stmt); if ($res && $row = mysqli_fetch_assoc($res)) { $current_end = $row['end_date'] ?? date('Y-m-d H:i:s'); $extend_from = (strtotime($current_end) > time()) ? $current_end : date('Y-m-d H:i:s'); $dt = new DateTime($extend_from); if ($months > 0) $dt->modify('+' . intval($months) . ' months'); $new_end = $dt->format('Y-m-d H:i:s'); $update = "UPDATE `" . $TABLE_PREFIX . "billing_orders` SET end_date = ?, status='Active', payment_txid = ?, paid_ts = ? WHERE order_id = ?"; if ($u = mysqli_prepare($db, $update)) { mysqli_stmt_bind_param($u, 'sssi', $new_end, $esc_txid, $now, $order_id); mysqli_stmt_execute($u); mysqli_stmt_close($u); $processed_count++; } } mysqli_stmt_close($stmt); } } else { // Create new order $months = (stripos($duration, 'year') !== false) ? ($qty * 12) : $qty; $dt = new DateTime('now'); if ($months > 0) $dt->modify('+' . intval($months) . ' months'); $end_date = $dt->format('Y-m-d H:i:s'); // Simpler insert using properly escaped values $esc_home = mysqli_real_escape_string($db, $home_name); $esc_rcon = mysqli_real_escape_string($db, $rcon_pw); $esc_ftp = mysqli_real_escape_string($db, $ftp_pw); $esc_duration = mysqli_real_escape_string($db, $duration); $price = number_format($invoice_amount, 2, '.', ''); $insert2 = sprintf( "INSERT INTO `%s` (user_id, service_id, home_name, ip, max_players, qty, invoice_duration, price, remote_control_password, ftp_password, status, order_date, end_date, payment_txid, paid_ts) VALUES (%d, %d, '%s', %d, %d, %d, '%s', %s, '%s', '%s', 'Active', '%s', '%s', '%s', '%s')", $TABLE_PREFIX . 'billing_orders', $user_id, $service_id, $esc_home, $ip, $max_players, $qty, $esc_duration, $price, $esc_rcon, $esc_ftp, $now, $end_date, $esc_txid, $now ); if (mysqli_query($db, $insert2)) { $new_order_id = mysqli_insert_id($db); $link = "UPDATE `" . $TABLE_PREFIX . "billing_invoices` SET order_id = ? WHERE invoice_id = ?"; if ($u = mysqli_prepare($db, $link)) { mysqli_stmt_bind_param($u, 'ii', $new_order_id, $invoice_id); mysqli_stmt_execute($u); mysqli_stmt_close($u); } $processed_count++; } else { error_log('[payment_processor] Failed to insert order: ' . mysqli_error($db)); } } } mysqli_close($db); if ($processed_count > 0) { error_log('[payment_processor] Processed ' . $processed_count . ' invoice(s)'); return true; } error_log('[payment_processor] No matching invoices processed for record: ' . json_encode($record)); return false; } ?>