<?php
$username = '8c4d89acc5393807';
$password = 'MzU4MmUyYjBmZDFmYTIxODc2ODBjYTAxMWRjOWRjOThiMjRmNjllYmMwMmQ4Yjc4YmM2MDAxZjg5YWY5MzdlYg==';

$Url = 'https://apisms.beem.africa/public/v1/vendors/balance';

$ch = curl_init($Url);
error_reporting(E_ALL);
ini_set('display_errors', 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt_array($ch, array(
    CURLOPT_HTTPGET => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_HTTPHEADER => array(
        'Authorization:Basic ' . base64_encode("$username:$password"),
        'Content-Type: application/json'
    ),
));

// Send the request
$response = curl_exec($ch);

if ($response === FALSE) {
    echo json_encode(['success' => false, 'message' => 'Failed to fetch balance']);
    die(curl_error($ch));
}

// Decode the JSON response
$responseData = json_decode($response, true);
if (isset($responseData['data']['credit_balance'])) {
    // Extract and send balance as JSON
    echo json_encode(['success' => true, 'balance' => $responseData['data']['credit_balance']]);
} else {
    // Handle case where balance is not found
    echo json_encode(['success' => false, 'message' => 'Balance not found in response']);
}

curl_close($ch);
?>
