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)
.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 === 'refund_out') {
// Balance already debited
markOrderAsRefunded({
originalCashInId: event.originalTransactionId,
refundId: event.transaction.id,
amount: event.transaction.amountCentavos,
});
}
res.json({ received: true });
});