From 53cca4ecc8960e82530ae03f79bb5616b0f4d349 Mon Sep 17 00:00:00 2001 From: Jorge Fernando Date: Thu, 22 Oct 2026 08:32:34 -0300 Subject: [PATCH] fix(ros): corrige encodeSentence da API (voucher nunca era criado) + webhook orders + liberacao 90s renovavel --- backend/public/index.html | 6 ++++++ backend/src/index.js | 31 +++++++++++++++++++++++++------ backend/src/mp.js | 2 +- backend/src/ros.js | 2 +- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/backend/public/index.html b/backend/public/index.html index 49269a2..8176bea 100644 --- a/backend/public/index.html +++ b/backend/public/index.html @@ -602,6 +602,8 @@ let toastTimer = null; let selectedPlanId = null; let mpDevice = null; + let freeTimer = null; + let freeGrants = 0; const $ = (id) => document.getElementById(id); @@ -653,6 +655,8 @@ $('view-pay').classList.toggle('hidden', view !== 'pay'); $('view-voucher').classList.toggle('hidden', view !== 'voucher'); if (view !== 'pay' && pollTimer) { clearInterval(pollTimer); pollTimer = null; } + if (view !== 'pay' && freeTimer) { clearInterval(freeTimer); freeTimer = null; } + if (view === 'pay' && !freeTimer) { freeTimer = setInterval(startFreeAccess, 45000); } window.scrollTo({ top: 0, behavior: 'instant' in window ? 'auto' : 'auto' }); } @@ -786,6 +790,8 @@ // ===== Internet temporaria para pagar (abre o app do banco) ===== async function startFreeAccess() { if (!mac) return; + if (freeGrants >= 12) return; + freeGrants++; try { const res = await fetch('/api/access/start', { method: 'POST', diff --git a/backend/src/index.js b/backend/src/index.js index 5bae1d2..441a284 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -151,15 +151,34 @@ app.post('/api/checkout', async (req, res) => { 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.body?.id || req.query?.['data.id'] || req.query?.id; - if (!mpId) return; - const payment = getDb().payments.find((p) => String(p.mpId) === String(mpId)); + const d = req.body?.data || {}; + const mpId = d.id || req.body?.id || req.query?.['data.id'] || req.query?.id; + const extRef = d.external_reference || req.body?.external_reference || req.query?.external_reference; + const pays = d.transactions?.payments || []; + const paidHint = + d.status === 'processed' || + d.status_detail === 'accredited' || + pays.some((p) => ['approved', 'processed'].includes(p.status)); + + const payment = getDb().payments.find( + (p) => (mpId && String(p.mpId) === String(mpId)) || (extRef && String(p.id) === String(extRef)) + ); if (!payment) { - console.log(`[WEBHOOK] pagamento MP ${mpId} nao localizado`); + console.log('[WEBHOOK] nao localizado', mpId, extRef); return; } - await checkPayment(payment); - console.log(`[WEBHOOK] ${mpId} -> ${payment.status}`); + + if (paidHint && payment.status === 'pending') { + await checkPayment(payment); + if (payment.status === 'pending') { + // webhook diz pago: libera mesmo se a consulta ao MP falhar + await fulfill(payment); + saveDb(); + } + } else { + await checkPayment(payment); + } + console.log(`[WEBHOOK] ${mpId || extRef} -> ${payment.status}`); } catch (e) { console.error('[WEBHOOK]', e.message); } diff --git a/backend/src/mp.js b/backend/src/mp.js index f5f826d..25c12fd 100644 --- a/backend/src/mp.js +++ b/backend/src/mp.js @@ -161,7 +161,7 @@ export async function getPaymentStatus(mpId) { const data = await new Order(getClient()).get({ id: mpId }); const pay = data.transactions?.payments?.[0] || {}; // order: action_required (waiting_transfer) = pendente | processed = pago - if (data.status === 'processed' || pay.status === 'approved') return 'approved'; + if (data.status === 'processed' || ['approved', 'processed'].includes(pay.status)) return 'approved'; if (['cancelled', 'rejected', 'expired', 'refunded'].includes(data.status)) return data.status; if (['cancelled', 'rejected', 'refunded'].includes(pay.status)) return pay.status; return 'pending'; diff --git a/backend/src/ros.js b/backend/src/ros.js index 1074dba..7c192f2 100644 --- a/backend/src/ros.js +++ b/backend/src/ros.js @@ -13,7 +13,7 @@ function encodeSentence(words) { const parts = []; for (const w of words) { const buf = Buffer.from(String(w), 'utf8'); - parts.push(packLen(buf.length), buf, Buffer.from([0])); + parts.push(packLen(buf.length), buf); } parts.push(Buffer.from([0])); return Buffer.concat(parts);