Files
2026-04-27 10:26:26 +07:00

317 lines
8.5 KiB
PHP

<?php
class Ssapix extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->db_onedev = $this->load->database("default", true);
}
function test_auth()
{
//$this->load->library("Satusehat");
$result = $this->put_token();
echo $result;
}
function test_gettoken()
{
//$this->load->library("Satusehat");
$result = $this->get_token();
echo $result;
}
function test_search_practicioner_by_nik()
{
$nik = "367400001111202";
$result = $this->search_practicioner_by_nik($nik);
$birthDate = $result->entry[0]->resource->birthDate;
$gender = $result->entry[0]->resource->gender;
$ihsNumber = $result->entry[0]->resource->id;
$dataPracticioner = array(
'birthDate' => $birthDate,
'gender' => $gender,
'ihsNumber' => $ihsNumber
);
print_r($dataPracticioner);
}
function test_search_patient_by_nik()
{
$nik = "9271060312000001";
$result = $this->search_patient_by_nik($nik);
$ihsNumber = $result->entry[0]->resource->id;
$name = $result->entry[0]->resource->name[0]->text;
$dataPatient = array(
'name' => $name,
'ihsNumber' => $ihsNumber
);
print_r($dataPatient);
}
function get_client_key($debug = "")
{
$sql = "select * from one_health.client where clientIsActive = 'Y'";
$qry = $this->db->query($sql);
if (!$qry) {
return [false, "", ""];
}
$rows = $qry->result_array();
if (count($rows) == 0) {
if ($debug != "") {
print_r([false, "", ""]);
}
return [false, "", ""];
}
if ($debug != "") {
print_r([true, $rows[0]["clientKey"], $rows[0]["clientSecret"]]);
}
return [true, $rows[0]["clientKey"], $rows[0]["clientSecret"]];
}
function put_token()
{
$auth_url = "https://api-satusehat-dev.dto.kemkes.go.id/oauth2/v1";
//api url
$url = $auth_url . "/accesstoken?grant_type=client_credentials";
//echo $url;
$data = [
"client_id" => "6pukkqo0rqqu0ckboc8ekgcxqysfpr4avkivmutgkx5xvva4",
"client_secret" => "89zqsmy3z5w7rvschtp9gjoawwiazg4a2uns3matw3dxbfxtdarssetubd8mrn3p"
];
$ch = curl_init($url);
# setup request to send json via post.
curl_setopt($ch, curlopt_postfields, http_build_query($data));
curl_setopt($ch, curlopt_customrequest, "post");
curl_setopt(
$ch,
curlopt_httpheader,
array(
'content-type: application/x-www-form-urlencoded'
)
);
# return response instead of printing.
curl_setopt($ch, curlopt_returntransfer, true);
# send request.
$result = curl_exec($ch);
curl_close($ch);
# print response.
//print_r($result);
//echo $token_rst->access_token;
if ($result) {
$token_rst = json_decode($result);
$sql = "select count(*) as xcount, tokenid
from one_health.token
where
tokenisactive = 'y'
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "get count token error";
exit;
}
$rst_count = $qry->row_array();
if ($rst_count['xcount'] > 0) {
$sql = "update one_health.token set tokenvalue = ?, tokenexpired = date_add(now(), interval 50 minute)
where tokenid = ?";
$qry = $this->db_onedev->query($sql, [$token_rst->access_token, $rst_count['tokenid']]);
if (!$qry) {
$this->sys_error_db("refresh token error", $this->db_onedev->last_query());
exit;
}
} else {
$sql = "update one_health.token set tokenisactive = 'n' where tokenisactive = 'y'";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "nonactive token error";
exit;
}
$sql = "insert into one_health.token(tokenvalue,tokenexpired) values(?,date_add(now(), interval 50 minute))";
$qry = $this->db_onedev->query($sql, [$token_rst->access_token]);
if (!$qry) {
echo "insert token error";
exit;
}
}
$sql = "select tokenvalue
from one_health.token
where
tokenisactive = 'y' limit 1
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "get token error";
exit;
}
return $qry->row()->tokenValue;
}
}
function get_token()
{
$sql = "SELECT COUNT(*) as xcount, tokenValue
FROM one_health.token
WHERE tokenIsActive = 'Y' AND NOW() < tokenExpired AND tokenValue IS NOT NULL
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "select token error";
exit;
}
$data_token = $qry->row_array();
//print_r($data_token);
if ($data_token['xcount'] > 0) {
return $data_token['tokenValue'];
} else {
return $this->put_token();
}
}
function search_practicioner_by_nik($nik)
{
$token = $this->get_token();
$authorization = "Authorization: Bearer " . $token;
$xbase_url = "https://api-satusehat-dev.dto.kemkes.go.id/fhir-r4/v1";
//API URL
$url = $xbase_url . "/Practitioner?identifier=https://fhir.kemkes.go.id/id/nik|" . $nik;
//echo $url;
$ch = curl_init($url);
# Setup request to send json via POST.
//$payload = json_encode($data);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
$data_rst = json_decode($result);
//print_r($result);
return $data_rst;
}
function search_patient_by_nik($nik)
{
$token = $this->get_token();
$authorization = "Authorization: Bearer " . $token;
$xbase_url = "https://api-satusehat-dev.dto.kemkes.go.id/fhir-r4/v1";
//API URL
$url = $xbase_url . "/Patient?identifier=https://fhir.kemkes.go.id/id/nik|" . $nik;
//echo $url;
$ch = curl_init($url);
# Setup request to send json via POST.
//$payload = json_encode($data);
//curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
# Return response instead of printing.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Send request.
$result = curl_exec($ch);
curl_close($ch);
# Print response.
$data_rst = json_decode($result);
//print_r($result);
return $data_rst;
}
function send_bundle($orderID)
{
$sql = "SELECT *
FROM one_health.bundle
WHERE BundleT_orderHeaderID = {$orderID} AND BundleIsActive = 'Y'
LIMIT 1
";
$qry = $this->db_onedev->query($sql);
if (!$qry) {
echo "select bundle error";
exit;
}
$data_blundle = $qry->row_array();
// print_r($data_blundle);
$json_data = json_decode($data_blundle['BundleJSON']);
$json_payload = json_encode($json_data);
$token = $this->get_token();
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api-satusehat-dev.dto.kemkes.go.id/fhir-r4/v1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $json_payload,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Authorization: Bearer ' . $token
),
));
$response = curl_exec($curl);
curl_close($curl);
//echo $response;
//$json_response = json_encode(json_decode($response));
//echo $json_response;
$sql = "UPDATE one_health.bundle SET BundleResponseJSON = '{$response}'
WHERE BundleT_orderHeaderID = {$orderID} AND BundleIsActive = 'Y'
";
$qry = $this->db_onedev->query($sql);
//echo $sql;
if (!$qry) {
echo "update bundle error";
exit;
}
echo $response;
}
protected function objToArray($obj)
{
// Not an object or array
if (!is_object($obj) && !is_array($obj)) {
return $obj;
}
// Parse array
foreach ($obj as $key => $value) {
$arr[$key] = $this->objToArray($value);
}
// Return parsed array
return $arr;
}
}