From 65f8dd211e3c40aeab18200623b19174c64cfbff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=AA=20Faleiros?= Date: Mon, 23 Nov 2026 10:33:29 -0300 Subject: [PATCH] fix: the Mercado Pago payer is the order's customer, with document and name, for PIX and card (checkout item 2) 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) --- app/core/models.py | 5 +++-- app/mercadopago.py | 23 +++++++++++++++++------ tests/test_mercadopago.py | 18 ++++++++++++++---- web/checkout.js | 10 +--------- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/app/core/models.py b/app/core/models.py index 06556c7..a3308eb 100644 --- a/app/core/models.py +++ b/app/core/models.py @@ -413,8 +413,9 @@ class PaymentMethod(StrictModel): # Up to 3, as Dropstar sells; the card form offers no more. installments: int = Field(default=1, ge=1, le=3, strict=True) 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 cardholder, not necessarily the company on the invoice. + # The card form's document. No longer sent nor used: the payer is the + # 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: str | None = Field(default=None, pattern=r'^[0-9]{11,14}$') diff --git a/app/mercadopago.py b/app/mercadopago.py index 8f1db00..500e37f 100644 --- a/app/mercadopago.py +++ b/app/mercadopago.py @@ -41,6 +41,22 @@ PIX_MINUTES = 30 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: name = 'mercadopago' @@ -76,9 +92,7 @@ class MercadoPagoPayment: body = {'transaction_amount': float(Decimal(total_cents) / 100), 'description': f'DTF - cotação {quote_id[:8]}', 'external_reference': quote_id, - 'payer': {'email': customer['mail'], - 'identification': {'type': 'CPF' if len(customer['cnpj']) == 11 else 'CNPJ', - 'number': customer['cnpj']}}} + 'payer': payer(customer)} if self.notification_url: body['notification_url'] = self.notification_url expires_at = None @@ -93,9 +107,6 @@ class MercadoPagoPayment: # it; asking it of every card was refused as a rule (10113). if method['payment_method_id'].startswith('deb'): 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. # The form's suggestion was refused for its own test cards # (10111, "the issuer does not have the BIN configured"). diff --git a/tests/test_mercadopago.py b/tests/test_mercadopago.py index 8638627..7264f05 100644 --- a/tests/test_mercadopago.py +++ b/tests/test_mercadopago.py @@ -137,6 +137,11 @@ class MercadoPagoTests(unittest.TestCase): self.mp.create('22222222-2222-3333-4444-555555555555', 1000, {'mail': 'a@example.test', 'cnpj': '52998224725'}) person = json.loads(self.requests[-1].content) 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')) # The code expires in 30 minutes, in the format Mercado Pago documents. expires = datetime.fromisoformat(body['date_of_expiration']) @@ -161,15 +166,20 @@ class MercadoPagoTests(unittest.TestCase): {'type': 'card', 'token': 'tok_def', 'payment_method_id': 'visa'}) self.assertNotEqual(self.requests[-1].headers['x-idempotency-key'], first_key) 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.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', - 'payer_document_type': 'CPF', 'payer_document': '12345678909'}) + 'payer_document_type': 'CPF', 'payer_document': '99999999999'}) debit = json.loads(self.requests[-1].content) 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'}) + # 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'}, {'type': 'card', 'token': 'tok_iss', 'payment_method_id': 'master', 'issuer_id': '24'}) self.assertNotIn('issuer_id', json.loads(self.requests[-1].content)) diff --git a/web/checkout.js b/web/checkout.js index 9b3ca5b..83554bc 100644 --- a/web/checkout.js +++ b/web/checkout.js @@ -784,13 +784,6 @@ }); 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 // 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 @@ -834,8 +827,7 @@ result=await api('/payments/intent',{quote_id:draftId,method:{ type:'card', token:data.token, payment_method_id:data.payment_method_id, installments:Number(data.installments)||1, - issuer_id:data.issuer_id==null?null:String(data.issuer_id), - ...cardholder(data.payer?.identification)}}); + issuer_id:data.issuer_id==null?null:String(data.issuer_id)}}); } catch(error) { // 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');