src/Service/Abha.php line 423

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use Symfony\Component\HttpClient\HttpClient;
  4. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use App\Repository\{PatientRepository};
  7. class Abha
  8. {
  9.     private $flash;
  10.     private $patientRepo;
  11.     public function __construct(FlashBagInterface $flash, private ManagerRegistry $doctrinePatientRepository $patientRepo)
  12.     {
  13.         $this->flash $flash;
  14.         $this->patientRepo $patientRepo;
  15.     }
  16.     public function GUID()
  17.     {
  18.         if (function_exists('com_create_guid') === true) {
  19.             return trim(com_create_guid(), '{}');
  20.         }
  21.         return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x'mt_rand(065535), mt_rand(065535), mt_rand(065535), mt_rand(1638420479), mt_rand(3276849151), mt_rand(065535), mt_rand(065535), mt_rand(065535));
  22.     }
  23.     //  public function getAccessToken()
  24.     // {
  25.     //     $now = new \DateTime('now', new \DateTimeZone('UTC'));
  26.     //     $isoTimestamp = $now->format('Y-m-d\TH:i:s.v\Z');
  27.     //     $reqId = self::GUID();
  28.     //     try {
  29.     //         $client = HttpClient::create(); 
  30.     //         // $sessionUrl = env('ABDM_URL') . '/v0.5/sessions';
  31.     //         $sessionUrl = 'https://dev.abdm.gov.in/api/hiecm/gateway/v3/sessions';
  32.     //         $tokenReq = $client->post($sessionUrl, [
  33.     //             'headers' => [
  34.     //                 'Content-Type' => 'application/json', 
  35.     //                 'X-CM-ID' => 'sbx', 
  36.     //                 'REQUEST-ID' => $reqId, 
  37.     //                 'TIMESTAMP' => $isoTimestamp],
  38.     //             'json' => [
  39.     //                 'clientId' => 'SBX_002132',
  40.     //                 'clientSecret' => 'dd468fe5-74f5-44f1-9fb5-47542de0f60b',
  41.     //                 'grantType' => 'client_credentials'
  42.     //             ]
  43.     //         ]);
  44.     //         // Log::debug($tokenReq->getBody());
  45.     //         $res = json_decode($tokenReq->getBody(), true);
  46.     //         return 'Bearer ' . $res['accessToken'];
  47.     //     } catch (\Exception $ex) {
  48.     //         info('Error in fetching access token');
  49.     //         info($ex->getMessage());
  50.     //     }
  51.     // }
  52.     public function getAccessToken()
  53.     {
  54.         $now = new \DateTime('now', new \DateTimeZone('UTC'));
  55.         $isoTimestamp $now->format('Y-m-d\TH:i:s.v\Z');
  56.         $reqId self::GUID();
  57.         $client HttpClient::create();
  58.         $response $client->request('POST''https://dev.abdm.gov.in/api/hiecm/gateway/v3/sessions', [
  59.             'headers' => [
  60.                 
  61.                 'Content-Type' => 'application/json'
  62.                     'X-CM-ID' => 'sbx'
  63.                     'REQUEST-ID' => $reqId
  64.                     'TIMESTAMP' => $isoTimestamp
  65.             ],
  66.             'json' => [
  67.                 'clientId' => 'SBX_002132',
  68.                     'clientSecret' => 'dd468fe5-74f5-44f1-9fb5-47542de0f60b',
  69.                     'grantType' => 'client_credentials'
  70.             ]
  71.         ]);
  72.         $decodedPayload $response->toArray();
  73.         return 'Bearer ' $decodedPayload['accessToken'];
  74.     }
  75.     public function getUserDetails($content)
  76.     {
  77.         $contentArr['token'] = $content['token'];
  78.         $contentArr['firstName'] = $content['first_name'];
  79.         $contentArr['middleName'] = $content['middle_name'];
  80.         $contentArr['lastName'] = $content['last_name'];
  81.         $contentArr['yearOfBirth'] = date('Y'strtotime($content['dob']));
  82.         $contentArr['monthOfBirth'] = date('m'strtotime($content['dob']));
  83.         $contentArr['dayOfBirth'] = date('d'strtotime($content['dob']));
  84.         $contentArr['dob'] = date('Y-m-d'strtotime($content['dob']));
  85.         $contentArr['gender'] = $content['gender'];
  86.         $contentArr['mobile'] = isset($content['identifiers']['mobile']) ? $content['identifiers']['mobile'] : '';
  87.         $contentArr['healthIdNumber'] = isset($content['identifiers']['health_number']) ? $content['identifiers']['health_number'] : '';
  88.         $contentArr['healthIdAddress'] = $content['id'];
  89.         $contentArr['refresh_token'] = isset($content['refreshToken']) ? $content['refreshToken'] : '';
  90.         return $contentArr;
  91.     }
  92.     /* M3 Part Start here*/
  93.     public function getConsentList()
  94.     {
  95.         $conn $this->doctrine->getConnection('cnmgts');
  96.         $sql "SELECT * FROM cnmgts_abha_consent_request ORDER BY id DESC";
  97.         return $conn->fetchAllAssociative($sql);
  98.     }
  99.     public function requestInt($data)
  100.     {
  101.         $expiry $data['expiry'];
  102.         $purpose $data['purpose'];
  103.         $fromTo $data['fromTo'];
  104.         $ArrDate explode('-'$fromTo);
  105.         $currentDate date('d-m-Y');
  106.         $from str_replace('/''-'trim($ArrDate[0]));
  107.         $to str_replace('/''-'trim($ArrDate[1]));
  108.         $current str_replace('/''-'$currentDate);
  109.         $expiryDate str_replace('/''-'$expiry);
  110.         $type $data['type'];
  111.         $healthAddress $data['health_id']; //TODO :: health address store in session
  112.         if ($_ENV['ABHA_ENV'] == 'sandbox') {
  113.             $suffix $_ENV['ABHA_SBX_SUFFIX'];
  114.         } else {
  115.             $suffix $_ENV['ABHA_PROD_SUFFIX'];
  116.         }
  117.         if (!strpos($healthAddress$suffix) !== false) {
  118.             $healthAddress $healthAddress $suffix;
  119.         }
  120.         $past $future $lt false;
  121.         if ($healthAddress != '' && $purpose != '' && $fromTo != '' && $expiryDate != '' && $type != '') {
  122.             if (strtotime($currentDate) < strtotime($from) || strtotime($currentDate) < strtotime($to)) { // TODO future date are not allowed
  123.                 $future true;
  124.                 $this->flash->add(
  125.                     'danger',
  126.                     'Future date not allowed.'
  127.                 );
  128.             }
  129.             if (strtotime($from) > strtotime($to)) {
  130.                 $lt true;
  131.                 $this->flash->add(
  132.                     'danger',
  133.                     'From date should be less than to date.'
  134.                 );
  135.             }
  136.             if (strtotime($current) > strtotime($expiryDate)) { // TODO past date are not allowed
  137.                 $past true;
  138.                 $this->flash->add(
  139.                     'danger',
  140.                     'Past date not allowed.'
  141.                 );
  142.             }
  143.             if ($lt || $future || $past) {
  144.                 return false;
  145.             }
  146.         } else {
  147.             $this->flash->add(
  148.                 'danger',
  149.                 'Please enter valid details.'
  150.             );
  151.             return false;
  152.         }
  153.         try {
  154.             $payload = [
  155.                 'healthAddress' => $healthAddress,
  156.                 'purpose' => $purpose,
  157.                 'from' => $from,
  158.                 'to' => $to,
  159.                 'expiry' => $expiryDate,
  160.                 'type' => $type,
  161.                 'hiu_name' => 'Rani Unchegaon',
  162.                 'hiu_id' => 'HIURANI'
  163.             ];
  164.             $url $_ENV['ABDM_BASE_URL'] . 'm3/request-init';
  165.             $this->patientRepo->insertPayload($payload$url);
  166.             $client HttpClient::create();
  167.             $client->request('POST'$url, [
  168.                 'headers' => [
  169.                     'Content-Type' => 'application/json'
  170.                 ],
  171.                 'json' => $payload
  172.             ]);
  173.             return true;
  174.         } catch (\Throwable $th) {
  175.             $this->flash->add(
  176.                 'danger',
  177.                 'Something went wrong. please try again.'
  178.             );
  179.             return false;
  180.         }
  181.     }
  182.     public function getDataRequest($id)
  183.     {
  184.         $conn $this->doctrine->getConnection('cnmgts');
  185.         $sql "SELECT artefact_id, request_from, request_to, expiry_date, key_material FROM cnmgts_abha_consent_request WHERE id = ? ORDER BY id DESC LIMIT 1";
  186.         $arrData $conn->fetchAllAssociative($sql, [$id]);
  187.          
  188.         // dd($arrData);
  189.         if ($arrData[0]) {
  190.             try {
  191.                 $payload = [
  192.                     'artefactId' => $arrData[0]['artefact_id'],
  193.                     'from' => $arrData[0]['request_from'],
  194.                     'to' => $arrData[0]['request_to'],
  195.                     'expiry' => $arrData[0]['expiry_date'],
  196.                     'keyMaterial' => $arrData[0]['key_material'],
  197.                 ];
  198.                 $url $_ENV['ABDM_BASE_URL'] .'m3/cm-request';
  199.                 $this->patientRepo->insertPayload($payload$url);
  200.                 $client HttpClient::create();
  201.                  $client->request('POST'$url, [
  202.                     'headers' => [
  203.                         'Content-Type' => 'application/json'
  204.                     ],
  205.                     'json' => $payload
  206.                 ]);
  207.                 // return true;
  208.                 // $contentData = json_decode($response->getContent(), true);
  209.                 $this->flash->add(
  210.                     'success',
  211.                     'Your request sent successfully.'
  212.                 );
  213.                 $flag true;
  214.             } catch (\Throwable $th) {
  215.                 $this->flash->add(
  216.                     'danger',
  217.                     'Something went wrong please try again.'
  218.                 );
  219.                 $flag true;
  220.             }
  221.         } else {
  222.             $flag true;
  223.         }
  224.         return $flag;
  225.     }
  226.     public function getViewData($id)
  227.     {
  228.         $htmlFinalArr = [];
  229.         try {
  230.             $conn $this->doctrine->getConnection('cnmgts');
  231.             $sql "SELECT artefact_id, key_material FROM cnmgts_abha_consent_request WHERE id = ? ORDER BY id DESC LIMIT 1";
  232.             $arrData $conn->fetchAllAssociative($sql, [$id]);
  233.             if ($arrData[0]) {
  234.                 $payload = [
  235.                     'artefactId' => $arrData[0]['artefact_id'],
  236.                     'keyMaterial' => $arrData[0]['key_material']
  237.                 ];
  238.                 $url $_ENV['ABDM_BASE_URL'] .'m3/data-decryption';
  239.                 $this->patientRepo->insertPayload($payload$url);
  240.                 $client HttpClient::create();
  241.                 $response $client->request('POST'$url, [
  242.                     'headers' => [
  243.                         'Content-Type' => 'application/json'
  244.                     ],
  245.                     'json' => $payload
  246.                 ]);
  247.                 $contentData json_decode($response->getContent(), true);
  248.                 if (!empty($contentData)) {
  249.                     foreach ($contentData as $cKey => $cData) {
  250.                         $htmlStr '';
  251.                         $htmlArr $htmlPatient $contentVal = [];
  252.                         foreach ($cData['entry'] as $data) {
  253.                             if ($data['resource']) {
  254.                                 if ($data['resource']['resourceType'] == "Patient") {
  255.                                     $htmlPatient[$data['resource']['resourceType']] = $data['resource']['text']['div'];
  256.                                 } else {
  257.                                     $htmlArr[$data['resource']['resourceType']] = $data['resource']['text']['div'];
  258.                                     if (isset($data['resource']['content'])) {
  259.                                         foreach ($data['resource']['content'] as $cVal) {
  260.                                             if ($cVal['attachment']['contentType'] == "application/pdf") {
  261.                                                 $contentVal[$cVal['attachment']['contentType']] = $cVal['attachment']['data'];
  262.                                             } else {
  263.                                                 $contentVal[$cVal['attachment']['contentType']] = $cVal['attachment']['data'];
  264.                                             }
  265.                                             $htmlFinalArr[$cKey]['content'] = $contentVal;
  266.                                         }
  267.                                     }
  268.                                 }
  269.                             }
  270.                         }
  271.                         $htmlPatient array_merge($htmlPatient$htmlArr);
  272.                         $htmlStr implode('<div style="border-bottom: 1px solid #EBEDF3;" class="my-2"></div>'$htmlPatient);
  273.                         $htmlFinalArr[$cKey]['str'] = $htmlStr// TODO: Care context wise string
  274.                     }
  275.                     $flag true;
  276.                 } else {
  277.                     $this->flash->add(
  278.                         'danger',
  279.                         'Something went wrong please try again. else'
  280.                     );
  281.                     $flag false;
  282.                 }
  283.             } else {
  284.                 $flag false;
  285.             }
  286.         } catch (\Throwable $th) {
  287.             $this->flash->add(
  288.                 'danger',
  289.                 'Something went wrong please try again. catch'
  290.             );
  291.             $flag false;
  292.         }
  293.         return array($flag$htmlFinalArr);
  294.     }
  295.     public function patientFind($healthId)
  296.     {
  297.         $requestId self::GUID();
  298.         $accessToken self::getAccessToken();
  299.         $name '';
  300.         // if ($_ENV['ABHA_ENV'] == 'sandbox') {
  301.         //     $suffix = $_ENV['ABHA_SBX_SUFFIX'];
  302.         // } else {
  303.         //     $suffix = $_ENV['ABHA_PROD_SUFFIX'];
  304.         // }
  305.         // $hiuId = $this->getUser()->getProfile()->getHospital()->getHIUCode();
  306.         $now = new \DateTime('now', new \DateTimeZone('UTC'));
  307.       $suffix '@sbx';
  308.         $isoTimestamp $now->format('Y-m-d\TH:i:s.v\Z');
  309.             $client HttpClient::create();
  310.             $response $client->request('POST''https://abhasbx.abdm.gov.in/abha/api/v3/phr/web/login/abha/search', [
  311.                 'headers' => [
  312.                     "REQUEST-ID" => $requestId,
  313.                     "TIMESTAMP" => $isoTimestamp,
  314.                     'Authorization' => $accessToken,
  315.                     'Content-Type' => 'application/json',
  316.                     'X-CM-ID' => 'sbx'
  317.                 ],
  318.                 'json' => [
  319.                             "abhaAddress" => $healthId $suffix
  320.                         ]
  321.             ]);
  322.             $decodedPayload $response->toArray();
  323.         return  $decodedPayload['abhaAddress'];
  324.             // $response->getStatusCode();
  325.         //  var_dump($response);
  326.         // return $response;
  327.         
  328.     }
  329.     /* End of m3 Part Here*/
  330. }