<?php
function getBalance($operatorCode, $providerCode, $username, $password, $secretKey) {
    $signature = strtoupper(md5($operatorCode . $password . $providerCode . $username . $secretKey));
    $apiUrl = "https://gsmd.336699bet.com/getBalance.aspx?operatorcode=$operatorCode&providercode=$providerCode&username=$username&password=$password&signature=$signature";
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $response = curl_exec($ch);
    curl_close($ch);
    
    return json_decode($response, true);
}

function getAllProviderBalances($operatorCode, $providerCode, $username, $password, $secretKey) {
    $balances = [];
    
    $query = "SELECT provider_code, provider_name FROM tb_provider";
    $result = $call->query($query);
    
    if ($result) {
        while ($provider = $result->fetch_assoc()) {
            $response = getBalance($operatorCode, $provider['provider_code'], $username, $password, $secretKey);
            
            if (isset($response['balance']) && $response['balance'] > 0) {
                $balances[] = [
                    'provider' => $provider['provider_name'],
                    'balance' => $response['balance']
                ];
            }
        }
    }
    
    return $balances;
}

// Contoh penggunaan
$operatorCode = "xf4i";
$username = strtolower("agenplayayidr");
$password = "xf4ixf4i!";
$secretKey = "9d8d2f40de61881b6a02d518cfb1e5fb";
include 'koneksi.php';

$balances = getAllProviderBalances($operatorCode, $providerCode, $username, $password, $secretKey);
print_r($balances);

?>
