From 8a1eab8aee2dc9e99a89cad4fc465ce771b48251 Mon Sep 17 00:00:00 2001 From: Zenit Shkreli <69572953+zenit2001@users.noreply.github.com> Date: Tue, 13 Feb 2024 13:41:18 +0100 Subject: [PATCH] fix: replaced /sessions with /paymentMethods --- .../renderGenericComponent.test.js.snap | 20 +++++----- .../__tests__/renderGenericComponent.test.js | 24 ++++++------ .../__tests__/renderGiftcardComponent.test.js | 2 +- .../adyen_checkout/checkoutConfiguration.js | 2 +- .../adyen_checkout/renderGenericComponent.js | 37 ++++++++++--------- .../adyen_checkout/renderGiftcardComponent.js | 4 +- .../default/js/amazonPayExpressPart1.js | 2 +- .../default/js/amazonPayExpressPart2.js | 2 +- .../client/default/js/applePayExpress.js | 2 +- .../cartridge/client/default/js/constants.js | 1 + .../getCheckoutPaymentMethods.test.js | 20 ++++++++-- .../adyen/getCheckoutPaymentMethods.js | 30 ++++++++------- .../checkout/billing/adyenComponentForm.isml | 3 +- 13 files changed, 83 insertions(+), 66 deletions(-) diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/__snapshots__/renderGenericComponent.test.js.snap b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/__snapshots__/renderGenericComponent.test.js.snap index 1edb17cf7..bbccdba32 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/__snapshots__/renderGenericComponent.test.js.snap +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/__snapshots__/renderGenericComponent.test.js.snap @@ -2,7 +2,10 @@ exports[`Render Generic Component should hide giftcard container 1`] = ` { - "amount": "mocked_amount", + "amount": { + "currency": "mocked_currency", + "value": "mocked_amount", + }, "countryCode": "mocked_countrycode", "paymentMethodsConfiguration": { "amazonpay": { @@ -18,18 +21,18 @@ exports[`Render Generic Component should hide giftcard container 1`] = ` "configuration": undefined, }, }, - "session": { - "adyenDescriptions": {}, - "id": "mock_id", + "paymentMethodsResponse": { "imagePath": "example.com", - "sessionData": "mock_session_data", }, } `; exports[`Render Generic Component should render 1`] = ` { - "amount": "mocked_amount", + "amount": { + "currency": "mocked_currency", + "value": "mocked_amount", + }, "countryCode": "mocked_countrycode", "paymentMethodsConfiguration": { "amazonpay": { @@ -45,11 +48,8 @@ exports[`Render Generic Component should render 1`] = ` "configuration": undefined, }, }, - "session": { - "adyenDescriptions": {}, - "id": "mock_id", + "paymentMethodsResponse": { "imagePath": "example.com", - "sessionData": "mock_session_data", }, } `; diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/renderGenericComponent.test.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/renderGenericComponent.test.js index da6527bb7..db31cc9c3 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/renderGenericComponent.test.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/renderGenericComponent.test.js @@ -5,7 +5,7 @@ jest.mock('../../commons'); jest.mock('../../../../../store'); const { renderGenericComponent, setInstallments, renderPosTerminals, isCartModified, renderGiftCardLogo, setGiftCardContainerVisibility, applyGiftCards } = require('../renderGenericComponent'); -const { createSession } = require('../../commons'); +const { getPaymentMethods } = require('../../commons'); const { fetchGiftCards } = require('../../commons'); const store = require('../../../../../store'); const giftCardHtml = ` @@ -59,19 +59,17 @@ beforeEach(() => { storedPaymentMethods: [{ supportedShopperInteractions: ['Ecommerce'] }], paymentMethods: [{ type: 'amazonpay' }], }, - options: { - amount: 'mocked_amount', - countryCode: 'mocked_countrycode', - }, })); window.installments = '[[0,2,["amex","hipercard"]]]'; store.checkout = { options: {} }; - createSession.mockReturnValue({ + store.checkoutConfiguration = { + amount: {value : 'mocked_amount', currency : 'mocked_currency'}, + countryCode: 'mocked_countrycode', + }; + getPaymentMethods.mockReturnValue({ adyenConnectedTerminals: { uniqueTerminalIds: ['mocked_id'] }, - id: 'mock_id', - sessionData: 'mock_session_data', imagePath: 'example.com', adyenDescriptions: {}, }); @@ -83,7 +81,7 @@ describe('Render Generic Component', () => { store.componentsObj = { foo: 'bar', bar: 'baz' }; store.checkoutConfiguration.paymentMethodsConfiguration = { amazonpay: {} }; await renderGenericComponent(); - expect(createSession).toBeCalled(); + expect(getPaymentMethods).toBeCalled(); expect(store.checkoutConfiguration).toMatchSnapshot(); expect( document.querySelector('input[type=radio][name=brandCode]').value, @@ -96,7 +94,7 @@ describe('Render Generic Component', () => { store.componentsObj = { foo: 'bar', bar: 'baz' }; store.checkoutConfiguration.paymentMethodsConfiguration = { amazonpay: {} }; await renderGenericComponent(); - expect(createSession).toBeCalled(); + expect(getPaymentMethods).toBeCalled(); expect(store.checkoutConfiguration).toMatchSnapshot(); expect( document.querySelector('.gift-card-selection').style.display, @@ -171,8 +169,8 @@ describe('Render Generic Component', () => { }); it('handles errors in initializeCheckout', async () => { - createSession.mockRejectedValue(new Error('Session creation failed')); - await expect(renderGenericComponent()).rejects.toThrow('Session creation failed'); + getPaymentMethods.mockRejectedValue(new Error('Payments method call failed')); + await expect(renderGenericComponent()).rejects.toThrow('Payments method call failed'); }); it('correctly sets Pos Terminals', () => { @@ -276,7 +274,7 @@ describe('Render Generic Component', () => { const renderGiftCardComponent = require('*/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent'); store.checkoutConfiguration = { amount: { currency: 'USD', value: 50 }, - session: { + paymentMethodsResponse: { imagePath: 'test_image_path', }, }; diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/renderGiftcardComponent.test.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/renderGiftcardComponent.test.js index d5ef52057..c3e64a4c3 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/renderGiftcardComponent.test.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/__tests__/renderGiftcardComponent.test.js @@ -35,7 +35,7 @@ beforeEach(() => { })); window.Configuration = { amount: 0 }; store.checkoutConfiguration = { - session: { + paymentMethodsResponse: { imagePath: 'test_image_path', }, }; diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/checkoutConfiguration.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/checkoutConfiguration.js index 7ec74e860..9e2b4cd3b 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/checkoutConfiguration.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/checkoutConfiguration.js @@ -24,7 +24,7 @@ function getCardConfig() { store.isValid = state.isValid; const method = state.data.paymentMethod.storedPaymentMethodId ? `storedCard${state.data.paymentMethod.storedPaymentMethodId}` - : store.selectedMethod; + : constants.SCHEME; store.updateSelectedPayment(method, 'isValid', store.isValid); store.updateSelectedPayment(method, 'stateData', state.data); }, diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGenericComponent.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGenericComponent.js index 12d455062..76022c4f8 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGenericComponent.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGenericComponent.js @@ -3,7 +3,7 @@ const store = require('../../../../store'); const { renderPaymentMethod } = require('./renderPaymentMethod'); const helpers = require('./helpers'); const { installmentLocales } = require('./localesUsingInstallments'); -const { createSession, fetchGiftCards } = require('../commons'); +const { getPaymentMethods, fetchGiftCards } = require('../commons'); const constants = require('../constants'); const { createElementsToShowRemainingGiftCardAmount, @@ -217,18 +217,17 @@ function setGiftCardContainerVisibility() { } async function initializeCheckout() { - const session = await createSession(); + const paymentMethodsResponse = await getPaymentMethods(); const giftCardsData = await fetchGiftCards(); - store.checkoutConfiguration.session = { - id: session.id, - sessionData: session.sessionData, - imagePath: session.imagePath, - adyenDescriptions: session.adyenDescriptions, + setCheckoutConfiguration(paymentMethodsResponse); + + store.checkoutConfiguration.paymentMethodsResponse = { + ...paymentMethodsResponse.AdyenPaymentMethods, + imagePath: paymentMethodsResponse.imagePath, }; store.checkout = await AdyenCheckout(store.checkoutConfiguration); setGiftCardContainerVisibility(); - const { totalDiscountedAmount, giftCards } = giftCardsData; if (giftCards?.length) { store.addedGiftCards = giftCards; @@ -236,8 +235,7 @@ async function initializeCheckout() { store.partialPaymentsOrderObj = { ...lastGiftCard, totalDiscountedAmount }; } - setCheckoutConfiguration(store.checkout.options); - setInstallments(store.checkout.options.amount); + setInstallments(paymentMethodsResponse.amount); setAmazonPayConfig(store.checkout.paymentMethodsResponse); document.querySelector('#paymentMethodsList').innerHTML = ''; @@ -251,20 +249,23 @@ async function initializeCheckout() { (pm) => pm.type !== constants.GIFTCARD, ); - renderStoredPaymentMethods( - storedPaymentMethodsWithoutGiftCards, - session.imagePath, - ); + // Rendering stored payment methods if one-click is enabled in BM + if (window.adyenRecurringPaymentsEnabled) { + renderStoredPaymentMethods( + storedPaymentMethodsWithoutGiftCards, + paymentMethodsResponse.imagePath, + ); + } await renderPaymentMethods( paymentMethodsWithoutGiftCards, - session.imagePath, - session.adyenDescriptions, + paymentMethodsResponse.imagePath, + paymentMethodsResponse.adyenDescriptions, ); - renderPosTerminals(session.adyenConnectedTerminals); + renderPosTerminals(paymentMethodsResponse.adyenConnectedTerminals); - renderGiftCardLogo(session.imagePath); + renderGiftCardLogo(paymentMethodsResponse.imagePath); const firstPaymentMethod = document.querySelector( 'input[type=radio][name=brandCode]', diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent.js index 37a35b292..470c93d4a 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent.js @@ -128,7 +128,7 @@ function renderGiftCardSelectForm() { return; } - const { imagePath } = store.checkoutConfiguration.session; + const { imagePath } = store.checkoutConfiguration.paymentMethodsResponse; giftCardBrands().forEach((giftCard) => { const newListItem = document.createElement('li'); @@ -277,7 +277,7 @@ function clearGiftCardsContainer() { function renderAddedGiftCard(card) { const giftCardData = card.giftCard; - const { imagePath } = store.checkoutConfiguration.session; + const { imagePath } = store.checkoutConfiguration.paymentMethodsResponse; const { giftCardsList, giftCardAddButton } = getGiftCardElements(); diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart1.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart1.js index e7e939bde..d3a3c4725 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart1.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart1.js @@ -17,7 +17,7 @@ async function mountAmazonPayComponent() { locale: window.locale, }); - const amazonPayConfig = paymentMethodsResponse.find( + const amazonPayConfig = paymentMethodsResponse?.paymentMethods.find( (pm) => pm.type === AMAZON_PAY, )?.configuration; if (!amazonPayConfig) return; diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js index 4e6895ffd..5ec6f00af 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js @@ -80,7 +80,7 @@ async function mountAmazonPayComponent() { try { const data = await getPaymentMethods(); const paymentMethodsResponse = data.AdyenPaymentMethods; - const amazonPayConfig = paymentMethodsResponse.find( + const amazonPayConfig = paymentMethodsResponse?.paymentMethods.find( (pm) => pm.type === 'amazonpay', )?.configuration; if (!amazonPayConfig) return; diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/applePayExpress.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/applePayExpress.js index 87815a5ba..a1d007aa7 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/applePayExpress.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/applePayExpress.js @@ -123,7 +123,7 @@ function callPaymentFromComponent(data, resolveApplePay, rejectApplePay) { initializeCheckout() .then(async () => { const applePayPaymentMethod = - paymentMethodsResponse?.AdyenPaymentMethods.find( + paymentMethodsResponse?.AdyenPaymentMethods?.paymentMethods.find( (pm) => pm.type === APPLE_PAY, ); diff --git a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/constants.js b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/constants.js index ac00b82e5..189eed34e 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/constants.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/client/default/js/constants.js @@ -8,6 +8,7 @@ module.exports = { NOTENOUGHBALANCE: 'NotEnoughBalance', SUCCESS: 'Success', GIFTCARD: 'giftcard', + SCHEME: 'scheme', GIROPAY: 'giropay', APPLE_PAY: 'applepay', ACTIONTYPE: { diff --git a/src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/__tests__/getCheckoutPaymentMethods.test.js b/src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/__tests__/getCheckoutPaymentMethods.test.js index 2875fde19..75a2dffe2 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/__tests__/getCheckoutPaymentMethods.test.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/__tests__/getCheckoutPaymentMethods.test.js @@ -24,9 +24,17 @@ describe('getCheckoutPaymentMethods', () => { it('returns AdyenPaymentMethods', () => { getCheckoutPaymentMethods(req, res, next); expect(res.json).toHaveBeenCalledWith({ - AdyenPaymentMethods: [{ - type: 'visa' - }, ], + AdyenPaymentMethods: { + paymentMethods: [ + { + "type": "visa", + }, + ], + }, + amount: { + currency: "EUR", + value: 1000, + }, adyenConnectedTerminals: { "foo": "bar", }, @@ -35,6 +43,7 @@ describe('getCheckoutPaymentMethods', () => { "paypal": "PayPal example description", }, imagePath: "mocked_loading_contextimages/logos/medium/", + countryCode: "NL", }); expect(next).toHaveBeenCalled(); }); @@ -47,6 +56,10 @@ describe('getCheckoutPaymentMethods', () => { getCheckoutPaymentMethods(req, res, next); expect(res.json).toHaveBeenCalledWith({ AdyenPaymentMethods: [], + amount: { + currency: "EUR", + value: 1000, + }, adyenConnectedTerminals: { "foo": "bar", }, @@ -55,6 +68,7 @@ describe('getCheckoutPaymentMethods', () => { "paypal": "PayPal example description", }, imagePath: "mocked_loading_contextimages/logos/medium/", + countryCode: "NL", }); }); }); diff --git a/src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/getCheckoutPaymentMethods.js b/src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/getCheckoutPaymentMethods.js index a078d7ffc..cf8a04e3f 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/getCheckoutPaymentMethods.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/controllers/middlewares/adyen/getCheckoutPaymentMethods.js @@ -25,32 +25,34 @@ function getConnectedTerminals() { function getCheckoutPaymentMethods(req, res, next) { const currentBasket = BasketMgr.getCurrentBasket(); - let countryCode = getCountryCode(currentBasket, req.locale); - const adyenURL = `${AdyenHelper.getLoadingContext()}images/logos/medium/`; - const connectedTerminals = getConnectedTerminals(); - if ( + const countryCode = currentBasket.getShipments().length > 0 && currentBasket.getShipments()[0].shippingAddress - ) { - countryCode = currentBasket - .getShipments()[0] - .shippingAddress.getCountryCode(); - } + ? currentBasket.getShipments()[0].shippingAddress.getCountryCode().value + : getCountryCode(currentBasket, req.locale).value; + const adyenURL = `${AdyenHelper.getLoadingContext()}images/logos/medium/`; + const connectedTerminals = JSON.parse(getConnectedTerminals()); + const currency = currentBasket.getTotalGrossPrice().currencyCode; + const paymentAmount = currentBasket.getTotalGrossPrice().isAvailable() + ? AdyenHelper.getCurrencyValueForApi(currentBasket.getTotalGrossPrice()) + : new dw.value.Money(1000, currency); let paymentMethods; try { paymentMethods = getPaymentMethods.getMethods( - BasketMgr.getCurrentBasket(), - countryCode.value?.toString() || countryCode.value, - ).paymentMethods; + currentBasket, + AdyenHelper.getCustomer(req.currentCustomer), + countryCode, + ); } catch (err) { paymentMethods = []; } - res.json({ AdyenPaymentMethods: paymentMethods, imagePath: adyenURL, adyenDescriptions: paymentMethodDescriptions, - adyenConnectedTerminals: JSON.parse(connectedTerminals), + adyenConnectedTerminals: connectedTerminals, + amount: { value: paymentAmount.value, currency }, + countryCode, }); return next(); } diff --git a/src/cartridges/int_adyen_SFRA/cartridge/templates/default/checkout/billing/adyenComponentForm.isml b/src/cartridges/int_adyen_SFRA/cartridge/templates/default/checkout/billing/adyenComponentForm.isml index a7d6c8478..394da914b 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/templates/default/checkout/billing/adyenComponentForm.isml +++ b/src/cartridges/int_adyen_SFRA/cartridge/templates/default/checkout/billing/adyenComponentForm.isml @@ -22,7 +22,8 @@ window.ShowConfirmationPaymentFromComponent = "${ShowConfirmationPaymentFromComponent}"; window.AdyenSFRA6Enabled = '${pdict.adyen.SFRA6Enabled}'; window.klarnaWidgetEnabled = ${adyenKlarnaWidgetEnabled}; - window.sessionsUrl = "${URLUtils.https('Adyen-Sessions')}"; + window.adyenRecurringPaymentsEnabled = ${adyenRecurringPaymentsEnabled}; + window.getPaymentMethodsURL = "${URLUtils.https('Adyen-GetPaymentMethods')}"; window.checkBalanceUrl = "${URLUtils.https('Adyen-CheckBalance')}"; window.partialPaymentsOrderUrl = "${URLUtils.https('Adyen-PartialPaymentsOrder')}"; window.partialPaymentUrl = "${URLUtils.https('Adyen-partialPayment')}";