feat: webhook MP (IPN) com polling como fallback
build-image / build-push (push) Successful in 1m39s

This commit is contained in:
jfeng committed 2026-10-21 18:13:15 -03:00
1 parent 286e9bd9d6
commit aec109e654
3 files changed
+20 -1

No files matched your search

+18
View File
@@ -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' });
+1
View File
@@ -20,6 +20,7 @@ export async function createPixPayment(amount, reference, description) {
payment_method_id: 'pix',
external_reference: reference,
description,
notification_url: config.baseUrl + '/api/mp/webhook',
payer: { email: '[email protected]' },
},
});