From 5f65029bcbc954d6a10f0aba8d6ec31242f47333 Mon Sep 17 00:00:00 2001 From: Wisley Alves Date: Mon, 11 Sep 2023 16:28:45 -0300 Subject: [PATCH] fix(banrisul): add route for request download and return for approval --- functions/lib/banrisul/auth/create-access.js | 2 +- .../lib/banrisul/calculate-our-number.js | 6 ++- functions/lib/banrisul/payload-to-billet.js | 4 +- .../routes/ecom/modules/create-transaction.js | 39 +++++++++++++------ functions/routes/homologacao.js | 15 +++++++ hosting/description.md | 4 +- 6 files changed, 52 insertions(+), 18 deletions(-) create mode 100644 functions/routes/homologacao.js diff --git a/functions/lib/banrisul/auth/create-access.js b/functions/lib/banrisul/auth/create-access.js index ee5300b..758dc91 100644 --- a/functions/lib/banrisul/auth/create-access.js +++ b/functions/lib/banrisul/auth/create-access.js @@ -15,7 +15,7 @@ module.exports = function (clientId, clientSecret, storeId, isSandbox, firestore this.preparing = new Promise((resolve, reject) => { const authenticate = (accessToken, documentRef) => { self.axios = createAxios(accessToken, false, isSandbox) - // self.documentRef = documentRef + self.documentRef = documentRef if (documentRef) { documentRef .set({ accessToken, isSandbox }, { merge: true }) diff --git a/functions/lib/banrisul/calculate-our-number.js b/functions/lib/banrisul/calculate-our-number.js index 9894b47..5ff1e6a 100644 --- a/functions/lib/banrisul/calculate-our-number.js +++ b/functions/lib/banrisul/calculate-our-number.js @@ -41,11 +41,13 @@ const mod11 = (number, mod10) => { return 11 - rest } -const ourNumber = (number) => { +const ourNumber = (number, isSandbox, storeId, orderId) => { const dvMod10 = mod10(number) const dvMod11 = mod11(number, dvMod10) + // console.log(`>> number: ${number} mod10: ${dvMod10} mod11: ${dvMod11} <= ${storeId} #${orderId}`) + const ourNumber = isSandbox ? '264' : `${number}${dvMod10}${dvMod11}` - return `${number}${dvMod10}${dvMod11}`.padStart(10, '0') + return ourNumber.padStart(10, '0') } module.exports = ourNumber diff --git a/functions/lib/banrisul/payload-to-billet.js b/functions/lib/banrisul/payload-to-billet.js index 2294c6e..4909ef0 100644 --- a/functions/lib/banrisul/payload-to-billet.js +++ b/functions/lib/banrisul/payload-to-billet.js @@ -20,7 +20,7 @@ const abbreviateAddress = (address) => { .substring(0, 35) } -const createBodyToBillet = (appData, params) => { +const createBodyToBillet = (appData, params, ourNumber) => { const { amount, buyer, to } = params const { fees, // Juros @@ -52,7 +52,7 @@ const createBodyToBillet = (appData, params) => { codigo: 3 }, valor_nominal: (amount.total).toFixed(2), - // nosso_numero: ourNumber, // OBS.: in case it is necessary to calculate our number + nosso_numero: ourNumber, // OBS.: in case it is necessary to calculate our number seu_numero: Date.now().toString() } diff --git a/functions/routes/ecom/modules/create-transaction.js b/functions/routes/ecom/modules/create-transaction.js index 684da2e..ec217d5 100644 --- a/functions/routes/ecom/modules/create-transaction.js +++ b/functions/routes/ecom/modules/create-transaction.js @@ -1,6 +1,6 @@ const { baseUri } = require('../../../__env') const Banrisul = require('../../../lib/banrisul/auth/create-access') -// const getOurNumber = require('../../../lib/banrisul/calculate-our-number') +const getOurNumber = require('../../../lib/banrisul/calculate-our-number') const { createBodyToBillet } = require('../../../lib/banrisul/payload-to-billet') exports.post = async ({ appSdk, admin }, req, res) => { @@ -46,26 +46,29 @@ exports.post = async ({ appSdk, admin }, req, res) => { await banrisul.preparing // OBS.: in case it is necessary to calculate our number - // const documentRef = banrisul.documentRef && await banrisul.documentRef.get() - // const docAuthBarisul = documentRef?.data() - // const lastBilletNumber = (docAuthBarisul?.lastBilletNumber || 0) + 1 + const documentRef = banrisul.documentRef && await banrisul.documentRef.get() + const docAuthBarisul = documentRef?.data() + const lastBilletNumber = (docAuthBarisul?.lastBilletNumber || 0) + 1 + const banrisulAxios = banrisul.axios - // const ourNumber = getOurNumber(lastBilletNumber) - const body = createBodyToBillet(appData, params) + const ourNumber = getOurNumber(lastBilletNumber, appData.envoriment === 'teste', storeId, orderId) + const body = createBodyToBillet(appData, params, ourNumber) console.log('>>body ', JSON.stringify(body)) redirectToPayment = false - // test - // const data = require('../../../lib/billet/billet-test') if (!appData.beneficiary_code && appData.envoriment !== 'teste') { throw new Error('Beneficiary code not found') } + // test + // const data = require('../../../lib/billet/billet-test') + + const beneficiaryCode = appData.envoriment === 'teste' ? '0010000001088' : appData.beneficiary_code const { data } = await banrisulAxios.post('/boletos', body, { headers: { - 'bergs-beneficiario': appData.envoriment === 'teste' ? '0010000001088' : appData.beneficiary_code + 'bergs-beneficiario': beneficiaryCode } }) @@ -96,9 +99,23 @@ exports.post = async ({ appSdk, admin }, req, res) => { await collectionBillet.doc(orderId).set({ ...data, storeId, envoriment: appData.envoriment }) + if (appData.envoriment && appData.envoriment !== 'produção') { + transaction.notes = `
+ + + +
` + + '
' + + `
+ + + +
` + } + // OBS.: in case it is necessary to calculate our number - // banrisul.documentRef.set({ lastBilletNumber }, { merge: true }) - // .catch(console.error) + banrisul.documentRef.set({ lastBilletNumber }, { merge: true }) + .catch(console.error) res.send({ redirect_to_payment: redirectToPayment, diff --git a/functions/routes/homologacao.js b/functions/routes/homologacao.js new file mode 100644 index 0000000..ebabc78 --- /dev/null +++ b/functions/routes/homologacao.js @@ -0,0 +1,15 @@ +exports.get = async ({ appSdk, admin }, req, res) => { + const { orderId, request, response } = req.query + let text = '' + let typeFile = '' + if (request) { + text = Buffer.from(request, 'base64url').toString('utf-8') + typeFile = 'resquest' + } else if (response) { + text = Buffer.from(response, 'base64url').toString('utf-8') + typeFile = 'response' + } + res.attachment(`${typeFile}-${orderId}.json`) + res.type('json') + res.send(text) +} diff --git a/hosting/description.md b/hosting/description.md index d01013e..3fe31d4 100644 --- a/hosting/description.md +++ b/hosting/description.md @@ -65,8 +65,8 @@ Após cadastro realizado no portal do Desenvolvedor do Banrisul, obtenha as cred ## 3) Homologação * Crie um pedido com a opção de ambiente selecionada em `homologação`. -* Em seguida na opção de ambiente selecione `produção` e crie 5 pedidos e salve os PDFs -* Envie os 5 PDFs juntamente com os códigos de retornos, que são códigos das transações, para o e-mail: `atendimento_teste_cobranca@banrisul.com.br` para conferência. +* Em seguida na opção de ambiente selecione `produção` e crie 5 pedidos e salve os PDFs e os arquivos de requisição e retorno. +* Envie os 5 PDFs juntamente com os arquivos de requisição e retorno, que são códigos das transações, para o e-mail: `atendimento_teste_cobranca@banrisul.com.br` para conferência.