<?php
/**
 * Sudanna Pay — Verify Payment Status
 * GET /api/partners/verify.php?payment_id=PAYxxx
 */
header('Access-Control-Allow-Origin: *');
if($_SERVER['REQUEST_METHOD']==='OPTIONS'){http_response_code(200);exit;}
require_once __DIR__.'/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';
Kernel::handle(['auth' => false, 'public' => true]);
require_once __DIR__ . '/../../engine/Logger.php';
require_once __DIR__.'/../../engine/IntegrationEngine.php';

$apiKey    = $_SERVER['HTTP_X_API_KEY'] ?? '';
$timestamp = $_SERVER['HTTP_X_TIMESTAMP'] ?? '';
$signature = $_SERVER['HTTP_X_SIGNATURE'] ?? '';
$paymentId = preg_replace('/[^A-Z0-9]/', '', strtoupper($_GET['payment_id'] ?? ''));

if(!$apiKey||!$timestamp||!$signature||!$paymentId){
    http_response_code(400);die(json_encode(['error'=>'Missing parameters']));
}

$partner = IntegrationEngine::authenticatePartner($apiKey, $timestamp, $signature, $paymentId);
if(!$partner){http_response_code(401);die(json_encode(['error'=>'Authentication failed']));}

$result = IntegrationEngine::verifyPayment($partner, $paymentId);
echo json_encode($result, JSON_UNESCAPED_UNICODE);
