"id parameter missing"]); exit; } $receiptNo = $_GET['id']; $url = "https://transactioninfo.ethiotelecom.et/receipt/" . $receiptNo; // Load Telebirr HTML ----------------------------------------- $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $html = curl_exec($ch); curl_close($ch); if (!$html) { echo json_encode(["error" => "Unable to load Telebirr page"]); exit; } // Parse HTML --------------------------------------------------- libxml_use_internal_errors(true); $doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); // Telebirr table cells $tds = $xpath->query("//td"); // Field map (Amharic + English) $map = [ "የክፍያው ሁኔታ" => "transaction_status", "transaction status" => "transaction_status", "የከፋይ ስም" => "payer_name", "payer name" => "payer_name", "የከፋይ ቴሌብር ቁ" => "payer_phone", "payer telebirr no" => "payer_phone", "የከፋይ አካውንት አይነት" => "payer_account_type", "payer account type" => "payer_account_type", "የገንዘብ ተቀባይ ስም" => "credited_party_name", "credited party name" => "credited_party_name", "የገንዘብ ተቀባይ ቴሌብር ቁ" => "credited_party_account", "credited party account no" => "credited_party_account", "የተከፈለው መጠን" => "settled_amount", "settled amount" => "settled_amount", "ጠቅላላ የተከፈለ" => "total_amount", "total amount paid" => "total_amount", "15% ቫት" => "vat_amount", "vat" => "vat_amount", "ቅናሽ" => "discount_amount", "discount amount" => "discount_amount", "የገንዘቡ በፊደል" => "amount_in_word", "total amount in word" => "amount_in_word", "የክፍያ መንገድ" => "payment_channel", "payment channel" => "payment_channel", "የክፍያ ዘዴ" => "payment_mode", "payment mode" => "payment_mode", "የክፍያ ምክንያት" => "payment_reason", "payment reason" => "payment_reason", "የክፍያ ቀን" => "payment_date", "payment date" => "payment_date" ]; // Date detection regex $dateRegex = '/(\d{4}[-\/]\d{2}[-\/]\d{2}|\d{2}\/\d{2}\/\d{4}|[A-Za-z]+\s+\d{1,2},\s*\d{4})/'; $response = []; // Extract fields from Telebirr table -------------------------- for ($i = 0; $i < $tds->length; $i++) { $key = strtolower(trim(preg_replace('/\s+/', ' ', $tds->item($i)->nodeValue))); $value = trim($tds->item($i + 1)->nodeValue); foreach ($map as $search => $field) { if (strpos($key, strtolower($search)) !== false) { // Special case: extract payment date only if real date exists if ($field === "payment_date") { if (preg_match($dateRegex, $value, $matches)) { $response["payment_date"] = $matches[0]; } continue; } // Clean numeric values if (in_array($field, ["settled_amount", "total_amount", "vat_amount", "discount_amount"])) { $value = floatval(str_replace(['Birr', ',', ' '], '', $value)); } $response[$field] = $value; } } } // Extract payment date from page header (fallback) ------------- if (!isset($response["payment_date"])) { $headers = $xpath->query("//h1|//h2|//h3|//h4|//h5|//p"); foreach ($headers as $node) { $text = trim($node->nodeValue); if (preg_match($dateRegex, $text, $m)) { $response["payment_date"] = $m[0]; break; } } } // Add receipt number always $response["receipt_no"] = $receiptNo; // Validate & output ------------------------------------------------ if (count($response) <= 1) { echo json_encode(["error" => "Receipt not found or invalid"]); exit; } echo json_encode($response, JSON_PRETTY_PRINT); ?>