martinms

Untitled

Apr 29th, 2024
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.99 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. date_default_timezone_set('UTC');
  4.  
  5. use GuzzleHttp\Client;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use LZCompressor\LZString;
  8.  
  9. class Bpjs extends CI_Controller
  10. {
  11.     /**
  12.      * @var string
  13.      */
  14.     protected $baseUrl;
  15.  
  16.     /**
  17.      * @var int
  18.      */
  19.     protected $consumerId;
  20.  
  21.     /**
  22.      * @var string
  23.      */
  24.     protected $consumerSecret;
  25.  
  26.     /**
  27.      * @var string
  28.      */
  29.     protected $userKey;
  30.  
  31.     /**
  32.      * Class constructor
  33.      */
  34.     public function __construct()
  35.     {
  36.         parent::__construct();
  37.  
  38.         $this->baseUrl = 'https://apijkn-dev.bpjs-kesehatan.go.id/';
  39.         $this->consumerId = 3972;
  40.         $this->consumerSecret = '2wJ5630AAA';
  41.         $this->userKey = 'a931e9e40ac3d692fbdc5bb608ff5707';
  42.  
  43.         $this->load->database();
  44.     }
  45.  
  46.     /**
  47.      * @return void
  48.      * @throws GuzzleException
  49.      */
  50.     public function getData()
  51.     {
  52.         $currentTimestamp = strval(time() - strtotime('1970-01-01 00:00:00'));
  53.  
  54.         $signatureData = "{$this->consumerId}&{$currentTimestamp}";
  55.  
  56.         $signature = hash_hmac('sha256', $signatureData, $this->consumerSecret, true);
  57.         $signature = base64_encode($signature);
  58.  
  59.         $headers = [
  60.             'X-cons-id' => $this->consumerId,
  61.             'X-Timestamp' => $currentTimestamp,
  62.             'X-Signature' => $signature,
  63.             'user_key' => $this->userKey,
  64.  
  65.         ];
  66.  
  67.         $client = new Client([
  68.             'base_uri' => $this->baseUrl,
  69.             'timeout' => 10,
  70.         ]);
  71.  
  72.         try {
  73.             $response = $client->get('antreanrs_dev/ref/poli', [
  74.                 'headers' => $headers,
  75.             ]);
  76.    
  77.             if ($response->getStatusCode() == 200) {
  78.                 $responseBody = $response->getBody();
  79.                 $content = $responseBody->getContents();
  80.    
  81.                 $data = json_decode($content);
  82.                 $response = $data->response;
  83.    
  84.                 $key = "{$this->consumerId}{$this->consumerSecret}{$currentTimestamp}";
  85.                 $dataHasilAPI = $this->decode($response, $key);
  86.  
  87.                 return $dataHasilAPI;
  88.             }
  89.             else {
  90.                 // error lain
  91.             }
  92.         }
  93.         catch (GuzzleException $e) {
  94.             $responseData = $e->getResponse()->getBody()->getContents();
  95.             $responseCode = $e->getResponse()->getStatusCode();
  96.  
  97.             if ($responseCode == 403) {
  98.                 echo 'User key atau detail akses lain salah.<br>';
  99.                 echo 'Balasan server: ' . $responseData;
  100.             }
  101.             else {
  102.                 echo $responseBody;
  103.             }
  104.         }
  105.     }
  106.  
  107.     public function index()
  108.     {
  109.         $data = $this->getData();
  110.  
  111.         $this->load->view('data', [
  112.             'data' => $data,
  113.         ]);
  114.     }
  115.  
  116.     public function simpan()
  117.     {
  118.         $data = $this->getData();
  119.  
  120.         foreach ($data as $item) {
  121.             $this->db->insert('namatabelnya', [
  122.                 'nmpoli' => $item->nmpoli,
  123.                 'kdsubspesialis' => $item->kdsubspesialis,
  124.                 'kdpoli' => $item->kdpoli,
  125.             ]);
  126.         }
  127.  
  128.         redirect('bpjs');
  129.     }
  130.  
  131.     protected function decode($string, $key)
  132.     {
  133.         $encryptMethod = 'AES-256-CBC';
  134.         $keyHash = hex2bin(hash('sha256', $key));
  135.  
  136.         $iv = substr(hex2bin(hash('sha256', $key)), 0, 16);
  137.  
  138.         $output = openssl_decrypt(base64_decode($string), $encryptMethod, $keyHash, OPENSSL_RAW_DATA, $iv);
  139.  
  140.         return json_decode(LZString::decompressFromEncodedURIComponent($output));
  141.     }
  142. }
  143.  
Add Comment
Please, Sign In to add comment