src/Service/Aeat/AeatService.php line 108

Open in your IDE?
  1. <?php
  2. namespace App\Service\Aeat;
  3. use App\Entity\AeatResponseLogs;
  4. use App\Entity\CustomerAeat;
  5. use App\Util\AeatResponseLogUtil;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Exception\GuzzleException;
  8. final class AeatService
  9. {
  10.     protected Client $client;
  11.     protected AeatResponseLogUtil $aeatResponseLogsUtil;
  12.     public function __construct(AeatResponseLogUtil $aeatResponseLogsUtil)
  13.     {
  14.         $this->client = new Client([
  15.             'base_uri' => $_ENV['AEAT_URI'],
  16.             'headers' => [
  17.                 'api-key' => $_ENV['AEAT_KEY'],
  18.             ]
  19.         ]);
  20.         $this->aeatResponseLogsUtil $aeatResponseLogsUtil;
  21.     }
  22.     public function esDomicilioRatificado(string $yearstring $nifCustomerAeat $customerAeat null): bool
  23.     {
  24.         $array = [
  25.             'year' => $year,
  26.             'nif' => $nif,
  27.         ];
  28.         try {
  29.             $response $this->client->get(sprintf('api/checkAddressStatus?%s'http_build_query($array)));
  30.             $statusCode $response->getStatusCode();
  31.             $parsed json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  32.             $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['ES_DOMICILIO'], 'cms/customer/{hash}/aeat/esDomicilioRatificado'$array$parsed$customerAeat$statusCodetrue);
  33.             return $parsed['isConfirmed'] ?? false;
  34.         } catch (\Exception $exception) {
  35.             if (isset($parsed$statusCode) && is_array($parsed)) {// @phpstan-ignore-line
  36.                 $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['ES_DOMICILIO'], 'cms/customer/{hash}/aeat/esDomicilioRatificado'$array$parsed$customerAeat$statusCodefalse);// @phpstan-ignore-line
  37.             }
  38.             throw new \RuntimeException($exception->getMessage());
  39.         }
  40.     }
  41.     /**
  42.      * @throws GuzzleException
  43.      * @throws \JsonException
  44.      */
  45.     public function ratificate(string $nifstring $referenceint $situationCustomerAeat $customerAeat null): array
  46.     {
  47.         $array = [
  48.             'nif' => $nif,
  49.             'reference' => $reference,
  50.             'situation' => "$situation",
  51.         ];
  52.         $response $this->client->post('api/ratificate', [
  53.             'json' => $array
  54.         ]);
  55.         $statusCode $response->getStatusCode();
  56.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  57.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['RATIFICATE'], 'cms/customer/{hash}/aeat/ratificate'$array$response$customerAeat$statusCode, !$response['error']);
  58.         return $response ?? [];
  59.     }
  60.     /**
  61.      * @throws GuzzleException
  62.      * @throws \JsonException
  63.      */
  64.     public function obtenerReferencia(string $nif, ?string $validity null, ?string $support null, ?float $check null, ?string $iban nullCustomerAeat $customerAeat null): array // @phpcs:ignore
  65.     {
  66.         $array = [
  67.             'nif' => $nif,
  68.             'validity' => $validity,
  69.             'support' => $support,
  70.             'check' => number_format($check2','''),
  71.             'iban' => $iban,
  72.         ];
  73.         $response $this->client->get(sprintf('api/reference?%s'http_build_query($array)));
  74.         $statusCode $response->getStatusCode();
  75.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  76.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['OBTENER_REFERENCIA'], 'cms/customer/{hash}/aeat/obtenerReferencia'$array$response$customerAeat$statusCode, !$response['error']);
  77.         return $response ?? [];
  78.     }
  79.     /**
  80.      * @throws GuzzleException
  81.      * @throws \JsonException
  82.      */
  83.     public function descargarDatosFiscales(string $nif, ?string $validity null, ?string $support null, ?float $check null, ?string $iban null, ?string $reference nullCustomerAeat $customerAeat): array // @phpcs:ignore
  84.     {
  85.         $array = [
  86.             'nif' => $nif,
  87.             'validity' => $validity,
  88.             'support' => $support,
  89.             'check' => $check number_format($check2',''') : null,
  90.             'iban' => $iban,
  91.             'reference' => $reference,
  92.         ];
  93.         $response $this->client->get(sprintf('api/data?%s'http_build_query($array)));
  94.         $statusCode $response->getStatusCode();
  95.         $response json_decode($response->getBody()->getContents(), true512JSON_THROW_ON_ERROR);
  96.         $this->aeatResponseLogsUtil->createLog(AeatResponseLogs::TYPE['DESCARGAR_DATOS'], 'cms/customer/{hash}/aeat/datosFiscales'$array$response$customerAeat$statusCode, !$response['error']);
  97.         return $response ?? [];
  98.     }
  99. }