diff --git a/src/providers/boleto-api-caixa/index.js b/src/providers/boleto-api-caixa/index.js index abcc2c4..74f4555 100644 --- a/src/providers/boleto-api-caixa/index.js +++ b/src/providers/boleto-api-caixa/index.js @@ -8,6 +8,7 @@ const { find, propEq, pipe, + equals, } = require('ramda') const { encodeBase64 } = require('../../lib/encoding') @@ -20,6 +21,7 @@ const { } = require('./formatter') const config = require('../../config/providers') +const { getPagarmeAddress } = require('../../resources/boleto/model') const { boleto_api_password: boletoApiPassword, @@ -114,6 +116,30 @@ const defineInterest = (boleto) => { return null } +const buyerAddressIsEqualsToPagarme = boleto => + equals(boleto.payer_address, boleto.company_address) + +const buyerAddressIsEqualsToCompany = boleto => + equals(boleto.payer_address, getPagarmeAddress()) + +const defineBuyerAddress = (boleto) => { + if ( + buyerAddressIsEqualsToCompany(boleto) || + buyerAddressIsEqualsToPagarme(boleto) + ) { + return undefined + } + return { + street: path(['payer_address', 'street'], boleto), + number: String(path(['payer_address', 'street_number'], boleto)), + complement: path(['payer_address', 'complementary'], boleto), + district: path(['payer_address', 'neighborhood'], boleto), + zipCode: String(path(['payer_address', 'zipcode'], boleto)), + city: path(['payer_address', 'city'], boleto), + stateCode: formatStateCode(boleto, 'payer_address'), + } +} + const defineFees = (boleto) => { const fine = defineFine(boleto) const interest = defineInterest(boleto) @@ -139,7 +165,6 @@ const getInstructions = (companyName, companyDocument, boleto) => { const buildPayload = (boleto, operationId) => { const formattedExpirationDate = formatDate(boleto.expiration_date) const formattedRecipientStateCode = formatStateCode(boleto, 'company_address') - const formattedBuyerStateCode = formatStateCode(boleto, 'payer_address') const companyName = pathOr('', ['company_name'], boleto) const companyDocument = pathOr('', ['company_document_number'], boleto) @@ -189,15 +214,7 @@ const buildPayload = (boleto, operationId) => { number: String(path(['payer_document_number'], boleto)), type: path(['payer_document_type'], boleto).toUpperCase(), }, - address: { - street: path(['payer_address', 'street'], boleto), - number: String(path(['payer_address', 'street_number'], boleto)), - complement: path(['payer_address', 'complementary'], boleto), - district: path(['payer_address', 'neighborhood'], boleto), - zipCode: String(path(['payer_address', 'zipcode'], boleto)), - city: path(['payer_address', 'city'], boleto), - stateCode: formattedBuyerStateCode, - }, + address: defineBuyerAddress(boleto), }, requestKey: operationId, } diff --git a/src/resources/boleto/model.js b/src/resources/boleto/model.js index 31dcdb2..dfd391d 100644 --- a/src/resources/boleto/model.js +++ b/src/resources/boleto/model.js @@ -97,8 +97,8 @@ const addBoletoCode = (boleto) => { }) } -const validateModel = (boleto) => { - const defaultAddress = { +const getPagarmeAddress = () => ( + { zipcode: '04551010', street: 'Rua Fidêncio Ramos', street_number: '308', @@ -107,6 +107,10 @@ const validateModel = (boleto) => { city: 'São Paulo', state: 'SP', } +) + +const validateModel = (boleto) => { + const defaultAddress = getPagarmeAddress() const requiredAddressFields = [ 'zipcode', @@ -319,4 +323,5 @@ module.exports = { buildModelResponse, validateModel, create, + getPagarmeAddress, } diff --git a/test/unit/providers/boleto-api-caixa/index.js b/test/unit/providers/boleto-api-caixa/index.js index c2b8b64..9ec5a68 100644 --- a/test/unit/providers/boleto-api-caixa/index.js +++ b/test/unit/providers/boleto-api-caixa/index.js @@ -12,6 +12,9 @@ import { import { getDocumentType, } from '../../../../src/providers/boleto-api-caixa/formatter' +import { + getPagarmeAddress, +} from '../../../../src/resources/boleto/model' const noStrictRules = { @@ -113,15 +116,7 @@ test('buildPayload with payer_address incomplete', async (t) => { const boleto = await createBoleto() const payload = buildPayload(boleto, operationId) - t.deepEqual(payload.buyer.address, { - street: 'Rua Fidêncio Ramos', - number: '308', - complement: '9º andar, conjunto 91', - zipCode: '04551010', - district: 'Vila Olímpia', - city: 'São Paulo', - stateCode: 'SP', - }) + t.deepEqual(payload.buyer.address, undefined) }) test('buildPayload with the instruction field filled should return the received instruction with the default instruction', async (t) => { @@ -405,7 +400,18 @@ test('buildPayload with interest per percentage and amount should return the int test('buildPayload complete', async (t) => { const operationId = cuid() - const boleto = await createBoleto({ company_name: 'company name test' }) + const boleto = await createBoleto({ + company_name: 'company name test', + payer_address: { + zipcode: '5555555', + street_number: '308', + street: 'Rua dos bancos', + complementary: '11º andar', + neighborhood: 'Brooklin', + city: 'São Paulo', + state: 'SP', + }, + }) const payload = buildPayload(boleto, operationId) let { instructions } = boleto @@ -473,11 +479,11 @@ test('buildPayload complete', async (t) => { number: boleto.payer_document_number, }, address: { - street: 'Rua Fidêncio Ramos', + zipCode: '5555555', number: '308', - complement: '9º andar, conjunto 91', - zipCode: '04551010', - district: 'Vila Olímpia', + street: 'Rua dos bancos', + complement: '11º andar', + district: 'Brooklin', city: 'São Paulo', stateCode: 'SP', }, @@ -486,6 +492,75 @@ test('buildPayload complete', async (t) => { }) }) +test('when payer_address is different than company_address and pagarme address then address should be buyer address', async (t) => { + const operationId = cuid() + const boleto = await createBoleto({ + company_name: 'company name test', + payer_address: { + zipcode: '5555555', + street_number: '308', + street: 'Rua dos bancos', + complementary: '11º andar', + neighborhood: 'Brooklin', + city: 'São Paulo', + state: 'SP', + }, + }) + + const payload = buildPayload(boleto, operationId) + + t.deepEqual(payload.buyer.address, { + zipCode: '5555555', + number: '308', + street: 'Rua dos bancos', + complement: '11º andar', + district: 'Brooklin', + city: 'São Paulo', + stateCode: 'SP', + }) +}) + +test('when payer_address is equals than company_address then buyer address should be undefined', async (t) => { + const operationId = cuid() + const boleto = await createBoleto({ + company_name: 'company name test', + payer_address: { + zipcode: '5555555', + street_number: '308', + street: 'Rua dos bancos', + complementary: '11º andar', + neighborhood: 'Brooklin', + city: 'São Paulo', + state: 'SP', + }, + company_address: { + zipcode: '5555555', + street_number: '308', + street: 'Rua dos bancos', + complementary: '11º andar', + neighborhood: 'Brooklin', + city: 'São Paulo', + state: 'SP', + }, + }) + + const payload = buildPayload(boleto, operationId) + + t.deepEqual(payload.buyer.address, undefined) +}) + +test('when payer_address is equals than address pagarme then buyer address should be undefined', async (t) => { + const operationId = cuid() + const boleto = await createBoleto({ + company_name: 'company name test', + payer_address: getPagarmeAddress(), + }) + + const payload = buildPayload(boleto, operationId) + + t.deepEqual(payload.buyer.address, undefined) +}) + test('buildPayload with payeeGuarantor fields null', async (t) => { const operationId = cuid() const boleto = await createBoleto({