feat: qualidade MP - items/payer/statement_descriptor/deviceId/cancel + form CPF no portal
build-image / build-push (push) Successful in 1m36s

This commit is contained in:
jfeng committed 2026-10-21 21:47:16 -03:00
1 parent 67608421b4
commit 59d6aa0ed3
4 files changed
+250 -12

No files matched your search

+118 -2
View File
@@ -9,6 +9,9 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Manrope:wght@500;600;700;800&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<!-- Mercado Pago: SDK JS V2 + Device ID (gera MP_DEVICE_SESSION_ID) -->
<script src="https://sdk.mercadopago.com/js/v2" defer></script>
<script src="https://www.mercadopago.com.br/integrations/v1/security-code.js" async crossorigin="anonymous"></script>
<style>
:root {
--graphite: #101820;
@@ -407,6 +410,50 @@
padding: 12px 0;
}
.field {
display: block;
margin-bottom: 14px;
}
.field span {
display: block;
font-size: 0.8rem;
font-weight: 600;
color: var(--gray-2);
margin-bottom: 6px;
letter-spacing: 0.02em;
text-transform: uppercase;
}
.field input {
width: 100%;
padding: 12px 14px;
font-family: var(--font-body);
font-size: 0.95rem;
color: var(--white);
background: var(--graphite-2);
border: 1px solid var(--line);
border-radius: var(--radius-sm);
outline: none;
transition: border-color 0.15s ease;
}
.field input:focus { border-color: var(--blue); }
.field input::placeholder { color: var(--gray); }
.mp-badge {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
margin-top: 14px;
font-size: 0.78rem;
color: var(--gray-2);
}
.mp-badge svg { flex: 0 0 auto; }
.skel {
height: 68px;
border-radius: var(--radius-md);
@@ -439,6 +486,29 @@
</div>
</main>
<!-- ===== DADOS DO PAGADOR ===== -->
<section id="view-payer" class="card hidden" aria-labelledby="payerTitle">
<div class="pay-head">
<h2 id="payerTitle">Seus dados</h2>
<p>Para emissão do PIX, informe nome e CPF</p>
</div>
<label class="field">
<span>Nome completo</span>
<input id="payerName" type="text" autocomplete="name" maxlength="80" placeholder="Ex.: João da Silva">
</label>
<label class="field">
<span>CPF</span>
<input id="payerCpf" type="text" inputmode="numeric" maxlength="14" placeholder="000.000.000-00">
</label>
<div class="row">
<button class="btn btn-ghost" id="payerBack" type="button">← Voltar</button>
<button class="btn btn-primary" id="payerNext" type="button">Gerar PIX</button>
</div>
</section>
<!-- ===== PAGAMENTO ===== -->
<section id="view-pay" class="card hidden" aria-labelledby="payTitle">
<div class="pay-head">
@@ -461,6 +531,14 @@
<span class="dot"></span><span id="payPillText">Aguardando pagamento…</span>
</div>
<div class="mp-badge">
<svg width="18" height="18" viewBox="0 0 24 24" aria-hidden="true">
<path fill="#009EE3" d="M12 2L3 6v6c0 5 3.8 9.4 9 10 5.2-.6 9-5 9-10V6l-9-4z"/>
<path fill="#00B1EA" d="M12 4.2L5 7.3v4.7c0 3.9 2.9 7.4 7 8 4.1-.6 7-4.1 7-8V7.3l-7-3.1z" opacity=".5"/>
</svg>
<span>Pagamento processado com segurança por <strong>Mercado Pago</strong></span>
</div>
<a class="back" href="#" id="backBtn">← Voltar aos planos</a>
</section>
@@ -507,9 +585,38 @@
let currentPaymentId = null;
let pollTimer = null;
let toastTimer = null;
let selectedPlanId = null;
let mpDevice = null;
const $ = (id) => document.getElementById(id);
// ===== Mercado Pago SDK V2 + Device ID =====
async function initMpSdk() {
try {
const res = await fetch('/api/mp/public-key');
const { publicKey } = await res.json();
if (publicKey && window.MercadoPago) {
const mp = new window.MercadoPago(publicKey);
if (typeof mp.getId === 'function') {
mpDevice = await Promise.resolve(mp.getId()).catch(() => null);
}
}
} catch { /* segue sem device id */ }
}
window.addEventListener('load', initMpSdk);
function deviceId() {
return mpDevice || window.MP_DEVICE_SESSION_ID || null;
}
function maskCpf(v) {
const d = v.replace(/\D/g, '').slice(0, 11);
return d
.replace(/^(\d{3})(\d)/, '$1.$2')
.replace(/^(\d{3})\.(\d{3})(\d)/, '$1.$2.$3')
.replace(/\.(\d{3})(\d{1,2})$/, '.$1-$2');
}
function toast(msg) {
const t = $('toast');
t.textContent = msg;
@@ -535,6 +642,7 @@
function show(view) {
$('view-plans').classList.toggle('hidden', view !== 'plans');
$('view-payer').classList.toggle('hidden', view !== 'payer');
$('view-pay').classList.toggle('hidden', view !== 'pay');
$('view-voucher').classList.toggle('hidden', view !== 'voucher');
if (view !== 'pay' && pollTimer) { clearInterval(pollTimer); pollTimer = null; }
@@ -565,7 +673,7 @@
btn.querySelector('h3').textContent = p.name;
btn.querySelector('.plan-meta').textContent = p.speed + ' · validade ' + p.uptime;
btn.querySelector('.plan-price').textContent = money(p.price);
btn.addEventListener('click', () => checkout(p.id));
btn.addEventListener('click', () => { selectedPlanId = p.id; show('payer'); });
box.appendChild(btn);
}
} catch {
@@ -575,10 +683,15 @@
async function checkout(planId) {
try {
const payerName = $('payerName').value.trim();
const payerCpf = $('payerCpf').value.trim();
if (payerName.length < 3) { toast('Informe seu nome completo'); show('payer'); return; }
if (payerCpf.replace(/\D/g, '').length !== 11) { toast('Informe um CPF com 11 dígitos'); show('payer'); return; }
const res = await fetch('/api/checkout', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ planId, mac }),
body: JSON.stringify({ planId, mac, payerName, payerCpf, deviceId: deviceId() }),
});
const data = await res.json();
if (data.error) throw new Error(data.error);
@@ -638,6 +751,9 @@
);
$('backBtn').addEventListener('click', (e) => { e.preventDefault(); show('plans'); });
$('backBtn2').addEventListener('click', (e) => { e.preventDefault(); show('plans'); });
$('payerBack').addEventListener('click', () => show('plans'));
$('payerNext').addEventListener('click', () => checkout(selectedPlanId));
$('payerCpf').addEventListener('input', (e) => { e.target.value = maskCpf(e.target.value); });
loadPlans();
</script>