feat: qualidade MP - items/payer/statement_descriptor/deviceId/cancel + form CPF no portal
build-image / build-push (push) Successful in 1m36s
build-image / build-push (push) Successful in 1m36s
This commit is contained in:
1 parent
67608421b4
commit
59d6aa0ed3
4 files changed
+250
-12
No files matched your search
+42
-3
@@ -3,7 +3,7 @@ import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import config from './config.js';
|
||||
import { initDb, getDb, saveDb, genCode } from './db.js';
|
||||
import { initMp, createPixPayment, getPaymentStatus } from './mp.js';
|
||||
import { initMp, createPixPayment, getPaymentStatus, cancelOrder, validCpf } from './mp.js';
|
||||
import { createHotspotUser } from './ros.js';
|
||||
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
@@ -51,9 +51,16 @@ app.get('/api/plans', (req, res) => {
|
||||
|
||||
app.post('/api/checkout', async (req, res) => {
|
||||
try {
|
||||
const { planId, mac } = req.body || {};
|
||||
const { planId, mac, payerName, payerCpf, deviceId } = req.body || {};
|
||||
const plan = getDb().plans.find((p) => p.id === planId && p.active);
|
||||
if (!plan) return res.status(400).json({ error: 'plano invalido' });
|
||||
if (payerCpf && !validCpf(payerCpf)) return res.status(400).json({ error: 'CPF invalido' });
|
||||
|
||||
// historico do comprador (primeira compra / ultima compra)
|
||||
const hist = getDb().payments.filter((p) => p.mac && p.mac === mac && p.status === 'paid');
|
||||
const firstPurchase = hist.length === 0;
|
||||
const registrationDate = (hist[0] && hist[0].createdAt) || new Date().toISOString();
|
||||
const lastPurchase = hist.length ? hist[hist.length - 1].paidAt || hist[hist.length - 1].createdAt : null;
|
||||
|
||||
const payment = {
|
||||
id: 'P' + Date.now() + genCode(4),
|
||||
@@ -67,7 +74,18 @@ app.post('/api/checkout', async (req, res) => {
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
const pix = await createPixPayment(plan.price, payment.id, `WiFi ${plan.name}`);
|
||||
const pix = await createPixPayment({
|
||||
amount: plan.price,
|
||||
reference: payment.id,
|
||||
planId: plan.id,
|
||||
planName: plan.name,
|
||||
payerName,
|
||||
payerCpf,
|
||||
firstPurchase,
|
||||
registrationDate,
|
||||
lastPurchase,
|
||||
deviceId,
|
||||
});
|
||||
payment.mpId = pix.mpId;
|
||||
payment.qrCodeBase64 = pix.qrCodeBase64;
|
||||
payment.copiaCola = pix.copiaCola;
|
||||
@@ -136,6 +154,27 @@ app.post('/api/admin/plans', adminAuth, (req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
app.get('/api/mp/public-key', (req, res) => {
|
||||
res.json({ publicKey: config.mpPublicKey || '' });
|
||||
});
|
||||
|
||||
app.post('/api/admin/payments/:id/cancel', adminAuth, async (req, res) => {
|
||||
const payment = getDb().payments.find((p) => p.id === req.params.id);
|
||||
if (!payment) return res.status(404).json({ error: 'nao encontrado' });
|
||||
if (!payment.mpId || payment.status !== 'pending') {
|
||||
return res.status(400).json({ error: 'apenas pendentes podem ser cancelados' });
|
||||
}
|
||||
try {
|
||||
await cancelOrder(payment.mpId);
|
||||
payment.status = 'cancelled';
|
||||
saveDb();
|
||||
console.log(`[CANCEL] order ${payment.mpId} cancelada`);
|
||||
res.json({ ok: true, status: payment.status });
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/health', async (req, res) => {
|
||||
res.json({ ok: true, mp: !!config.mpAccessToken && !config.mpAccessToken.includes('COLE_SEU'), time: new Date().toISOString() });
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user