src/IntegrationBundle/Service/Diia/DiiaService.php line 198

Open in your IDE?
  1. <?php
  2. namespace IntegrationBundle\Service\Diia;
  3. use CoreBundle\Entity\Dealer;
  4. use CoreBundle\Model\Eusphpe\Eusphpe;
  5. use GuzzleHttp\Client as HttpClient;
  6. use IntegrationBundle\Exception\HandlerNotFoundException;
  7. use IntegrationBundle\Exception\ResponseErrorException;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\Yaml\Yaml;
  10. class DiiaService
  11. {
  12.     const CACHE_KEY 'diia_token';
  13.     const CACHE_LIFETIME_IN_SECONDS 7100;
  14.     const DIIA_CERT 'Diia_2022.cer';
  15. /*    Токен авторизації "Віді страхування" PV-4222*/
  16.     const DEF_AUTH_ACQUIRER_TOKEN 'YWNxdWlyZXJfNDc5OnZpZGlfdGVzdF90b2tlbl85aWt3NDU=';
  17.     use MakesHttpRequests;
  18.     /**
  19.      * The acquirer token, which you can get from the employee of 'Diia' service.
  20.      *
  21.      * @var string
  22.      */
  23.     protected $acquirerToken;
  24.     /**
  25.      * The session token, which you can get by authorizing with acquirer token.
  26.      *
  27.      * @var string|null
  28.      */
  29.     protected $sessionToken;
  30.     /**
  31.      * @var SessionHandlerProvider
  32.      */
  33.     private $handlerProvider;
  34.     /**
  35.      * @var Eusphpe
  36.      */
  37.     private $eusphpe;
  38.     /**
  39.      * @var false|string
  40.      */
  41.     private $vidiPrivateKey;
  42.     /**
  43.      * @var false|string
  44.      */
  45.     private $vidiPrivateKeyPassword;
  46.     /**
  47.      * @var false|string
  48.      */
  49.     private $diiaSert;
  50.     private ?string $diiaCredentialsPath;
  51.     private ?string $vidiPrivateKeyFilePath;
  52.     private ?string $vidiDefPrivateKey;
  53.     private ?string $diiaSertFilePath;
  54.     /**
  55.      * @param string $acquirerToken
  56.      * @param SessionHandlerProvider $handlerProvider
  57.      * @param LoggerInterface $logger
  58.      * @param Eusphpe $eusphpe
  59.      * @param string $diia_credentials_path
  60.      * @param string $vidiPrivateKeyFilePath
  61.      * @param string $vidiPrivateKeyPasswordFilePath
  62.      * @param string $diiaSertFilePath
  63.      * @param bool $testingMode
  64.      * @param HttpClient|null $guzzle
  65.      */
  66.     public function __construct(
  67.         string $acquirerToken,
  68.         SessionHandlerProvider $handlerProvider,
  69.         LoggerInterface $logger,
  70.         Eusphpe $eusphpe,
  71.         string $diia_credentials_path,
  72.         string $vidiPrivateKeyFilePath,
  73.         string $vidiPrivateKeyPasswordFilePath,
  74.         string $diiaSertFilePath,
  75.         bool $testingMode false,
  76.         HttpClient $guzzle null
  77.     ) {
  78.         $this->acquirerDefToken $acquirerToken;
  79.         $this->testingMode $testingMode;
  80.         $this->handlerProvider $handlerProvider;
  81.         $this->eusphpe $eusphpe;
  82.         // If there were no guzzle instance provided, make the default one.
  83.         $this->setLogger($logger);
  84.         $this->diiaCredentialsPath $diia_credentials_path;
  85.         if (file_exists($vidiPrivateKeyFilePath)) {
  86.             $this->vidiDefPrivateKey file_get_contents($vidiPrivateKeyFilePathFILE_USE_INCLUDE_PATH);
  87.         }
  88.         if (file_exists($vidiPrivateKeyPasswordFilePath)) {
  89.             $this->vidiDefPrivateKeyPassword file_get_contents($vidiPrivateKeyPasswordFilePathFILE_USE_INCLUDE_PATH);
  90.         }
  91.         if (file_exists($diiaSertFilePath)) {
  92.             $this->diiaDefSert file_get_contents($diiaSertFilePathFILE_USE_INCLUDE_PATH);
  93.         }
  94.     }
  95.     /**
  96.      * Transform the items of the collection to the given class.
  97.      *
  98.      * @param array $collection
  99.      * @param string $class
  100.      * @param array $extraData
  101.      * @return array
  102.      */
  103.     public function transformCollection(array $collection$class$extraData = []): array
  104.     {
  105.         return array_map(fn($data) => new $class($data $extraData$this), $collection);
  106.     }
  107.     public function getCredentials(Dealer $dealer)
  108.     {
  109.         if (!file_exists($this->diiaCredentialsPath))
  110.             return false;
  111.         $credFileContent Yaml::parseFile($this->diiaCredentialsPath);
  112.         $credentials = [];
  113.         if ($credFileContent && isset($credFileContent[$dealer->getUniqueId()])){
  114.             $credentials $credFileContent[$dealer->getUniqueId()];
  115.         } else {
  116.             return false;
  117.         }
  118.         if (file_exists($credentials['vidiPrivateKey'])) {
  119.             $credentials['vidiPrivateKey'] = file_get_contents($credentials['vidiPrivateKey'], FILE_USE_INCLUDE_PATH);
  120.         }
  121.         if (file_exists($credentials['vidiPrivateKeyPassword'])) {
  122.             $credentials['vidiPrivateKeyPassword'] = file_get_contents($credentials['vidiPrivateKeyPassword'], FILE_USE_INCLUDE_PATH);
  123.         }
  124.         return $credentials;
  125.     }
  126.     public function setCredentials(Dealer $dealer$testMode false)
  127.     {
  128.         $credentials self::getCredentials($dealer);
  129.         if ($credentials){
  130.                     $this->acquirerToken $credentials['acquirerToken'];
  131.                     if ($testMode == true && $dealer->getId() == 14){
  132.                         $this->testingMode true;
  133.                         $this->acquirerToken 'vidielite_test_token_byw932';
  134.                     }
  135.                     $this->vidiPrivateKey $credentials['vidiPrivateKey'];
  136.                     $this->vidiPrivateKeyPassword $credentials['vidiPrivateKeyPassword'];
  137.                     $this->diiaSert $this->diiaDefSert;
  138.                     $this->initGuzzle(null);
  139.                     $this->obtainSessionToken($this->acquirerToken$credentials['auth_acquirer_token'], $dealer->getId());
  140.         } else {
  141.             $this->acquirerToken $this->acquirerDefToken;
  142.             $this->vidiPrivateKey $this->vidiDefPrivateKey;
  143.             $this->vidiPrivateKeyPassword $this->vidiDefPrivateKeyPassword;
  144.             $this->diiaSert $this->diiaDefSert;
  145.             $this->initGuzzle(null);
  146.             $this->obtainSessionToken($this->acquirerToken);
  147.         }
  148.     }
  149.     /**
  150.      * Transform the items of the collection to the given class.
  151.      *
  152.      * @return void
  153.      * @throws HandlerNotFoundException
  154.      */
  155.     public function invalidateSessionToken()
  156.     {
  157.         $this->handlerProvider->getSessionKeyHandler($this->getMode());
  158.     }
  159.     public function decryptData($encodedData)
  160.     {
  161.         $res =$this->eusphpe->developBankIdData($this->vidiPrivateKey$this->vidiPrivateKeyPassword$encodedDatanull);
  162.         $this->logger->info('decryptData'$res);
  163.         return $res;
  164.     }
  165.     /**
  166.      * Obtain session token, using provided acquirer token/
  167.      *
  168.      * @param string $acquirerToken
  169.      * @throws HandlerNotFoundException
  170.      */
  171.     protected function obtainSessionToken($acquirerToken$auth_acquirer_token self::DEF_AUTH_ACQUIRER_TOKEN$dealerId)
  172.     {
  173.         $sessionKeyHandler $this->handlerProvider->getSessionKeyHandler($this->getMode());
  174.         try {
  175.             if (!$this->sessionToken $sessionKeyHandler->handle($dealerId)) {
  176.                 $this->applyDefaultOptions(['headers' => ['Authorization' => "Basic "$auth_acquirer_token]]);
  177.                 $this->sessionToken $this->get("v1/auth/acquirer/{$acquirerToken}")["token"];
  178.                 //https://docs.google.com/document/d/1lM7Yd954GwtR1PRfo3afTR89YmC8k8_MrjyWhlZx4xw/edit#heading=h.4enep6lpiym1
  179.                 //Set session token  який був отриманий не раніше, ніж 2 години тому
  180.                 $sessionKeyHandler->setToken($this->sessionToken$dealerId);
  181.             }
  182.         } catch (ResponseErrorException $e) {
  183.             $this->logger->critical($e->getMessage());
  184.         }
  185.         //Для оновлення токена. Проблема в applyDefaultOptions (array_merge_recursive)
  186.         if (isset($this->guzzleCurrentOptions['headers']['Authorization'])){
  187.             unset($this->guzzleCurrentOptions['headers']['Authorization']);
  188.         }
  189.         $this->applyDefaultOptions(['headers' => ['Authorization' => "Bearer {$this->sessionToken}"]]);
  190.     }
  191.     public function hashFiles($files)
  192.     {
  193.         $hashFiles = [];
  194.         foreach ($files as $file){
  195.             $hashFiles[] = [
  196.                 'fileName' => $file['name'],
  197.                 'fileHash' => $this->eusphpe->hashFile($file['path'], true)
  198.             ];
  199.         }
  200.         return $hashFiles;
  201.     }
  202.     public function hashData($data)
  203.     {
  204.         $hashFiles = [];
  205.         foreach ($data as $file){
  206.             $hashFiles[] = [
  207.                 'fileName' => $file['name'],
  208.                 'fileHash' => $this->eusphpe->hashFile($file['data'])
  209.             ];
  210.         }
  211.         return $hashFiles;
  212.     }
  213.     public function checkSignature($signature$hashData)
  214.     {
  215.         $verify $this->eusphpe->verify($signature$hashData);
  216.         return $verify;
  217.     }
  218. }