<?php
// File: cek_saldo_admin.php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json");
error_reporting(0);

// 1. KREDENSIAL (Diambil dari api.php Anda)
$memberID = "OK2829570";      
$password = "wasalamL050";      
$pin      = "0502";          

// 2. URL API Cek Saldo OkeConnect
$url = "https://h2h.okeconnect.com/trx/balance?memberID=$memberID&pin=$pin&password=$password";

// 3. EKSEKUSI REQUEST
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
curl_close($ch);

// 4. PARSING RESPON
// Contoh Respon Asli: "Saldo 284.939"
if (empty($response)) {
    echo json_encode(['status' => 'error', 'msg' => 'Koneksi ke OkeConnect Gagal']);
} 
elseif (stripos($response, 'Saldo') !== false) {
    // Hapus kata "Saldo" agar sisa angkanya saja
    $angka = str_ireplace("Saldo", "", $response);
    $angka = trim($angka);
    
    echo json_encode([
        'status' => 'success', 
        'raw' => $response,
        'saldo_formatted' => $angka // Hasil: "284.939"
    ]);
} 
else {
    echo json_encode(['status' => 'error', 'msg' => 'Respon: ' . $response]);
}
?>