<?php
// FILE: cek_status.php
// LOGIKA: SAMA DENGAN FILE LAMA (Menggunakan &check=1)
// UPDATE: Akun Baru

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
error_reporting(0);

// --- KONFIGURASI BARU ---
$memberID = "OK2829570";      
$password = "wasalamL050";      
$pin      = "0502";          

$input = json_decode(file_get_contents("php://input"), true);
$tujuan = isset($input['tujuan']) ? $input['tujuan'] : '';
$kode_produk = isset($input['kode_produk']) ? $input['kode_produk'] : '';
$refID = isset($input['refID']) ? $input['refID'] : ''; 

if(empty($refID)) {
    echo json_encode(['status' => 'PENDING', 'sn' => 'Menunggu data...']);
    exit();
}

// Susun URL Cek Status (Fitur &check=1 dari file lama)
$url = "https://h2h.okeconnect.com/trx?memberID=" . $memberID . 
       "&pin=" . $pin . 
       "&password=" . $password .
       "&product=" . urlencode($kode_produk) . 
       "&dest=" . urlencode($tujuan) . 
       "&refID=" . $refID . 
       "&check=1"; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response_text = curl_exec($ch);
curl_close($ch);

// --- PARSING RESPON ---
$status_final = "PENDING";
$sn_final = $response_text;

// Logika deteksi dari file lama Anda
if (stripos($response_text, 'status Sukses') !== false || stripos($response_text, 'SUKSES') !== false) {
    $status_final = "BERHASIL";
    // Ambil SN saja
    $parts = explode("SN:", $response_text);
    if (isset($parts[1])) {
        $sn_final = "SN:" . $parts[1];
    }
} elseif (stripos($response_text, 'status Gagal') !== false || stripos($response_text, 'GAGAL') !== false || stripos($response_text, 'TIDAK ADA') !== false) {
    $status_final = "GAGAL";
}

echo json_encode([
    'status' => $status_final,
    'sn' => $sn_final
]);
?>