fix(ros): corrige encodeSentence da API (voucher nunca era criado) + webhook orders + liberacao 90s renovavel
build-image / build-push (push) Successful in 1m44s
build-image / build-push (push) Successful in 1m44s
This commit is contained in:
1 parent
49f4bb92bd
commit
53cca4ecc8
4 files changed
+33
-8
No files matched your search
+25
-6
@@ -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);
|
||||
}
|
||||
|
||||
+1
-1
@@ -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';
|
||||
|
||||
+1
-1
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user