From 6ee6f4d08b69069f89dccc0aaba6bdbae15c6bf4 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Mon, 30 Sep 2024 17:48:57 -0300 Subject: [PATCH] fix(modules): Ensure BR phone numbers formatted up to 11 digits on checkout To handle masked info (partial login) edited on checkout --- packages/apps/pagarme/src/pagarme-create-transaction.ts | 9 +++++++-- packages/modules/src/firebase/checkout.ts | 7 +++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/apps/pagarme/src/pagarme-create-transaction.ts b/packages/apps/pagarme/src/pagarme-create-transaction.ts index 6c573d7d4..3d279ceb7 100644 --- a/packages/apps/pagarme/src/pagarme-create-transaction.ts +++ b/packages/apps/pagarme/src/pagarme-create-transaction.ts @@ -144,13 +144,18 @@ export default async (modBody: AppModuleBody<'create_transaction'>) => { external_id: buyer.customer_id, type: buyer.registry_type === 'j' ? 'corporation' : 'individual', country: (buyer.doc_country || 'BR').toLowerCase(), - phone_numbers: [`+${(buyer.phone.country_code || '55')}${buyer.phone.number}`], documents: [{ type: buyer.registry_type === 'j' ? 'cnpj' : 'cpf', number: buyer.doc_number, }], }; - const birthDate = buyer.birth_date; + const { phone, birth_date: birthDate } = buyer; + if (phone?.number) { + pagarmeTransaction.customer.phone_numbers = [ + `+${(phone.country_code || '55')}` + + `${phone.number.substring(phone.number.length - 11)}`, + ]; + } if (birthDate && birthDate.year && birthDate.day && birthDate.month) { const yearDiff = date.getFullYear() - birthDate.year; if ( diff --git a/packages/modules/src/firebase/checkout.ts b/packages/modules/src/firebase/checkout.ts index c5111d8e6..b88539c5d 100644 --- a/packages/modules/src/firebase/checkout.ts +++ b/packages/modules/src/firebase/checkout.ts @@ -84,6 +84,13 @@ export default async (req: Request, res: Response) => { customer.phones = savedCustomer.phones; } } + customer.phones?.forEach((phone) => { + if (phone.country_code === 55 || !phone.country_code) { + // BR + if (phone.number.length <= 11) return; + phone.number = `${phone.number.substring(phone.number.length - 11)}`; + } + }); customer._id = customerId; const fixMaskedAddr = (bodyAddr: typeof body.shipping.to) => { if (bodyAddr.line_address?.includes('***') || bodyAddr.name?.includes('***')) {