diff --git a/e2e/cardToken/create.spec.ts b/e2e/cardToken/create.spec.ts index b7dac8a1..4ae7330a 100644 --- a/e2e/cardToken/create.spec.ts +++ b/e2e/cardToken/create.spec.ts @@ -1,5 +1,8 @@ import MercadoPago, { CardToken, Customer, CustomerCard } from '@src/index'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; +import { createCardToken } from '@src/mocks/createCardToken'; + describe('IT, create card token', () => { test('should make a request and return created card token id', async () => { const client = new MercadoPago({ accessToken: config.test_access_token }); @@ -9,12 +12,12 @@ describe('IT, create card token', () => { const email = createEmailTestUser(); const emailBody = { - email: email, + email, }; const createCustomer = await customer.create({ body: emailBody }); expect(createCustomer).toHaveProperty('id'); - const createToken = await createCardToken(); + const createToken = await createCardToken(client.accessToken); const customerBody = { token: createToken.id }; @@ -42,35 +45,4 @@ describe('IT, create card token', () => { security_code_length: expect.any(Number), })); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.test_access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - site_id: 'MLB', - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/customer/create.spec.ts b/e2e/customer/create.spec.ts index 7f839d84..aa12f66a 100644 --- a/e2e/customer/create.spec.ts +++ b/e2e/customer/create.spec.ts @@ -1,5 +1,6 @@ import MercadoPago, { Customer } from '@src/index'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; describe('IT customer, create', () => { test('should create a client and match response object', async () => { @@ -9,7 +10,7 @@ describe('IT customer, create', () => { const email = createEmailTestUser(); const body = { - email: email, + email, first_name: 'Jhon', last_name: 'Doe', phone: { @@ -36,7 +37,7 @@ describe('IT customer, create', () => { const createCustomer = await customer.create({ body }); expect(createCustomer).toHaveProperty('status', 'active'); expect(createCustomer).toEqual(expect.objectContaining({ - email: email, + email, first_name: 'Jhon', last_name: 'Doe', date_registered: '2023-10-20T11:37:30.000-04:00', @@ -77,10 +78,4 @@ describe('IT customer, create', () => { const removeCustomer = await customer.remove({ customerId: createCustomer.id }); expect(removeCustomer).toHaveProperty('id', removeCustomer.id); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } }); diff --git a/e2e/customer/get.spec.ts b/e2e/customer/get.spec.ts index b787f170..2b737f36 100644 --- a/e2e/customer/get.spec.ts +++ b/e2e/customer/get.spec.ts @@ -1,5 +1,6 @@ import MercadoPago, { Customer } from '@src/index'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; describe('IT customer, get', () => { test('should get a client and match response object', async () => { @@ -9,7 +10,7 @@ describe('IT customer, get', () => { const email = createEmailTestUser(); const body = { - email: email, + email, }; const createCustomer = await customer.create({ body }); @@ -23,10 +24,4 @@ describe('IT customer, get', () => { const removeCustomer = await customer.remove({ customerId: customerGet.id }); expect(removeCustomer).toHaveProperty('id', removeCustomer.id); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } }); diff --git a/e2e/customer/remove.spec.ts b/e2e/customer/remove.spec.ts index 9ea2b84e..1ecd29ac 100644 --- a/e2e/customer/remove.spec.ts +++ b/e2e/customer/remove.spec.ts @@ -1,5 +1,6 @@ import MercadoPago, { Customer } from '@src/index'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; describe('IT customer, remove', () => { test('should delete a customer and match response object', async () => { @@ -9,7 +10,7 @@ describe('IT customer, remove', () => { const email = createEmailTestUser(); const body = { - email: email, + email, }; const createCustomer = await customer.create({ body }); @@ -30,12 +31,5 @@ describe('IT customer, remove', () => { address: expect.any(Object), date_last_updated: expect.any(String), })); - }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } }); diff --git a/e2e/customer/update.spec.ts b/e2e/customer/update.spec.ts index 719fb75c..84012ba1 100644 --- a/e2e/customer/update.spec.ts +++ b/e2e/customer/update.spec.ts @@ -1,5 +1,6 @@ import MercadoPago, { Customer } from '@src/index'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; describe('IT customer, update', () => { test('should update a customer and match response object', async () => { @@ -8,7 +9,7 @@ describe('IT customer, update', () => { const email = createEmailTestUser(); const body = { - email: email, + email, }; const createCustomer = await customer.create({ body }); @@ -40,10 +41,4 @@ describe('IT customer, update', () => { const removeCustomer = await customer.remove({ customerId: createCustomer.id }); expect(removeCustomer).toHaveProperty('id', removeCustomer.id); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } }); diff --git a/e2e/customerCard/create.spec.ts b/e2e/customerCard/create.spec.ts index 77e45d31..b2dde142 100644 --- a/e2e/customerCard/create.spec.ts +++ b/e2e/customerCard/create.spec.ts @@ -1,6 +1,7 @@ import MercadoPago, { Customer, CustomerCard } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; +import { createCardToken } from '@src/mocks/createCardToken'; describe('Testing customer cards, create', () => { test('should create customer card and match response object', async () => { @@ -10,12 +11,12 @@ describe('Testing customer cards, create', () => { const email = createEmailTestUser(); const emailBody = { - email: email, + email, }; const createCustomer = await customer.create({ body: emailBody }); expect(createCustomer).toHaveProperty('id'); - const createToken = await createCardToken(); + const createToken = await createCardToken(client.accessToken); const customerBody = { token: createToken.id }; @@ -59,35 +60,4 @@ describe('Testing customer cards, create', () => { const removeCustomer = await customer.remove({ customerId: createCustomer.id }); expect(removeCustomer.api_response.status).toBe(200); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.test_access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - site_id: 'MLB', - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/customerCard/get.spec.ts b/e2e/customerCard/get.spec.ts index 7b480650..80e71fb0 100644 --- a/e2e/customerCard/get.spec.ts +++ b/e2e/customerCard/get.spec.ts @@ -1,6 +1,7 @@ import MercadoPago, { Customer, CustomerCard } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; +import { createCardToken } from '@src/mocks/createCardToken'; describe('Testing customer card, get', () => { test('should get customer card and match response object', async () => { @@ -10,12 +11,12 @@ describe('Testing customer card, get', () => { const email = createEmailTestUser(); const emailBody = { - email: email, + email, }; const createCustomer = await customer.create({ body: emailBody }); expect(createCustomer).toHaveProperty('id'); - const createToken = await createCardToken(); + const createToken = await createCardToken(client.accessToken); const customerBody = { token: createToken.id }; @@ -62,35 +63,4 @@ describe('Testing customer card, get', () => { const removeCustomer = await customer.remove({ customerId: createCustomer.id }); expect(removeCustomer.api_response.status).toBe(200); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.test_access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - site_id: 'MLB', - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/customerCard/list.spec.ts b/e2e/customerCard/list.spec.ts index 6ab75ca6..273a2f0d 100644 --- a/e2e/customerCard/list.spec.ts +++ b/e2e/customerCard/list.spec.ts @@ -1,6 +1,7 @@ import MercadoPago, { Customer, CustomerCard } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; +import { createCardToken } from '@src/mocks/createCardToken'; describe('Testing customer card, get', () => { test('should get list of cards and match response object', async () => { @@ -10,12 +11,12 @@ describe('Testing customer card, get', () => { const email = createEmailTestUser(); const emailBody = { - email: email, + email, }; const createCustomer = await customer.create({ body: emailBody }); expect(createCustomer).toHaveProperty('id'); - const createToken = await createCardToken(); + const createToken = await createCardToken(client.accessToken); const customerBody = { token: createToken.id }; @@ -63,35 +64,4 @@ describe('Testing customer card, get', () => { const removeCustomer = await customer.remove({ customerId: createCustomer.id }); expect(removeCustomer.api_response.status).toBe(200); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.test_access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - site_id: 'MLB', - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/customerCard/remove.spec.ts b/e2e/customerCard/remove.spec.ts index 96b486ef..dd7a2d72 100644 --- a/e2e/customerCard/remove.spec.ts +++ b/e2e/customerCard/remove.spec.ts @@ -1,6 +1,7 @@ import MercadoPago, { Customer, CustomerCard } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; +import { createCardToken } from '@src/mocks/createCardToken'; describe('Testing customer card, get', () => { test('should remove card and match response object', async () => { @@ -10,12 +11,12 @@ describe('Testing customer card, get', () => { const email = createEmailTestUser(); const emailBody = { - email: email, + email, }; const createCustomer = await customer.create({ body: emailBody }); expect(createCustomer).toHaveProperty('id'); - const createToken = await createCardToken(); + const createToken = await createCardToken(client.accessToken); const customerBody = { token: createToken.id }; @@ -61,35 +62,4 @@ describe('Testing customer card, get', () => { const removeCustomer = await customer.remove({ customerId: createCustomer.id }); expect(removeCustomer.api_response.status).toBe(200); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.test_access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - site_id: 'MLB', - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/customerCard/update.spec.ts b/e2e/customerCard/update.spec.ts index 1a9ab780..5ae91564 100644 --- a/e2e/customerCard/update.spec.ts +++ b/e2e/customerCard/update.spec.ts @@ -1,6 +1,7 @@ import MercadoPago, { Customer, CustomerCard } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; +import { createCardToken } from '@src/mocks/createCardToken'; describe('Testing customer card, get', () => { test('should update card and match response object', async () => { @@ -10,12 +11,12 @@ describe('Testing customer card, get', () => { const email = createEmailTestUser(); const emailBody = { - email: email, + email, }; const createCustomer = await customer.create({ body: emailBody }); expect(createCustomer).toHaveProperty('id'); - const createToken = await createCardToken(); + const createToken = await createCardToken(client.accessToken); const customerBody = { token: createToken.id }; @@ -77,35 +78,4 @@ describe('Testing customer card, get', () => { const removeCustomer = await customer.remove({ customerId: createCustomer.id }); expect(removeCustomer.api_response.status).toBe(200); }); - - function createEmailTestUser() { - const random = Math.floor(Math.random() * 1000000); - const email = 'test_user' + random + '@testuser.com'; - return email; - } - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.test_access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - site_id: 'MLB', - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/payment/cancel.spec.ts b/e2e/payment/cancel.spec.ts index 7965c73f..733db1a6 100644 --- a/e2e/payment/cancel.spec.ts +++ b/e2e/payment/cancel.spec.ts @@ -1,11 +1,14 @@ import MercadoPago, { Payment } from '@src/index'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; describe('IT, cancel', () => { test('should cancel Payment and match response object', async () => { const client = new MercadoPago({ accessToken: config.access_token }); const payment = new Payment(client); + const email = createEmailTestUser(); + const paymentBody = { body: { additional_info: { @@ -19,7 +22,7 @@ describe('IT, cancel', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, }, transaction_amount: 110.00, installments: 1, diff --git a/e2e/payment/capture.spec.ts b/e2e/payment/capture.spec.ts index ae87504a..14bfee96 100644 --- a/e2e/payment/capture.spec.ts +++ b/e2e/payment/capture.spec.ts @@ -1,15 +1,17 @@ import MercadoPago, { Payment } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; +import { createCardToken } from '@src/mocks/createCardToken'; describe('IT, capture', () => { test('should capture, return partial transaction_amount passed at the request and match response object', async () => { const client = new MercadoPago({ accessToken: config.access_token }); const payment = new Payment(client); - const cardToken = await createCardToken(); + const cardToken = await createCardToken(client.accessToken); expect(cardToken).toHaveProperty('id'); + const email = createEmailTestUser(); const paymentBody = { body: { additional_info: { @@ -23,7 +25,7 @@ describe('IT, capture', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, }, transaction_amount: 110.00, installments: 1, @@ -79,9 +81,11 @@ describe('IT, capture', () => { const client = new MercadoPago({ accessToken: config.access_token }); const payment = new Payment(client); - const cardToken = await createCardToken(); + const cardToken = await createCardToken(client.accessToken); expect(cardToken).toHaveProperty('id'); + const email = createEmailTestUser(); + const paymentBody = { body: { additional_info: { @@ -95,7 +99,7 @@ describe('IT, capture', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, }, transaction_amount: 110.00, installments: 1, @@ -146,28 +150,4 @@ describe('IT, capture', () => { transaction_amount_refunded: expect.any(Number), })); }); - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/payment/create.spec.ts b/e2e/payment/create.spec.ts index 3cfd8a1a..b0010f70 100644 --- a/e2e/payment/create.spec.ts +++ b/e2e/payment/create.spec.ts @@ -1,5 +1,6 @@ import MercadoPago, { Payment } from '@src/index'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; import type { PaymentCreateData } from '@src/clients/payment/create/types'; describe('IT, create', () => { @@ -7,6 +8,68 @@ describe('IT, create', () => { const client = new MercadoPago({ accessToken: config.access_token }); const payment = new Payment(client); + const email = createEmailTestUser(); + + const body: PaymentCreateData = { + body: { + additional_info: { + items: [ + { + id: 'MLB2907679857', + title: 'Point Mini', + quantity: 1, + unit_price: 58.8 + } + ] + }, + payer: { + email, + }, + transaction_amount: 110.00, + installments: 1, + payment_method_id: 'pix' + } + }; + + const response = await payment.create(body); + + expect(response).toHaveProperty('id'); + expect(response.additional_info.items[0]).toEqual(expect.objectContaining({ + id: 'MLB2907679857', + title: 'Point Mini', + quantity: '1', + unit_price: '58.8' + })); + expect(response.transaction_amount).toBe(110.00); + + expect(response).toEqual(expect.objectContaining({ + collector_id: expect.any(Number), + date_created: expect.any(String), + id: expect.any(Number), + payment_method_id: expect.any(String), + payment_type_id: expect.any(String), + status: expect.any(String), + status_detail: expect.any(String), + transaction_amount: expect.any(Number), + point_of_interaction: expect.objectContaining({ + transaction_data: expect.objectContaining({ + qr_code: expect.any(String), + qr_code_base64: expect.any(String), + ticket_url: expect.any(String), + }) + }), + payer: expect.objectContaining({ + id: expect.any(String), + }), + })); + }); + + test('should create Payment when payer`s "address" field is filled', async () => { + const client = new MercadoPago({ accessToken: config.access_token }); + const payment = new Payment(client); + + const email = createEmailTestUser(); + const body: PaymentCreateData = { body: { additional_info: { @@ -20,7 +83,15 @@ describe('IT, create', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, + address: { + zip_code: '06233200', + street_name: 'street_name', + street_number: 123, + neighborhood: 'neighborhood', + city: 'city', + federal_unit: 'federal_unit', + }, }, transaction_amount: 110.00, installments: 1, diff --git a/e2e/payment/get.spec.ts b/e2e/payment/get.spec.ts index f8752bd2..fc16e039 100644 --- a/e2e/payment/get.spec.ts +++ b/e2e/payment/get.spec.ts @@ -1,5 +1,6 @@ import MercadoPago, { Payment } from '@src/index'; import { config } from '../e2e.config'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; import type { PaymentCreateData } from '@src/clients/payment/create/types'; describe('IT, get', () => { @@ -7,6 +8,8 @@ describe('IT, get', () => { const client = new MercadoPago({ accessToken: config.access_token }); const payment = new Payment(client); + const email = createEmailTestUser(); + const body: PaymentCreateData = { body: { additional_info: { @@ -20,7 +23,7 @@ describe('IT, get', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, }, transaction_amount: 110.00, installments: 1, diff --git a/e2e/paymentRefund/create.spec.ts b/e2e/paymentRefund/create.spec.ts index 0ba48e58..9fa5504a 100644 --- a/e2e/paymentRefund/create.spec.ts +++ b/e2e/paymentRefund/create.spec.ts @@ -1,6 +1,7 @@ import MercadoPago, { Payment, PaymentRefund } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createCardToken } from '@src/mocks/createCardToken'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; import type { PaymentRefundCreateData } from '@src/clients/paymentRefund/create/types'; describe('IT refunds, create', () => { @@ -9,9 +10,11 @@ describe('IT refunds, create', () => { const refund = new PaymentRefund(client); const payment = new Payment(client); try { - const cardToken = await createCardToken(); + const cardToken = await createCardToken(client.accessToken); expect(cardToken).toHaveProperty('id'); + const email = createEmailTestUser(); + const paymentBody = { body: { additional_info: { @@ -25,7 +28,7 @@ describe('IT refunds, create', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, }, transaction_amount: 110.00, installments: 1, @@ -66,28 +69,4 @@ describe('IT refunds, create', () => { console.error(e, 'error'); } }, 10000); - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/paymentRefund/get.spec.ts b/e2e/paymentRefund/get.spec.ts index f43fc4eb..8533ba05 100644 --- a/e2e/paymentRefund/get.spec.ts +++ b/e2e/paymentRefund/get.spec.ts @@ -1,8 +1,9 @@ import MercadoPago, { Payment, PaymentRefund } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; -import type { PaymentRefundCreateData } from '@src/clients/paymentRefund/create/types'; +import { createCardToken } from '@src/mocks/createCardToken'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; import type { PaymentRefundGetData } from '@src/clients/paymentRefund/get/types'; +import type { PaymentRefundCreateData } from '@src/clients/paymentRefund/create/types'; describe('IT refunds, get', () => { test('should make a request and match response object', async () => { @@ -10,9 +11,11 @@ describe('IT refunds, get', () => { const refund = new PaymentRefund(client); const payment = new Payment(client); try { - const cardToken = await createCardToken(); + const cardToken = await createCardToken(client.accessToken); expect(cardToken).toHaveProperty('id'); + const email = createEmailTestUser(); + const paymentBody = { body: { additional_info: { @@ -26,7 +29,7 @@ describe('IT refunds, get', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, }, transaction_amount: 140.00, installments: 1, @@ -74,28 +77,4 @@ describe('IT refunds, get', () => { console.error(e); } }, 10000); - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/paymentRefund/list.spec.ts b/e2e/paymentRefund/list.spec.ts index 634c68bf..8f20936a 100644 --- a/e2e/paymentRefund/list.spec.ts +++ b/e2e/paymentRefund/list.spec.ts @@ -1,6 +1,7 @@ import MercadoPago, { Payment, PaymentRefund } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createCardToken } from '@src/mocks/createCardToken'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; import type { PaymentRefundCreateData } from '@src/clients/paymentRefund/create/types'; import type { PaymentRefundListData } from '@src/clients/paymentRefund/list/types'; @@ -10,9 +11,11 @@ describe('IT refunds, list', () => { const refund = new PaymentRefund(client); const payment = new Payment(client); try { - const cardToken = await createCardToken(); + const cardToken = await createCardToken(client.accessToken); expect(cardToken).toHaveProperty('id'); + const email = createEmailTestUser(); + const paymentBody = { body: { additional_info: { @@ -26,7 +29,7 @@ describe('IT refunds, list', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, }, transaction_amount: 110.00, installments: 1, @@ -74,28 +77,4 @@ describe('IT refunds, list', () => { console.error(e); } }, 10000); - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/e2e/paymentRefund/total.spec.ts b/e2e/paymentRefund/total.spec.ts index 64eee565..82365d95 100644 --- a/e2e/paymentRefund/total.spec.ts +++ b/e2e/paymentRefund/total.spec.ts @@ -1,6 +1,7 @@ import MercadoPago, { Payment, PaymentRefund } from '@src/index'; -import fetch from 'node-fetch'; import { config } from '../e2e.config'; +import { createCardToken } from '@src/mocks/createCardToken'; +import { createEmailTestUser } from '@src/mocks/createEmailTestUser'; import type { PaymentCreateData } from '@src/clients/payment/create/types'; import type { PaymentRefundTotalData } from '@src/clients/paymentRefund/total/types'; @@ -10,7 +11,7 @@ describe('IT refunds, total', () => { const refund = new PaymentRefund(client); const payment = new Payment(client); try { - const cardToken = await createCardToken(); + const cardToken = await createCardToken(client.accessToken); expect(cardToken).toHaveProperty('id'); const paymentBody = createPayment(cardToken.id); @@ -45,6 +46,7 @@ describe('IT refunds, total', () => { } }, 10000); + const email = createEmailTestUser(); function createPayment(token: string): PaymentCreateData { const body = { @@ -60,7 +62,7 @@ describe('IT refunds, total', () => { ] }, payer: { - email: 'test_user_123@testuser.com', + email, }, transaction_amount: 110.00, installments: 1, @@ -70,28 +72,4 @@ describe('IT refunds, total', () => { }; return body; } - - async function createCardToken() { - const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { - method: 'POST', - headers: { - 'Authorization': 'Bearer ' + config.access_token, - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - card_number: '5031433215406351', - expiration_year: '2025', - expiration_month: '11', - security_code: '123', - cardholder: { - identification: { - type: 'CPF', - number: '01234567890' - }, - name: 'APRO' - } - }) - }); - return await response.json(); - } }); diff --git a/src/clients/payment/cancel/index.ts b/src/clients/payment/cancel/index.ts index 2af68217..c3ad2db8 100644 --- a/src/clients/payment/cancel/index.ts +++ b/src/clients/payment/cancel/index.ts @@ -1,5 +1,4 @@ import { RestClient } from '@utils/restClient'; - import type { PaymentResponse } from '../commonTypes'; import type { PaymentCancelClient } from './types'; diff --git a/src/clients/payment/capture/index.ts b/src/clients/payment/capture/index.ts index 3d3a0d2c..49f677b1 100644 --- a/src/clients/payment/capture/index.ts +++ b/src/clients/payment/capture/index.ts @@ -1,5 +1,4 @@ import { RestClient } from '@utils/restClient'; - import type { PaymentResponse } from '../commonTypes'; import type { PaymentCaptureClient } from './types'; diff --git a/src/clients/payment/commonTypes.ts b/src/clients/payment/commonTypes.ts index b9935675..02ffe0c0 100644 --- a/src/clients/payment/commonTypes.ts +++ b/src/clients/payment/commonTypes.ts @@ -1,6 +1,6 @@ import { ApiResponse } from '@src/types'; -import type { Address, Items, Tax } from '../commonTypes'; import { RefundResponse } from '../paymentRefund/commonTypes'; +import type { Address, Items, Tax } from '../commonTypes'; export declare type MerchantAccount = { merchant_account_id?: string; diff --git a/src/clients/payment/create/types.ts b/src/clients/payment/create/types.ts index 794b425d..e95fc771 100644 --- a/src/clients/payment/create/types.ts +++ b/src/clients/payment/create/types.ts @@ -1,6 +1,6 @@ -import type { Items, Shipments } from '@src/clients/commonTypes'; +import type { Address, Items, Shipments } from '@src/clients/commonTypes'; import type { MercadoPagoConfig } from '@src/mercadoPagoConfig'; -import type { Payer } from '../commonTypes'; +import type { Identification, Payer, Phone } from '../commonTypes'; import type { Options } from '@src/types'; export declare type PaymentCreateClient = { @@ -34,7 +34,55 @@ export declare type PaymentCreateRequest = { statement_descriptor?: string, token?: string, transaction_amount?: number, - payer?: Payer, + payer?: PayerRequest, + point_of_interaction?: PointOfInteractionRequest, +}; + +export declare type PayerRequest = { + type?: string, + id?: string, + email?: string, + identification?: Identification, + phone?: Phone, + first_name?: string, + last_name?: string, + entity_type?: string, + address?: AddressRequest, +} + +export declare interface AddressRequest extends Address { + neighborhood?: string, + city?: string, + federal_unit?: string, +} + +export declare type PointOfInteractionRequest = { + linkedTo?: string, + type?: string, + transaction_data?: TransactionDataRequest, +}; + +export declare type TransactionDataRequest = { + first_time_use?: boolean, + subscription_sequence?: SubscriptionSequenceRequest, + subscription_id?: string, + invoice_period?: InvoicePeriodRequest, + payment_reference?: PaymentReferenceRequest, + billing_date?: string, +}; + +export declare type SubscriptionSequenceRequest = { + number?: number, + total?: number, +}; + +export declare type InvoicePeriodRequest = { + period?: number; + type?: string; +}; + +export declare type PaymentReferenceRequest = { + id?: string; }; export declare type AdditionalInfo = { diff --git a/src/clients/payment/get/index.ts b/src/clients/payment/get/index.ts index 2684335b..cfb59781 100644 --- a/src/clients/payment/get/index.ts +++ b/src/clients/payment/get/index.ts @@ -1,5 +1,4 @@ import { RestClient } from '@utils/restClient'; - import type { PaymentResponse } from '../commonTypes'; import type { PaymentGetClient } from './types'; diff --git a/src/mocks/createCardToken.ts b/src/mocks/createCardToken.ts new file mode 100644 index 00000000..30780b37 --- /dev/null +++ b/src/mocks/createCardToken.ts @@ -0,0 +1,30 @@ +import fetch from 'node-fetch'; + +async function createCardToken(token: string) { + const response = await fetch('https://api.mercadopago.com/v1/card_tokens', { + method: 'POST', + headers: { + 'Authorization': 'Bearer ' + token, + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + site_id: 'MLB', + card_number: '5031433215406351', + expiration_year: '2025', + expiration_month: '11', + security_code: '123', + cardholder: { + identification: { + type: 'CPF', + number: '01234567890' + }, + name: 'APRO' + } + }) + }); + return await response.json(); +} + +export { + createCardToken +}; diff --git a/src/mocks/createEmailTestUser.ts b/src/mocks/createEmailTestUser.ts new file mode 100644 index 00000000..9ccd7953 --- /dev/null +++ b/src/mocks/createEmailTestUser.ts @@ -0,0 +1,10 @@ +function createEmailTestUser() { + const random = Math.floor(Math.random() * 1000000); + const email = 'test_user' + random + '@testuser.com'; + return email; +} + +export { + createEmailTestUser +}; + diff --git a/tsconfig.production.json b/tsconfig.production.json index 2b8c2ed8..8ead8d5a 100644 --- a/tsconfig.production.json +++ b/tsconfig.production.json @@ -1,4 +1,4 @@ { "extends": "./tsconfig", - "exclude": ["node_modules", "**/*spec.ts", "src/examples", "e2e"] + "exclude": ["node_modules", "**/*spec.ts", "src/examples", "e2e", "src/mocks"] }