feat: implementation UsetrenoQRCodeProvider
This commit is contained in:
89
src/Service/Remote/UsetrenoQRCodeProvider.php
Normal file
89
src/Service/Remote/UsetrenoQRCodeProvider.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user