Skip to content

Commit

Permalink
Add unit test for capture payment and refund payment
Browse files Browse the repository at this point in the history
  • Loading branch information
leungkinghin-ct committed Mar 12, 2024
1 parent 3b636bb commit 9a59101
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
62 changes: 60 additions & 2 deletions processor/test/services/adyen-payment.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { describe, test, expect, afterEach, jest, beforeEach } from '@jest/globa
import { ConfigResponse, ModifyPayment, StatusResponse } from '../../src/services/types/operation.type';
import { paymentSDK } from '../../src/payment-sdk';
import { DefaultPaymentService } from '@commercetools/connect-payments-sdk/dist/commercetools/services/ct-payment.service';
import { mockGetPaymentResult, mockUpdatePaymentResult, mockAdyenPaymentMethodsResponse, mockAdyenCancelPaymentResponse } from '../utils/mock-payment-data';
import {
mockGetPaymentResult,
mockUpdatePaymentResult,
mockAdyenPaymentMethodsResponse,
mockAdyenCancelPaymentResponse,
mockAdyenCapturePaymentResponse,
mockAdyenRefundPaymentResponse
} from '../utils/mock-payment-data';
import * as Config from '../../src/config/config';
import { AbstractPaymentService } from '../../src/services/abstract-payment.service';
import { AdyenPaymentService } from '../../src/services/adyen-payment.service';
Expand All @@ -19,6 +26,7 @@ import {
} from '@commercetools/connect-payments-sdk';
import {SupportedPaymentComponentsSchemaDTO} from "../../src/dtos/operations/payment-componets.dto";


interface FlexibleConfig {
[key: string]: string; // Adjust the type according to your config values
}
Expand Down Expand Up @@ -99,7 +107,7 @@ describe('adyen-payment.service', () => {
expect(result?.checks[1]?.details).toBeDefined();
});

test('modifyPayment', async () => {
test('cancelPayment', async () => {
const modifyPaymentOpts: ModifyPayment = {
paymentId: 'dummy-paymentId',
data: {
Expand All @@ -119,4 +127,54 @@ describe('adyen-payment.service', () => {
const result = await paymentService.modifyPayment(modifyPaymentOpts);
expect(result?.outcome).toStrictEqual('received');
});

test('capturePayment', async () => {
const modifyPaymentOpts: ModifyPayment = {
paymentId: 'dummy-paymentId',
data: {
actions: [
{
action: 'capturePayment',
amount: {
centAmount: 150000,
currencyCode: 'USD',
},
},
],
},
};

jest.spyOn(DefaultPaymentService.prototype, 'getPayment').mockResolvedValue(mockGetPaymentResult);
jest.spyOn(DefaultPaymentService.prototype, 'updatePayment').mockResolvedValue(mockUpdatePaymentResult);
jest.spyOn(DefaultPaymentService.prototype, 'updatePayment').mockResolvedValue(mockUpdatePaymentResult);
jest.spyOn(ModificationsApi.prototype, 'captureAuthorisedPayment').mockResolvedValue(mockAdyenCapturePaymentResponse);

const result = await paymentService.modifyPayment(modifyPaymentOpts);
expect(result?.outcome).toStrictEqual('received');
});

test('refundPayment', async () => {
const modifyPaymentOpts: ModifyPayment = {
paymentId: 'dummy-paymentId',
data: {
actions: [
{
action: 'refundPayment',
amount: {
centAmount: 150000,
currencyCode: 'USD',
},
},
],
},
};

jest.spyOn(DefaultPaymentService.prototype, 'getPayment').mockResolvedValue(mockGetPaymentResult);
jest.spyOn(DefaultPaymentService.prototype, 'updatePayment').mockResolvedValue(mockUpdatePaymentResult);
jest.spyOn(DefaultPaymentService.prototype, 'updatePayment').mockResolvedValue(mockUpdatePaymentResult);
jest.spyOn(ModificationsApi.prototype, 'refundCapturedPayment').mockResolvedValue(mockAdyenRefundPaymentResponse);

const result = await paymentService.modifyPayment(modifyPaymentOpts);
expect(result?.outcome).toStrictEqual('received');
});
});
28 changes: 28 additions & 0 deletions processor/test/utils/mock-payment-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Payment } from '@commercetools/platform-sdk';
import { Transaction } from '@commercetools/platform-sdk/dist/declarations/src';
import { PaymentCancelResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentCancelResponse'
import { PaymentMethodsResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentMethodsResponse';
import { PaymentCaptureResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentCaptureResponse';
import { PaymentRefundResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentRefundResponse';

export const mockGetPaymentResult: Payment = {
id: '123456',
Expand Down Expand Up @@ -68,3 +70,29 @@ export const mockAdyenCancelPaymentResponse : PaymentCancelResponse = {
export const mockAdyenPaymentMethodsResponse : PaymentMethodsResponse = {
paymentMethods : [{ name : 'card'}]
}

export const mockAdyenCapturePaymentResponse : PaymentCaptureResponse = {
status : PaymentCaptureResponse.StatusEnum.Received,
paymentPspReference : '24680',
pspReference: '123456',
merchantAccount: 'ABC',
reference: '123456',
amount: {
currency: 'USD',
value: 150000
},
}


export const mockAdyenRefundPaymentResponse : PaymentRefundResponse = {
status : PaymentRefundResponse.StatusEnum.Received,
paymentPspReference : '24680',
pspReference: '123456',
merchantAccount: 'ABC',
reference: '123456',
amount: {
currency: 'USD',
value: 150000
},
}

0 comments on commit 9a59101

Please sign in to comment.