From a4485c94b9d7e3be4b33deabc93add19af7fa794 Mon Sep 17 00:00:00 2001 From: Flavio Takeuchi Date: Tue, 16 Jun 2020 23:42:41 -0300 Subject: [PATCH 1/3] =?UTF-8?q?fix(src\class\utils.ts):=20corre=C3=A7?= =?UTF-8?q?=C3=A3o=20no=20retorno=20de=20erro=20da=20recorr=C3=AAncia?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Corrige o retorno quando ocorrer erro na criação ou atualização da recorrência #73 --- src/class/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/class/utils.ts b/src/class/utils.ts index 4443be2..405a5b6 100644 --- a/src/class/utils.ts +++ b/src/class/utils.ts @@ -85,7 +85,7 @@ export class Utils { res.on('end', () => { const response = (chunks.length > 0 && this.validateJSON(chunks)) ? JSON.parse(chunks) : ''; - if (res.statusCode && [200, 201].indexOf(res.statusCode) === -1) return reject(this.parseHttpRequestError(options, data, response)); + if (res.statusCode && [200, 201].indexOf(res.statusCode) === -1) return reject(this.parseHttpRequestError(options, data, res)); if (options.method === 'PUT' && chunks.length === 0) return resolve(this.parseHttpPutResponse(res)); return resolve({ ...this.parseHttpPutResponse(res), From eaf223f77984f43b0ee69a28a1bbc7784af27303 Mon Sep 17 00:00:00 2001 From: Flavio Takeuchi Date: Tue, 16 Jun 2020 23:45:03 -0300 Subject: [PATCH 2/3] =?UTF-8?q?fix(src/models/payment.request.model.ts):?= =?UTF-8?q?=20corre=C3=A7=C3=A0o=20de=20model?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Corrige o estilo da model para o padrão camelcase BREAKING CHANGE: Propriedade *returnUrl* estava definida como *ReturnUrl*. Foi atualizada para seguir o padrão camelcase. --- src/models/payment.request.model.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/payment.request.model.ts b/src/models/payment.request.model.ts index 37b88c2..6adc916 100644 --- a/src/models/payment.request.model.ts +++ b/src/models/payment.request.model.ts @@ -35,7 +35,7 @@ export interface PaymentRequestModel { /** * Texto impresso na fatura bancaria comprador - Exclusivo para VISA/MASTER - não permite caracteres especiais - Ver Anexo */ - softDescriptor: string; + softDescriptor?: string; /** * Dados do cartão */ @@ -63,7 +63,7 @@ export interface PaymentRequestModel { /** * URI para onde o usuário será redirecionado após o fim do pagamento */ - ReturnUrl?: string; + returnUrl?: string; /** * Informações de recorrência de pagamento From 83a2be0d44fab9a7952da952f963109c2fc87f41 Mon Sep 17 00:00:00 2001 From: Flavio Takeuchi Date: Tue, 16 Jun 2020 23:46:18 -0300 Subject: [PATCH 3/3] =?UTF-8?q?test(tests\recurrent.test.ts):=20adiciona?= =?UTF-8?q?=20teste=20para=20erro=20na=20recorr=C3=AAncia?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adiciona um teste para verificar o retorno de erro na recorrência. --- tests/recurrent.test.ts | 397 +++++++++++++++++++++++++--------------- 1 file changed, 246 insertions(+), 151 deletions(-) diff --git a/tests/recurrent.test.ts b/tests/recurrent.test.ts index 75116b4..8e0c50c 100644 --- a/tests/recurrent.test.ts +++ b/tests/recurrent.test.ts @@ -1,167 +1,262 @@ -import { EnumRecurrentPaymentInterval, EnumRecurrentPaymentUpdateInterval } from './../src/enums'; +import { + EnumRecurrentPaymentInterval, + EnumRecurrentPaymentUpdateInterval, +} from './../src/enums'; import test from 'tape'; import { - CieloConstructor, - Cielo, - RecurrentCreateModel, - RecurrentModifyIntervalModel, - RecurrentModifyCustomerModel, - RecurrentModifyAmountModel, - RecurrentModifyNextPaymentDateModel, - RecurrentModifyModel, - ConsultTransactionRecurrentPaymentIdRequestModel, + CieloConstructor, + Cielo, + RecurrentCreateModel, + RecurrentModifyIntervalModel, + RecurrentModifyCustomerModel, + RecurrentModifyAmountModel, + RecurrentModifyNextPaymentDateModel, + RecurrentModifyModel, + ConsultTransactionRecurrentPaymentIdRequestModel, } from '../src'; import { EnumCardType, EnumBrands } from '../src/enums'; const cieloParams: CieloConstructor = { - merchantId: "dbe5e423-ed15-4c27-843a-fedf325ea67c", - merchantKey: "NPGKHFARFASEZEPYEYLTXJMWACSWDEMJWBAKWPQD", - sandbox: true, + merchantId: 'dbe5e423-ed15-4c27-843a-fedf325ea67c', + merchantKey: 'NPGKHFARFASEZEPYEYLTXJMWACSWDEMJWBAKWPQD', + sandbox: true, }; const cielo = new Cielo(cieloParams); function error(err: Object) { - console.log('Ocorreu o seguinte erro', err) + console.log('Ocorreu o seguinte erro', err); } test(`Recorrencia`, async (t) => { + const createRecurrencyParams: RecurrentCreateModel = { + merchantOrderId: '2014113245231706', + customer: { + name: 'Comprador rec programada', + }, + payment: { + type: EnumCardType.CREDIT, + amount: 1500, + installments: 1, + softDescriptor: '123456789ABCD', + currency: 'BRL', + country: 'BRA', + recurrentPayment: { + authorizeNow: true, + endDate: '2022-12-01', + interval: EnumRecurrentPaymentInterval.SEMIANNUAL, + }, + creditCard: { + cardNumber: '4024007197692931', + holder: 'Teste Holder', + expirationDate: '12/2030', + securityCode: '262', + saveCard: false, + brand: 'Visa' as EnumBrands, + }, + }, + }; + + const firstRecurrency = await cielo.recurrent + .create(createRecurrencyParams) + .catch(error); + if (!firstRecurrency) { + t.end('Erro na criação da recorrência'); + return; + } + t.assert( + firstRecurrency.payment.recurrentPayment.reasonCode === 0, + 'Pagamento recorrente criado' + ); + t.assert( + firstRecurrency.payment.status === 1, + 'Status transacional autorizado (1)' + ); + t.assert( + firstRecurrency.payment.recurrentPayment.interval === 6, + 'Intervalo de recorrência correto (6)' + ); + + const modifyRecurrencyParams: RecurrentModifyIntervalModel = { + paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, + interval: EnumRecurrentPaymentUpdateInterval.MONTHLY, + }; + const modifyRecurrency = await cielo.recurrent + .modifyInterval(modifyRecurrencyParams) + .catch(error); + if (modifyRecurrency) { + t.assert( + modifyRecurrency.statusCode === 200, + 'StatusCode da modificação da recorrência para mensal correto (200)' + ); + } + + const updateCustomer: RecurrentModifyCustomerModel = { + paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, + customer: { + name: 'Customer', + email: 'customer@teste.com', + birthdate: '1999-12-12', + identity: '22658954236', + identityType: 'CPF', + address: { + street: 'Rua Teste', + number: '174', + complement: 'AP 201', + zipCode: '21241140', + city: 'Rio de Janeiro', + state: 'RJ', + country: 'BRA', + }, + deliveryAddress: { + street: 'Outra Rua Teste', + number: '123', + complement: 'AP 111', + zipCode: '21241111', + city: 'Qualquer Lugar', + state: 'QL', + country: 'BRA', + }, + }, + }; - const createRecurrencyParams: RecurrentCreateModel = { - merchantOrderId: '2014113245231706', - customer: { - name: 'Comprador rec programada' - }, - payment: { - type: EnumCardType.CREDIT, - amount: 1500, - installments: 1, - softDescriptor: '123456789ABCD', - currency: 'BRL', - country: 'BRA', - recurrentPayment: { - authorizeNow: true, - endDate: '2022-12-01', - interval: EnumRecurrentPaymentInterval.SEMIANNUAL - }, - creditCard: { - cardNumber: '4024007197692931', - holder: 'Teste Holder', - expirationDate: '12/2030', - securityCode: '262', - saveCard: false, - brand: 'Visa' as EnumBrands - } + const customer = await cielo.recurrent + .modifyCustomer(updateCustomer) + .catch(error); + if (customer) { + t.assert( + customer.statusCode === 200, + 'StatusCode da modificação do Customer correto (200)' + ); } - } - - const firstRecurrency = await cielo.recurrent.create(createRecurrencyParams).catch(error); - if (!firstRecurrency) { - t.end('Erro na criação da recorrência'); - return; - } - t.assert(firstRecurrency.payment.recurrentPayment.reasonCode === 0, 'Pagamento recorrente criado') - t.assert(firstRecurrency.payment.status === 1, 'Status transacional autorizado (1)') - t.assert(firstRecurrency.payment.recurrentPayment.interval === 6, 'Intervalo de recorrência correto (6)') - - const modifyRecurrencyParams: RecurrentModifyIntervalModel = { - paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, - interval: EnumRecurrentPaymentUpdateInterval.MONTHLY - } - const modifyRecurrency = await cielo.recurrent.modifyInterval(modifyRecurrencyParams).catch(error) - if (modifyRecurrency) { - t.assert(modifyRecurrency.statusCode === 200, 'StatusCode da modificação da recorrência para mensal correto (200)') - } - - const updateCustomer: RecurrentModifyCustomerModel = { - paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, - customer: { - name: 'Customer', - email: 'customer@teste.com', - birthdate: '1999-12-12', - identity: '22658954236', - identityType: 'CPF', - address: { - street: 'Rua Teste', - number: '174', - complement: 'AP 201', - zipCode: '21241140', - city: 'Rio de Janeiro', - state: 'RJ', - country: 'BRA' - }, - deliveryAddress: { - street: 'Outra Rua Teste', - number: '123', - complement: 'AP 111', - zipCode: '21241111', - city: 'Qualquer Lugar', - state: 'QL', - country: 'BRA', - } + + const endDate = await cielo.recurrent + .modifyEndDate({ + paymentId: + firstRecurrency.payment.recurrentPayment.recurrentPaymentId, + endDate: '2021-01-09', + }) + .catch(error); + if (endDate) { + t.assert( + endDate.statusCode === 200, + 'StatusCode da modificação da recorrência terminar dia 09/01/2021 correto (200)' + ); + } + + const recurrencyDay = await cielo.recurrent + .modifyRecurrencyDay({ + paymentId: + firstRecurrency.payment.recurrentPayment.recurrentPaymentId, + recurrencyDay: 10, + }) + .catch(error); + if (recurrencyDay) { + t.assert( + recurrencyDay.statusCode === 200, + 'StatusCode da modificação da recorrência para dia 10 correto (200)' + ); + } + + const updateAmount: RecurrentModifyAmountModel = { + paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, + amount: 156, // Valor do Pedido em centavos: 156 equivale a R$ 1,56 + }; + const amount = await cielo.recurrent + .modifyAmount(updateAmount) + .catch(error); + if (amount) { + t.assert( + amount.statusCode === 200, + 'StatusCode da modificação do valor para 156 correto (200)' + ); } - } - - const customer = await cielo.recurrent.modifyCustomer(updateCustomer).catch(error); - if (customer) { - t.assert(customer.statusCode === 200, 'StatusCode da modificação do Customer correto (200)') - } - - const endDate = await cielo.recurrent.modifyEndDate({ - paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, - endDate: '2021-01-09' - }).catch(error) - if (endDate) { - t.assert(endDate.statusCode === 200, 'StatusCode da modificação da recorrência terminar dia 09/01/2021 correto (200)') - } - - const recurrencyDay = await cielo.recurrent.modifyRecurrencyDay({ - paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, - recurrencyDay: 10 - }).catch(error) - if (recurrencyDay) { - t.assert(recurrencyDay.statusCode === 200, 'StatusCode da modificação da recorrência para dia 10 correto (200)') - } - - const updateAmount: RecurrentModifyAmountModel = { - paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, - amount: 156 // Valor do Pedido em centavos: 156 equivale a R$ 1,56 - } - const amount = await cielo.recurrent.modifyAmount(updateAmount).catch(error) - if (amount) { - t.assert(amount.statusCode === 200, 'StatusCode da modificação do valor para 156 correto (200)') - } - - let newRecurrencyDate = new Date() - newRecurrencyDate.setDate(newRecurrencyDate.getDate() + 7) // Altera para a próxima semana - let nextRecurrency = `${newRecurrencyDate.getFullYear()}-${newRecurrencyDate.getMonth() + 1}-${newRecurrencyDate.getDate()}` - const updateNextPaymentDate: RecurrentModifyNextPaymentDateModel = { - paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, - nextPaymentDate: nextRecurrency - } - const nextPaymentDate = await cielo.recurrent.modifyNextPaymentDate(updateNextPaymentDate).catch(error) - if (nextPaymentDate) { - t.assert(nextPaymentDate.statusCode === 200, 'StatusCode da modificação da data do próximo pagamento para 15/06/2016 correto (200)') - } - - const deactivateRecurrencyParams: RecurrentModifyModel = { - paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId - } - const deactivateRecurrency = await cielo.recurrent.deactivate(deactivateRecurrencyParams).catch(error) - if (deactivateRecurrency) { - t.assert(deactivateRecurrency.statusCode === 200, 'StatusCode da desativação da recorrência correto (200)') - } - - const recurrencyConsultingParams: ConsultTransactionRecurrentPaymentIdRequestModel = { - recurrentPaymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId - } - const recurrencyConsulting = await cielo.consult.recurrent(recurrencyConsultingParams).catch(error) - if (recurrencyConsulting) { - t.assert(recurrencyConsulting.recurrentPayment.status === 3, 'Status da recorrência correto (3 - desativada pelo lojista)') - t.assert(recurrencyConsulting.recurrentPayment.interval === 'Monthly', 'Intervalo da recorrência correto (Monthly)') - t.assert(recurrencyConsulting.customer.email === 'customer@teste.com', 'Dados do cliente alterados com sucesso') - } - - t.end() - - -}); \ No newline at end of file + + let newRecurrencyDate = new Date(); + newRecurrencyDate.setDate(newRecurrencyDate.getDate() + 7); // Altera para a próxima semana + let nextRecurrency = `${newRecurrencyDate.getFullYear()}-${ + newRecurrencyDate.getMonth() + 1 + }-${newRecurrencyDate.getDate()}`; + const updateNextPaymentDate: RecurrentModifyNextPaymentDateModel = { + paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, + nextPaymentDate: nextRecurrency, + }; + const nextPaymentDate = await cielo.recurrent + .modifyNextPaymentDate(updateNextPaymentDate) + .catch(error); + if (nextPaymentDate) { + t.assert( + nextPaymentDate.statusCode === 200, + 'StatusCode da modificação da data do próximo pagamento para 15/06/2016 correto (200)' + ); + } + + const deactivateRecurrencyParams: RecurrentModifyModel = { + paymentId: firstRecurrency.payment.recurrentPayment.recurrentPaymentId, + }; + const deactivateRecurrency = await cielo.recurrent + .deactivate(deactivateRecurrencyParams) + .catch(error); + if (deactivateRecurrency) { + t.assert( + deactivateRecurrency.statusCode === 200, + 'StatusCode da desativação da recorrência correto (200)' + ); + } + + const recurrencyConsultingParams: ConsultTransactionRecurrentPaymentIdRequestModel = { + recurrentPaymentId: + firstRecurrency.payment.recurrentPayment.recurrentPaymentId, + }; + const recurrencyConsulting = await cielo.consult + .recurrent(recurrencyConsultingParams) + .catch(error); + if (recurrencyConsulting) { + t.assert( + recurrencyConsulting.recurrentPayment.status === 3, + 'Status da recorrência correto (3 - desativada pelo lojista)' + ); + t.assert( + recurrencyConsulting.recurrentPayment.interval === 'Monthly', + 'Intervalo da recorrência correto (Monthly)' + ); + t.assert( + recurrencyConsulting.customer.email === 'customer@teste.com', + 'Dados do cliente alterados com sucesso' + ); + } + + const testeError: RecurrentCreateModel = { + merchantOrderId: '12345678', + customer: { + name: 'Fulano de Tal', + }, + payment: { + type: EnumCardType.CREDIT, + amount: 50, + installments: 1, + returnUrl: 'http://google.com.br', + recurrentPayment: { + authorizeNow: true, + endDate: '2020-12-12', + interval: EnumRecurrentPaymentInterval.MONTHLY, + }, + creditCard: { + cardNumber: '522aaa4049 1585 0591', + holder: 'Fulano de Tal', + expirationDate: '05/2022', + securityCode: '111', + saveCard: false, + brand: EnumBrands.MASTER, + }, + }, + }; + + // Testa uma recorrência com erro + const recurrencyError = await cielo.recurrent + .create(testeError) + .catch((error) => error); + + t.assert(recurrencyError.statusCode !== 200, 'Código do erro na transação correto'); + + t.end(); +});