import express from 'express';
import crypto from 'crypto';
const app = express();
app.use(express.raw({ type: 'application/json' })); // raw body for HMAC
const SECRET = process.env.NTXPAY_WEBHOOK_SECRET!;
app.post('/webhooks/ntxpay', (req, res) => {
const sig = req.header('X-NTXPay-Signature') ?? '';
const expected = 'sha256=' + crypto
.createHmac('sha256', SECRET)
.update(req.body) // req.body is Buffer
.digest('hex');
if (!crypto.timingSafeEqual(Buffer.from(sig), Buffer.from(expected))) {
return res.status(401).end();
}
const event = JSON.parse(req.body.toString());
if (event.event === 'cash_out') {
if (event.transaction.status === 'CONFIRMED') {
markPayoutSettled(event.transaction.id);
} else if (event.transaction.status === 'FAILED') {
markPayoutFailed(event.transaction.id);
}
}
res.json({ received: true });
});