Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/37526 custom price line items #100

Merged
merged 12 commits into from
Dec 6, 2024
2 changes: 1 addition & 1 deletion paypal-commercetools-custom-application/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"formik": "2.2.9",
"graphql": "^16.8.0",
"graphql-tag": "^2.12.6",
"jest": "29.5.0",
"jest": "^29.7.0",
"jest-runner-eslint": "2.0.0",
"jest-watch-typeahead": "2.2.2",
"moment": "^2.29.4",
Expand Down
20 changes: 10 additions & 10 deletions paypal-commercetools-custom-application/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3617,7 +3617,7 @@
jest-util "^29.7.0"
slash "^3.0.0"

"@jest/core@^29.5.0", "@jest/core@^29.7.0":
"@jest/core@^29.7.0":
version "29.7.0"
resolved "https://registry.yarnpkg.com/@jest/core/-/core-29.7.0.tgz#b6cccc239f30ff36609658c5a5e2291757ce448f"
integrity sha512-n7aeXWKMnGtDA48y8TLWJPJmLmmZ642Ceo78cYWEpiD7FzDgmNDV/GCVRorPABdXLJZ/9wzzgZAlHjXjxDHGsg==
Expand Down Expand Up @@ -3808,7 +3808,7 @@
"@types/yargs" "^15.0.0"
chalk "^4.0.0"

"@jest/types@^29.5.0", "@jest/types@^29.6.3":
"@jest/types@^29.6.3":
version "29.6.3"
resolved "https://registry.yarnpkg.com/@jest/types/-/types-29.6.3.tgz#1131f8cf634e7e84c5e77bab12f052af585fba59"
integrity sha512-u3UPsIilWKOM3F9CXtrG8LEJmNxwoCQC/XVj4IKYXvvpx7QIi/Kg1LI5uDmDpKlac62NUtX7eLjRh+jVZcLOzw==
Expand Down Expand Up @@ -8923,7 +8923,7 @@ jest-circus@^29.7.0:
slash "^3.0.0"
stack-utils "^2.0.3"

jest-cli@^29.5.0:
jest-cli@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest-cli/-/jest-cli-29.7.0.tgz#5592c940798e0cae677eec169264f2d839a37995"
integrity sha512-OVVobw2IubN/GSYsxETi+gOe7Ka59EFMR/twOU3Jb2GnKKeMGJB5SGUUrEz3SFVmJASUdZUzy83sLNNQ2gZslg==
Expand Down Expand Up @@ -9316,15 +9316,15 @@ jest-worker@^29.7.0:
merge-stream "^2.0.0"
supports-color "^8.0.0"

jest@29.5.0:
version "29.5.0"
resolved "https://registry.yarnpkg.com/jest/-/jest-29.5.0.tgz#f75157622f5ce7ad53028f2f8888ab53e1f1f24e"
integrity sha512-juMg3he2uru1QoXX078zTa7pO85QyB9xajZc6bU+d9yEGwrKX6+vGmJQ3UdVZsvTEUARIdObzH68QItim6OSSQ==
jest@^29.7.0:
version "29.7.0"
resolved "https://registry.yarnpkg.com/jest/-/jest-29.7.0.tgz#994676fc24177f088f1c5e3737f5697204ff2613"
integrity sha512-NIy3oAFp9shda19hy4HK0HRTWKtPJmGdnvywu01nOqNC2vZg+Z+fvJDxpMQA88eb2I9EcafcdjYgsDthnYTvGw==
dependencies:
"@jest/core" "^29.5.0"
"@jest/types" "^29.5.0"
"@jest/core" "^29.7.0"
"@jest/types" "^29.6.3"
import-local "^3.0.2"
jest-cli "^29.5.0"
jest-cli "^29.7.0"

