Skip to content

Commit

Permalink
Added missing shared endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
matasarei committed Jul 15, 2024
1 parent d99f40f commit 1a041c3
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 698 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: "Tests"
on: [push]
permissions:
contents: "read"
jobs:
composer:
name: Composer config validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Validate composer.json
run: composer validate --strict
php:
name: PHP syntax validation
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check PHP syntax
run: php -l src/ tests/
phpunit:
name: PHPUnit tests
runs-on: ubuntu-latest
strategy:
matrix:
php_version: [7.4, 8.0, 8.1, 8.2]
steps:
- uses: actions/checkout@v3
- uses: php-actions/composer@v6
with:
php_version: ${{ matrix.php_version }}
command: install --ignore-platform-req=ext-eusphpe
- run: vendor/bin/phpunit
3 changes: 0 additions & 3 deletions Dockerfile

This file was deleted.

695 changes: 21 additions & 674 deletions LICENSE

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# Diia (Дія) API Client for PHP
![CI workflow](https://github.com/grinchenkoedu/diia-php/actions/workflows/tests.yml/badge.svg)

## Tests and development
1. Prepare image
1. Install vendors
```bash
docker build -t diia-php .
docker run --rm -v $(pwd):/app -w /app composer:lts composer install
```
2. Install vendors
2. Run tests
```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
docker run --rm -v $(pwd):/app -w /app composer:lts vendor/bin/phpunit
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "grinchenkoedu/diia-php",
"description": "Unofficial Diia library for PHP",
"type": "library",
"license": "GPL-3.0",
"license": "MIT",
"authors": [
{
"name": "Yevhen Matasar",
Expand Down
48 changes: 47 additions & 1 deletion src/Client/AcquirersClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
use GrinchenkoUniversity\Diia\Dto\Request\DocumentRequest;
use GrinchenkoUniversity\Diia\Dto\Request\ItemsListRequest;
use GrinchenkoUniversity\Diia\Dto\Request\OfferRequest;
use GrinchenkoUniversity\Diia\Dto\Response\CreateResourceResponse;
use GrinchenkoUniversity\Diia\Dto\Response\ItemsListResponse;
use GrinchenkoUniversity\Diia\Dto\Response\OfferResponse;
use GrinchenkoUniversity\Diia\Mapper\Request\RequestJsonMapper;
use GrinchenkoUniversity\Diia\Mapper\Response\ResponseJsonMapper;
use GrinchenkoUniversity\Diia\Provider\HttpHeadersProvider;
use GuzzleHttp\ClientInterface;
use UnexpectedValueException;

class AcquirersClient
{
Expand Down Expand Up @@ -185,4 +185,50 @@ public function documentRequest(DocumentRequest $documentRequest): void
]
);
}

public function documentRequestStatus(string $barcode, string $requestId): ?string
{
$response = $this->httpClient->request(
'GET',
'/api/v1/acquirers/document-request/status',
[
'headers' => $this->httpHeadersProvider->getDefaultHeaders(),
'query' => [
'barcode' => $barcode,
'requestId' => $requestId,
],
]
);

$data = json_decode($response->getBody()->getContents(), true);

if (($data['status'] ?? null) === null) {
throw new UnexpectedValueException('Unexpected status response, failed to fetch status.');
}

return $data['status'];
}

public function offerRequestStatus(string $otp, string $requestId): ?string
{
$response = $this->httpClient->request(
'GET',
'/api/v1/acquirers/offer-request/status',
[
'headers' => $this->httpHeadersProvider->getDefaultHeaders(),
'query' => [
'otp' => $otp,
'requestId' => $requestId,
],
]
);

$data = json_decode($response->getBody()->getContents(), true);

if (($data['status'] ?? null) === null) {
throw new UnexpectedValueException('Unexpected status response, failed to fetch status.');
}

return $data['status'];
}
}
17 changes: 15 additions & 2 deletions src/Dto/Request/OfferRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class OfferRequest
private string $offerId;
private ?string $returnLink = null;
private string $requestId;
private string $signAlgo = self::SIGN_ALGO_ECDSA;
private ?string $signAlgo = null;
private ?bool $useDiia = null;
private array $files = [];

public function __construct(
Expand Down Expand Up @@ -79,7 +80,7 @@ public function setRequestId(string $requestId): self
return $this;
}

public function getSignAlgo(): string
public function getSignAlgo(): ?string
{
return $this->signAlgo;
}
Expand All @@ -90,4 +91,16 @@ public function setSignAlgo(string $signAlgo): self

return $this;
}

public function getUseDiia(): ?bool
{
return $this->useDiia;
}

public function setUseDiia(?bool $useDiia): self
{
$this->useDiia = $useDiia;

return $this;
}
}
6 changes: 6 additions & 0 deletions src/Enum/ScopesSharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ScopesSharing
public const NAME = 'sharing';

public const SCOPES_ALL = [
self::SCOPE_PASSPORT,
self::SCOPE_INTERNAL_PASSPORT,
self::SCOPE_FOREIGN_PASSPORT,
self::SCOPE_TAXPAYER_CARD,
Expand All @@ -20,6 +21,11 @@ class ScopesSharing
self::SCOPE_EDUCATION_DOCUMENT,
];

/**
* Паспорт
*/
public const SCOPE_PASSPORT = 'passport';

/**
* Паспорт громадянина України у формі ID-картки
*/
Expand Down
32 changes: 32 additions & 0 deletions src/Mapper/Request/DocumentRequestMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace GrinchenkoUniversity\Diia\Mapper\Request;

use GrinchenkoUniversity\Diia\Dependency\SupportedDependencyInterface;
use GrinchenkoUniversity\Diia\Dto\Request\DocumentRequest;

class DocumentRequestMapper implements RequestMapperInterface, SupportedDependencyInterface
{
/**
* @param DocumentRequest $dto
* @return array
*/
public function mapToRequest($dto): array
{
$request = [
'branchId' => $dto->getBranchId(),
'barcode' => $dto->getBarcode(),
'requestId' => $dto->getRequestId(),
'useDiiaId' => $dto->isUseDiiaId(),
];

return $request;
}

public function isSupported($value): bool
{
return $value instanceof DocumentRequest;
}
}
21 changes: 15 additions & 6 deletions src/Mapper/Request/OfferRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,20 @@ public function mapToRequest($dto): array
$request = [
'offerId' => $dto->getOfferId(),
'requestId' => $dto->getRequestId(),
'signAlgo' => $dto->getSignAlgo(),
];

if ($dto->getSignAlgo() !== null) {
$request['signAlgo'] = $dto->getSignAlgo();
}

if ($dto->getReturnLink() !== null) {
$request['returnLink'] = $dto->getReturnLink();
}

if ($dto->getUseDiia() !== null) {
$request['useDiia'] = $dto->getUseDiia();
}

$hashedFiles = [];

foreach ($dto->getFiles() as $name => $hash) {
Expand All @@ -34,11 +41,13 @@ public function mapToRequest($dto): array
];
}

$request['data'] = [
'hashedFilesSigning' => [
'hashedFiles' => $hashedFiles,
]
];
if (count($hashedFiles) > 0) {
$request['data'] = [
'hashedFilesSigning' => [
'hashedFiles' => $hashedFiles,
]
];
}

return $request;
}
Expand Down
90 changes: 87 additions & 3 deletions tests/AcquirersClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
use GrinchenkoUniversity\Diia\Dependency\DependencyResolver;
use GrinchenkoUniversity\Diia\Dto\Acquirers\Branch;
use GrinchenkoUniversity\Diia\Dto\Acquirers\Offer;
use GrinchenkoUniversity\Diia\Dto\Request\DocumentRequest;
use GrinchenkoUniversity\Diia\Dto\Request\ItemsListRequest;
use GrinchenkoUniversity\Diia\Dto\Request\OfferRequest;
use GrinchenkoUniversity\Diia\Dto\Scopes;
use GrinchenkoUniversity\Diia\Enum\ScopesDiiaId;
use GrinchenkoUniversity\Diia\Enum\ScopesSharing;
use GrinchenkoUniversity\Diia\Mapper\Acquirers\BranchMapper;
use GrinchenkoUniversity\Diia\Mapper\Acquirers\OfferMapper;
use GrinchenkoUniversity\Diia\Mapper\Request\DocumentRequestMapper;
use GrinchenkoUniversity\Diia\Mapper\Request\ItemsListRequestMapper;
use GrinchenkoUniversity\Diia\Mapper\Request\OfferRequestMapper;
use GrinchenkoUniversity\Diia\Mapper\Request\RequestJsonMapper;
Expand All @@ -21,9 +26,6 @@
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 Down Expand Up @@ -58,6 +60,7 @@ protected function setUp(): void
->addDependency($branchMapper)
->addDependency($offerMapper)
->addDependency(new OfferRequestMapper())
->addDependency(new DocumentRequestMapper())
;
$this->requestJsonMapper = new RequestJsonMapper($dependencyResolver);

Expand Down Expand Up @@ -414,4 +417,85 @@ public function testOfferRequest()
$offerResponse->getDeepLink()
);
}

public function testDocumentRequest()
{
$documentRequest = new DocumentRequest(
'branchId',
'3535267635434',
'requestId'
);

$this
->httpClient
->expects($this->once())
->method('request')
->with(
'POST',
'/api/v1/acquirers/document-request',
[
'headers' => $this->httpHeadersProvider->getDefaultHeaders(),
'body' => $this->requestJsonMapper->mapToJson($documentRequest),
]
)
->willReturn(new Response(200));

$this->acquirersClient->documentRequest($documentRequest);
}

public function testDocumentRequestStatus()
{
$barcode = 'barcode';
$requestId = 'requestId';
$expectedStatus = 'status';

$this
->httpClient
->expects($this->once())
->method('request')
->with(
'GET',
'/api/v1/acquirers/document-request/status',
[
'headers' => $this->httpHeadersProvider->getDefaultHeaders(),
'query' => [
'barcode' => $barcode,
'requestId' => $requestId,
],
]
)
->willReturn(new Response(200, [], json_encode(['status' => $expectedStatus])));

$status = $this->acquirersClient->documentRequestStatus($barcode, $requestId);

$this->assertEquals($expectedStatus, $status);
}

public function testOfferRequestStatus()
{
$otp = 'otp';
$requestId = 'requestId';
$expectedStatus = 'status';

$this
->httpClient
->expects($this->once())
->method('request')
->with(
'GET',
'/api/v1/acquirers/offer-request/status',
[
'headers' => $this->httpHeadersProvider->getDefaultHeaders(),
'query' => [
'otp' => $otp,
'requestId' => $requestId,
],
]
)
->willReturn(new Response(200, [], json_encode(['status' => $expectedStatus])));

$status = $this->acquirersClient->offerRequestStatus($otp, $requestId);

$this->assertEquals($expectedStatus, $status);
}
}

0 comments on commit 1a041c3

Please sign in to comment.