diff --git a/functions/ecom.config.js b/functions/ecom.config.js index 03cda84..05ede49 100644 --- a/functions/ecom.config.js +++ b/functions/ecom.config.js @@ -165,12 +165,17 @@ const app = { }, hide: true }, - is_homologation: { + envoriment: { schema: { - type: 'boolean', - title: 'Ambiente de Homologação', - description: 'Habilitar ambiente para Homologação', - default: false + type: 'string', + enum: [ + 'teste', + 'homologação', + 'produção' + ], + default: 'produção', + title: 'Ambiente', + description: 'Definir o ambiente' }, hide: false }, diff --git a/functions/lib/banrisul/auth/create-access.js b/functions/lib/banrisul/auth/create-access.js index a5e1652..ee5300b 100644 --- a/functions/lib/banrisul/auth/create-access.js +++ b/functions/lib/banrisul/auth/create-access.js @@ -1,7 +1,7 @@ const createAxios = require('./create-axios') const getOAuth = require('./get-token') -module.exports = function (clientId, clientSecret, storeId, firestoreColl = 'banrisul_auth') { +module.exports = function (clientId, clientSecret, storeId, isSandbox, firestoreColl = 'banrisul_auth') { const self = this let documentRef @@ -14,11 +14,11 @@ module.exports = function (clientId, clientSecret, storeId, firestoreColl = 'ban this.preparing = new Promise((resolve, reject) => { const authenticate = (accessToken, documentRef) => { - self.axios = createAxios(accessToken) + self.axios = createAxios(accessToken, false, isSandbox) // self.documentRef = documentRef if (documentRef) { documentRef - .set({ accessToken }, { merge: true }) + .set({ accessToken, isSandbox }, { merge: true }) .catch(console.error) } resolve(self) @@ -26,7 +26,7 @@ module.exports = function (clientId, clientSecret, storeId, firestoreColl = 'ban const handleAuth = () => { console.log('> Banrisul Auth02 ', storeId) - getOAuth(clientId, clientSecret) + getOAuth(clientId, clientSecret, isSandbox) .then((accessToken) => { console.log(`>> s:${storeId} token => ${accessToken}`) authenticate(accessToken, documentRef) diff --git a/functions/lib/banrisul/auth/create-axios.js b/functions/lib/banrisul/auth/create-axios.js index 56b0631..72fc219 100644 --- a/functions/lib/banrisul/auth/create-axios.js +++ b/functions/lib/banrisul/auth/create-axios.js @@ -1,7 +1,6 @@ const axios = require('axios') -module.exports = (accessToken, isGetToken) => { - const isSandbox = false // TODO: false - +module.exports = (accessToken, isGetToken, isSandbox) => { + console.log('>> ', isSandbox) const headers = { 'Content-Type': 'application/json' } diff --git a/functions/lib/banrisul/auth/get-token.js b/functions/lib/banrisul/auth/get-token.js index c9a2112..a767b4b 100644 --- a/functions/lib/banrisul/auth/get-token.js +++ b/functions/lib/banrisul/auth/get-token.js @@ -1,8 +1,8 @@ const axios = require('./create-axios') const qs = require('qs') -module.exports = (clientId, clientSecret) => new Promise((resolve, reject) => { - const banrisulAxios = axios(null, true) +module.exports = (clientId, clientSecret, isSandbox) => new Promise((resolve, reject) => { + const banrisulAxios = axios(null, true, isSandbox) const request = async (isRetry) => { try { diff --git a/functions/lib/banrisul/check-billet.js b/functions/lib/banrisul/check-billet.js index 65c5cc0..c354b5f 100644 --- a/functions/lib/banrisul/check-billet.js +++ b/functions/lib/banrisul/check-billet.js @@ -51,7 +51,7 @@ module.exports = async (admin, appSdk) => { storeIds.push(storeId) const auth = await appSdk.getAuth(storeId) const appData = await getAppData({ appSdk, storeId, auth }) - const banrisul = new Banrisul(appData.client_id, appData.client_secret, storeId) + const banrisul = new Banrisul(appData.client_id, appData.client_secret, storeId, appData.envoriment === 'teste') await banrisul.preparing const barisulAxios = banrisul.axios @@ -59,14 +59,14 @@ module.exports = async (admin, appSdk) => { for (let j = 0; j < docsBillets.length; j++) { try { const { titulo, orderId, refDoc } = docsBillets[j] - const idBoleto = titulo.nosso_numero - console.log('>> orderId: ', orderId, ' nosso numero', idBoleto) + const idBoleto = appData.envoriment === 'teste' ? '264' : titulo.nosso_numero + console.log('>> orderId: ', orderId, ' nosso numero', idBoleto, ' envoriment: ', appData.envoriment) const { data: billet } = await barisulAxios.get( `/boletos/${idBoleto}`, { headers: { - 'bergs-beneficiario': appData.beneficiary_code + 'bergs-beneficiario': appData.envoriment === 'teste' ? '0010000001088' : appData.beneficiary_code } } ) diff --git a/functions/lib/banrisul/payload-to-billet.js b/functions/lib/banrisul/payload-to-billet.js index 432fd2e..9d03482 100644 --- a/functions/lib/banrisul/payload-to-billet.js +++ b/functions/lib/banrisul/payload-to-billet.js @@ -10,14 +10,14 @@ const parseAddress = to => ({ complement: to.complement || '' }) -const createBodyToBillet = (appData, params, ourNumber) => { +const createBodyToBillet = (appData, params) => { const { amount, buyer, to } = params const { fees, // Juros tax // Multa } = appData - const isHomologation = appData.is_homologation + const isTest = appData.envoriment === 'homologação' || appData.envoriment === 'teste' const daysToExpiry = appData.days_to_expiry const createdAt = new Date() @@ -115,7 +115,7 @@ const createBodyToBillet = (appData, params, ourNumber) => { Object.assign(titulo, { pagador }) - return { ambiente: isHomologation ? 'T' : 'P', titulo } + return { ambiente: isTest ? 'T' : 'P', titulo } } module.exports = createBodyToBillet diff --git a/functions/routes/ecom/modules/create-transaction.js b/functions/routes/ecom/modules/create-transaction.js index 0b67d03..65b0053 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) => { @@ -34,56 +34,60 @@ exports.post = async ({ appSdk, admin }, req, res) => { if (params.payment_method.code === 'banking_billet') { try { - const banrisul = new Banrisul(appData.client_id, appData.client_secret, storeId) - console.log('>> s: ', storeId, ' beneficiary Code: ', appData.beneficiary_code) - if (appData.beneficiary_code) { - await banrisul.preparing + console.log('>> s: ', storeId, ' beneficiary Code: ', appData.beneficiary_code, ' envoriment: ', appData.envoriment) + const banrisul = new Banrisul(appData.client_id, appData.client_secret, storeId, appData.envoriment === 'teste') + // if (appData.beneficiary_code) { + await banrisul.preparing - // - // const documentRef = banrisul.documentRef && await banrisul.documentRef.get() - // const docAuthBarisul = documentRef?.data() - // const lastBilletNumber = (docAuthBarisul?.lastBilletNumber || 0) + 1 - const banrisulAxios = banrisul.axios + // + // 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) + const body = createBodyToBillet(appData, params) - console.log('>>body ', JSON.stringify(body)) - redirectToPayment = false + console.log('>>body ', JSON.stringify(body)) + redirectToPayment = false - // test - // const data = require('../../../lib/billet/billet-test') - const { data } = await banrisulAxios.post('/boletos', body, { - headers: { - 'bergs-beneficiario': appData.beneficiary_code - } - }) + // test + // const data = require('../../../lib/billet/billet-test') + if (!appData.beneficiary_code && appData.envoriment !== 'teste') { + throw new Error('Beneficiary code not found') + } - console.log('>> boleto ', JSON.stringify(data)) - transaction.banking_billet = { - code: data.titulo?.linha_digitavel, - valid_thru: new Date(data.titulo?.data_vencimento).toISOString(), - link: `${baseUri}/billet?orderId=${orderId}` + const { data } = await banrisulAxios.post('/boletos', body, { + headers: { + 'bergs-beneficiario': appData.envoriment === 'teste' ? '0010000001088' : appData.beneficiary_code } + }) - transaction.intermediator = { - transaction_id: data?.titulo?.nosso_numero, - transaction_reference: data?.titulo?.nosso_numero, - transaction_code: data.retorno - } + console.log('>> boleto ', JSON.stringify(data)) + transaction.banking_billet = { + code: data.titulo?.linha_digitavel, + valid_thru: new Date(data.titulo?.data_vencimento).toISOString(), + link: `${baseUri}/billet?orderId=${orderId}` + } - await collectionBillet.doc(orderId).set({ ...data, storeId, isHomologation: appData.is_homologation }) + transaction.intermediator = { + transaction_id: data?.titulo?.nosso_numero, + transaction_reference: data?.titulo?.nosso_numero, + transaction_code: data.retorno + } - // banrisul.documentRef.set({ lastBilletNumber }, { merge: true }) - // .catch(console.error) + await collectionBillet.doc(orderId).set({ ...data, storeId, envoriment: appData.envoriment }) - res.send({ - redirect_to_payment: redirectToPayment, - transaction - }) - } else { - throw new Error('Beneficiary code not found') - } + // banrisul.documentRef.set({ lastBilletNumber }, { merge: true }) + // .catch(console.error) + + res.send({ + redirect_to_payment: redirectToPayment, + transaction + }) + // } else { + // throw new Error('Beneficiary code not found') + // } } catch (error) { console.log(error.response) // try to debug request error diff --git a/functions/routes/ecom/modules/list-payments.js b/functions/routes/ecom/modules/list-payments.js index f16ce92..3e7d83f 100644 --- a/functions/routes/ecom/modules/list-payments.js +++ b/functions/routes/ecom/modules/list-payments.js @@ -19,7 +19,7 @@ exports.post = ({ appSdk }, req, res) => { payment_gateways: [] } const appData = Object.assign({}, application.data, application.hidden_data) - const isHomologation = appData.is_homologation + // const isProdution = appData.envoriment === 'produção' const amount = { ...params.amount } || {} @@ -42,7 +42,7 @@ exports.post = ({ appSdk }, req, res) => { code: 'banking_billet', name: 'Boleto Bancário' }, - label: `Boleto Bancário${isHomologation ? ' - Homologação' : ''}`, + label: `Boleto Bancário${appData.envoriment !== 'produção' ? ` - ${appData.envoriment}` : ''}`, expiration_date: appData.expiration_date || 7 } diff --git a/hosting/description.md b/hosting/description.md index 5691f6e..d01013e 100644 --- a/hosting/description.md +++ b/hosting/description.md @@ -64,8 +64,8 @@ Após cadastro realizado no portal do Desenvolvedor do Banrisul, obtenha as cred ## 3) Homologação -* Crie um pedido com a opção homologação habilitada. -* Em seguida desabilite a opção homologação e crie 5 pedidos e salve os PDFs +* 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.