<?php 

$serverKeyApi = 'Mid-server-aSXAMMosYIoElA_k9eSQb1_w';
$clientKeyApi = 'Mid-client-SEflqrSLhskCXBxZ';

// Redirect jika index.php ada di URL
$requestUri = $_SERVER['REQUEST_URI'];
if (strpos($requestUri, 'index.php') !== false) {
    $newUrl = str_replace('index.php', '', $requestUri);
    header('Location: ' . $newUrl);
    exit;
}


// Validasi dan sanitasi input
$order_id = isset($_GET['order_id']) ? $_GET['order_id'] : '';
$order_id = filter_var($order_id, FILTER_SANITIZE_STRING);

// Validasi agar order_id tidak kosong
if (empty($order_id)) {
    http_response_code(400); // Bad Request
    echo "Invalid order_id.";
    exit;
}

// Include konfigurasi dengan perlindungan keamanan
include '../config/connect.php';

try {
    // Query untuk mengambil data dari tabel apk_payment
    $stmt = $pdo->prepare("SELECT status FROM apk_payment WHERE order_id = :order_id");
    $stmt->bindParam(':order_id', $order_id, PDO::PARAM_STR);
    $stmt->execute();
    $result = $stmt->fetch(PDO::FETCH_ASSOC);
    
    if ($result === false) {
        http_response_code(404); // Not Found
        echo "Order not found.";
        exit;
    }

    if ($result['status'] == 'BELUM DIBAYAR') {
        include "pay.php";
    } elseif ($result['status'] == 'PENDING' || $result['status'] == 'LUNAS' || $result['status'] == 'EXPAIRED') {
        include "pending.php";
    } else {
        http_response_code(400); // Bad Request
        echo "Unknown status.";
        exit;
    }

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