pcp-server-nodejs-sdk
Version:
[](https://sonarcloud.io/summary/new_code?id=PAYONE-GmbH_PCP-server-nodeJS-SDK) [ || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import fetch from 'node-fetch';
import { beforeEach, describe, expect, test, vi } from 'vitest';
import { CommunicatorConfiguration } from '../CommunicatorConfiguration.js';
import { PaymentChannel, PaymentType, } from '../models/index.js';
import { createResponseMock, createEmptyErrorResponseMock } from '../testutils/mock-response.js';
import { ApiErrorResponseException } from '../errors/ApiErrorResponseException.js';
import { ApiResponseRetrievalException } from '../errors/ApiResponseRetrievalException.js';
import { PaymentInformationApiClient } from './PaymentInformationApiClient.js';
vi.mock('node-fetch', (importOriginal) => __awaiter(void 0, void 0, void 0, function* () {
return Object.assign(Object.assign({}, (yield importOriginal())), { default: vi.fn() });
}));
const mockedFetch = vi.mocked(fetch, true);
describe('PaymentInformationApiClient', () => {
let paymentInformationApiClient;
beforeEach(() => {
paymentInformationApiClient = new PaymentInformationApiClient(new CommunicatorConfiguration('apiKey', 'apiSecret', 'https://test.com'));
});
describe('createPaymentInformation', () => {
const payload = {
amountOfMoney: {
amount: 1289,
currencyCode: 'EUR',
},
type: PaymentType.Sale,
paymentProductId: 1331,
paymentChannel: PaymentChannel.ECOMMERCE,
};
test('given request was successful, should return response', () => __awaiter(void 0, void 0, void 0, function* () {
const expectedResponse = {
paymentChannel: PaymentChannel.POS,
terminalId: 'terminalId',
cardPaymentDetails: {
maskedCardNumber: '672559XXXXXX1108',
paymentProcessingToken: '0ca037cc-9079-4df7-8f6f-f2a3443ee521',
reportingToken: '12a037cc-833d-8b45-8f6f-11c34171f4e1',
cardAuthorizationId: '260042',
},
events: [],
};
mockedFetch.mockResolvedValueOnce(createResponseMock(200, expectedResponse));
const res = yield paymentInformationApiClient.createPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', payload);
expect(res).toEqual(expectedResponse);
}));
test('given request was not successful (400), then return errorresponse', () => __awaiter(void 0, void 0, void 0, function* () {
const expectedResponse = { errorId: 'error-id' };
mockedFetch.mockResolvedValueOnce(createResponseMock(400, expectedResponse));
expect.assertions(1);
try {
yield paymentInformationApiClient.createPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', payload);
}
catch (error) {
expect(error).toEqual(new ApiErrorResponseException(400, JSON.stringify(expectedResponse)));
}
}));
test('given request was not successful (500), then return errorresponse', () => __awaiter(void 0, void 0, void 0, function* () {
mockedFetch.mockResolvedValueOnce(createEmptyErrorResponseMock(500));
expect.assertions(1);
try {
yield paymentInformationApiClient.createPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', payload);
}
catch (error) {
expect(error).toEqual(new ApiResponseRetrievalException(500, ''));
}
}));
test('a required param is empty, throw an error', () => __awaiter(void 0, void 0, void 0, function* () {
yield expect(() => paymentInformationApiClient.createPaymentInformation('', 'commerceCaseId', 'checkoutId', payload)).rejects.toThrowError('Merchant ID is required');
yield expect(() => paymentInformationApiClient.createPaymentInformation('merchantId', '', 'checkoutId', payload)).rejects.toThrowError('Commerce Case ID is required');
yield expect(() => paymentInformationApiClient.createPaymentInformation('merchantId', 'commerceCaseId', '', payload)).rejects.toThrowError('Checkout ID is required');
}));
});
describe('getPaymentInformation', () => {
test('given request was successful, should return response', () => __awaiter(void 0, void 0, void 0, function* () {
const expectedResponse = {
commerceCaseId: 'commerceCaseId',
checkoutId: 'checkoutId',
paymentProductId: 4040,
};
mockedFetch.mockResolvedValueOnce(createResponseMock(200, expectedResponse));
const res = yield paymentInformationApiClient.getPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', 'paymentInformationId');
expect(res).toEqual(expectedResponse);
}));
test('given request was not successful (400), then return errorresponse', () => __awaiter(void 0, void 0, void 0, function* () {
const expectedResponse = { errorId: 'error-id' };
mockedFetch.mockResolvedValueOnce(createResponseMock(400, expectedResponse));
expect.assertions(1);
try {
yield paymentInformationApiClient.getPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', 'paymentInformationId');
}
catch (error) {
expect(error).toEqual(new ApiErrorResponseException(400, JSON.stringify(expectedResponse)));
}
}));
test('given request was not successful (500), then return errorresponse', () => __awaiter(void 0, void 0, void 0, function* () {
mockedFetch.mockResolvedValueOnce(createEmptyErrorResponseMock(500));
expect.assertions(1);
try {
yield paymentInformationApiClient.getPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', 'paymentInformationId');
}
catch (error) {
expect(error).toEqual(new ApiResponseRetrievalException(500, ''));
}
}));
test('a required param is empty, throw an error', () => __awaiter(void 0, void 0, void 0, function* () {
yield expect(() => paymentInformationApiClient.getPaymentInformation('', 'commerceCaseId', 'checkoutId', 'paymentInformationId')).rejects.toThrowError('Merchant ID is required');
yield expect(() => paymentInformationApiClient.getPaymentInformation('merchantId', '', 'checkoutId', 'paymentInformationId')).rejects.toThrowError('Commerce Case ID is required');
yield expect(() => paymentInformationApiClient.getPaymentInformation('merchantId', 'commerceCaseId', '', 'paymentInformationId')).rejects.toThrowError('Checkout ID is required');
yield expect(() => paymentInformationApiClient.getPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', '')).rejects.toThrowError('Payment Information ID is required');
}));
});
describe('refundPaymentInformation', () => {
const payload = {
amountOfMoney: {
amount: 1289,
currencyCode: 'EUR',
},
};
test('given request was successful, should return response', () => __awaiter(void 0, void 0, void 0, function* () {
const expectedResponse = {
payment: {},
};
mockedFetch.mockResolvedValueOnce(createResponseMock(200, expectedResponse));
const res = yield paymentInformationApiClient.refundPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', 'paymentInformationId', payload);
expect(res).toEqual(expectedResponse);
}));
test('given request was not successful (400), then return errorresponse', () => __awaiter(void 0, void 0, void 0, function* () {
const expectedResponse = { errorId: 'error-id' };
mockedFetch.mockResolvedValueOnce(createResponseMock(400, expectedResponse));
expect.assertions(1);
try {
yield paymentInformationApiClient.refundPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', 'paymentInformationId', payload);
}
catch (error) {
expect(error).toEqual(new ApiErrorResponseException(400, JSON.stringify(expectedResponse)));
}
}));
test('given request was not successful (500), then return errorresponse', () => __awaiter(void 0, void 0, void 0, function* () {
mockedFetch.mockResolvedValueOnce(createEmptyErrorResponseMock(500));
expect.assertions(1);
try {
yield paymentInformationApiClient.refundPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', 'paymentInformationId', payload);
}
catch (error) {
expect(error).toEqual(new ApiResponseRetrievalException(500, ''));
}
}));
test('a required param is empty, throw an error', () => __awaiter(void 0, void 0, void 0, function* () {
yield expect(() => paymentInformationApiClient.refundPaymentInformation('', 'commerceCaseId', 'checkoutId', 'paymentInformationId', payload)).rejects.toThrowError('Merchant ID is required');
yield expect(() => paymentInformationApiClient.refundPaymentInformation('merchantId', '', 'checkoutId', 'paymentInformationId', payload)).rejects.toThrowError('Commerce Case ID is required');
yield expect(() => paymentInformationApiClient.refundPaymentInformation('merchantId', 'commerceCaseId', '', 'paymentInformationId', payload)).rejects.toThrowError('Checkout ID is required');
yield expect(() => paymentInformationApiClient.refundPaymentInformation('merchantId', 'commerceCaseId', 'checkoutId', '', payload)).rejects.toThrowError('Payment Information ID is required');
}));
});
});
//# sourceMappingURL=PaymentInformationApiClient.test.js.map