<?php
require_once "/var/www/html/portal/config.php";

$license   = $_GET['license'] ?? '';
$terminal  = $_GET['mt5'] ?? '';

if(!$license || !$terminal){
    die("INVALID");
}

$stmt = $conn->prepare("
    SELECT id, expires_at
    FROM fxh_orders
    WHERE license_key = ?
      AND payment_status = 'PAID'
");

$stmt->bind_param("s",$license);
$stmt->execute();
$res = $stmt->get_result();

if(!$res->num_rows){
    die("INVALID");
}

$lic = $res->fetch_assoc();

if(!empty($lic['expires_at']) && time() > strtotime($lic['expires_at'])){
    die("EXPIRED");
}

/* OPTIONAL: terminal binding */
$conn->query("
   UPDATE fxh_orders SET bound_terminal='$terminal'
   WHERE id={$lic['id']} AND bound_terminal IS NULL
");

echo "VALID";
