Skip to content

Commit

Permalink
Merge pull request #101 from mediaopt/bugfix/37560-submit-0pice-items…
Browse files Browse the repository at this point in the history
…-to-PayPal

Bugfix/37560 submit 0 price items to pay pal
  • Loading branch information
LiudmylaMasliuk authored Jan 17, 2025
2 parents 9793502 + 737a224 commit dc8f117
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 21 deletions.
2 changes: 1 addition & 1 deletion paypal-commercetools-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"dependencies": {
"@commercetools-backend/loggers": "^22.35.0",
"@commercetools/platform-sdk": "^4.0.0",
"@commercetools/platform-sdk": "^7.22.0",
"@commercetools/sdk-client-v2": "^2.0.0",
"@types/nodemailer": "^6.4.14",
"axios": "^1.5.1",
Expand Down
26 changes: 18 additions & 8 deletions paypal-commercetools-extension/src/utils/map.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ const mapCommercetoolsLineItemsToPayPalItems = (
return {
unit_amount: {
value: mapCommercetoolsMoneyToPayPalMoney({
centAmount: taxedNetAmount != null
? taxedNetAmount / lineItem.quantity
: lineItem.price.value.centAmount,
centAmount:
taxedNetAmount != null
? taxedNetAmount / lineItem.quantity
: lineItem.price.value.centAmount,
fractionDigits,
currencyCode,
type: lineItem.price.value.type,
Expand Down Expand Up @@ -184,19 +185,17 @@ export const mapValidCommercetoolsLineItemsToPayPalItems = (
if (!matchingAmounts || !lineItems) {
return null;
}
const relevantLineItems = lineItems.filter(
({ lineItemMode }) => lineItemMode !== 'GiftLineItem'
);
const payPalItems = relevantLineItems.map((lineItem) =>
const payPalItems = lineItems.map((lineItem) =>
mapCommercetoolsLineItemsToPayPalItems(lineItem, isShipped, locale)
);
return payPalItems.some((item) => parseFloat(item.unit_amount.value) <= 0)
return payPalItems.some((item) => parseFloat(item.unit_amount.value) < 0)
? null
: payPalItems;
};

export const mapCommercetoolsCartToPayPalPriceBreakdown = ({
lineItems,
discountOnTotalPrice,
taxedShippingPrice,
}: Cart) => {
if (!lineItems || !lineItems[0]) {
Expand Down Expand Up @@ -239,6 +238,17 @@ export const mapCommercetoolsCartToPayPalPriceBreakdown = ({
type,
} as TypedMoney),
},
discount: discountOnTotalPrice
? {
currency_code: currencyCode,
value: mapCommercetoolsMoneyToPayPalMoney({
centAmount: discountOnTotalPrice.discountedAmount.centAmount ?? 0,
fractionDigits,
currencyCode,
type,
} as TypedMoney),
}
: undefined,
};
};

Expand Down
35 changes: 35 additions & 0 deletions paypal-commercetools-extension/tests/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1361,3 +1361,38 @@ export const discountedLineItems = [
taxedPricePortions: [],
},
];

export const discountOnTotalPrice = {
discountedAmount: {
type: 'centPrecision',
currencyCode: 'EUR',
centAmount: 500,
fractionDigits: 2,
},
includedDiscounts: [
{
discount: {
typeId: 'cart-discount',
id: 'c4b96843-e2ae-4c3c-987e-7c9819a13a40',
},
discountedAmount: {
type: 'centPrecision',
currencyCode: 'EUR',
centAmount: 500,
fractionDigits: 2,
},
},
],
discountedNetAmount: {
type: 'centPrecision',
currencyCode: 'EUR',
centAmount: 420,
fractionDigits: 2,
},
discountedGrossAmount: {
type: 'centPrecision',
currencyCode: 'EUR',
centAmount: 500,
fractionDigits: 2,
},
};
13 changes: 7 additions & 6 deletions paypal-commercetools-extension/tests/payment.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ const currencyData = {
const taxedPrice = {
totalNet: {
...currencyData,
centAmount: 16723,
centAmount: 16303,
},
totalGross: {
...currencyData,
centAmount: 19900,
centAmount: 19400,
},
totalTax: {
...currencyData,
centAmount: 3177,
centAmount: 3097,
},
};

const prices = {
totalPrice: {
...currencyData,
centAmount: 19900,
centAmount: 19400,
},
taxedPrice,
};
Expand All @@ -43,6 +43,7 @@ const mockConfigModule = () => {
locale: 'en',
lineItems: discountedLineItems,
...prices,
discountOnTotalPrice,
};
}),
}));
Expand All @@ -61,7 +62,7 @@ const mockConfigModule = () => {
mockConfigModule();

import { paymentController } from '../src/controllers/payments.controller';
import { discountedLineItems } from './constants';
import { discountedLineItems, discountOnTotalPrice } from './constants';

const amountPlanned = {
centAmount: 8200,
Expand Down Expand Up @@ -234,7 +235,7 @@ async function createValidTransaction(
}

const amountPlannedCentsWithTestResult: [number, string, string][] = [
[19900, 'same as cart', '199.00'],
[19400, 'same as cart', '194.00'],
[200000, 'more than in cart', '2000.00'],
[4200, 'less than in cart', '42.00'],
];
Expand Down
34 changes: 28 additions & 6 deletions paypal-commercetools-extension/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,18 @@
triple-beam "1.4.1"
winston "3.13.0"

"@commercetools/platform-sdk@^4.0.0":
version "4.11.0"
resolved "https://registry.yarnpkg.com/@commercetools/platform-sdk/-/platform-sdk-4.11.0.tgz#501310f8143376939ee31c9b15b9c5db9c5bdf41"
integrity sha512-ftcq6mCxzpIG9wmGpTED6KQCApk4nyURh81J3PRP3d48oCLOrkZSyzDDfvflGoVZQeIcox+YdtyqZoryFrRtmQ==
"@commercetools/platform-sdk@^7.22.0":
version "7.22.0"
resolved "https://registry.yarnpkg.com/@commercetools/platform-sdk/-/platform-sdk-7.22.0.tgz#fec48e41190bbb03f53ca7aaaad4b46fb26ed7af"
integrity sha512-4HyT0A5EcE+6jdlteSo59Ox7gfCSiwpyXqClDJAJBzCO4WrUOppMywUvENxMDH38T9GCg3ZuCOXSDBr5LixyUw==
dependencies:
"@commercetools/sdk-client-v2" "^2.1.6"
"@commercetools/sdk-client-v2" "^2.5.0"
"@commercetools/sdk-middleware-auth" "^7.0.0"
"@commercetools/sdk-middleware-http" "^7.0.0"
"@commercetools/sdk-middleware-logger" "^3.0.0"
"@commercetools/ts-client" "^2.1.4"

"@commercetools/sdk-client-v2@^2.0.0", "@commercetools/sdk-client-v2@^2.1.6":
"@commercetools/sdk-client-v2@^2.0.0", "@commercetools/sdk-client-v2@^2.5.0":
version "2.5.0"
resolved "https://registry.yarnpkg.com/@commercetools/sdk-client-v2/-/sdk-client-v2-2.5.0.tgz#3fe33f733d059472896f4fd0820cf899a30cd150"
integrity sha512-v1y++O6yllG+IRTYm9jPE8s667+GapnysyGIf8NJDZbVwhvcanixZL4d20imXjCpOr4u1iZrgRftc90mgYqblw==
Expand All @@ -344,6 +345,15 @@
resolved "https://registry.yarnpkg.com/@commercetools/sdk-middleware-logger/-/sdk-middleware-logger-3.0.0.tgz#4bfc7441d485b3f1046001f44200d12926accb84"
integrity sha512-DhMXAA2yIch/AaGxy7st85Z1HFmeLtHWGkr9z5rX4xKjan4PHGB/IE5saAR+SNGHhs6+1Lp8vZEHDo3tFqVLmg==

"@commercetools/ts-client@^2.1.4":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@commercetools/ts-client/-/ts-client-2.1.5.tgz#9b1c04866a230da08114f2704d14cbc6732e3876"
integrity sha512-LsygC5cQNM6pU03iX6zm5MJHgy/H1GFJtldmtKZfrR3v8xAoU08DBYojnnZQ38i5GcssyJI/pGIMinESndjfCQ==
dependencies:
abort-controller "3.0.0"
buffer "^6.0.3"
node-fetch "^2.6.1"

"@dabh/diagnostics@^2.0.2":
version "2.0.3"
resolved "https://registry.yarnpkg.com/@dabh/diagnostics/-/diagnostics-2.0.3.tgz#7f7e97ee9a725dffc7808d93668cc984e1dc477a"
Expand Down Expand Up @@ -1065,6 +1075,13 @@
"@typescript-eslint/types" "5.62.0"
eslint-visitor-keys "^3.3.0"

[email protected]:
version "3.0.0"
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==
dependencies:
event-target-shim "^5.0.0"

accepts@~1.3.8:
version "1.3.8"
resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.8.tgz#0bf0be125b67014adcb0b0921e62db7bffe16b2e"
Expand Down Expand Up @@ -1897,6 +1914,11 @@ etag@~1.8.1:
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==

event-target-shim@^5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789"
integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==

execa@^5.0.0:
version "5.1.1"
resolved "https://registry.yarnpkg.com/execa/-/execa-5.1.1.tgz#f80ad9cbf4298f7bd1d4c9555c21e93741c411dd"
Expand Down

0 comments on commit dc8f117

Please sign in to comment.