Skip to content

[Feature] Endpoint Cancel #350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
root = true

[*]
indent_style = space
indent_style = tab
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
trim_trailing_whitespace = false
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
repos:
# Websec hook is MANDATORY, DO NOT comment it.
- repo: https://github.com/mercadolibre/fury_websec-git-hooks
rev: v1.0.5
- repo: https://github.com/melisource/fury_websec-git-hooks
rev: v2.0.0
hooks:
- id: pre_commit_hook
stages: [commit]
Expand Down
58 changes: 58 additions & 0 deletions e2e/order/cancel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import MercadoPago from '@src/index';
import { config } from '../e2e.config';
import { Order } from '@src/clients/order';
import { CreateOrderData } from '@src/clients/order/create/types';
import { createCardToken } from '@src/mocks/createCardToken';

const mercadoPagoConfig = new MercadoPago({ accessToken: config.access_token });

function createBodyOrder(token: string): CreateOrderData {
return {
body: {
type: 'online',
processing_mode: 'automatic',
total_amount: '200.00',
external_reference: 'ext_ref_1234',
transactions: {
payments: [
{
amount: '200.00',
payment_method: {
id: 'master',
type: 'credit_card',
token: token,
installments: 1
}
}
]
},
payer: {
email: '[email protected]'
},
type_config: {
capture_mode: 'manual'
}
}
};
}

describe('Cancel Order integration test', () => {
test('should cancel an Order successfully', async () => {
const cardTokenResponse = await createCardToken(config.access_token);
const token = cardTokenResponse.id;
const orderClient = new Order(mercadoPagoConfig);
const body = createBodyOrder(token);

const order = await orderClient.create(body);
const orderId = order.id;
const processOrder = await orderClient.cancel({ id: orderId });

expect(processOrder.id).toBeTruthy();
expect(processOrder.id).toBe(orderId);
expect(processOrder.status).toBe('cancelled');
expect(processOrder.status_detail).toBe('cancelled');
expect(processOrder.transactions.payments[0].amount).toBe('200.00');
expect(processOrder.transactions.payments[0].status).toBe('cancelled');
expect(processOrder.transactions.payments[0].status_detail).toBe('cancelled_transaction');
});
});
41 changes: 41 additions & 0 deletions e2e/order/create.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import MercadoPago from '@src/index';
import { config } from '../e2e.config';
import { Order } from '@src/clients/order';
import { CreateOrderData } from '@src/clients/order/create/types';

describe('Create Order integration test', () => {
test('should create Order', async () => {
const mercadoPagoConfig = new MercadoPago({ accessToken: config.access_token });
const order = new Order(mercadoPagoConfig);
const body: CreateOrderData = {
body: {
type: 'online',
total_amount: '1000.00',
external_reference: 'ext_ref_1234',
transactions: {
payments: [
{
amount: '1000.00',
payment_method: {
id: 'pix',
type: 'bank_transfer',
},
},
],
},
payer: {
email: '[email protected]',
},
},
};

const response = await order.create(body);

expect(response.id).toBeTruthy();
expect(response.type).toBe('online');
expect(response.total_amount).toBe('1000.00');
expect(response.external_reference).toBe('ext_ref_1234');
expect(response.transactions.payments[0].amount).toBe('1000.00');
expect(response.transactions.payments[0].payment_method.id).toBe('pix');
});
});
44 changes: 44 additions & 0 deletions e2e/order/get.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import MercadoPago from '@src/index';
import { config } from '../e2e.config';
import { Order } from '@src/clients/order';
import { CreateOrderData } from '@src/clients/order/create/types';

describe('Get Order integration test', () => {
test('should get an Order by Id', async () => {
const mercadoPagoConfig = new MercadoPago({ accessToken: config.access_token });
const orderClient = new Order(mercadoPagoConfig);
const body: CreateOrderData = {
body: {
type: 'online',
total_amount: '1000.00',
external_reference: 'ext_ref_1234',
transactions: {
payments: [
{
amount: '1000.00',
payment_method: {
id: 'pix',
type: 'bank_transfer',
},
},
],
},
payer: {
email: '[email protected]',
},
},
};

const order = await orderClient.create(body);
const orderId = order.id;
const getOrder = await orderClient.get({ id: orderId });

expect(getOrder.id).toBeTruthy();
expect(getOrder.id).toBe(orderId);
expect(getOrder.type).toBe('online');
expect(getOrder.total_amount).toBe('1000.00');
expect(getOrder.external_reference).toBe('ext_ref_1234');
expect(getOrder.transactions.payments[0].amount).toBe('1000.00');
expect(getOrder.transactions.payments[0].payment_method.id).toBe('pix');
});
});
57 changes: 57 additions & 0 deletions e2e/order/process.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import MercadoPago from '@src/index';
import { config } from '../e2e.config';
import { Order } from '@src/clients/order';
import { CreateOrderData } from '@src/clients/order/create/types';
import { createCardToken } from '@src/mocks/createCardToken';

