Created
May 1, 2025 19:45
-
-
Save azihassan/6f0e1ad62a84a962386361609bc5cca4 to your computer and use it in GitHub Desktop.
PHP win by inwi balance verification script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$subscriptionId = 'XXXXXXXXXXXXXXXXXX'; | |
$login = in_array('--login', $argv); | |
if($login) | |
{ | |
if(!in_array('--username', $argv) || !in_array('--password', $argv)) | |
{ | |
echo "Usage: php {$argv[0]} --login --username <username> --password <password>\n"; | |
exit(1); | |
} | |
$username = $argv[array_search('--username', $argv) + 1]; | |
$password = $argv[array_search('--password', $argv) + 1]; | |
login($username, $password); | |
} | |
else | |
{ | |
getBalance($subscriptionId); | |
} | |
function login(string $username, string $password) | |
{ | |
$ch = curl_init('https://api.win.ma/api/v1/auth/login'); | |
curl_setopt_array($ch, [ | |
CURLOPT_HTTPHEADER => ['Content-Type: application/json'], | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0', | |
CURLOPT_REFERER => 'https://win.ma/', | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => json_encode(['email' => $username, 'password' => $password]) | |
]); | |
$response = curl_exec($ch); | |
if($response === false) | |
{ | |
throw new Exception("Failed to retrieve offer details:\n" . curl_error($ch)); | |
} | |
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if($status >= 400) | |
{ | |
throw new Exception("Failed with status $status\n" . print_r(curl_getinfo($ch), true)); | |
} | |
$response = json_decode($response); | |
echo "Logged in as {$response->account->firstName} {$response->account->lastName}\n"; | |
file_put_contents(__DIR__ . '/win-forfait.json', json_encode($response->tokens)); | |
echo "Tokens stored successfully\n"; | |
} | |
function getBalance(string $subscriptionId) | |
{ | |
['accessToken' => $accessToken, 'refreshToken' => $refreshToken] = getTokens(); | |
$url = "https://api.win.ma/api/v1/subscriptions/{$subscriptionId}/offer-detail"; | |
$ch = curl_init($url); | |
$headers = []; | |
curl_setopt_array($ch, [ | |
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $accessToken], | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_USERAGENT => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0', | |
CURLOPT_REFERER => 'https://win.ma/', | |
]); | |
$response = curl_exec($ch); | |
if($response === false) | |
{ | |
echo "Failed to retrieve offer details:\n"; | |
echo curl_error($ch); | |
exit(1); | |
} | |
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
if($status >= 400) | |
{ | |
echo "Failed with status $status\n"; | |
print_r(curl_getinfo($ch)); | |
exit(1); | |
} | |
//$response = file_get_contents('win.json'); | |
$offerDetails = json_decode($response); | |
file_put_contents('win-forfait.cache', $response); | |
$balance = array_values(array_filter($offerDetails->assets, fn($asset) => $asset->name === 'Internet 4G'))[0]->balance; | |
printf("Until %s\n", $balance->expireDate); | |
printf("%.2f MB / %.2f MB (%.2f %%)\n", $balance->restValue / 1000 / 1000, $balance->defaultValue / 1000 / 1000, $balance->restValue / $balance->defaultValue * 100.0); | |
} | |
function getTokens(): array | |
{ | |
$tokens = json_decode(file_get_contents(__DIR__ . '/win-forfait.json'), true); | |
$accessToken = parseToken($tokens['accessToken']); | |
if($accessToken['exp'] > time()) | |
{ | |
echo "Tokens valid, continuing\n"; | |
return $tokens; | |
} | |
refreshTokens(); | |
return json_decode(file_get_contents(__DIR__ . '/win-forfait.json'), true); | |
} | |
function refreshTokens() | |
{ | |
$tokens = json_decode(file_get_contents(__DIR__ . '/win-forfait.json')); | |
$ch = curl_init('https://api.win.ma/api/v1/auth/refresh-token?refreshToken=' . $tokens->refreshToken); | |
curl_setopt_array($ch, [ | |
CURLOPT_POST => true, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_RETURNTRANSFER => true, | |
]); | |
$response = curl_exec($ch); | |
if($response === false) | |
{ | |
echo "Failed to refresh tokens:\n"; | |
echo curl_error($ch), "\n"; | |
echo "Try logging in with php {$argv[0]} --login\n"; | |
exit(1); | |
} | |
file_put_contents(__DIR__ . '/win-forfait.json', $response); | |
echo "Tokens refreshed\n"; | |
} | |
function parseToken(string $token) | |
{ | |
return json_decode(base64_decode(explode('.', $token)[1]), true); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment