<?php if (!defined('DASHBOARD_ROOT')) { http_response_code(403); exit; } function renderFraudScore(int $orderId): string { $order = Database::fetchOne("SELECT id, ip_address, user_agent, time_at_checkout, created_at FROM orders WHERE id=:oid", [":oid"=>$orderId]); if (!$order) return "<div class=\"fraud-score\"><span>No order data</span></div>"; $payLogs = Database::fetchAll("SELECT method, status, amount_cad, created_at FROM payment_logs WHERE order_id=:oid ORDER BY created_at DESC", [":oid"=>$orderId]); $failedAttempts = 0; foreach ($payLogs as $pl) { if ($pl["status"] !== 'completed' && $pl["status"] !== 'succeeded') $failedAttempts++; } $timeAtCheckout = (int)($order["time_at_checkout"] ?? 0); $riskScore = 0; if ($failedAttempts > 2) $riskScore += 30; if ($timeAtCheckout > 0 && $timeAtCheckout < 10) $riskScore += 25; $riskScore = min(100, $riskScore); $level = $riskScore > 70 ? "danger" : ($riskScore > 40 ? "warning" : "success"); $html = "<div class=\"fraud-score\"><h4>Risk Assessment</h4>" . UI::badge($riskScore . "/100", $level); $html .= "<p>Failed payments: {$failedAttempts} | Checkout time: {$timeAtCheckout}s</p>"; if (!empty($payLogs)) { $html .= "<h5>Payment Log</h5>"; foreach ($payLogs as $pl) { $html .= "<div class=\"event-row\"><span>" . UI::e($pl["method"]) . "</span><span>$" . number_format((float)$pl["amount_cad"], 2) . "</span><span>" . UI::e($pl["status"]) . "</span><span>" . UI::e($pl["created_at"]) . "</span></div>"; } } $html .= "</div>"; return $html; } 