jiti@^1.18.2:
version "1.21.6"
Expand Down
6 changes: 3 additions & 3 deletions paypal-commercetools-events/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
"@typescript-eslint/eslint-plugin": "^5.45.1",
"@typescript-eslint/parser": "^5.45.1",
"concurrently": "^8.2.0",
"eslint": "^8.46.0",
"jest": "^29.6.2",
"eslint": "^9.16.0",
"jest": "^29.7.0",
"nodemon": "^3.0.1",
"prettier": "^3.0.1",
"rimraf": "^5.0.1",
"rimraf": "^6.0.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.1",
"typescript": "^5.1.6"
Expand Down
1,215 changes: 562 additions & 653 deletions paypal-commercetools-events/yarn.lock

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions paypal-commercetools-extension/jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
module.exports = {
displayName: 'Tests Typescript Application - paypal-commercetools-extension',
setupFiles: ["<rootDir>/tests/setup-tests.ts"],
setupFiles: ['<rootDir>/tests/setup-tests.ts'],
moduleDirectories: ['node_modules', 'src'],
testMatch: ['**/tests/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
testPathIgnorePatterns: ['<rootDir>/tests/setup-tests.ts'],
testPathIgnorePatterns: ['<rootDir>/tests/setup-tests.ts', 'constants.ts'],
preset: 'ts-jest',
testEnvironment: 'node',
"collectCoverageFrom": [
"src/{!(paypal),}/*"
]
collectCoverageFrom: ['src/{!(paypal),}/*'],
};
4 changes: 2 additions & 2 deletions paypal-commercetools-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"@typescript-eslint/eslint-plugin": "^5.46.0",
"@typescript-eslint/parser": "^5.46.0",
"concurrently": "^7.4.0",
"eslint": "^8.24.0",
"jest": "^29.5.0",
"eslint": "^9.16.0",
"jest": "^29.7.0",
"ngrok": "^5.0.0-beta.2",
"nodemon": "^3.0.0",
"prettier": "^2.7.1",
Expand Down
35 changes: 18 additions & 17 deletions paypal-commercetools-extension/src/service/payments.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { logger } from '../utils/logger.utils';
import {
mapCommercetoolsCarrierToPayPalCarrier,
mapCommercetoolsCartToPayPalPriceBreakdown,
mapCommercetoolsLineItemsToPayPalItems,
mapValidCommercetoolsLineItemsToPayPalItems,
mapCommercetoolsMoneyToPayPalMoney,
mapPayPalAuthorizationStatusToCommercetoolsTransactionState,
mapPayPalCaptureStatusToCommercetoolsTransactionState,
Expand Down Expand Up @@ -109,7 +109,8 @@ async function prepareCreateOrderRequest(
cart.locale ?? Object.keys(settings?.paymentDescription)[0]
]
: undefined;
const matchingAmounts = amountPlanned.centAmount === cart.totalPrice.centAmount;
const matchingAmounts =
amountPlanned.centAmount === cart.totalPrice.centAmount;
request = {
intent:
settings?.payPalIntent.toUpperCase() ?? CheckoutPaymentIntent.Capture,
Expand All @@ -118,7 +119,9 @@ async function prepareCreateOrderRequest(
amount: {
currency_code: amountPlanned.currencyCode,
value: mapCommercetoolsMoneyToPayPalMoney(amountPlanned),
breakdown: matchingAmounts ? mapCommercetoolsCartToPayPalPriceBreakdown(cart) : null,
breakdown: matchingAmounts
? mapCommercetoolsCartToPayPalPriceBreakdown(cart)
: null,
},
shipping: !cart.shippingAddress
? undefined
Expand All @@ -136,14 +139,13 @@ async function prepareCreateOrderRequest(
},
invoice_id: request?.custom_invoice_id ?? payment.id,
description: paymentDescription,
items: matchingAmounts ? cart?.lineItems?.map((lineItem) =>
mapCommercetoolsLineItemsToPayPalItems(
lineItem,
paymentSource?.experience_context?.shipping_preference !==
'NO_SHIPPING' || !!cart.shippingAddress,
cart.locale
)
) : null,
items: mapValidCommercetoolsLineItemsToPayPalItems(
matchingAmounts,
paymentSource?.experience_context?.shipping_preference !==
'NO_SHIPPING' || !!cart.shippingAddress,
cart?.lineItems,
cart.locale
),
},
],
...request,
Expand Down Expand Up @@ -500,12 +502,11 @@ export const handleUpdateOrderRequest = async (
{
op: 'replace',
path: "/purchase_units/@reference_id=='default'/items",
value: cart?.lineItems?.map((lineItem) =>
mapCommercetoolsLineItemsToPayPalItems(
lineItem,
!!cart.shippingAddress,
cart.locale
)
value: mapValidCommercetoolsLineItemsToPayPalItems(
true,
!!cart.shippingAddress,
cart?.lineItems,
cart.locale
),
} as Patch,
],
Expand Down
23 changes: 22 additions & 1 deletion paypal-commercetools-extension/src/utils/map.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export const mapPayPalPaymentSourceToCommercetoolsMethodInfo = (
}
};

export const mapCommercetoolsLineItemsToPayPalItems = (
const mapCommercetoolsLineItemsToPayPalItems = (
lineItem: LineItem,
isShipped: boolean,
locale?: string
Expand All @@ -140,6 +140,7 @@ export const mapCommercetoolsLineItemsToPayPalItems = (
const taxedNetAmount = lineItem.taxedPrice?.totalNet?.centAmount;
const currencyCode = lineItem.price.value.currencyCode;
const fractionDigits = lineItem.price.value.fractionDigits;

return {
unit_amount: {
value: mapCommercetoolsMoneyToPayPalMoney({
Expand Down Expand Up @@ -174,6 +175,26 @@ export const mapCommercetoolsLineItemsToPayPalItems = (
} as Item;
};

export const mapValidCommercetoolsLineItemsToPayPalItems = (
matchingAmounts: boolean,
isShipped: boolean,
lineItems?: LineItem[],
locale?: string
) => {
if (!matchingAmounts || !lineItems) {
return null;
}
const relevantLineItems = lineItems.filter(
LiudmylaMasliuk marked this conversation as resolved.
Show resolved Hide resolved
({ lineItemMode }) => lineItemMode !== 'GiftLineItem'
);
const payPalItems = relevantLineItems.map((lineItem) =>
mapCommercetoolsLineItemsToPayPalItems(lineItem, isShipped, locale)
);
return payPalItems.some((item) => parseFloat(item.unit_amount.value) <= 0)
? null
: payPalItems;
};

export const mapCommercetoolsCartToPayPalPriceBreakdown = ({
lineItems,
taxedShippingPrice,
Expand Down
Loading
Loading