<?php

sleep(1);

$username = "ismailsult";
$auth_token = "2302554:MqGE9ZFeNmBakH8tKiAvjVQl0pf6yJzL";
$app_reg_id = "cuh1R_VfR4SaTuvLfNaFVL";
$phone_uuid = "cuh1R_VfR4SaTuvLfNaFVL";
$phone_model = "CPH2179";
$phone_android_version = "10";
$app_version_code = "250918";
$app_version_name = "25.09.18";
$ui_mode = "light";

$token = explode(':', $auth_token)[0];

$url = "https://app.orderkuota.com/api/v2/qris/mutasi/" . $token;

$timestamp = round(microtime(true) * 1000);

$post_data = [
    'app_reg_id' => $app_reg_id,
    'phone_uuid' => $phone_uuid,
    'phone_model' => $phone_model,
    'requests[qris_history][keterangan]' => '',
    'requests[qris_history][jumlah]' => '',
    'request_time' => $timestamp,
    'phone_android_version' => $phone_android_version,
    'app_version_code' => $app_version_code,
    'auth_username' => $username,
    'requests[qris_history][page]' => '1',
    'auth_token' => $auth_token,
    'app_version_name' => $app_version_name,
    'ui_mode' => $ui_mode,
    'requests[qris_history][dari_tanggal]' => '',
    'requests[0]' => 'account',
    'requests[qris_history][ke_tanggal]' => ''
];

$post_fields = http_build_query($post_data);

$signature = hash('sha512', $timestamp . $auth_token . $username);

$curl = curl_init();

curl_setopt_array($curl, [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => '',
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $post_fields,
    CURLOPT_HTTPHEADER => [
        'User-Agent: okhttp/4.12.0',
        'Accept-Encoding: gzip',
        'Content-Type: application/x-www-form-urlencoded',
        'signature: ' . $signature,
        'timestamp: ' . $timestamp,
    ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

curl_close($curl);

header('Content-Type: application/json');

if ($err) {
    http_response_code(500);
    echo json_encode([
        'status' => 'error',
        'message' => 'cURL Error: ' . $err,
        'data' => null
    ]);
} else {
    $response_data = json_decode($response, true);
    echo json_encode([
        'status' => 'success',
        'http_code' => $http_code,
        'data' => $response_data ?: $response
    ], JSON_PRETTY_PRINT);
}
?>
