Files
Siro/walletintaleq.intaleq.xyz/v2/main/sms_webhook/webhook.php
2026-06-16 22:44:11 +03:00

85 lines
3.3 KiB
PHP
Executable File

<?php
//webhook.php
// تضمين ملف الاتصال الذي يتحقق أيضاً من توكن JWT
include "../jwtconnect.php";
// --- 1. قراءة البيانات المرسلة ---
$expectedToken = trim(file_get_contents('/home/intaleq-wallet/.webhookKey') ?: 'secret'); // Replace with actual key
$receivedToken = $_SERVER['HTTP_X_AUTH_TOKEN'] ?? '';
if (!hash_equals($expectedToken, $receivedToken)) {
http_response_code(401);
echo json_encode(["status" => "error", "message" => "Authentication failed."]);
exit;
}
$json_data = file_get_contents('php://input');
$data = json_decode($json_data, true);
if ($data === null || !isset($data['sender']) || !isset($data['message'])) {
http_response_code(400); // Bad Request
echo json_encode(['status' => 'error', 'message' => 'Invalid JSON data received']);
exit();
}
// --- 2. استخراج البيانات ---
$sender = $data['sender'];
$message_body = $data['message'];
$received_at = date('Y-m-d H:i:s');
$log_entry = "[$received_at] From: $sender | Message: $message_body";
// --- 3. تحليل الرسالة (يركز على Orange Money حالياً) ---
$pattern_orangemoney_jo = '/تم استقبال حوالة مالية من (\d+)\s+من مزود الخدمة:\s+Orange Money إلى محفظتك بمبلغ ([\d,.]+)\s+دينار/';
if (preg_match($pattern_orangemoney_jo, $message_body, $matches)) {
$payer_phone_raw = $matches[1];
$amount_str = $matches[2];
$amount = (float) str_replace(',', '', $amount_str);
// توحيد صيغة رقم الهاتف (إزالة 0096 إذا وجدت وإضافة 0)
$payer_phone = $payer_phone_raw;
if (substr($payer_phone_raw, 0, 4) === '0096') {
$payer_phone = '0' . substr($payer_phone_raw, 4);
}
$log_entry .= " | MATCH: Orange Money | SUCCESS: Parsed Amount = $amount, Payer Phone = $payer_phone";
// --- 4. منطق تحديث الفاتورة ---
try {
// البحث عن أحدث فاتورة مطابقة (نفس الرقم والمبلغ) بحالة انتظار وتحديثها
$sql = "UPDATE invoices_sms SET status = 'completed'
WHERE user_phone = :phone
AND amount = :amount
AND status = 'pending'
ORDER BY created_at DESC
LIMIT 1";
$stmt = $con->prepare($sql);
$stmt->execute([
':phone' => $payer_phone,
':amount' => $amount
]);
if ($stmt->rowCount() > 0) {
$log_entry .= " | DB: SUCCESS - Invoice found and updated." . PHP_EOL;
// يمكنك هنا إضافة كود لإرسال إشعار للمستخدم
} else {
$log_entry .= " | DB: WARNING - No pending invoice found for this transaction." . PHP_EOL;
}
} catch (PDOException $e) {
$log_entry .= " | DB: ERROR - " . $e->getMessage() . PHP_EOL;
}
} else {
$log_entry .= " | INFO: Message did not match any known payment pattern. Ignored." . PHP_EOL;
}
// كتابة السجل (مهم لتصحيح الأخطاء)
file_put_contents('sms_log.txt', $log_entry, FILE_APPEND);
// إرسال رد إلى تطبيق الأندرويد
http_response_code(200);
echo json_encode(['status' => 'success', 'message' => 'Webhook processed.']);
?>