fix: the Mercado Pago payer is the order's customer, with document and name, for PIX and card (checkout item 2)
All checks were successful
Build and deploy / Validate source (push) Successful in 1m42s
Build and deploy / Integration suite on a real stack (push) Successful in 8m39s
Build and deploy / Secret scan and release gate (push) Successful in 12s
Build and deploy / Publish images (push) Successful in 2m12s

Marcus, 2026-11-18: finance reconciles the payment by the customer and
the invoice is issued to them; half the payments showed 999.999.999-99
and no name. The payer now always carries the order's CPF or CNPJ, the
first and last name from the order and the e-mail. The card form's
document is no longer sent by the Site nor used (the cardholder's data
travel in the card token); the field stays accepted for pages loaded
before.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-23 10:33:29 -03:00
parent 177694edd3
commit 65f8dd211e
4 changed files with 35 additions and 21 deletions

View File

@@ -413,8 +413,9 @@ class PaymentMethod(StrictModel):
# Up to 3, as Dropstar sells; the card form offers no more. # Up to 3, as Dropstar sells; the card form offers no more.
installments: int = Field(default=1, ge=1, le=3, strict=True) installments: int = Field(default=1, ge=1, le=3, strict=True)
issuer_id: str | None = Field(default=None, max_length=40) issuer_id: str | None = Field(default=None, max_length=40)
# The cardholder's document from the card form: for a card, the payer is # The card form's document. No longer sent nor used: the payer is the
# the cardholder, not necessarily the company on the invoice. # order's customer (app/mercadopago.py payer, Marcus 2026-11-18); kept so a
# page loaded before the change still pays.
payer_document_type: Literal['CPF', 'CNPJ'] | None = None payer_document_type: Literal['CPF', 'CNPJ'] | None = None
payer_document: str | None = Field(default=None, pattern=r'^[0-9]{11,14}$') payer_document: str | None = Field(default=None, pattern=r'^[0-9]{11,14}$')

View File

@@ -41,6 +41,22 @@ PIX_MINUTES = 30
BRASILIA = timezone(timedelta(hours=-3)) BRASILIA = timezone(timedelta(hours=-3))
def payer(customer):
"""Who pays, as Dropstar's finance reconciles it and the invoice is issued
(Marcus, 2026-11-18): the customer's own CPF or CNPJ from the order, with
their name and e-mail, for PIX and card alike, never a placeholder such
as 999.999.999-99. The card form's document is not the payer: the
cardholder's data travel inside the card token."""
document = customer['cnpj']
who = {'email': customer['mail'],
'identification': {'type': 'CPF' if len(document) == 11 else 'CNPJ', 'number': document}}
names = (customer.get('name') or '').split()
if names:
who['first_name'] = names[0]
who['last_name'] = ' '.join(names[1:]) or names[0]
return who
class MercadoPagoPayment: class MercadoPagoPayment:
name = 'mercadopago' name = 'mercadopago'
@@ -76,9 +92,7 @@ class MercadoPagoPayment:
body = {'transaction_amount': float(Decimal(total_cents) / 100), body = {'transaction_amount': float(Decimal(total_cents) / 100),
'description': f'DTF - cotação {quote_id[:8]}', 'description': f'DTF - cotação {quote_id[:8]}',
'external_reference': quote_id, 'external_reference': quote_id,
'payer': {'email': customer['mail'], 'payer': payer(customer)}
'identification': {'type': 'CPF' if len(customer['cnpj']) == 11 else 'CNPJ',
'number': customer['cnpj']}}}
if self.notification_url: if self.notification_url:
body['notification_url'] = self.notification_url body['notification_url'] = self.notification_url
expires_at = None expires_at = None
@@ -93,9 +107,6 @@ class MercadoPagoPayment:
# it; asking it of every card was refused as a rule (10113). # it; asking it of every card was refused as a rule (10113).
if method['payment_method_id'].startswith('deb'): if method['payment_method_id'].startswith('deb'):
body['three_d_secure_mode'] = 'optional' body['three_d_secure_mode'] = 'optional'
if method.get('payer_document_type') and method.get('payer_document'):
body['payer']['identification'] = {'type': method['payer_document_type'],
'number': method['payer_document']}
# No issuer_id: Mercado Pago takes the issuer from the card number. # No issuer_id: Mercado Pago takes the issuer from the card number.
# The form's suggestion was refused for its own test cards # The form's suggestion was refused for its own test cards
# (10111, "the issuer does not have the BIN configured"). # (10111, "the issuer does not have the BIN configured").

View File

