diff --git a/extension/src/paymentHandler/line-items-utils.js b/extension/src/paymentHandler/line-items-utils.js index 4e7466948..3efa8c1a7 100644 --- a/extension/src/paymentHandler/line-items-utils.js +++ b/extension/src/paymentHandler/line-items-utils.js @@ -47,6 +47,7 @@ function _getLocales(cart, payment) { } function _createAdyenLineItemFromLineItem(ctpLineItem, locales) { + const quantity = ctpLineItem.quantity return { id: ctpLineItem.variant.sku, quantity: ctpLineItem.quantity, @@ -55,12 +56,21 @@ function _createAdyenLineItemFromLineItem(ctpLineItem, locales) { locales, KLARNA_DEFAULT_LINE_ITEM_NAME, ), - amountIncludingTax: ctpLineItem.price.value.centAmount, + amountExcludingTax: parseFloat( + (ctpLineItem.taxedPrice.totalNet.centAmount / quantity).toFixed(0), + ), + amountIncludingTax: parseFloat( + (ctpLineItem.taxedPrice.totalGross.centAmount / quantity).toFixed(0), + ), + taxAmount: parseFloat( + (ctpLineItem.taxedPrice.totalTax.centAmount / quantity).toFixed(0), + ), taxPercentage: ctpLineItem.taxRate.amount * ADYEN_PERCENTAGE_MINOR_UNIT, } } function _createAdyenLineItemFromCustomLineItem(ctpLineItem, locales) { + const quantity = ctpLineItem.quantity return { id: ctpLineItem.id, quantity: ctpLineItem.quantity, @@ -69,7 +79,15 @@ function _createAdyenLineItemFromCustomLineItem(ctpLineItem, locales) { locales, KLARNA_DEFAULT_LINE_ITEM_NAME, ), - amountIncludingTax: ctpLineItem.money.centAmount, + amountExcludingTax: parseFloat( + (ctpLineItem.taxedPrice.totalNet.centAmount / quantity).toFixed(0), + ), + amountIncludingTax: parseFloat( + (ctpLineItem.taxedPrice.totalGross.centAmount / quantity).toFixed(0), + ), + taxAmount: parseFloat( + (ctpLineItem.taxedPrice.totalTax.centAmount / quantity).toFixed(0), + ), taxPercentage: ctpLineItem.taxRate.amount * ADYEN_PERCENTAGE_MINOR_UNIT, } } @@ -81,7 +99,9 @@ function _createShippingInfoAdyenLineItem(shippingInfo, locales) { description: _getShippingMethodDescription(shippingInfo, locales) || KLARNA_DEFAULT_SHIPPING_METHOD_DESCRIPTION, - amountIncludingTax: shippingInfo.price.centAmount, + amountExcludingTax: shippingInfo.taxedPrice.totalNet.centAmount, + amountIncludingTax: shippingInfo.taxedPrice.totalGross.centAmount, + taxAmount: shippingInfo.taxedPrice.totalTax.centAmount, taxPercentage: shippingInfo.taxRate.amount * ADYEN_PERCENTAGE_MINOR_UNIT, } } diff --git a/extension/test/unit/create-lineitems-session-request.handler.spec.js b/extension/test/unit/create-lineitems-session-request.handler.spec.js index 3ae01b80c..041da09e7 100644 --- a/extension/test/unit/create-lineitems-session-request.handler.spec.js +++ b/extension/test/unit/create-lineitems-session-request.handler.spec.js @@ -74,6 +74,23 @@ describe('create-lineitems-session-request::execute', () => { expect(ctpLineItem.price.value.centAmount).to.equal( adyenLineItem.amountIncludingTax, ) + expect( + parseFloat( + ( + ctpLineItem.price.value.centAmount / + (1 + ctpLineItem.taxRate.amount) + ).toFixed(0), + ), + ).to.equal(adyenLineItem.amountExcludingTax) + expect( + ctpLineItem.price.value.centAmount - + parseFloat( + ( + ctpLineItem.price.value.centAmount / + (1 + ctpLineItem.taxRate.amount) + ).toFixed(0), + ), + ).to.equal(adyenLineItem.taxAmount) expect(ctpLineItem.taxRate.amount * ADYEN_PERCENTAGE_MINOR_UNIT).to.equal( adyenLineItem.taxPercentage, ) @@ -85,6 +102,23 @@ describe('create-lineitems-session-request::execute', () => { expect(ctpShippingInfo.price.centAmount).to.equal( adyenShippingInfo.amountIncludingTax, ) + expect( + parseFloat( + ( + ctpShippingInfo.price.centAmount / + (1 + ctpShippingInfo.taxRate.amount) + ).toFixed(0), + ), + ).to.equal(adyenShippingInfo.amountExcludingTax) + expect( + ctpShippingInfo.price.centAmount - + parseFloat( + ( + ctpShippingInfo.price.centAmount / + (1 + ctpShippingInfo.taxRate.amount) + ).toFixed(0), + ), + ).to.equal(adyenShippingInfo.taxAmount) expect( ctpShippingInfo.taxRate.amount * ADYEN_PERCENTAGE_MINOR_UNIT, ).to.equal(adyenShippingInfo.taxPercentage) diff --git a/extension/test/unit/fixtures/ctp-cart-custom-shipping-method.json b/extension/test/unit/fixtures/ctp-cart-custom-shipping-method.json index 9febfbcb0..f671a0841 100644 --- a/extension/test/unit/fixtures/ctp-cart-custom-shipping-method.json +++ b/extension/test/unit/fixtures/ctp-cart-custom-shipping-method.json @@ -209,6 +209,12 @@ "currencyCode": "EUR", "centAmount": 1710, "fractionDigits": 2 + }, + "totalTax": { + "type": "centPrecision", + "currencyCode": "EUR", + "centAmount": 273, + "fractionDigits": 2 } }, "lineItemMode": "Standard" @@ -234,6 +240,12 @@ "centAmount": 9690, "fractionDigits": 2 }, + "totalTax": { + "type": "centPrecision", + "currencyCode": "EUR", + "centAmount": 1548, + "fractionDigits": 2 + }, "taxPortions": [ { "rate": 0.19, @@ -311,6 +323,12 @@ "currencyCode": "EUR", "centAmount": 3780, "fractionDigits": 2 + }, + "totalTax": { + "type": "centPrecision", + "currencyCode": "EUR", + "centAmount": 604, + "fractionDigits": 2 } }, "shippingMethodState": "MatchesCart" @@ -370,6 +388,12 @@ "currencyCode": "EUR", "centAmount": 4200, "fractionDigits": 2 + }, + "totalTax": { + "type": "centPrecision", + "currencyCode": "EUR", + "centAmount": 671, + "fractionDigits": 2 } } } diff --git a/extension/test/unit/fixtures/ctp-cart.json b/extension/test/unit/fixtures/ctp-cart.json index 89f8c051f..7286374f0 100644 --- a/extension/test/unit/fixtures/ctp-cart.json +++ b/extension/test/unit/fixtures/ctp-cart.json @@ -53,7 +53,7 @@ "value": { "type": "centPrecision", "currencyCode": "EUR", - "centAmount": 1000, + "centAmount": 856, "fractionDigits": 2 }, "id": "944f69af-6996-40ac-be82-9fead65e0e26" @@ -197,13 +197,19 @@ "totalNet": { "type": "centPrecision", "currencyCode": "EUR", - "centAmount": 1437, + "centAmount": 1438, "fractionDigits": 2 }, "totalGross": { "type": "centPrecision", "currencyCode": "EUR", - "centAmount": 1710, + "centAmount": 1711, + "fractionDigits": 2 + }, + "totalTax": { + "type": "centPrecision", + "currencyCode": "EUR", + "centAmount": 273, "fractionDigits": 2 } }, @@ -230,6 +236,12 @@ "centAmount": 8610, "fractionDigits": 2 }, + "totalTax": { + "type": "centPrecision", + "currencyCode": "EUR", + "centAmount": 1375, + "fractionDigits": 2 + }, "taxPortions": [ { "rate": 0.19, @@ -248,7 +260,7 @@ "price": { "type": "centPrecision", "currencyCode": "EUR", - "centAmount": 3000, + "centAmount": 2700, "fractionDigits": 2 }, "shippingRate": { @@ -357,6 +369,12 @@ "currencyCode": "EUR", "centAmount": 2700, "fractionDigits": 2 + }, + "totalTax": { + "type": "centPrecision", + "currencyCode": "EUR", + "centAmount": 431, + "fractionDigits": 2 } }, "shippingMethodState": "MatchesCart" @@ -416,6 +434,12 @@ "currencyCode": "EUR", "centAmount": 4200, "fractionDigits": 2 + }, + "totalTax": { + "type": "centPrecision", + "currencyCode": "EUR", + "centAmount": 671, + "fractionDigits": 2 } } }