Skip to content

Commit

Permalink
Merge branch 'master' into feature/paymentMethod-integration-test
Browse files Browse the repository at this point in the history
  • Loading branch information
lucmkz authored Oct 16, 2023
2 parents 0d6cb7c + 6db72b8 commit 872b6b8
Show file tree
Hide file tree
Showing 11 changed files with 448 additions and 14 deletions.
93 changes: 93 additions & 0 deletions e2e/paymentRefund/create.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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';

describe('IT refunds, create', () => {
test('should make a request with partial amount and match response object', async () => {
const client = new MercadoPago({ accessToken: config.access_token });
const refund = new PaymentRefund(client);
const payment = new Payment(client);
try {
const cardToken = await createCardToken();
expect(cardToken).toHaveProperty('id');

const paymentBody = {
body: {
additional_info: {
items: [
{
id: 'MLB2907679857',
title: 'Point Mini',
quantity: 1,
unit_price: 58.8
}
]
},
payer: {
email: '[email protected]',
},
transaction_amount: 110.00,
installments: 1,
token: cardToken.id,
payment_method_id: 'master',
}
};
const createdPayment = await payment.create(paymentBody);
expect(createdPayment).toHaveProperty('id');

const request: PaymentRefundCreateData = {
payment_id: createdPayment.id,
body: {
amount: 5
},
};

const refunded = await refund.create(request);
expect(refunded).toHaveProperty('id');
expect(refunded).toHaveProperty('payment_id', request.payment_id);
expect(refunded).toHaveProperty('amount', request.body.amount);
expect(refunded).toEqual(expect.objectContaining({
payment_id: expect.any(Number),
date_created: expect.any(String),
id: expect.any(Number),
amount: expect.any(Number),
source: expect.objectContaining({
id: expect.any(String),
name: expect.any(String),
type: expect.any(String),
}),
refund_mode: expect.any(String),
adjustment_amount: expect.any(Number),
status: expect.any(String),
amount_refunded_to_payer: expect.any(Number),
}));
} catch(e) {
console.log(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();
}
});
101 changes: 101 additions & 0 deletions e2e/paymentRefund/get.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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 type { PaymentRefundGetData } from '@src/clients/paymentRefund/get/types';

describe('IT refunds, get', () => {
test('should make a request and match response object', async () => {
const client = new MercadoPago({ accessToken: config.access_token });
const refund = new PaymentRefund(client);
const payment = new Payment(client);
try {
const cardToken = await createCardToken();
expect(cardToken).toHaveProperty('id');

const paymentBody = {
body: {
additional_info: {
items: [
{
id: 'MLB2907679857',
title: 'Point Mini',
quantity: 1,
unit_price: 70
}
]
},
payer: {
email: '[email protected]',
},
transaction_amount: 140.00,
installments: 1,
token: cardToken.id,
payment_method_id: 'master',
}
};
const createdPayment = await payment.create(paymentBody);
expect(createdPayment).toHaveProperty('id');

const requestRefund: PaymentRefundCreateData = {
payment_id: createdPayment.id,
body: {
amount: 5
},
};
const refunded = await refund.create(requestRefund);
expect(refunded).toHaveProperty('id');

const requestRefundGet: PaymentRefundGetData = {
payment_id: refunded.payment_id,
refund_id: refunded.id,
};

const getRefund = await refund.get(requestRefundGet);
expect(getRefund).toHaveProperty('id');
expect(getRefund).toHaveProperty('payment_id', requestRefundGet.payment_id);
expect(getRefund).toHaveProperty('id', requestRefundGet.refund_id);
expect(getRefund).toEqual(expect.objectContaining({
payment_id: expect.any(Number),
date_created: expect.any(String),
id: expect.any(Number),
amount: expect.any(Number),
source: expect.objectContaining({
id: expect.any(String),
name: expect.any(String),
type: expect.any(String),
}),
refund_mode: expect.any(String),
adjustment_amount: expect.any(Number),
status: expect.any(String),
amount_refunded_to_payer: expect.any(Number),
}));
} catch (e) {
console.log(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();
}
});
101 changes: 101 additions & 0 deletions e2e/paymentRefund/list.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
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 type { PaymentRefundListData } from '@src/clients/paymentRefund/list/types';

describe('IT refunds, list', () => {
test('should make a request, return a list and match response object', async () => {
const client = new MercadoPago({ accessToken: config.access_token });
const refund = new PaymentRefund(client);
const payment = new Payment(client);
try {
const cardToken = await createCardToken();
expect(cardToken).toHaveProperty('id');

const paymentBody = {
body: {
additional_info: {
items: [
{
id: 'MLB2907679857',
title: 'Point Mini',
quantity: 1,
unit_price: 58.8
}
]
},
payer: {
email: '[email protected]',
},
transaction_amount: 110.00,
installments: 1,
token: cardToken.id,
payment_method_id: 'master',
}
};

const createdPayment = await payment.create(paymentBody);
expect(createdPayment).toHaveProperty('id');

const requestRefund: PaymentRefundCreateData = {
payment_id: createdPayment.id,
body: {
amount: 5
},
};
const refunded = await refund.create(requestRefund);
expect(refunded).toHaveProperty('id');

const requestList: PaymentRefundListData = {
payment_id: refunded.payment_id,
};

const refundList = await refund.list(requestList);
expect(refundList[0]).toHaveProperty('payment_id', requestList.payment_id);
expect(Array.isArray(refundList)).toBe(true);
expect(refundList[0]).toEqual(expect.objectContaining({
id: expect.any(Number),
payment_id: expect.any(Number),
date_created: expect.any(String),
amount: expect.any(Number),
source: expect.objectContaining({
id: expect.any(String),
name: expect.any(String),
type: expect.any(String),
}),
refund_mode: expect.any(String),
adjustment_amount: expect.any(Number),
status: expect.any(String),
amount_refunded_to_payer: expect.any(Number),
})
);
} catch(e) {
console.log(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();
}
});
97 changes: 97 additions & 0 deletions e2e/paymentRefund/total.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import MercadoPago, { Payment, PaymentRefund } from '@src/index';
import fetch from 'node-fetch';
import { config } from '../e2e.config';
import type { PaymentCreateData } from '@src/clients/payment/create/types';
import type { PaymentRefundTotalData } from '@src/clients/paymentRefund/total/types';

describe('IT refunds, total', () => {
test('should make a request, refund total amount and match response object', async () => {
const client = new MercadoPago({ accessToken: config.access_token });
const refund = new PaymentRefund(client);
const payment = new Payment(client);
try {
const cardToken = await createCardToken();
expect(cardToken).toHaveProperty('id');
const paymentBody = createPayment(cardToken.id);

const createdPayment = await payment.create(paymentBody);
expect(createdPayment).toHaveProperty('id');

const request: PaymentRefundTotalData = {
payment_id: createdPayment.id,
};

const refunded = await refund.total(request);
expect(refunded).toHaveProperty('id');
expect(refunded).toHaveProperty('payment_id', request.payment_id);
expect(refunded).toHaveProperty('amount', 110.00);
expect(refunded).toEqual(expect.objectContaining({
payment_id: expect.any(Number),
date_created: expect.any(String),
id: expect.any(Number),
amount: expect.any(Number),
source: expect.objectContaining({
id: expect.any(String),
name: expect.any(String),
type: expect.any(String),
}),
refund_mode: expect.any(String),
adjustment_amount: expect.any(Number),
status: expect.any(String),
amount_refunded_to_payer: expect.any(Number),
}));
} catch(e) {
console.error(e);
}
}, 10000);


function createPayment(token: string): PaymentCreateData {
const body = {
body: {
additional_info: {
items: [
{
id: 'MLB2907679857',
title: 'Point Mini',
quantity: 1,
unit_price: 58.8
}
]
},
payer: {
email: '[email protected]',
},
transaction_amount: 110.00,
installments: 1,
token: token,
payment_method_id: 'master',
}
};
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();
}
});
Loading

0 comments on commit 872b6b8

Please sign in to comment.