self::COUNTRY_SYRIA, '00963' => self::COUNTRY_SYRIA, '962' => self::COUNTRY_JORDAN, '00962' => self::COUNTRY_JORDAN, '20' => self::COUNTRY_EGYPT, '0020' => self::COUNTRY_EGYPT, ]; private static array $countryPhonePrefixes = [ self::COUNTRY_SYRIA => '963', self::COUNTRY_JORDAN => '962', self::COUNTRY_EGYPT => '20', ]; private static array $countryEnvKeys = [ self::COUNTRY_SYRIA => 'SIRO_API_URL_SYRIA', self::COUNTRY_JORDAN => 'SIRO_API_URL_JORDAN', self::COUNTRY_EGYPT => 'SIRO_API_URL_EGYPT', ]; /** * Detect country from phone number */ public static function detectCountry(string $phone): string { $cleaned = preg_replace('/[^0-9]/', '', $phone); foreach (self::$countryPrefixes as $prefix => $country) { if (strpos($cleaned, $prefix) === 0) { return $country; } } return self::COUNTRY_SYRIA; } /** * Get country-specific API base URL */ public static function getApiUrl(string $country): string { $envKey = self::$countryEnvKeys[$country] ?? self::$countryEnvKeys[self::COUNTRY_SYRIA]; return rtrim(getenv($envKey) ?: 'https://api-syria.siromove.com/siro', '/'); } /** * Format phone number to international format for the given country */ public static function formatPhone(string $phone, string $country): string { $cleaned = preg_replace('/[^0-9]/', '', $phone); $prefix = self::$countryPhonePrefixes[$country] ?? '963'; if (strpos($cleaned, $prefix) === 0) { return $cleaned; } if (strpos($cleaned, '00' . $prefix) === 0) { return $prefix . substr($cleaned, strlen($prefix) + 2); } if (strpos($cleaned, '0') === 0) { return $prefix . substr($cleaned, 1); } return $prefix . $cleaned; } /** * Upload a document image to Siro and return the signed URL */ public static function uploadDocument( string $country, string $driverId, string $docType, string $imagePath, string $imageMimeType ): ?string { $apiUrl = self::getApiUrl($country); $apiKey = getenv('NABEH_API_KEY') ?: ''; $uploadUrl = rtrim($apiUrl, '/') . '/nabeh/upload_document.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $uploadUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); $headers = [ 'X-API-Key: ' . $apiKey, ]; $body = [ 'driver_id' => $driverId, 'doc_type' => $docType, ]; if (file_exists($imagePath)) { $body['file'] = new \CURLFile($imagePath, $imageMimeType); } else { error_log("[SiroService] Image not found: {$imagePath}"); return null; } curl_setopt($ch, CURLOPT_POSTFIELDS, $body); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200) { error_log("[SiroService] Upload failed for {$docType}: HTTP {$httpCode} - {$res}"); return null; } $data = json_decode($res, true); $fileUrl = $data['message']['file_url'] ?? null; if (!$fileUrl) { error_log("[SiroService] Upload response missing file_url: {$res}"); } return $fileUrl; } /** * Register a driver in Siro via the simplified Nabeh integration endpoint */ public static function registerDriver(array $driverData, array $carData, array $documentUrls, string $country): ?array { $apiUrl = self::getApiUrl($country); $apiKey = getenv('NABEH_API_KEY') ?: ''; $registerUrl = $apiUrl . '/nabeh/register.php'; $payload = array_merge($driverData, $carData, [ 'id_front_url' => $documentUrls['id_front'] ?? '', 'id_back_url' => $documentUrls['id_back'] ?? '', 'driver_license_front_url' => $documentUrls['driving_license_front'] ?? '', 'driver_license_back_url' => $documentUrls['driving_license_back'] ?? '', 'vehicle_license_front_url' => $documentUrls['vehicle_license_front'] ?? '', 'vehicle_license_back_url' => $documentUrls['vehicle_license_back'] ?? '', 'criminal_record_url' => $documentUrls['criminal_record'] ?? '', 'profile_picture' => $documentUrls['profile_picture'] ?? '', ]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $registerUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'X-API-Key: ' . $apiKey, 'Content-Type: application/json', ]); curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200) { error_log("[SiroService] Registration failed: HTTP {$httpCode} - {$res}"); return null; } return json_decode($res, true); } /** * Check driver registration status in Siro */ public static function checkDriverStatus(string $phone, string $country): ?array { $apiUrl = self::getApiUrl($country); $apiKey = getenv('NABEH_API_KEY') ?: ''; $statusUrl = $apiUrl . '/nabeh/driver_status.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $statusUrl . '?phone=' . urlencode($phone)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'X-API-Key: ' . $apiKey, ]); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200) { return null; } return json_decode($res, true); } /** * Query Siro platform for driver info, trips, stats, or trip details */ public static function querySiro(string $country, array $params): ?array { $apiUrl = self::getApiUrl($country); $apiKey = getenv('NABEH_API_KEY') ?: ''; $queryUrl = $apiUrl . '/nabeh/query.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $queryUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params)); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'X-API-Key: ' . $apiKey, 'Content-Type: application/json', ]); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200) { error_log("[SiroService] Query failed: HTTP {$httpCode} - {$res}"); return null; } return json_decode($res, true); } /** * Get OCR prompts for a specific country */ public static function getDocumentPrompts(string $country): array { $syriaPrompts = [ "id_front" => << << << << << << << << << << << << << << << << << << $jordanPrompts, self::COUNTRY_EGYPT => $egyptPrompts, default => $syriaPrompts, }; } /** * Fetch recent rides for a user from Siro */ public static function getUserRides(string $country, string $phone, int $limit = 5): ?array { $apiUrl = self::getApiUrl($country); $apiKey = getenv('NABEH_API_KEY') ?: ''; $ridesUrl = $apiUrl . '/nabeh/get_user_rides.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ridesUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ 'phone' => $phone, 'limit' => $limit, ])); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'X-API-Key: ' . $apiKey, 'Content-Type: application/json', ]); curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200) { error_log("[SiroService] getUserRides failed: HTTP {$httpCode} - {$res}"); return null; } $data = json_decode($res, true); return $data['rides'] ?? $data['data'] ?? null; } /** * Submit a complaint with AI analysis via Siro */ public static function submitComplaint(string $country, string $phone, string $rideId, string $complaintText): ?array { $apiUrl = self::getApiUrl($country); $apiKey = getenv('NABEH_API_KEY') ?: ''; $complaintUrl = $apiUrl . '/nabeh/submit_complaint.php'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $complaintUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ 'phone' => $phone, 'ride_id' => $rideId, 'complaint_text' => $complaintText, ])); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'X-API-Key: ' . $apiKey, 'Content-Type: application/json', ]); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200) { error_log("[SiroService] submitComplaint failed: HTTP {$httpCode} - {$res}"); return null; } return json_decode($res, true); } }