<?php
// verifyPayment.php - MangoFy API
header('Content-Type: application/json');

// Lê o JSON do front
$input = json_decode(file_get_contents('php://input'), true);
$paymentCode = $input['id'] ?? null;

if (!$paymentCode) {
    echo json_encode([
        'status'  => 'error',
        'message' => 'Payment code não informado'
    ]);
    exit;
}

// ================================
// CONFIG MANGOFY (carregado do secrets-config.php)
// ================================
$_secrets = require __DIR__ . '/../../secrets-config.php';

$MANGOFY_API_TOKEN = $_secrets['mangofy_api_token'];
$MANGOFY_STORE_CODE = $_secrets['mangofy_store_code'];
$_mangofy_base_url = $_secrets['mangofy_api_url'];

unset($_secrets);

// Endpoint correto para buscar pagamento
$URL = $_mangofy_base_url . '/' . urlencode($paymentCode);

// Requisição
$ch = curl_init($URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Accept: application/json',
    'Authorization: ' . $MANGOFY_API_TOKEN,
    'Store-Code: ' . $MANGOFY_STORE_CODE
]);
$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

// Valida resposta
$data = json_decode($response, true);

if (!is_array($data) || $http !== 200) {
    echo json_encode([
        'status' => 'error',
        'raw'    => $response
    ]);
    exit;
}

// Pega o status do pagamento
// MangoFy usa: approved, pending, refunded, error
$status = strtolower($data['payment_status'] ?? '');

// Mapeia para o formato esperado pelo JS
// JS espera: completed, paid, approved
$mappedStatus = $status;
if ($status === 'approved') {
    $mappedStatus = 'approved'; // já está correto
}

// Devolve no formato esperado pelo JS
echo json_encode([
    'status' => $mappedStatus,
    'raw' => $data
]);