· 7 years ago · Apr 06, 2018, 02:08 AM
1<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3class Bpjs_lib {
4
5 public $bpjs_config;
6 public $headers;
7 public $cons_id;
8 public $timestamp;
9 public $signature;
10 public $secret_key;
11 public $authorization;
12 public $user_pcare;
13 public $password_pcare;
14 public $app_code;
15
16 public function __construct()
17 {
18 $this->ci =& get_instance();
19 $this->ci->config->load('bpjs_config');
20 $this->bpjs_config = $this->ci->config->item('bpjs_config');
21 }
22 public function initialize()
23 {
24 foreach ($this->bpjs_config as $configName => $configValue) {
25 $this->$configName = $configValue;
26 }
27 $this->setTimestamp()->setSignature()->setAuthorization()->setHeaders();
28 return $this;
29 }
30 public function setAuthorization()
31 {
32 $this->authorization = base64_encode($this->user_pcare.':'.$this->password_pcare.':'.$this->app_code);
33 return $this;
34 }
35 public function setHeaders()
36 {
37 $this->headers = [
38 'X-cons-id' => $this->cons_id,
39 'X-Timestamp' => $this->timestamp,
40 'X-Signature' => $this->signature,
41 'X-Authorization' => 'Basic '.$this->authorization,
42 ];
43 return $this;
44 }
45 public function setSignature()
46 {
47 $data = $this->cons_id.'&'.$this->timestamp;
48 $signature = hash_hmac('sha256', $data, $this->secret_key, true);
49 $encodedSignature = base64_encode($signature);
50 $this->signature = $encodedSignature;
51 return $this;
52 }
53 public function setTimestamp()
54 {
55 date_default_timezone_set('UTC');
56 $this->timestamp = strval(time()-strtotime('1970-01-01 00:00:00'));
57 return $this;
58 }
59 public function getProfile($keyword)
60 {
61 $url = 'dvlp.bpjs-kesehatan.go.id:9080/pcare-rest-dev/v2/peserta/'.$keyword;
62 return $this->curlThis($url);
63 }
64 public function curlThis($url,$method="GET")
65 {
66 $ch = curl_init( $url );
67 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
68 curl_setopt($ch, CURLOPT_HTTPHEADER, $this->initialize()->headers);
69 $result = curl_exec($ch);
70 curl_close($ch);
71 # Print response.
72 print_r($result);
73 print_r($this->initialize());
74 }
75
76}
77
78/* End of file Bpjs_lib.php */