<?php
// Receber o webhook
// Explicação: https://xflowpayments.com/blog/api-pix-php/
// webhook.php
$raw = file_get_contents('php://input');
$assinatura = $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '';
$esperado = 'sha256=' . hash_hmac('sha256', $raw, getenv('XFLOW_WEBHOOK_SECRET'));

if (!hash_equals($esperado, $assinatura)) {
    http_response_code(401);
    exit;
}

$evento = json_decode($raw, true);
$data = $evento['data'];

try {
    if ($evento['event'] === 'transaction.paid' && $data['livemode'] === true) {
        darBaixaNoPedido($data['external_reference'], $data['pix']['endToEnd']); // sua função
    }
    http_response_code(200);
} catch (Throwable $e) {
    http_response_code(500); // sem 2xx, a entrega é reenviada
}
