$url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => [ "Accept: application/json", "X-Country: $country", "X-Currency: $currency", "Authorization: Bearer $token" ], ]); $response = curl_exec($curl); if (curl_errno($curl)) { $error = curl_error($curl); curl_close($curl); return [ "status" => "ERROR", "message" => "cURL Error: $error" ]; } curl_close($curl); // Decode response $result = json_decode($response, true); // Handle invalid JSON if (!$result) { return [ "status" => "ERROR", "message" => "Invalid JSON response from Airtel API" ]; } // Check for API structure if (!isset($result["status"])) { return [ "status" => "ERROR", "message" => "Unexpected API response format".print_r($result,true) ]; } $statusCode = $result["status"]["code"] ?? null; $message = $result["status"]["message"] ?? "Unknown error"; // ✅ SUCCESS case if ($statusCode == "200" && isset($result["data"])) { return [ "status" => "SUCCESS", "balance" => $result["data"]["balance"] ?? "0", "currency" => $result["data"]["currency"] ?? $currency, "account_status" => $result["data"]["account_status"] ?? "Unknown" ]; } // ❌ ERROR case (e.g. "User not found", "Invalid token", etc.) return [ "status" => "ERROR", "message" => $message, "code" => $statusCode, "responseCode" => $result["status"]["response_code"] ?? null, "resultCode" => $result["status"]["result_code"] ?? null ]; } function authenticate($baseURL, $wallet, $password) { // JSON payload $postData = json_encode([ 'client_id' => $wallet, 'client_secret' => $password, 'grant_type' => "client_credentials" ]); // Initialize cURL $ch = curl_init($baseURL); // Set cURL options curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return the response curl_setopt($ch, CURLOPT_POST, true); // Use POST method curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);// Set the request body curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($postData) ]); // Execute the request $response = curl_exec($ch); // Check for cURL errors if (curl_errno($ch)) { curl_close($ch); return [ 'success' => false, 'error' => 'Curl error: ' . curl_error($ch) ]; } // Get HTTP status code $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); // Decode JSON response $result = json_decode($response, true); // Check if token is present if ($httpCode === 200 && isset($result['access_token'])) { return [ 'success' => true, 'token' => $result['access_token'] ]; } else { return [ 'success' => false, 'error' => $result['error_description'] ?? 'Unknown error', 'details' => $result['error'] ?? [] ]; } } ?>