refactoring, airtelmoney test
This commit is contained in:
170
app/Library/AirtelMoneyMw/check_balance.php
Normal file
170
app/Library/AirtelMoneyMw/check_balance.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
$clientID="94351d4d-4909-4056-ad9d-8052a332d6b9";
|
||||
$clientSecret="bf665590-2519-49af-8d1f-7cd0dce1dc7a";
|
||||
|
||||
//CONTINENTAL CAPITAL
|
||||
$clientID="9ff18a6d-331e-4ec5-9ecc-4e512e13747c";
|
||||
$clientSecret="40f44254-10e7-4eb8-b161-38125117f4ba";
|
||||
|
||||
$authURL="https://openapiuat.airtel.africa/auth/oauth2/token";
|
||||
$res=authenticate($authURL, $clientID, $clientSecret);
|
||||
|
||||
if($res['success']){
|
||||
$bearerToken=$res['token'];
|
||||
$country = "MW";
|
||||
$currency = "MWK";
|
||||
//enquire trans status
|
||||
$res=getAirtelBalance($country, $currency, $bearerToken);
|
||||
|
||||
|
||||
if ($res["status"] === "SUCCESS") {
|
||||
echo "Balance: {$res['balance']} {$res['currency']}\n";
|
||||
echo "Account Status: {$res['account_status']}";
|
||||
} else {
|
||||
echo "Error: " . $res["message"];
|
||||
}
|
||||
|
||||
|
||||
}else{
|
||||
echo(print_r($res,true));
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
|
||||
function getAirtelBalance($country, $currency, $token) {
|
||||
$url = "https://openapiuat.airtel.africa/standard/v1/users/balance";
|
||||
|
||||
// Initialize cURL
|
||||
$curl = curl_init();
|
||||
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => $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'] ?? []
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
Reference in New Issue
Block a user