<?php
require_once __DIR__.'/../../config.php';
require_once __DIR__ . '/../../engine/Kernel.php';
require_once __DIR__.'/../../engine/Logger.php';
require_once __DIR__.'/../../engine/SudannaPayEngine.php';
if ($_SERVER['REQUEST_METHOD']!=='POST'){http_response_code(405);exit;}
$ctx = Kernel::handle(['auth' => true]);
$u   = $ctx['user'];
$b = json_decode(file_get_contents('php://input'),true)??[];
$txnRef = trim($b['txn_ref']??'');
$pin    = trim($b['pin']??'');
if(!$txnRef||!$pin){http_response_code(400);die(json_encode(['error'=>'missing_fields']));}
$result = SudannaPayEngine::reverseTransaction((int)$u['id'], $txnRef, $pin);
http_response_code(($result['ok']??false)?200:400);
echo json_encode($result, JSON_UNESCAPED_UNICODE);