@@ -137,6 +137,11 @@ class MercadoPagoTests(unittest.TestCase):
self.mp.create('22222222-2222-3333-4444-555555555555', 1000, {'mail': 'a@example.test', 'cnpj': '52998224725'}) self.mp.create('22222222-2222-3333-4444-555555555555', 1000, {'mail': 'a@example.test', 'cnpj': '52998224725'})
person = json.loads(self.requests[-1].content) person = json.loads(self.requests[-1].content)
self.assertEqual(person['payer']['identification'], {'type': 'CPF', 'number': '52998224725'}) self.assertEqual(person['payer']['identification'], {'type': 'CPF', 'number': '52998224725'})
# A one-word name is both first and last name.
self.mp.create('33333333-2222-3333-4444-555555555555', 1000,
{'mail': 'a@example.test', 'cnpj': '52998224725', 'name': 'Kenia'})
self.assertEqual({k: v for k, v in json.loads(self.requests[-1].content)['payer'].items() if 'name' in k},
{'first_name': 'Kenia', 'last_name': 'Kenia'})
self.assertEqual((created['pix_qr_code'], created['status']), ('000201PIX', 'pending')) self.assertEqual((created['pix_qr_code'], created['status']), ('000201PIX', 'pending'))
# The code expires in 30 minutes, in the format Mercado Pago documents. # The code expires in 30 minutes, in the format Mercado Pago documents.
expires = datetime.fromisoformat(body['date_of_expiration']) expires = datetime.fromisoformat(body['date_of_expiration'])
@@ -161,15 +166,20 @@ class MercadoPagoTests(unittest.TestCase):
{'type': 'card', 'token': 'tok_def', 'payment_method_id': 'visa'}) {'type': 'card', 'token': 'tok_def', 'payment_method_id': 'visa'})
self.assertNotEqual(self.requests[-1].headers['x-idempotency-key'], first_key) self.assertNotEqual(self.requests[-1].headers['x-idempotency-key'], first_key)
self.assertTrue(first_key.startswith('dtf-quote-q-card-')) self.assertTrue(first_key.startswith('dtf-quote-q-card-'))
# 3-D Secure is asked of debit only; the cardholder is the payer. # 3-D Secure is asked of debit only. The payer is the order's customer,
# never the card form's document (Marcus, 2026-11-18): finance
# reconciles by it and the invoice is issued to it.
self.assertNotIn('three_d_secure_mode', body) self.assertNotIn('three_d_secure_mode', body)
self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'}, self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181', 'name': 'Maria da Silva'},
{'type': 'card', 'token': 'tok_deb', 'payment_method_id': 'debmaster', {'type': 'card', 'token': 'tok_deb', 'payment_method_id': 'debmaster',
'payer_document_type': 'CPF', 'payer_document': '12345678909'}) 'payer_document_type': 'CPF', 'payer_document': '99999999999'})
debit = json.loads(self.requests[-1].content) debit = json.loads(self.requests[-1].content)
self.assertEqual(debit['three_d_secure_mode'], 'optional') self.assertEqual(debit['three_d_secure_mode'], 'optional')
self.assertEqual(debit['payer']['identification'], {'type': 'CPF', 'number': '12345678909'}) self.assertEqual(debit['payer'], {'email': 'a@example.test', 'first_name': 'Maria', 'last_name': 'da Silva',
'identification': {'type': 'CNPJ', 'number': '11222333000181'}})
self.assertEqual(body['payer']['identification'], {'type': 'CNPJ', 'number': '11222333000181'}) self.assertEqual(body['payer']['identification'], {'type': 'CNPJ', 'number': '11222333000181'})
# An order from before the Site asked the name sends none.
self.assertNotIn('first_name', body['payer'])
self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'}, self.mp.create('q', 1000, {'mail': 'a@example.test', 'cnpj': '11222333000181'},
{'type': 'card', 'token': 'tok_iss', 'payment_method_id': 'master', 'issuer_id': '24'}) {'type': 'card', 'token': 'tok_iss', 'payment_method_id': 'master', 'issuer_id': '24'})
self.assertNotIn('issuer_id', json.loads(self.requests[-1].content)) self.assertNotIn('issuer_id', json.loads(self.requests[-1].content))

View File

@@ -784,13 +784,6 @@
}); });
return sdkLoading; return sdkLoading;
} }
// The cardholder's document as the card form collected it.
function cardholder(id) {
const number=String(id?.number||'').replace(/\D/g,'');
const type=String(id?.type||'').toUpperCase();
return ['CPF','CNPJ'].includes(type) && /^[0-9]{11,14}$/.test(number)
? {payer_document_type:type, payer_document:number} : {};
}
// Each mount of the card form has its own number and its own holder. One // 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, // 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 // is stale: Mercado Pago no longer finds its fields, so its callbacks are
@@ -834,8 +827,7 @@
result=await api('/payments/intent',{quote_id:draftId,method:{ result=await api('/payments/intent',{quote_id:draftId,method:{
type:'card', token:data.token, payment_method_id:data.payment_method_id, type:'card', token:data.token, payment_method_id:data.payment_method_id,
installments:Number(data.installments)||1, installments:Number(data.installments)||1,
issuer_id:data.issuer_id==null?null:String(data.issuer_id), issuer_id:data.issuer_id==null?null:String(data.issuer_id)}});
...cardholder(data.payer?.identification)}});
} catch(error) { } catch(error) {
// Rejecting stops the form's spinner; the reason is shown above it. // Rejecting stops the form's spinner; the reason is shown above it.
message(error.message || 'Não foi possível concluir o pagamento. Tente de novo.','error'); message(error.message || 'Não foi possível concluir o pagamento. Tente de novo.','error');