<?php
header('Content-Type: application/json; charset=utf-8');
require_once 'config.php';
checkApiKey();

$invoice_id = intval($_GET['id'] ?? $_POST['id'] ?? 0);

if (!$invoice_id) {
    echo json_encode(['status' => false, 'message' => 'شناسه فاکتور نامعتبر']);
    exit;
}

try {
    $pdo = getDB();
    $stmt = $pdo->prepare("SELECT id, invoice_number, user_id, amount, package_id, package_name, track_id, ref_id, card_pan, status, created_at, verified_at FROM invoices WHERE id = ?");
    $stmt->execute([$invoice_id]);
    $invoice = $stmt->fetch();

    if (!$invoice) {
        echo json_encode(['status' => 'not_found', 'message' => 'فاکتور یافت نشد']);
        exit;
    }

    echo json_encode(['status' => $invoice['status'], 'invoice' => $invoice]);
} catch (Exception $e) {
    echo json_encode(['status' => false, 'message' => $e->getMessage()]);
}