const mercadoPagoConfig = new MercadoPago({ accessToken: config.access_token });

function createBodyOrder(token: string): CreateOrderData {
return {
body: {
type: 'online',
processing_mode: 'manual',
total_amount: '200.00',
external_reference: 'ext_ref_1234',
transactions: {
payments: [
{
amount: '200.00',
payment_method: {
id: 'master',
type: 'credit_card',
token: token,
installments: 1
}
}
]
},
payer: {
email: '[email protected]'
}
}
};
}

describe('Process Order integration test', () => {
test('should process an Order successfully', async () => {
const cardTokenResponse = await createCardToken(config.access_token);
const token = cardTokenResponse.id;
const orderClient = new Order(mercadoPagoConfig);
const body = createBodyOrder(token);

const order = await orderClient.create(body);
const orderId = order.id;
const processOrder = await orderClient.process({ id: orderId });

expect(processOrder.id).toBeTruthy();
expect(processOrder.id).toBe(orderId);
expect(processOrder.type).toBe('online');
expect(processOrder.total_amount).toBe('200.00');
expect(processOrder.status).toBe('processed');
expect(processOrder.status_detail).toBe('accredited');
expect(processOrder.external_reference).toBe('ext_ref_1234');
expect(processOrder.transactions.payments[0].amount).toBe('200.00');
expect(processOrder.transactions.payments[0].payment_method.id).toBe('master');
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"dist"
],
"dependencies": {
"node-fetch": "^2.6.12",
"node-fetch": "^2.7.0",
"uuid": "^9.0.0"
}
}
48 changes: 48 additions & 0 deletions src/clients/order/cancel/cancel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { RestClient } from '@utils/restClient';
import { MercadoPagoConfig } from '@src/mercadoPagoConfig';
import cancel from './index';
import { OrderResponse } from '../commonTypes';

jest.mock('@utils/restClient');

describe('Cancel Order', () => {
test('should cancel an Order', async () => {
const config = new MercadoPagoConfig({ accessToken: 'access_token' });
const orderId = '01JE48XZBYRK6W22V7DFQRABEV';
const mockOrderResponse: OrderResponse = {
api_response: {
status: 200,
headers: [
'Content-Type', ['application/json']
]
},
id: orderId,
type: 'online',
processing_mode: 'manual',
status: 'cancelled',
status_detail: 'cancelled',
total_amount: '200.00',
transactions: {
payments: [
{
status: 'cancelled',
status_detail: 'cancelled_transaction'
}
]
}
};
const spyFetch = jest.spyOn(RestClient, 'fetch').mockResolvedValue(mockOrderResponse);

const result = await cancel({ id: orderId, config });

expect(spyFetch).toHaveBeenCalledWith('/v1/orders/01JE48XZBYRK6W22V7DFQRABEV/cancel',
{
method: 'POST',
headers: {
Authorization: 'Bearer access_token',
}
}
);
expect(result).toEqual(mockOrderResponse);
});
});
17 changes: 17 additions & 0 deletions src/clients/order/cancel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { RestClient } from '@src/utils/restClient';
import { CancelOrderClient } from './types';
import { OrderResponse } from '../commonTypes';

export default function cancel({ id, config }:
CancelOrderClient): Promise<OrderResponse> {
return RestClient.fetch<OrderResponse>(
`/v1/orders/${id}/cancel`,
{
method: 'POST',
headers: {
'Authorization': `Bearer ${config.accessToken}`,
},
...config.options
}
);
}
11 changes: 11 additions & 0 deletions src/clients/order/cancel/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { MercadoPagoConfig } from '@src/mercadoPagoConfig';
import type { Options } from '@src/types';

export declare type CancelOrderData = {
id: string;
requestOptions?: Options;
}

export declare type CancelOrderClient = CancelOrderData & {
config: MercadoPagoConfig;
}
Loading
Loading