An unofficial wrapper client for lknpd.nalog.ru API
Via Composer
$ composer require shoman4eg/moy-nalog
Also you need one of packages suggests psr/http-client-implementation
Recommends symfony/http-client
or guzzlehttp/guzzle
// If need set timezone use this
date_default_timezone_set('Europe/Kaliningrad');
// or set timezone through new DateTimeZone
$operationTime = new \DateTimeImmutable('now', new \DateTimeZone('Europe/Kaliningrad'))
use Shoman4eg\Nalog\ApiClient;
$apiClient = ApiClient::create();
// If known accessToken skip this step
try {
$accessToken = $apiClient->createNewAccessToken($username, $password);
} catch (\Shoman4eg\Nalog\Exception\Domain\UnauthorizedException $e) {
var_dump($e->getMessage());
}
$apiClient->authenticate($accessToken);
Authorization by phone number takes place in two steps. You need to request authorization by phone, temporarily save the returned challenge token, receive an SMS with a confirmation code, and then pass the phone, the challenge token and the confirmation code from the SMS by a second request.
Please note: there is a limit for sending SMS with confirmation code (one SMS every 1-2 minutes).
1. Send an SMS with a confirmation code to your phone number and temporarily save the challenge token:
use Shoman4eg\Nalog\ApiClient;
try {
$phoneChallengeResponse = ApiClient::createPhoneChallenge('79999999999');
/**
* $phoneChallengeResponse = [
* 'challengeToken' => '00000000-0000-0000-0000-000000000000',
* 'expireDate' => 2022-11-24T00:20:19.135436Z,
* 'expireIn' => 120,
* ];
*/
} catch (\Shoman4eg\Nalog\Exception\Domain\UnauthorizedException $e) {
var_dump($e->getMessage());
}
//Save $response['challengeToken'] until you get the confirmation code from the SMS. You need it for the second step.
use Shoman4eg\Nalog\ApiClient;
$apiClient = ApiClient::create();
try {
$accessToken = $apiClient->createNewAccessTokenByPhone(
'79999999999',
'00000000-0000-0000-0000-000000000000',
'111111'
);
} catch (\Shoman4eg\Nalog\Exception\Domain\UnauthorizedException $e) {
var_dump($e->getMessage());
}
$apiClient->authenticate($accessToken);
$name = 'Предоставление информационных услуг #970/2495';
$amount = 1800.30;
$quantity = 1;
$operationTime = new DateTimeImmutable('2020-12-31 12:12:00');
$createdIncome = $apiClient->income()->create($name, $amount, $quantity, $operationTime);
$name = 'Предоставление информационных услуг #970/2495';
$items = [
new \Shoman4eg\Nalog\DTO\IncomeServiceItem($name, $amount = 1800.30, $quantity = 1),
new \Shoman4eg\Nalog\DTO\IncomeServiceItem($name, $amount = 900, $quantity = 2),
new \Shoman4eg\Nalog\DTO\IncomeServiceItem($name, $amount = '1399.99', $quantity = 3),
];
$operationTime = new DateTimeImmutable('2020-12-31 12:12:00');
$createdIncome = $apiClient->income()->createMultipleItems($items, $operationTime);
$name = 'Предоставление информационных услуг #970/2495';
$amount = 1800.30;
$quantity = 1;
$operationTime = new \DateTimeImmutable('2020-12-31 12:12:00');
$client = new \Shoman4eg\Nalog\DTO\IncomeClient(); // Default. All fields are empty IncomeType is FROM_INDIVIDUAL
// or
$client = new \Shoman4eg\Nalog\DTO\IncomeClient('+79009000000', 'Вася Пупкин', \Shoman4eg\Nalog\Enum\IncomeType::INDIVIDUAL, '390000000000');
// or
$client = new \Shoman4eg\Nalog\DTO\IncomeClient(null, 'Facebook Inc.', \Shoman4eg\Nalog\Enum\IncomeType::FOREIGN_AGENCY, '390000000000');
// or
$client = new \Shoman4eg\Nalog\DTO\IncomeClient(null, 'ИП Вася Пупкин Валерьевич', \Shoman4eg\Nalog\Enum\IncomeType::LEGAL_ENTITY, '7700000000');
$createdIncome = $apiClient->income()->create($name, $amount, $quantity, $operationTime, $client);
$receiptUuid = "20hykdxbp8"
$comment = \Shoman4eg\Nalog\Enum\CancelCommentType::CANCEL;
$partnerCode = null; // Default null
$operationTime = new \DateTimeImmutable('now'); //Default 'now'
$requestTime = new \DateTimeImmutable('now'); //Default 'now'
$incomeInfo = $apiClient->income()->cancel($receiptUuid, $comment, $operationTime, $requestTime, $partnerCode);
// todo
// todo
// todo
$userInfo = $apiClient->user()->get();
// $receiptUuid = $createdincome->getApprovedReceiptUuid();
// Get print url
$receipt = $apiClient->receipt()->printUrl($receiptUuid);
// Json data
$receipt = $apiClient->receipt()->json($receiptUuid);
Автоматизация для самозанятых: как интегрировать налог с IT проектом
JS lib alexstep/moy-nalog
Changelog: A complete changelog
If this project help you reduce time to develop, you can give me a cup of coffee :)
The MIT License (MIT). Please see License File for more information.