feat(access): libera 90s de internet para pagamento + reconcile e deteccao de pagamento pela transacao
build-image / build-push (push) Successful in 5m6s

This commit is contained in:
jfeng committed 2026-10-22 07:56:19 -03:00
1 parent 9748c8e9e0
commit 49f4bb92bd
6 files changed
+131 -6

No files matched your search

+50 -2
View File
@@ -4,7 +4,7 @@ import { fileURLToPath } from 'node:url';
import config from './config.js';
import { initDb, getDb, saveDb, genCode } from './db.js';
import { initMp, createPixPayment, getPaymentStatus, cancelOrder, validCpf } from './mp.js';
import { createHotspotUser } from './ros.js';
import { createHotspotUser, allowPaymentAccess, revokePaymentAccess } from './ros.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const app = express();
@@ -23,6 +23,9 @@ async function fulfill(payment) {
payment.voucher = { user, pass };
payment.status = 'paid';
payment.paidAt = new Date().toISOString();
const db = getDb();
db.tempAccess = (db.tempAccess || []).filter((t) => t.mac !== payment.mac);
if (payment.mac) revokePaymentAccess(payment.mac).catch(() => {});
saveDb();
console.log(`[PAGA] ${payment.planId} -> voucher ${user}`);
return payment;
@@ -45,6 +48,39 @@ async function checkPayment(payment) {
// ===== Rotas =====
// ===== Internet temporaria para pagar (bypass do hotspot) =====
app.post('/api/access/start', async (req, res) => {
const { mac } = req.body || {};
if (!mac) return res.status(400).json({ error: 'mac obrigatorio' });
try {
const r = await allowPaymentAccess(mac);
const now = Date.now();
const db = getDb();
db.tempAccess = (db.tempAccess || []).filter((t) => t.mac !== mac);
db.tempAccess.push({
mac,
ip: r.ip,
createdAt: new Date(now).toISOString(),
expiresAt: now + config.freeAccessSeconds * 1000,
});
saveDb();
res.json({ ok: true, seconds: config.freeAccessSeconds, ip: r.ip });
} catch (e) {
console.error('[ACESSO]', e.message);
res.status(500).json({ error: e.message });
}
});
// ===== Reconciliacao manual (caso o webhook falhe) =====
app.post('/api/reconcile', async (req, res) => {
const { orderId } = req.body || {};
if (!orderId) return res.status(400).json({ error: 'orderId obrigatorio' });
const payment = getDb().payments.find((p) => String(p.mpId) === String(orderId));
if (!payment) return res.status(404).json({ error: 'pedido nao encontrado' });
await checkPayment(payment);
res.json({ status: payment.status, voucher: payment.voucher, orderId: payment.mpId });
});
app.get('/api/plans', (req, res) => {
res.json(getDb().plans.filter((p) => p.active));
});
@@ -115,7 +151,7 @@ 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.query?.['data.id'];
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));
if (!payment) {
@@ -190,6 +226,18 @@ setInterval(async () => {
for (const p of pendings) await checkPayment(p);
}, 15000);
// ===== Expira acessos temporarios de pagamento =====
setInterval(async () => {
const db = getDb();
const now = Date.now();
const expired = (db.tempAccess || []).filter((t) => t.expiresAt <= now);
if (!expired.length) return;
for (const t of expired) await revokePaymentAccess(t.mac).catch(() => {});
db.tempAccess = (db.tempAccess || []).filter((t) => t.expiresAt > now);
saveDb();
console.log(`[ACESSO] ${expired.length} liberacao(oes) temporaria(s) expirada(s)`);
}, 15000);
app.listen(config.port, '0.0.0.0', () => {
console.log(`pix-hotspot rodando em ${config.baseUrl} (porta ${config.port})`);
console.log(`MikroTik: ${config.mikrotik.host}:${config.mikrotik.port}`);