Skip to content

Commit

Permalink
Passing paypal line items (#1069)
Browse files Browse the repository at this point in the history
* feat: (SFI-541)add paypal lineItems

* chore: fix eslint issues

* feat: (SFI-541)get all lineItems

* fix: remove default itemCategory

* fix: unit tests
  • Loading branch information
shanikantsingh authored Apr 22, 2024
1 parent cc5d044 commit 0e5fbec
Show file tree
Hide file tree
Showing 10 changed files with 441 additions and 252 deletions.
499 changes: 289 additions & 210 deletions jest/sfccCartridgeMocks.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions jest/sfccPathSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,8 @@ jest.mock('*/cartridge/scripts/adyenCustomLogs', () => {

jest.mock('*/cartridge/scripts/util/giftCardsHelper', () => {
return require('../src/cartridges/int_adyen_overlay/cartridge/scripts/util/giftCardsHelper');
}, {virtual: true});

jest.mock('*/cartridge/scripts/paypalHelper', () => {
return require('../src/cartridges/int_adyen_overlay/cartridge/scripts/paypalHelper');
}, {virtual: true});
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const paypalHelper = require('../paypalHelper')
describe('paypalHelper', () => {
let args,lineItem, result
beforeEach(() => {
args = (item) => ({
Order: {
getAllLineItems: jest.fn(() => ([item]))
}
})

lineItem = {
productName: 'test',
productID: '123',
quantityValue: '1',
getAdjustedTax: '1000',
adjustedNetPrice: '10000',
category: 'PHYSICAL_GOODS',
}

result = {
quantity: '1',
description: 'test',
itemCategory: 'PHYSICAL_GOODS',
sku: '123',
amountExcludingTax: '10000',
taxAmount: '1000'
}
})
it('should return lineItems for paypal', () => {
const paypalLineItems = paypalHelper.getLineItems(args(lineItem))
expect(paypalLineItems[0]).toStrictEqual(result)
})

it('should return lineItems for paypal with default itemCategory when category is not as per paypal', () => {
const paypalLineItems = paypalHelper.getLineItems(args({...lineItem, category: 'TEST_GOODS'}))
expect(paypalLineItems[0]).toStrictEqual({
quantity: '1',
description: 'test',
sku: '123',
amountExcludingTax: '10000',
taxAmount: '1000'
})
})

it('should return no lineItems for paypal if order or basket is not defined', () => {

const paypalLineItems = paypalHelper.getLineItems({})
expect(paypalLineItems).toBeNull()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const AdyenGetOpenInvoiceData = require('*/cartridge/scripts/adyenGetOpenInvoice
const adyenLevelTwoThreeData = require('*/cartridge/scripts/adyenLevelTwoThreeData');
const constants = require('*/cartridge/adyenConstants/constants');
const AdyenLogs = require('*/cartridge/scripts/adyenCustomLogs');
const paypalHelper = require('*/cartridge/scripts/paypalHelper')

function createPaymentRequest(args) {
try {
Expand Down Expand Up @@ -143,6 +144,11 @@ function createPaymentRequest(args) {
}
}

//add line items for paypal
if (paymentRequest.paymentMethod.type.indexOf('paypal') > -1) {
paymentRequest.lineItems = paypalHelper.getLineItems(args);
}

//Set tokenisation
if (AdyenConfigs.getAdyenTokenisationEnabled()) {
paymentRequest.storePaymentMethod = true;
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
50 changes: 50 additions & 0 deletions src/cartridges/int_adyen_overlay/cartridge/scripts/paypalHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
* Adyen Salesforce Commerce Cloud
* Copyright (c) 2021 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Add all product and shipping line items to request
*/

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

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 = LineItemHelper.getAllLineItems(orderOrBasket.getAllLineItems());
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);
if (lineItem.hasOwnProperty('category')) {
if (PAYPAL_ITEM_CATEGORY.indexOf(lineItem.category) > -1) {
lineItemObject.itemCategory = lineItem.category
}
}
lineItemObject.quantity= quantity;
lineItemObject.description= description;
lineItemObject.sku= id;
lineItemObject.amountExcludingTax= itemAmount.getValue().toFixed();
lineItemObject.taxAmount= vatAmount.getValue().toFixed()
return lineItemObject;
});
}

module.exports.getLineItems = getLineItems;
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 0e5fbec

Please sign in to comment.