Skip to content

Commit

Permalink
fix(processor): fix the tax rate to taxPercentage converter logic to …
Browse files Browse the repository at this point in the history
…take into account decimal rates
  • Loading branch information
joey-koster-ct committed Feb 5, 2025
1 parent 36961a4 commit ab2f4c8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 45 deletions.
48 changes: 4 additions & 44 deletions processor/src/services/converters/helper.converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Order,
NormalizedShipping,
CurrencyConverters,
TaxRateConverter,
} from '@commercetools/connect-payments-sdk';
import {
getAllowedPaymentMethodsFromContext,
Expand All @@ -26,9 +27,8 @@ export const mapCoCoLineItemToAdyenLineItem = (lineItem: CoCoLineItem): LineItem
amountExcludingTax: getItemAmount(getAmountExcludingTax(lineItem), lineItem.quantity),
amountIncludingTax: getItemAmount(getAmountIncludingTax(lineItem), lineItem.quantity),
taxAmount: getItemAmount(getTaxAmount(lineItem), lineItem.quantity),
taxPercentage: convertTaxPercentageToAdyenMinorUnits(
taxPercentage: TaxRateConverter.convertCoCoTaxPercentage(
lineItem.totalPrice.fractionDigits,
lineItem.totalPrice.currencyCode,
lineItem.taxRate?.amount,
),
};
Expand All @@ -42,9 +42,8 @@ export const mapCoCoCustomLineItemToAdyenLineItem = (customLineItem: CustomLineI
amountExcludingTax: getItemAmount(getAmountExcludingTax(customLineItem), customLineItem.quantity),
amountIncludingTax: getItemAmount(getAmountIncludingTax(customLineItem), customLineItem.quantity),
taxAmount: getItemAmount(getTaxAmount(customLineItem), customLineItem.quantity),
taxPercentage: convertTaxPercentageToAdyenMinorUnits(
taxPercentage: TaxRateConverter.convertCoCoTaxPercentage(
customLineItem.totalPrice.fractionDigits,
customLineItem.totalPrice.currencyCode,
customLineItem.taxRate?.amount,
),
};
Expand Down Expand Up @@ -85,9 +84,8 @@ export const mapCoCoShippingInfoToAdyenLineItem = (normalizedShippings: Normaliz
amountExcludingTax: amountExcludingTaxValue,
amountIncludingTax: amountIncludingTaxValue,
taxAmount: taxAmountValue,
taxPercentage: convertTaxPercentageToAdyenMinorUnits(
taxPercentage: TaxRateConverter.convertCoCoTaxPercentage(
shipping.shippingInfo.price.fractionDigits,
shipping.shippingInfo.price.currencyCode,
shipping.shippingInfo.taxRate?.amount,
),
};
Expand Down Expand Up @@ -303,41 +301,3 @@ const getTaxAmount = (lineItem: CoCoLineItem | CustomLineItem): number => {
currencyCode: lineItem.taxedPrice.totalTax.currencyCode,
});
};

/**
* Convert the CoCo tax percentage, which ranges from 0-1 as floating point numbers to the expected Adyen minor units.
*
* This function applies the given fractionDigit to get the correct minor units. This also takes into account the deviations Adyen has with regards to the fractionDigit.
*
* @example CoCo taxRate of 0.21, normalized is 21% with currencyCode EUR and fractionDigit of 2 = expressed in Adyen minor units value as 2100
* @example CoCo taxRate of 0.21, normalized is 21% with currencyCode CLP, according to ISO standard has fractionDigit of 0 but Adyen deviats from it and so it has fractionDigit of 2 = expressed in Adyen minor units value as 2100
* @example CoCo taxRate of 0.21, normalized is 21% with currencyCode JPY and fractionDigit of 0 = expressed in Adyen minor units value as 21
*
* @param fractionDigit the fractionDigit that are applicable for the currencyCode according to ISO_4217
* @param currencyCode the applicable currencyCode
* @param decimalTaxRate the tax rate expressed in decimals between 0-1 as floating point numbers taken from the CoCo taxRate (see docs below)
*
* @see https://docs.commercetools.com/api/projects/taxCategories#ctp:api:type:TaxRate
* @see https://docs.adyen.com/api-explorer/Checkout/latest/post/sessions#request-lineItems-taxPercentage
* @see https://docs.adyen.com/development-resources/currency-codes/#minor-units
*/
const convertTaxPercentageToAdyenMinorUnits = (
fractionDigit: number,
currencyCode: string,
decimalTaxRate?: number,
): number => {
if (!decimalTaxRate) {
return 0;
}
// First go from the range of 0 - 1 (floating numbers) to value expressed as "non-decimals". I.e. 0.15% from CoCo becomes 15% tax rate value.
const normalizedTaxRate = decimalTaxRate * 100;

const result = CurrencyConverters.convertWithMapping({
mapping: CURRENCIES_FROM_ISO_TO_ADYEN_MAPPING,
amount: normalizedTaxRate,
currencyCode,
fractionDigit,
});

return result;
};
3 changes: 2 additions & 1 deletion processor/test/services/converters/helper.converter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ describe('helper.converter', () => {
});

test('should map the CoCo line items to Adyen line items taking into account Adyen deviations', () => {
// TODO: SCC-2901: once answer given from Adyen, adjust this unit-test accordingly.
// CLP currencyCode according to ISO_4217 has 0 fractionDigits but Adyen expects 2 fractionDigits
const input = CoCoCartCLPJSON.lineItems as CoCoLineItem[];

Expand All @@ -275,7 +276,7 @@ describe('helper.converter', () => {
amountExcludingTax: 13400,
amountIncludingTax: 15000,
taxAmount: 1600,
taxPercentage: 1200,
taxPercentage: 12,
};

expect(actual).toEqual(expected);
Expand Down

0 comments on commit ab2f4c8

Please sign in to comment.