Skip to content

Commit

Permalink
Add new methods for payments, taxes history (#32)
Browse files Browse the repository at this point in the history
* add new methods
* update tests and codestyle
* codestyle
* add errorhadler
* update codestyle, fix psalm errors
* update readme
  • Loading branch information
shoman4eg authored Jul 23, 2024
1 parent 049c4c7 commit 7327a77
Show file tree
Hide file tree
Showing 22 changed files with 674 additions and 12 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,27 @@ $apiClient->authenticate($accessToken);
$userInfo = $apiClient->user()->get();
```

### Получить информацию о необходимых платежах
```php
$apiClient->authenticate($accessToken);

$userInfo = $apiClient->tax()->get();
```

### Получить информацию о платежах
```php
$apiClient->authenticate($accessToken);

$userInfo = $apiClient->tax()->payments();
```

### Получить информацию о прошлых платежах
```php
$apiClient->authenticate($accessToken);

$userInfo = $apiClient->tax()->history();
```

## Использованные ресурсы
Статья на Habr: [Автоматизация для самозанятых: как интегрировать налог с IT проектом](https://habr.com/ru/post/436656/)

Expand All @@ -265,4 +286,4 @@ $userInfo = $apiClient->user()->get();
[Сделать пожертвование автору](https://www.tinkoff.ru/cf/7rZnC7N4bOO)

## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.
The MIT License (MIT). Please see [License File](LICENSE) for more information.
59 changes: 59 additions & 0 deletions src/Api/Tax.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);

namespace Shoman4eg\Nalog\Api;

use Psr\Http\Client\ClientExceptionInterface;
use Shoman4eg\Nalog\ErrorHandler;
use Shoman4eg\Nalog\Exception\DomainException;
use Shoman4eg\Nalog\Model\Tax\HistoryRecords;
use Shoman4eg\Nalog\Model\Tax\PaymentRecords;
use Shoman4eg\Nalog\Model\Tax\Tax as TaxModel;

/**
* @author Artem Dubinin <artem@dubinin.me>
*/
final class Tax extends BaseHttpApi
{
/**
* @throws ClientExceptionInterface
*/
public function get(): TaxModel
{
$response = $this->httpGet('/taxes');

return $this->hydrator->hydrate($response, TaxModel::class);
}

/**
* @throws \JsonException
* @throws ClientExceptionInterface
*/
public function history(?string $oktmo = null): HistoryRecords
{
$response = $this->httpPost('/taxes/history', [
'oktmo' => $oktmo,
]);

return $this->hydrator->hydrate($response, HistoryRecords::class);
}

/**
* @throws \JsonException
* @throws ClientExceptionInterface
* @throws DomainException
*/
public function payments(?string $oktmo = null, bool $onlyPaid = false): PaymentRecords
{
$response = $this->httpPost('/taxes/payments', [
'oktmo' => $oktmo,
'onlyPaid' => $onlyPaid,
]);

if ($response->getStatusCode() >= 400) {
(new ErrorHandler())->handleResponse($response);
}

return $this->hydrator->hydrate($response, PaymentRecords::class);
}
}
7 changes: 7 additions & 0 deletions src/Api/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
namespace Shoman4eg\Nalog\Api;

use Psr\Http\Client\ClientExceptionInterface;
use Shoman4eg\Nalog\ErrorHandler;
use Shoman4eg\Nalog\Exception\DomainException;
use Shoman4eg\Nalog\Model\User\UserType;

/**
Expand All @@ -13,11 +15,16 @@ final class User extends BaseHttpApi
{
/**
* @throws ClientExceptionInterface
* @throws DomainException
*/
public function get(): UserType
{
$response = $this->httpGet('/user');

if ($response->getStatusCode() >= 400) {
(new ErrorHandler())->handleResponse($response);
}

return $this->hydrator->hydrate($response, UserType::class);
}
}
9 changes: 7 additions & 2 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* @author Artem Dubinin <artem@dubinin.me>
*/
class ApiClient
final class ApiClient
{
private RequestBuilder $requestBuilder;
private ClientConfigurator $clientConfigurator;
Expand Down Expand Up @@ -120,7 +120,7 @@ public function createNewAccessTokenByPhone(string $phone, string $challengeToke

/**
* Authenticate the client with an access token. This should be the full access token object with
* refresh token and expirery timestamps.
* refresh token and expire timestamps.
*
* ```php
* $accessToken = $client->createNewAccessToken('inn', 'password');
Expand Down Expand Up @@ -173,6 +173,11 @@ public function paymentType(): Api\PaymentType
return new Api\PaymentType($this->getHttpClient(), $this->requestBuilder);
}

public function tax(): Api\Tax
{
return new Api\Tax($this->getHttpClient(), $this->requestBuilder);
}

