Запрос данных чека в nalog налог
```php
<?php
class NalogQR {
public static $version = '2024-05-06';
private static $host = 'irkkt-mobile.nalog.ru:8888';
private static $deviceOS = 'iOS';
private static $os = 'Android';
private static $clientVersion = '2.9.0';
private static $deviceId = '7C82010F-16CC-446B-8F66-FC4080C66521';
private static $accept = '*/*';
private static $userAgent = 'billchecker/2.9.0 (iPhone; iOS 13.6; Scale/2.00)';
private static $acceptLanguage = 'ru-RU;q=1, en-US;q=0.9';
private static $clientSecret = 'IyvrAbKt9h/8p6a7QPh8gpkXYQ4=';
private static function request(string $url, array $payload = [], array $headers = []): string {
//print_r(['function request', $url, $payload, $headers]);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, (empty($payload) ? false : true));
// SSL
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($payload)) {
// curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($payload));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
$headers['Content-Type'] = 'application/json';
}
if (!empty($headers)) {
$headersFinal = [];
foreach ($headers as $name => $value)
$headersFinal[] = "$name: $value";
curl_setopt($curl, CURLOPT_HTTPHEADER, $headersFinal);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Только возврат, а не echo
$result = curl_exec($curl);
$error = curl_error($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if (empty($result)) $result = '';
if ($error) $result .= 'err: '.$error;
if ($http_code >= 400) $result .= 'err_http_code: '.$http_code;
return $result;
}
private static function getHeaders(): array {
return [
'Host' => self::$host,
'Accept' => self::$accept,
'Device-OS' => self::$deviceOS,
'Device-Id' => self::$deviceId,
'clientVersion' => self::$clientVersion,
'Accept-Language' => self::$acceptLanguage,
'User-Agent' => self::$userAgent,
];
}
private static function responseToArray(string $json): array {
$result = json_decode($json, true);
return empty($result) ? [
'err' => 'impossible decode json',
'jsonText' => $json
] : $result;
}
public static function getCodeInSms(string $phone):array {
$headers = self::getHeaders();
$response = self::request(
'https://'.self::$host.'/v2/auth/phone/request',
[
'phone' => $phone,
'client_secret' => self::$clientSecret,
'os' => self::$os
],
$headers
);
return (empty($response)) ?
['ok' => 1] :
['err' => $response];
}
public static function getSessionId(string $phone, string $code): array {
$headers = self::getHeaders();
$response = self::request(
'https://'.self::$host.'/v2/auth/phone/verify',
[
'phone' => $phone,
'client_secret' => self::$clientSecret,
'code' => $code,
'os' => self::$os
],
$headers
);
return self::responseToArray($response);
/*[sessionId]
[refresh_token]*/
}
public static function refreshSessionId(string $refreshToken): array {
$headers = self::getHeaders();
$response = self::request(
'https://'.self::$host.'/v2/mobile/users/refresh',
[
'refresh_token' => $refreshToken,
'client_secret' => self::$clientSecret,
],
$headers
);
return self::responseToArray($response);
/*[sessionId]
[refresh_token]*/
}
public static function getTicketId(string $qr, string $sessionId): array {
$headers = self::getHeaders();
$headers['sessionId'] = $sessionId;
$response = self::request(
'https://'.self::$host.'/v2/ticket',
['qr' => $qr],
$headers
);
return self::responseToArray($response);
/*[kind] => kkt
[id] => 6628000000000
[status] => 2
[statusReal] => 2*/
}
public static function getTicket(string $id, string $sessionId) {
$headers = self::getHeaders();
$headers['Content-Type'] = 'application/json';
$headers['sessionId'] = $sessionId;
$response = self::request(
'https://'.self::$host.'/v2/tickets/'.$id,
[],
$headers
);
return self::responseToArray($response);
}
}
// Tests
//echo '<pre>';
// print_r(NalogQR::getCodeInSms('+7000000'));
// print_r(NalogQR::getSessionId('+70000000', '0000'));
// print_r(NalogQR::refreshSessionId('df60000170c'));
// print_r(NalogQR::getTicketId('t=20240000641304194&n=1', '65a110000da6b'));
// print_r(NalogQR::getTicket('6620000fb2c2', '65a10000317'));
```