Skip to content

Commit

Permalink
fix(processor): apply fraction digits mapping for adyen
Browse files Browse the repository at this point in the history
  • Loading branch information
joey-koster-ct committed Dec 27, 2024
1 parent 1497f84 commit 2a7fc00
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 12 deletions.
28 changes: 28 additions & 0 deletions processor/src/constants/currencies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { describe, test, expect } from '@jest/globals';
import { CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING, CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING } from './currencies';

describe('constants.currencies', () => {
test('CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING', () => {
const expected = new Map<string, number>([
['CLP', -2],
['CVE', 2],
['IDR', 2],
['ISK', -2],
['CVE', 2],
]);

expect(CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING).toEqual(expected);
});

test('CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING', () => {
const expected = new Map<string, number>([
['CLP', 2],
['CVE', -2],
['IDR', -2],
['ISK', 2],
['CVE', -2],
]);

expect(CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING).toEqual(expected);
});
});
20 changes: 20 additions & 0 deletions processor/src/constants/currencies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Mappings are based on the deviatons from https://en.wikipedia.org/wiki/ISO_4217 defined on the Adyen table defined https://docs.adyen.com/development-resources/currency-codes/#note

// TODO: SCC-2800: figure out if we can move some of the mapping stuff to here using a updated connect-payments-sdk
const CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING = new Map<string, number>([
['CLP', -2],
['CVE', 2],
['IDR', 2],
['ISK', -2],
['CVE', 2],
]);

const CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING = new Map<string, number>([
['CLP', 2],
['CVE', -2],
['IDR', -2],
['ISK', 2],
['CVE', -2],
]);

export { CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING, CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING };
4 changes: 2 additions & 2 deletions processor/src/services/adyen-payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class AdyenPaymentService extends AbstractPaymentService {
try {
const res = await AdyenApi().PaymentsApi.sessions(adyenRequestData);
return {
sessionData: res,
sessionData: this.createSessionConverter.convertResponse({ response: res }),
paymentReference: ctPayment.id,
};
} catch (e) {
Expand Down Expand Up @@ -379,7 +379,7 @@ export class AdyenPaymentService extends AbstractPaymentService {
public async processNotification(opts: { data: NotificationRequestDTO }): Promise<void> {
log.info('Processing notification', { notification: JSON.stringify(opts.data) });
try {
const updateData = await this.notificationConverter.convert(opts);
const updateData = this.notificationConverter.convert(opts);

for (const tx of updateData.transactions) {
const updatedPayment = await this.ctPaymentService.updatePayment({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
CommercetoolsCartService,
CommercetoolsOrderService,
ErrorReferencedResourceNotFound,
MoneyConverters,
} from '@commercetools/connect-payments-sdk';
import { CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING } from '../../constants/currencies';

/**
* These payment methods require line items to be send to Adyen for capturing payments
Expand Down Expand Up @@ -39,7 +41,11 @@ export class CapturePaymentConverter {
reference: opts.payment.id,
amount: {
currency: opts.amount.currencyCode,
value: opts.amount.centAmount,
value: MoneyConverters.convertWithMapping(
CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING,
opts.amount.centAmount,
opts.amount.currencyCode,
),
},
lineItems: adyenLineItems,
};
Expand Down
9 changes: 7 additions & 2 deletions processor/src/services/converters/create-payment.converter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { PaymentRequest } from '@adyen/api-library/lib/src/typings/checkout/paymentRequest';
import { config } from '../../config/config';
import { ThreeDSRequestData } from '@adyen/api-library/lib/src/typings/checkout/threeDSRequestData';
import { Cart, Payment } from '@commercetools/connect-payments-sdk';
import { Cart, MoneyConverters, Payment } from '@commercetools/connect-payments-sdk';
import { buildReturnUrl, mapCoCoCartItemsToAdyenLineItems, populateCartAddress } from './helper.converter';
import { CreatePaymentRequestDTO } from '../../dtos/adyen-payment.dto';
import { getFutureOrderNumberFromContext } from '../../libs/fastify/context/context';
import { CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING } from '../../constants/currencies';

export class CreatePaymentConverter {
public convertRequest(opts: { data: CreatePaymentRequestDTO; cart: Cart; payment: Payment }): PaymentRequest {
Expand All @@ -14,7 +15,11 @@ export class CreatePaymentConverter {
return {
...requestData,
amount: {
value: opts.payment.amountPlanned.centAmount,
value: MoneyConverters.convertWithMapping(
CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING,
opts.payment.amountPlanned.centAmount,
opts.payment.amountPlanned.currencyCode,
),
currency: opts.payment.amountPlanned.currencyCode,
},
reference: opts.payment.id,
Expand Down
24 changes: 22 additions & 2 deletions processor/src/services/converters/create-session.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
mapCoCoCartItemsToAdyenLineItems,
} from './helper.converter';
import { CreateSessionRequestDTO } from '../../dtos/adyen-payment.dto';
import { Cart, Payment } from '@commercetools/connect-payments-sdk';
import { Cart, MoneyConverters, Payment } from '@commercetools/connect-payments-sdk';
import { getFutureOrderNumberFromContext } from '../../libs/fastify/context/context';
import { CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING, CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING } from '../../constants/currencies';
import { CreateCheckoutSessionResponse } from '@adyen/api-library/lib/src/typings/checkout/createCheckoutSessionResponse';

export class CreateSessionConverter {
public convertRequest(opts: {
Expand All @@ -21,7 +23,11 @@ export class CreateSessionConverter {
return {
...opts.data,
amount: {
value: opts.payment.amountPlanned.centAmount,
value: MoneyConverters.convertWithMapping(
CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING,
opts.payment.amountPlanned.centAmount,
opts.payment.amountPlanned.currencyCode,
),
currency: opts.payment.amountPlanned.currencyCode,
},
reference: opts.payment.id,
Expand All @@ -41,4 +47,18 @@ export class CreateSessionConverter {
...(futureOrderNumber && { merchantOrderReference: futureOrderNumber }),
};
}

public convertResponse(opts: { response: CreateCheckoutSessionResponse }): CreateCheckoutSessionResponse {
return {
...opts.response,
amount: {
value: MoneyConverters.convertWithMapping(
CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING,
opts.response.amount.value,
opts.response.amount.currency,
),
currency: opts.response.amount.currency,
},
};
}
}
1 change: 1 addition & 0 deletions processor/src/services/converters/helper.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const mapCoCoOrderItemsToAdyenLineItems = (
export const mapCoCoCartItemsToAdyenLineItems = (
cart: Pick<Cart, 'lineItems' | 'customLineItems' | 'shippingInfo' | 'discountOnTotalPrice'>,
): LineItem[] => {
// TODO: SCC-2800: fix amount parsing values
const aydenLineItems: LineItem[] = [];

cart.lineItems.forEach((lineItem) => aydenLineItems.push(mapCoCoLineItemToAdyenLineItem(lineItem)));
Expand Down
11 changes: 9 additions & 2 deletions processor/src/services/converters/notification.converter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { NotificationRequestItem } from '@adyen/api-library/lib/src/typings/notification/notificationRequestItem';
import { NotificationRequestDTO } from '../../dtos/adyen-payment.dto';
import { TransactionData, Money } from '@commercetools/connect-payments-sdk';
import { TransactionData, Money, MoneyConverters } from '@commercetools/connect-payments-sdk';
import { UnsupportedNotificationError } from '../../errors/adyen-api.error';
import { paymentMethodConfig } from '../../config/payment-method.config';
import { NotificationUpdatePayment } from '../types/service.type';
import { CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING } from '../../constants/currencies';

export class NotificationConverter {
public convert(opts: { data: NotificationRequestDTO }): NotificationUpdatePayment {
Expand Down Expand Up @@ -107,8 +108,14 @@ export class NotificationConverter {
}

private populateAmount(item: NotificationRequestItem): Money {
const isoCorrectedCentAmount = MoneyConverters.convertWithMapping(
CURRENCIES_FROM_ADYEN_TO_ISO_MAPPING,
item.amount.value as number,
item.amount.currency as string,
);

return {
centAmount: item.amount.value as number,
centAmount: isoCorrectedCentAmount,
currencyCode: item.amount.currency as string,
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { config } from '../../config/config';
import { PaymentMethodsRequest } from '@adyen/api-library/lib/src/typings/checkout/paymentMethodsRequest';
import { CommercetoolsCartService } from '@commercetools/connect-payments-sdk';
import { CommercetoolsCartService, MoneyConverters } from '@commercetools/connect-payments-sdk';
import { convertPaymentMethodFromAdyenFormat } from './helper.converter';
import { getAllowedPaymentMethodsFromContext, getCartIdFromContext } from '../../libs/fastify/context/context';
import { PaymentMethodsRequestDTO, PaymentMethodsResponseDTO } from '../../dtos/adyen-payment.dto';
import { PaymentMethodsResponse } from '@adyen/api-library/lib/src/typings/checkout/paymentMethodsResponse';
import { CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING } from '../../constants/currencies';

export class PaymentMethodsConverter {
private ctCartService: CommercetoolsCartService;
Expand All @@ -24,7 +25,11 @@ export class PaymentMethodsConverter {
return {
...opts.data,
amount: {
value: paymentAmount.centAmount,
value: MoneyConverters.convertWithMapping(
CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING,
paymentAmount.centAmount,
paymentAmount.currencyCode,
),
currency: paymentAmount.currencyCode,
},
countryCode: cart.country,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { config } from '../../config/config';
import { PaymentRefundRequest } from '@adyen/api-library/lib/src/typings/checkout/paymentRefundRequest';
import { RefundPaymentRequest } from '../types/operation.type';
import { MoneyConverters } from '@commercetools/connect-payments-sdk';
import { CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING } from '../../constants/currencies';

export class RefundPaymentConverter {
public convertRequest(opts: RefundPaymentRequest): PaymentRefundRequest {
Expand All @@ -9,7 +11,11 @@ export class RefundPaymentConverter {
reference: opts.payment.id,
amount: {
currency: opts.amount.currencyCode,
value: opts.amount.centAmount,
value: MoneyConverters.convertWithMapping(
CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING,
opts.amount.centAmount,
opts.amount.currencyCode,
),
},
};
}
Expand Down

0 comments on commit 2a7fc00

Please sign in to comment.