diff --git a/app/modules_app/payments/upload_receipt.php b/app/modules_app/payments/upload_receipt.php index b94fd89..46ab2b5 100644 --- a/app/modules_app/payments/upload_receipt.php +++ b/app/modules_app/payments/upload_receipt.php @@ -96,10 +96,41 @@ try { // 3. AI Analysis of receipt image $aiResult = analyzeReceipt($filepath, $payment); - // 4. Calculate match score + // 4. Smart Match: Use AI-extracted reference to search bank_transactions + $aiExtractedRef = trim($aiResult['reference_number'] ?? ''); + if (!empty($aiExtractedRef) && $aiExtractedRef !== 'unknown') { + $stmt = $db->prepare("SELECT * FROM bank_transactions WHERE bank_reference = ? AND is_claimed = 0 LIMIT 1"); + $stmt->execute([$aiExtractedRef]); + $aiMatchTransaction = $stmt->fetch(); + + if ($aiMatchTransaction) { + $expectedAmount = (float)$payment['amount_jod']; + $actualAmount = (float)$aiMatchTransaction['amount']; + + if (abs($expectedAmount - $actualAmount) < 0.1) { + // AI FOUND THE MATCH! + activateSubscription($db, $payment, $decoded['user_id']); + + $stmt = $db->prepare("UPDATE payment_requests SET status = 'approved', bank_reference = ?, verified_at = NOW() WHERE id = ?"); + $stmt->execute([$aiExtractedRef, $paymentId]); + + $stmt = $db->prepare("UPDATE bank_transactions SET is_claimed = 1 WHERE id = ?"); + $stmt->execute([$aiMatchTransaction['id']]); + + json_success([ + 'status' => 'approved', + 'auto_verified' => true, + 'method' => 'ai_ref_matching', + 'message' => 'تم العثور على الحوالة بنجاح وتفعيل الاشتراك آلياً!' + ], 'تم تفعيل الاشتراك بنجاح'); + } + } + } + + // 5. Calculate match score (for legacy manual review fallback) $matchScore = calculateMatchScore($aiResult, $payment); - // 5. Update payment request + // 6. Update payment request with AI data $newStatus = $matchScore >= 85.0 ? 'verified' : 'uploaded'; $stmt = $db->prepare(" @@ -119,29 +150,24 @@ try { $paymentId ]); - // 6. If high confidence match, auto-activate subscription - if ($matchScore >= 85.0) { + // 7. Final attempt activation if high confidence match score + if ($matchScore >= 90.0) { activateSubscription($db, $payment, $decoded['user_id']); - $stmt = $db->prepare("UPDATE payment_requests SET status = 'approved', verified_at = NOW() WHERE id = ?"); $stmt->execute([$paymentId]); - + json_success([ - 'status' => 'approved', + 'status' => 'approved', 'match_score' => $matchScore, - 'message' => 'تم التحقق من الدفع وتفعيل الاشتراك تلقائياً!', - 'extracted' => $aiResult, - ], 'تم اعتماد الدفع وتفعيل الاشتراك'); + 'message' => 'تم التحقق من الوصل وتفعيل الاشتراك تلقائياً بنسبة مطابقة عالية.' + ], 'تم تفعيل الاشتراك'); } json_success([ - 'status' => $newStatus, + 'status' => $newStatus, 'match_score' => $matchScore, - 'message' => $matchScore >= 60 - ? 'تم رفع الوصل. جاري المراجعة من الإدارة.' - : 'لم نتمكن من التحقق التلقائي. تم إرسال الطلب للمراجعة اليدوية.', - 'extracted' => $aiResult, - ], 'تم رفع وصل الدفع'); + 'message' => $matchScore >= 70 ? 'تم استلام الوصل بنجاح، جاري المراجعة النهائية.' : 'تم استلام الوصل، بانتظار تأكيد الحوالة من البنك.' + ], 'تم رفع الوصل'); } catch (\Exception $e) { error_log("Payment Receipt Upload Error: " . $e->getMessage()); @@ -182,7 +208,7 @@ function analyzeReceipt(string $imagePath, array $payment): array الاسم المستعار CliQ: {$payment['cliq_alias']} PROMPT; - $model = env('GEMINI_MODEL', 'gemini-1.5-flash'); + $model = env('GEMINI_MODEL'); $url = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent?key={$apiKey}"; $payload = [ diff --git a/public/shell.php b/public/shell.php index 433b9ca..b35ebbf 100644 --- a/public/shell.php +++ b/public/shell.php @@ -2347,63 +2347,57 @@