<?php
require __DIR__ . '/../config.php';

$id = isset($_GET['transaction_id']) ? $_GET['transaction_id'] : '';
if (!preg_match('/^[\w-]{1,64}$/', $id)) json_out(['status' => 'unknown'], 400);

$MAP = [
  'PAID' => 'paid',
  'PENDING' => 'pending',
  'REFUNDED' => 'refunded',
  'REFUSED' => 'refused',
  'FAILED' => 'refused',
  'EXPIRED' => 'expired',
  'CHARGEBACK' => 'refunded',
  'PRECHARGEBACK' => 'pending',
  'ERROR' => 'refused',
];

list($status, $json, $raw) = vennox_request('/v1/payment-transaction/info/' . $id);

if ($status < 200 || $status >= 300) {
  error_log('Vennox status falhou [' . $status . ']: ' . $raw);
  json_out(['status' => 'pending']);
}

$info = vennox_unwrap($json);
$rawStatus = strtoupper(isset($info['status']) ? (string)$info['status'] : '');

$out = ['status' => isset($MAP[$rawStatus]) ? $MAP[$rawStatus] : 'pending'];
if (isset($info['amount']) && is_numeric($info['amount'])) $out['amount'] = $info['amount'] / 100;

json_out($out);
