<?php
// API Token yang Anda miliki
$apiToken = "aat.MTAw.eyJ2IjoxLCJ1Ijo2MzQ1NjksImQiOjg2MzYwNSwiYWkiOjQ4NTAwLCJhayI6ImYwYzA3NjI1LWM4MTktNDM2NS05NWFjLTFjZWZkNDE5Y2IyMSIsImFuIjoid2lzZWxpZS5jby5pZCIsImFwIjoiZmFkNDljMWUtYWU3Zi00YzdjLWI3ZDQtMGUyZGU3ZDg3OTQxIiwidCI6MTcyMTc0OTM4OTMwOX0.3peR67aKWWjT+d0go8fm4eRRL6neRMC/WvyAiAZDbN8pXJcytyXCiqAbgC3xCxwzz38UMYMEvqt0+yQtfdPY0gz40na4xvX0BS6z/9pynRDQQghm0NyV977PyFSWuS6uABwl8FQIRsex7RunGclPW2Awg0LOFXHxD9NHdlCH+CUAIWgDBWZE7cTgd9sjVXTfJD75O5hfkZJO3c5m/t4F7w==.2nWe05M7/W7xvtOf6O+LEZtUIbdlTXwhFF9IemNXrLw";

// Signature Secret
$signatureSecret = "fPvWlLX0QvIPepZSDWuZEINbSw7oxUI6gdtvxzTOdEotyFnFnAa0TS6BmogYvS3R";

// URL dasar API
$baseUrl = "https://public.accurate.id/accurate/api";

// Fungsi untuk membuat permintaan POST dengan format JSON ke endpoint API
function makeApiPostRequest($endpoint, $apiToken, $signatureSecret, $data) {
    global $baseUrl; // Mengakses $baseUrl dari luar fungsi

    // Buat URL lengkap untuk permintaan API
    $url = $baseUrl . $endpoint;

    // Ambil timestamp UTC
    $timestamp = gmdate('Y-m-d\TH:i:s\Z');

    // Buat signature
    $signature = hash_hmac('sha256', $timestamp, $signatureSecret);

    // Inisialisasi cURL
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    // Set headers
    $headers = array(
        "Authorization: Bearer $apiToken",
        "X-Api-Timestamp: $timestamp",
        "X-Api-Signature: $signature", // Signature yang diperlukan
        "Content-Type: application/json" // Content-Type untuk body request JSON
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // Set data POST sebagai JSON
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    // Eksekusi cURL
    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        die("cURL Error: " . curl_error($ch));
    }
    curl_close($ch);

    // Decode dan return respons JSON
    return json_decode($response, true);
}

// Data untuk permintaan dengan parameter ID
$data = [
    'id' => 863605, // ID yang sesuai dengan permintaan
    'name' => 'Test Item', // Nama item
    'itemType' => 'INVENTORY' // Tipe item
];

// Permintaan ke endpoint item/save.do
$endpoint = '/item/save.do';
$response = makeApiPostRequest($endpoint, $apiToken, $signatureSecret, $data);

// Menampilkan respons
echo "<h3>Response from $endpoint</h3>";
echo "<pre>";
print_r($response);
echo "</pre>";
?>
