fix: a card form still mounting when the customer picks PIX is dropped quietly (DTF-WEB-T)
All checks were successful
Build and deploy / Validate source (push) Successful in 5s
Build and deploy / Integration suite on a real stack (push) Successful in 6m47s
Build and deploy / Secret scan and release gate (push) Successful in 6s
Build and deploy / Publish images (push) Successful in 1m9s

Clicking PIX while Mercado Pago's card form was still mounting removed its
holder; the mount went on, found no fields and its onError showed "Erro no
formulário do cartão". Each mount now has its own number and holder id;
a stale mount's callbacks are ignored and the late form is unmounted when
it arrives. A mount that fails on the current form says so in red.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-17 10:11:23 -03:00
parent b6837023bf
commit 5d9a78b63c

View File

@@ -772,23 +772,31 @@
return ['CPF','CNPJ'].includes(type) && /^[0-9]{11,14}$/.test(number) return ['CPF','CNPJ'].includes(type) && /^[0-9]{11,14}$/.test(number)
? {payer_document_type:type, payer_document:number} : {}; ? {payer_document_type:type, payer_document:number} : {};
} }
let cardBrick=null; // Each mount of the card form has its own number and its own holder. One
// still mounting when the customer switched to PIX, or the page re-rendered,
// is stale: Mercado Pago no longer finds its fields, so its callbacks are
// ignored and it is taken down once it arrives (Sentry DTF-WEB-T).
let cardBrick=null, cardMount=0;
function unmountCard() { function unmountCard() {
cardMount++;
if (cardBrick) { try { cardBrick.unmount(); } catch(_) {} cardBrick=null; } if (cardBrick) { try { cardBrick.unmount(); } catch(_) {} cardBrick=null; }
} }
async function showCard(approved, into, version) { async function showCard(approved, into, version) {
unmountCard();
const mount=cardMount;
const holder=node('div'); const holder=node('div');
holder.id='cardPaymentBrick'; holder.id='cardPaymentBrick-'+mount;
const live=()=>mount===cardMount && holder.isConnected && version===refreshVersion;
const espera=node('div',null,'skel skelBloco'); const espera=node('div',null,'skel skelBloco');
into.append(espera,holder); into.append(espera,holder);
try { await loadMercadoPago(); } try { await loadMercadoPago(); }
catch(error) { espera.remove(); message(error.message,'error'); return; } catch(error) { espera.remove(); message(error.message,'error'); return; }
const session=await ready; const session=await ready;
// The customer may have switched to PIX, or the page re-rendered, meanwhile. if (!live()) return;
if (!holder.isConnected || version!==refreshVersion) return;
unmountCard();
const mp=new window.MercadoPago(session.payment_public_key,{locale:'pt-BR'}); const mp=new window.MercadoPago(session.payment_public_key,{locale:'pt-BR'});
cardBrick=await mp.bricks().create('cardPayment','cardPaymentBrick',{ let brick;
try {
brick=await mp.bricks().create('cardPayment',holder.id,{
initialization:{amount:approved.total_cents/100, payer:{email:approved.customer.mail}}, initialization:{amount:approved.total_cents/100, payer:{email:approved.customer.mail}},
// Credit cards only, in up to 3 installments, as Dropstar decided. // Credit cards only, in up to 3 installments, as Dropstar decided.
customization:{paymentMethods:{maxInstallments:3,types:{excluded:['debit_card']}}, customization:{paymentMethods:{maxInstallments:3,types:{excluded:['debit_card']}},
@@ -797,8 +805,8 @@
visual:{hideFormTitle:true, visual:{hideFormTitle:true,
style:{customVariables:{baseColor:'#FFA81A',buttonTextColor:'#03060B'}}}}, style:{customVariables:{baseColor:'#FFA81A',buttonTextColor:'#03060B'}}}},
callbacks:{ callbacks:{
onReady:()=>{ espera.remove(); }, onReady:()=>{ if (live()) espera.remove(); },
onError:error=>{ console.error(error); message('Erro no formulário do cartão. Confira os dados e tente de novo.','error'); }, onError:error=>{ if (!live()) return; console.error(error); message('Erro no formulário do cartão. Confira os dados e tente de novo.','error'); },
onSubmit:async data=>{ onSubmit:async data=>{
if (!cartOk()) { await refresh(); throw new Error('cart changed'); } if (!cartOk()) { await refresh(); throw new Error('cart changed'); }
message(''); message('');
@@ -829,7 +837,12 @@
} }
} }
}); });
if (!holder.isConnected || version!==refreshVersion) unmountCard(); } catch(error) {
if (!live()) return;
espera.remove(); message('Não foi possível abrir o formulário do cartão. Recarregue a página ou pague com PIX.','error'); return;
}
if (!live()) { try { brick.unmount(); } catch(_) {} return; }
cardBrick=brick;
} }
const quoteFromPortal=new URLSearchParams(location.search).get('quote'); const quoteFromPortal=new URLSearchParams(location.search).get('quote');
if(quoteFromPortal && /^[0-9a-f-]{36}$/.test(quoteFromPortal)){ if(quoteFromPortal && /^[0-9a-f-]{36}$/.test(quoteFromPortal)){