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

$json = file_get_contents("php://input");
$data = json_decode($json, true);

file_put_contents("webhook_with.txt", '[' . date('Y-m-d H:i:s') . '] - ' . $json . PHP_EOL, FILE_APPEND);

if ($data && isset($data['externalreference']) && $data['status'] === 'COMPLETED') {
    file_put_contents("webhook_with.txt", 'ENTROU NO IF' . PHP_EOL, FILE_APPEND);
    $stmtUpdateTransaction = $pdo->prepare("UPDATE withdrawal SET status = :status WHERE external_reference = :external_reference");
    $stmtUpdateTransaction->execute([
        ':status' => 'approved',
        ':external_reference' => $data['externalreference']
    ]);
}
