Skip to content

Commit

Permalink
Sharing part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
matasarei committed Mar 25, 2024
1 parent f4db30f commit d99f40f
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 17 deletions.
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM php:7.4-cli
RUN apt-get update && apt-get install unzip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Diia (Дія) API Client for PHP

## Tests and development
1. Prepare image
```bash
docker build -t diia-php .
```
2. Install vendors
```bash
docker run --rm -v $(pwd):/app -w /app diia-php composer install
```
3. Run tests
```bash
docker run --rm -v $(pwd):/app -w /app diia-php vendor/bin/phpunit
```
13 changes: 13 additions & 0 deletions src/Client/AcquirersClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GrinchenkoUniversity\Diia\Dto\Acquirers\Branch;
use GrinchenkoUniversity\Diia\Dto\Acquirers\Offer;
use GrinchenkoUniversity\Diia\Dto\ApiResource;
use GrinchenkoUniversity\Diia\Dto\Request\DocumentRequest;
use GrinchenkoUniversity\Diia\Dto\Request\ItemsListRequest;
use GrinchenkoUniversity\Diia\Dto\Request\OfferRequest;
use GrinchenkoUniversity\Diia\Dto\Response\CreateResourceResponse;
Expand Down Expand Up @@ -172,4 +173,16 @@ public function makeOfferRequest(string $branchId, OfferRequest $offerRequest):

return $this->offerResponseMapper->mapFromResponse($response->getBody()->getContents());
}

public function documentRequest(DocumentRequest $documentRequest): void
{
$this->httpClient->request(
'POST',
'/api/v1/acquirers/document-request',
[
'headers' => $this->httpHeadersProvider->getDefaultHeaders(),
'body' => $this->requestJsonMapper->mapToJson($documentRequest),
]
);
}
}
2 changes: 2 additions & 0 deletions src/Client/AuthClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function __construct(ClientInterface $httpClient)
}

/**
* Returns a session token.
*
* @param Credentials $credentials
*
* @return string
Expand Down
45 changes: 45 additions & 0 deletions src/Dto/Request/DocumentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace GrinchenkoUniversity\Diia\Dto\Request;

class DocumentRequest
{
private string $branchId;
private string $barcode;
private string $requestId;
private bool $useDiiaId;

public function __construct(
string $branchId,
string $barcode,
string $requestId,
bool $useDiiaId = true
) {
$this->branchId = $branchId;
$this->barcode = $barcode;
$this->requestId = $requestId;
$this->useDiiaId = $useDiiaId;
}

public function getBranchId(): string
{
return $this->branchId;
}

public function getBarcode(): string
{
return $this->barcode;
}

public function getRequestId(): string
{
return $this->requestId;
}

public function isUseDiiaId(): bool
{
return $this->useDiiaId;
}
}
16 changes: 16 additions & 0 deletions src/Enum/ScopesDiiaId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace GrinchenkoUniversity\Diia\Enum;

class ScopesDiiaId
{
public const NAME = 'diiaId';

public const SCOPES_ALL = [
self::SCOPE_HASHED_FILES_SIGNING,
];

public const SCOPE_HASHED_FILES_SIGNING = 'hashedFilesSigning';
}
67 changes: 67 additions & 0 deletions src/Enum/ScopesSharing.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace GrinchenkoUniversity\Diia\Enum;

class ScopesSharing
{
public const NAME = 'sharing';

public const SCOPES_ALL = [
self::SCOPE_INTERNAL_PASSPORT,
self::SCOPE_FOREIGN_PASSPORT,
self::SCOPE_TAXPAYER_CARD,
self::SCOPE_REFERENCE_INTERNALLY_DISPLACED_PERSON,
self::SCOPE_BIRTH_CERTIFICATE,
self::SCOPE_DRIVER_LICENSE,
self::SCOPE_VEHICLE_LICENSE,
self::SCOPE_STUDENT_ID_CARD,
self::SCOPE_EDUCATION_DOCUMENT,
];

/**
* Паспорт громадянина України у формі ID-картки
*/
public const SCOPE_INTERNAL_PASSPORT = 'internal-passport';

/**
* Закордонний паспорт
*/
public const SCOPE_FOREIGN_PASSPORT = 'foreign-passport';

/**
* РНОКПП (ІПН)
*/
public const SCOPE_TAXPAYER_CARD = 'taxpayer-card';

/**
* Довідка внутрішньо переміщеної особи (ВПО)
*/
public const SCOPE_REFERENCE_INTERNALLY_DISPLACED_PERSON = 'reference-internally-displaced-person';

/**
* Свідоцтво про народження дитини
*/
public const SCOPE_BIRTH_CERTIFICATE = 'birth-certificate';

/**
* Водійські права
*/
public const SCOPE_DRIVER_LICENSE = 'driver-license';

/**
* Техпаспорт
*/
public const SCOPE_VEHICLE_LICENSE = 'vehicle-license';

/**
* Студентський квиток
*/
public const SCOPE_STUDENT_ID_CARD = 'student-id-card';

/**
* Освітні документи
*/
public const SCOPE_EDUCATION_DOCUMENT = 'education-document';
}
6 changes: 1 addition & 5 deletions src/Mapper/Acquirers/OfferMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ public function mapToRequest($dto): array
{
$data = [
'name' => $dto->getName(),
'scopes' => [
'diiaId' => [
'hashedFilesSigning',
],
],
'scopes' => $this->scopesMapper->mapToRequest($dto->getScopes()),
];

if ($dto->getReturnLink() !== null) {
Expand Down
14 changes: 5 additions & 9 deletions src/Mapper/ScopesMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,21 @@

class ScopesMapper implements ResponseMapperInterface, RequestMapperInterface
{
private array $defaultScopes;
private Scopes $defaultScopes;

public function __construct(array $defaultScopes = [])
public function __construct(Scopes $defaultScopes = null)
{
$this->defaultScopes = $defaultScopes;
$this->defaultScopes = $defaultScopes ?? new Scopes();
}

/**
* @param Scopes $dto
* @param Scopes|null $dto
*
* @return array
*/
public function mapToRequest($dto): array
{
if ($dto === null || count($dto->getAll()) < 1) {
return $this->defaultScopes;
}

return $dto->getAll();
return ($dto ?? $this->defaultScopes)->getAll();
}

public function mapFromResponse(array $response): Scopes
Expand Down
12 changes: 9 additions & 3 deletions tests/AcquirersClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use GrinchenkoUniversity\Diia\Dto\Scopes;
use GrinchenkoUniversity\Diia\Enum\ScopesDiiaId;
use GrinchenkoUniversity\Diia\Enum\ScopesSharing;

class AcquirersClientTest extends TestCase
{
Expand All @@ -40,9 +43,12 @@ protected function setUp(): void
);
$this->httpHeadersProvider = new HttpHeadersProvider($tokenProvider);

$scopesMapper = new ScopesMapper([
'diiaId' => ['hashedFilesSigning'],
]);
$defaultScopes = new Scopes();
$defaultScopes
->addScopes(ScopesDiiaId::NAME, ScopesDiiaId::SCOPES_ALL)
->addScopes(ScopesSharing::NAME, ScopesSharing::SCOPES_ALL)
;
$scopesMapper = new ScopesMapper($defaultScopes);

$branchMapper = new BranchMapper($scopesMapper);
$offerMapper = new OfferMapper($scopesMapper);
Expand Down

0 comments on commit d99f40f

Please sign in to comment.