Skip to content

Commit

Permalink
feat: (SFI-541)get all lineItems
Browse files Browse the repository at this point in the history
  • Loading branch information
shanikantsingh committed Apr 22, 2024
1 parent c0e7a90 commit 4edc344
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 55 deletions.
1 change: 1 addition & 0 deletions jest/sfccCartridgeMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ jest.mock(
getValue: jest.fn(() => lineItem.getAdjustedTax / quantity),
})),
})),
getAllLineItems: jest.fn((lineItem) => lineItem),
}),
{ virtual: true },
);
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ describe('paypalHelper', () => {
beforeEach(() => {
args = (item) => ({
Order: {
getProductLineItems: jest.fn(() => {
return {
toArray: jest.fn(() => ([item]))
}
})
getAllLineItems: jest.fn(() => ([item]))
}
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Generate the parameters needed for the redirect to the Adyen Hosted Payment Page.
* A signature is calculated based on the configured HMAC code
*/

// script include
Expand All @@ -26,43 +24,29 @@ const LineItemHelper = require('*/cartridge/scripts/util/lineItemHelper');
function getLineItems({ Order: order, Basket: basket, addTaxPercentage }) {
if (!(order || basket)) return null;
const orderOrBasket = order || basket;
const allLineItems = orderOrBasket.getAllLineItems();
const allLineItems = LineItemHelper.getAllLineItems(orderOrBasket.getAllLineItems());

// Add all product and shipping line items to request
const lineItems = [];
for (const item in allLineItems) {
const lineItem = allLineItems[item];
if (
(lineItem instanceof dw.order.ProductLineItem &&
!lineItem.bonusProductLineItem) ||
lineItem instanceof dw.order.ShippingLineItem ||
(lineItem instanceof dw.order.PriceAdjustment &&
lineItem.promotion.promotionClass ===
dw.campaign.Promotion.PROMOTION_CLASS_ORDER)
) {
const lineItemObject = {};
const description = LineItemHelper.getDescription(lineItem);
const id = LineItemHelper.getId(lineItem);
const quantity = LineItemHelper.getQuantity(lineItem);
const itemAmount = LineItemHelper.getItemAmount(lineItem).divide(quantity);
const vatAmount = LineItemHelper.getVatAmount(lineItem).divide(quantity);
const vatPercentage = LineItemHelper.getVatPercentage(lineItem);
return allLineItems.map((lineItem) => {
const lineItemObject = {};
const description = LineItemHelper.getDescription(lineItem);
const id = LineItemHelper.getId(lineItem);
const quantity = LineItemHelper.getQuantity(lineItem);
const itemAmount = LineItemHelper.getItemAmount(lineItem).divide(quantity);
const vatAmount = LineItemHelper.getVatAmount(lineItem).divide(quantity);
const vatPercentage = LineItemHelper.getVatPercentage(lineItem);

lineItemObject.amountExcludingTax = itemAmount.getValue().toFixed();
lineItemObject.taxAmount = vatAmount.getValue().toFixed();
lineItemObject.amountIncludingTax = itemAmount.getValue() + vatAmount.getValue();
lineItemObject.description = description;
lineItemObject.id = id;
lineItemObject.quantity = quantity;
lineItemObject.taxPercentage = addTaxPercentage ? (
new Number(vatPercentage) * 10000
).toFixed() : 0;

lineItems.push(lineItemObject);
}
}

return lineItems;
lineItemObject.amountExcludingTax = itemAmount.getValue().toFixed();
lineItemObject.taxAmount = vatAmount.getValue().toFixed();
lineItemObject.amountIncludingTax = itemAmount.getValue() + vatAmount.getValue();
lineItemObject.description = description;
lineItemObject.id = id;
lineItemObject.quantity = quantity;
lineItemObject.taxPercentage = addTaxPercentage ? (
new Number(vatPercentage) * 10000
).toFixed() : 0;
return lineItemObject;
})
}

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@

const LineItemHelper = require('*/cartridge/scripts/util/lineItemHelper');

const PAYPAL_ITEM_CATEGORY = {
DIGITAL_GOODS: "DIGITAL_GOODS",
PHYSICAL_GOODS: "PHYSICAL_GOODS",
DONATION: "DONATION"
}
const PAYPAL_ITEM_CATEGORY = ["PHYSICAL_GOODS","DIGITAL_GOODS","DONATION"]
function getLineItems({ Order: order, Basket: basket }) {
if (!(order || basket)) return null;
const orderOrBasket = order || basket;
const allLineItems = orderOrBasket.getProductLineItems();
return allLineItems.toArray().map((lineItem) => {
const allLineItems = LineItemHelper.getAllLineItems(orderOrBasket.getAllLineItems());
return allLineItems.map((lineItem) => {
const description = LineItemHelper.getDescription(lineItem);
const id = LineItemHelper.getId(lineItem);
const quantity = LineItemHelper.getQuantity(lineItem);
const itemAmount = LineItemHelper.getItemAmount(lineItem).divide(quantity);
const vatAmount = LineItemHelper.getVatAmount(lineItem).divide(quantity);
let category = PAYPAL_ITEM_CATEGORY[0];
if (lineItem.hasOwnProperty('category')) {
category = PAYPAL_ITEM_CATEGORY.indexOf(lineItem.category) > -1 ? lineItem.category : PAYPAL_ITEM_CATEGORY[0]
}
return {
quantity: quantity,
description: description,
itemCategory: Object.values(PAYPAL_ITEM_CATEGORY).indexOf(lineItem.category) > -1 ? lineItem.category : PAYPAL_ITEM_CATEGORY.PHYSICAL_GOODS,
itemCategory: category,
sku: id,
amountExcludingTax: itemAmount.getValue().toFixed(),
taxAmount: vatAmount.getValue().toFixed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ const __LineItemHelper = {
}
return new dw.value.Money(0, lineItem.getPrice().getCurrencyCode());
},

getAllLineItems(allLineItems) {
const lineItems = [];
for (const item in allLineItems) {
const lineItem = allLineItems[item];
if (
(lineItem instanceof dw.order.ProductLineItem &&
!lineItem.bonusProductLineItem) ||
lineItem instanceof dw.order.ShippingLineItem ||
(lineItem instanceof dw.order.PriceAdjustment &&
lineItem.promotion.promotionClass ===
dw.campaign.Promotion.PROMOTION_CLASS_ORDER)
) {
lineItems.push(lineItem)
}
}
return lineItems
}
};

module.exports = __LineItemHelper;
4 changes: 2 additions & 2 deletions tests/playwright/pages/CheckoutPageSFRA5.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ export default class CheckoutPageSFRA5 {
};

submitPayment = async () => {
await this.page.waitForLoadState('networkidle', { timeout: 20000 });
await this.page.waitForLoadState('load', { timeout: 30000 });
await this.submitPaymentButton.click();
};
placeOrder = async () => {
await this.page.waitForLoadState('networkidle', { timeout: 20000 });
await this.page.waitForLoadState('load', { timeout: 30000 });
await this.placeOrderButton.click();
};

Expand Down
4 changes: 2 additions & 2 deletions tests/playwright/pages/CheckoutPageSFRA6.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ export default class CheckoutPageSFRA {
};

submitPayment = async () => {
await this.page.waitForLoadState('load', { timeout: 20000 });
await this.page.waitForLoadState('load', { timeout: 30000 });
await this.submitPaymentButton.click();
};

placeOrder = async () => {
let retries = 3;
while (retries > 0) {
try {
await this.page.waitForLoadState('load', { timeout: 20000 });
await this.page.waitForLoadState('load', { timeout: 30000 });
await this.placeOrderButton.click();
break; // Break out of the loop if successful
} catch (error) {
Expand Down
2 changes: 0 additions & 2 deletions tests/playwright/pages/PaymentMethodsPage.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ export default class PaymentMethodsPage {
timeout: 20000,
});
await giftCardBrand.click();
await this.page.waitForLoadState('networkidle', { timeout: 20000 });

const giftCardNumberInputField = giftCardComponentWrapper
.frameLocator('.adyen-checkout__card__cardNumber__input iframe')
Expand Down Expand Up @@ -322,7 +321,6 @@ export default class PaymentMethodsPage {
};

initiateKlarnaPayment = async (klarnaVariant) => {
await this.page.waitForLoadState('networkidle', { timeout: 20000 });
let klarnaSelector = this.page.locator('#rb_klarna');
if (klarnaVariant) {
klarnaSelector =
Expand Down

0 comments on commit 4edc344

Please sign in to comment.