<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

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

include "/www/wwwroot/bilspro/config/connect.php";

// Validasi metode
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
    echo json_encode(['success' => false, 'message' => 'Metode tidak diperbolehkan']);
    exit;
}

// Validasi input
$order_id = $_POST['order_id'] ?? '';
if (empty($order_id)) {
    echo json_encode(['success' => false, 'message' => 'Order ID tidak ditemukan']);
    exit;
}

// Cek status dari database
$stmt = $pdo->prepare("SELECT status FROM apk_payment WHERE order_id = :order_id");
$stmt->bindParam(':order_id', $order_id);
$stmt->execute();
$data = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$data) {
    echo json_encode(['success' => false, 'message' => 'Transaksi tidak ditemukan']);
    exit;
}

$status = strtoupper($data['status']);

if (in_array($status, ['SUCCESS', 'PENDING', 'CANCELED', 'LUNAS'])) {
    echo json_encode([
        'success' => true,
        'status' => ucfirst(strtolower($status)) // Output: Success, Pending, etc.
    ]);
} else {
    echo json_encode([
        'success' => false,
        'message' => 'Status tidak dikenali: ' . $status
    ]);
}