private function getHttpClient(): ClientInterface
{
return $this->clientConfigurator->createConfiguredClient();
Expand Down
3 changes: 2 additions & 1 deletion src/Exception/HydrationException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
declare(strict_types=1);

namespace Shoman4eg\Nalog\Exception;

Expand All @@ -7,4 +8,4 @@
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class HydrationException extends \RuntimeException implements Exception {}
final class HydrationException extends \RuntimeException implements Exception {}
2 changes: 1 addition & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class InvalidArgumentException extends \InvalidArgumentException implements Exception {}
final class InvalidArgumentException extends \InvalidArgumentException implements Exception {}
6 changes: 6 additions & 0 deletions src/Http/AuthenticationPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Http\Client\Common\Plugin;
use Http\Promise\Promise;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Shoman4eg\Nalog\Util\JSON;
Expand All @@ -28,6 +29,11 @@ public function __construct(Authenticator $authenticator, string $accessToken)
$this->accessToken = JSON::decode($accessToken);
}

/**
* @throws \Exception
* @throws \JsonException
* @throws ClientExceptionInterface
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise
{
if ($this->accessToken === [] || $request->hasHeader('Authorization')) {
Expand Down
1 change: 0 additions & 1 deletion src/Model/PaymentType/PaymentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ private function __construct() {}
public static function createFromArray(array $data): self
{
$model = new self();

$model->id = $data['id'];
$model->type = $data['type'];
$model->bankName = $data['bankName'];
Expand Down
4 changes: 3 additions & 1 deletion src/Model/PaymentType/PaymentTypeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*/
final class PaymentTypeCollection extends AbstractCollection implements CreatableFromArray
{
private function __construct() {}

public static function createFromArray(array $data): self
{
$items = array_map(fn (array $item) => PaymentType::createFromArray($item), $data['items']);
$items = array_map(static fn (array $item) => PaymentType::createFromArray($item), $data['items']);

$model = new self();
$model->setItems($items);
Expand Down
123 changes: 123 additions & 0 deletions src/Model/Tax/History.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
declare(strict_types=1);

namespace Shoman4eg\Nalog\Model\Tax;

use Shoman4eg\Nalog\Model\CreatableFromArray;

/**
* @author Artem Dubinin <artem@dubinin.me>
*/
final class History implements CreatableFromArray
{
private int $taxPeriodId;
private float $taxAmount;
private float $bonusAmount;
private float $paidAmount;
private ?float $taxBaseAmount;
private ?\DateTimeImmutable $chargeDate;
private ?\DateTimeImmutable $dueDate;
private string $oktmo;
private string $regionName;
private string $kbk;
private string $taxOrganCode;
private string $type;
private int $krsbTaxChargeId;
private int $receiptCount;

private function __construct() {}

/**
* @throws \Exception
*/
public static function createFromArray(array $data): self
{
$model = new self();
$model->taxPeriodId = $data['taxPeriodId'];
$model->taxAmount = $data['taxAmount'];
$model->bonusAmount = $data['bonusAmount'];
$model->paidAmount = $data['paidAmount'];
$model->taxBaseAmount = $data['taxBaseAmount'];
$model->chargeDate = $data['chargeDate'] ? new \DateTimeImmutable($data['chargeDate']) : null;
$model->dueDate = $data['dueDate'] ? new \DateTimeImmutable($data['dueDate']) : null;
$model->oktmo = $data['oktmo'];
$model->regionName = $data['regionName'];
$model->kbk = $data['kbk'];
$model->taxOrganCode = $data['taxOrganCode'];
$model->type = $data['type'];
$model->krsbTaxChargeId = $data['krsbTaxChargeId'];
$model->receiptCount = $data['receiptCount'];

return $model;
}

public function getPaidAmount(): float
{
return $this->paidAmount;
}

public function getBonusAmount(): float
{
return $this->bonusAmount;
}

public function getTaxAmount(): float
{
return $this->taxAmount;
}

public function getTaxPeriodId(): int
{
return $this->taxPeriodId;
}

public function getOktmo(): string
{
return $this->oktmo;
}

public function getRegionName(): string
{
return $this->regionName;
}

public function getKbk(): string
{
return $this->kbk;
}

public function getTaxOrganCode(): string
{
return $this->taxOrganCode;
}

public function getKrsbTaxChargeId(): int
{
return $this->krsbTaxChargeId;
}

public function getReceiptCount(): int
{
return $this->receiptCount;
}

public function getType(): string
{
return $this->type;
}

public function getChargeDate(): ?\DateTimeImmutable
{
return $this->chargeDate;
}

public function getDueDate(): ?\DateTimeImmutable
{
return $this->dueDate;
}

public function getTaxBaseAmount(): ?float
{
return $this->taxBaseAmount;
}
}
30 changes: 30 additions & 0 deletions src/Model/Tax/HistoryRecords.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Shoman4eg\Nalog\Model\Tax;

use Shoman4eg\Nalog\Model\AbstractCollection;
use Shoman4eg\Nalog\Model\CreatableFromArray;

/**
* @author Artem Dubinin <artem@dubinin.me>
*
* @extends AbstractCollection<History>
*/
final class HistoryRecords extends AbstractCollection implements CreatableFromArray
{
private function __construct() {}

/**
* @throws \Exception
*/
public static function createFromArray(array $data): self
{
$items = array_map(static fn (array $record) => History::createFromArray($record), $data['records']);

$model = new self();
$model->setItems($items);

return $model;
}
}
Loading

0 comments on commit 7327a77

Please sign in to comment.