<?php
// Konfigurasi database (jika diperlukan)
include '../config/connect.php'; // Pastikan path-nya benar

// Ambil data JSON dari request body
$requestBody = file_get_contents('php://input');
$data = json_decode($requestBody, true);

// Validasi data
if ($data === null || !isset($data['transaction_id']) || !isset($data['status'])) {
    http_response_code(400); // Bad Request
    echo "Invalid data.";
    exit;
}

// Sanitasi data
$transaction_id = filter_var($data['transaction_id'], FILTER_SANITIZE_STRING);
$status = filter_var($data['status'], FILTER_SANITIZE_STRING);

// Validasi status transaksi
$validStatuses = ['settlement', 'pending', 'expired', 'failed'];
if (!in_array($status, $validStatuses)) {
    http_response_code(400); // Bad Request
    echo "Unknown status.";
    exit;
}

// Simpan atau proses data transaksi
try {
    $stmt = $pdo->prepare("UPDATE transactions SET status = :status WHERE transaction_id = :transaction_id");
    $stmt->bindParam(':transaction_id', $transaction_id, PDO::PARAM_STR);
    $stmt->bindParam(':status', $status, PDO::PARAM_STR);
    $stmt->execute();

    // Validasi apakah update berhasil
    if ($stmt->rowCount() === 0) {
        http_response_code(404); // Not Found
        echo "Transaction not found.";
        exit;
    }

    // Response sukses
    http_response_code(200); // OK
    echo "Transaction status updated.";
} catch (PDOException $e) {
    // Tangani kesalahan database
    http_response_code(500); // Internal Server Error
    echo "Database error: " . htmlspecialchars($e->getMessage());
    exit;
}
?>
