Skip to content

Latest commit

 

History

History
187 lines (148 loc) · 6.4 KB

README.en.md

File metadata and controls

187 lines (148 loc) · 6.4 KB

Unofficial MoyNalog API client

Php version Latest Version Total Downloads Scrutinizer code quality Packagist License Donate

An unofficial wrapper client for lknpd.nalog.ru API

Install

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

Usage

Settings

// 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'))

Authorization by INN & password

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

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.

2. Exchange your phone number, challenge token and code from SMS for the access token:

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);

Create income with default client

$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);

Create income with multiple items

$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);

Create income with custom client

$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);

Cancel income

$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);

Create Invoice

// todo

Cancel Invoice

// todo

Change payment type in Invoice

// todo

Get user info

$userInfo = $apiClient->user()->get();

Get receipt info

// $receiptUuid = $createdincome->getApprovedReceiptUuid();

// Get print url
$receipt = $apiClient->receipt()->printUrl($receiptUuid);

// Json data
$receipt = $apiClient->receipt()->json($receiptUuid);

References

Автоматизация для самозанятых: как интегрировать налог с IT проектом

JS lib alexstep/moy-nalog

Changelog

Changelog: A complete changelog

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Link to donate

License

The MIT License (MIT). Please see License File for more information.