feat: implementation UsetrenoQRCodeProvider

This commit is contained in:
2024-01-18 14:20:10 +01:00
parent 450187aa23
commit 264e0260cf
9 changed files with 328 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace App\Service\Remote\Edge\Exceptions;
class MissingParameterException extends \RuntimeException
{
}

View File

@@ -0,0 +1,62 @@
<?php
declare(strict_types=1);
namespace App\Service\Remote\Edge;
use App\Entity\QRCode\QRCode;
use App\Entity\QRCode\QRCodeMoney;
use App\Entity\QRCode\QRCodePaymentIdentification;
use App\Entity\QRCode\QRCodeQROptions;
use App\Entity\Remote\Usetreno\Edge\EdgeQRCode;
use App\Entity\Remote\Usetreno\Edge\EdgeQRCodeMoney;
use App\Entity\Remote\Usetreno\Edge\EdgeQRCodePaymentIdentification;
use App\Entity\Remote\Usetreno\Edge\EdgeQRCodeQROptions;
use App\Service\Remote\Edge\Exceptions\MissingParameterException;
class QRCodeEntityConverter
{
public function __construct() { }
public function convert(QRCode $code): EdgeQRCode {
return new EdgeQRCode(
$code->getIban() ?? throw new MissingParameterException("iban not set"),
$code->getDueDate() ? $code->getDueDate()->format('Y-m-d') : throw new MissingParameterException("due date not set"),
$code->getMessage() ?? throw new MissingParameterException("message not set"),
$this->convertMoney($code->getMoney() ?? throw new MissingParameterException("money not set")),
$this->convertCodeOptions($code->getCodeQROptions() ?? throw new MissingParameterException("codeQROptions not set")),
$this->convertIdentification($code->getPaymentIdentification())
);
}
protected function convertMoney(QRCodeMoney $money): EdgeQRCodeMoney
{
return new EdgeQRCodeMoney(
$money->getAmount() ?? throw new MissingParameterException("amount not set"),
$money->getCurrency() ?? throw new MissingParameterException("currency not set")
);
}
protected function convertCodeOptions(QRCodeQROptions $options): EdgeQRCodeQROptions
{
return new EdgeQRCodeQROptions(
$options->getScale(),
$options->getMargin()
);
}
protected function convertIdentification(?QRCodePaymentIdentification $ident): EdgeQRCodePaymentIdentification {
// OMG: proc? symboly v ostatnich statech nejsou, prijde me lepsi posilat NULL, nebo empty string ;-)
return match ($ident) {
null => new EdgeQRCodePaymentIdentification(
"0",
"0",
"0"
),
default => new EdgeQRCodePaymentIdentification(
$ident->getVariableSymbol() ?? "0",
$ident->getSpecificSymbol() ?? "0",
$ident->getConstantSymbol() ?? "0"
)
};
}
}

View File

@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace App\Service\Remote\Exception;
use App\Service\Exception\QRCodeGeneratorException;
class UsetrenoQRCodeException extends QRCodeGeneratorException
{
}

View File

@@ -0,0 +1,89 @@
<?php
declare(strict_types=1);
namespace App\Service\Remote;
use App\Entity\QRCode\QRCode;
use App\Service\QRCodeGeneratorInterface;
use App\Service\Remote\Edge\QRCodeEntityConverter;
use App\Service\Remote\Exception\AuthorizeException;
use App\Service\Remote\Exception\UsetrenoQRCodeException;
use Psr\Log\LoggerInterface;
class UsetrenoQRCodeProvider implements QRCodeGeneratorInterface
{
const QRCODE_API = 'https://topapi.top-test.cz/chameleon/api/v1/qr-code/create-for-bank-account-payment';
const QRCODE_METHOD = 'POST';
public function __construct(protected readonly LoggerInterface $logger,
protected readonly UsetrenoHttpClient $client,
protected readonly QRCodeEntityConverter $codeEntityConverter) { }
public function generateQRCodeFromEntity(QRCode $entity): string
{
$edgeEntity = $this->codeEntityConverter->convert($entity);
$this->logger->debug("Sending request for QR code", [
"entity" => $entity,
"edgeEntity" => $edgeEntity
]);
$response = $this->client->request(static::QRCODE_METHOD, static::QRCODE_API, [
'json' => $edgeEntity,
]);
$statusCode = $response->getStatusCode();
$responseData = $response->getContent(false);
if ($statusCode != 200) {
$this->logger->error("qrcode request status code is not ok", [
"AUTHORIZE_API" => static::QRCODE_API,
"statusCode" => $statusCode,
"content" => $responseData,
]);
throw new UsetrenoQRCodeException("Return code is not 200 OK (got: code: $statusCode)");
}
$this->logger->debug("QRCode generation success", [
"responseContent" => $responseData,
]);
return $this->parseBase64String($this->processQRCodeResponseEntity($responseData));
}
protected function parseBase64String(string $content): string {
$data = explode(';base64,', $content);
// check if response is image (we support png only now)
if ($data[0] === "image/png") {
throw new UsetrenoQRCodeException("Unsupported image type $data[0]");
}
return $data[1];
}
/**
* @param string $responseData
* @return string Bearer token
* @throws UsetrenoQRCodeException
*/
protected function processQRCodeResponseEntity(string $responseData): string {
$data = json_decode($responseData);
if (!is_object($data)) {
$this->logger->error("qrcode: received null response data", [
"responseData" => $responseData
]);
throw new UsetrenoQRCodeException("Can't decode json response");
}
if (!isset($data->data->base64Data)) {
$this->logger->error("qrcode: empty base64Data in response data", [
"responseData" => $responseData
]);
throw new UsetrenoQRCodeException("Got invalid json response");
}
return $data->data->base64Data;
}
}