feat: webhook MP (IPN) com polling como fallback
build-image / build-push (push) Successful in 1m39s
build-image / build-push (push) Successful in 1m39s
This commit is contained in:
1 parent
286e9bd9d6
commit
aec109e654
3 files changed
+20
-1
No files matched your search
@@ -87,6 +87,24 @@ app.post('/api/checkout', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// ===== Webhook MP (IPN): confirma pagamento na hora =====
|
||||
app.post('/api/mp/webhook', async (req, res) => {
|
||||
res.sendStatus(200); // responde rápido para o MP não reenviar
|
||||
try {
|
||||
const mpId = req.body?.data?.id || req.query?.['data.id'];
|
||||
if (!mpId) return;
|
||||
const payment = getDb().payments.find((p) => String(p.mpId) === String(mpId));
|
||||
if (!payment) {
|
||||
console.log(`[WEBHOOK] pagamento MP ${mpId} nao localizado`);
|
||||
return;
|
||||
}
|
||||
await checkPayment(payment);
|
||||
console.log(`[WEBHOOK] ${mpId} -> ${payment.status}`);
|
||||
} catch (e) {
|
||||
console.error('[WEBHOOK]', e.message);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/status/:id', async (req, res) => {
|
||||
const payment = getDb().payments.find((p) => p.id === req.params.id);
|
||||
if (!payment) return res.status(404).json({ error: 'nao encontrado' });
|
||||
|
||||
Reference in new issue
Block a user