diff --git a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/__tests__/selectShippingMethods.test.js b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/__tests__/selectShippingMethods.test.js index ddae272ca..006d010a8 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/__tests__/selectShippingMethods.test.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/__tests__/selectShippingMethods.test.js @@ -33,9 +33,9 @@ describe('callSelectShippingMethod', () => { currentBasket = BasketMgr.getCurrentBasket.mockReturnValueOnce(null); callSelectShippingMethod(req, res, next); expect(res.json).toHaveBeenCalledWith({ - error: true, - redirectUrl: URLUtils.url('Cart-Show').toString(), - }); + error: true, + redirectUrl: URLUtils.url('Cart-Show').toString(), + }); expect(next).toHaveBeenCalled(); }); @@ -51,8 +51,8 @@ describe('callSelectShippingMethod', () => { expect(res.setStatusCode).toHaveBeenCalledWith(500); expect(res.json).toHaveBeenCalledWith({ - errorMessage: 'mocked_error.cannot.select.shipping.method', - }); + errorMessage: 'mocked_error.cannot.select.shipping.method', + }); expect(next).toHaveBeenCalled(); }); @@ -90,4 +90,4 @@ describe('callSelectShippingMethod', () => { }); expect(next).toHaveBeenCalled(); }); -}); +}); \ No newline at end of file diff --git a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/__tests__/shippingMethods.test.js b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/__tests__/shippingMethods.test.js index 0c389b93e..72d844088 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/__tests__/shippingMethods.test.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/__tests__/shippingMethods.test.js @@ -15,12 +15,12 @@ beforeEach(() => { req = { form: { data: JSON.stringify({address:{ - city: 'Amsterdam', - countryCode: 'NL', - stateCode: 'AMS', - postalCode: '1001', - shipmentUUID: 'mocked_uuid', - }}) + city: 'Amsterdam', + countryCode: 'NL', + stateCode: 'AMS', + postalCode: '1001', + shipmentUUID: 'mocked_uuid', + }}) }, }; @@ -79,28 +79,27 @@ describe('Shipping methods', () => { }); }); - it('Should update shipping address for the basket', () => { - const Logger = require('../../../../../../../../jest/__mocks__/dw/system/Logger'); - const setCityMock = jest.fn() - const setPostalCodeMock = jest.fn() - const setStateCodeMock = jest.fn() - const setCountryCodeMock = jest.fn() - const currentBasketMock = { - getDefaultShipment: jest.fn(() =>({ - createShippingAddress: jest.fn(() => ({ - setCity: setCityMock, - setPostalCode: setPostalCodeMock, - setStateCode: setStateCodeMock, - setCountryCode: setCountryCodeMock - })) - })), - }; - BasketMgr.getCurrentBasket.mockReturnValueOnce(currentBasketMock); - callGetShippingMethods(req, res, next); - expect(setCityMock).toHaveBeenCalledWith('Amsterdam'); - expect(setPostalCodeMock).toHaveBeenCalledWith('1001'); - expect(setStateCodeMock).toHaveBeenCalledWith('AMS'); - expect(setCountryCodeMock).toHaveBeenCalledWith('NL'); - expect(Logger.error.mock.calls.length).toBe(0); - }); - +it('Should update shipping address for the basket', () => { + const Logger = require('../../../../../../../../jest/__mocks__/dw/system/Logger'); + const setCityMock = jest.fn() + const setPostalCodeMock = jest.fn() + const setStateCodeMock = jest.fn() + const setCountryCodeMock = jest.fn() + const currentBasketMock = { + getDefaultShipment: jest.fn(() =>({ + createShippingAddress: jest.fn(() => ({ + setCity: setCityMock, + setPostalCode: setPostalCodeMock, + setStateCode: setStateCodeMock, + setCountryCode: setCountryCodeMock + })) + })), + }; + BasketMgr.getCurrentBasket.mockReturnValueOnce(currentBasketMock); + callGetShippingMethods(req, res, next); + expect(setCityMock).toHaveBeenCalledWith('Amsterdam'); + expect(setPostalCodeMock).toHaveBeenCalledWith('1001'); + expect(setStateCodeMock).toHaveBeenCalledWith('AMS'); + expect(setCountryCodeMock).toHaveBeenCalledWith('NL'); + expect(Logger.error.mock.calls.length).toBe(0); +}); diff --git a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/selectShippingMethods.js b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/selectShippingMethods.js index 2c197c57a..16a072faf 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/selectShippingMethods.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/selectShippingMethods.js @@ -16,7 +16,16 @@ const paypalHelper = require('*/cartridge/adyen/utils/paypalHelper'); */ // eslint-disable-next-line complexity function callSelectShippingMethod(req, res, next) { - const currentBasket = BasketMgr.getCurrentBasket(); + const { + basketId, + shipmentUUID, + methodID, + currentPaymentData, + paymentMethodType, + } = JSON.parse(req.form.data); + const currentBasket = basketId + ? BasketMgr.getTemporaryBasket(basketId) + : BasketMgr.getCurrentBasket(); if (!currentBasket) { res.json({ @@ -27,8 +36,6 @@ function callSelectShippingMethod(req, res, next) { return next(); } try { - const { shipmentUUID, methodID, currentPaymentData, paymentMethodType } = - JSON.parse(req.body); let shipment; if (shipmentUUID) { shipment = shippingHelper.getShipmentByUUID(currentBasket, shipmentUUID); diff --git a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/shippingMethods.js b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/shippingMethods.js index 6e5b544fe..d2610ef10 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/shippingMethods.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/expressPayments/shippingMethods.js @@ -26,15 +26,19 @@ function updateShippingAddress(currentBasket, address) { } } +function getBasket(basketId) { + return basketId + ? BasketMgr.getTemporaryBasket(basketId) + : BasketMgr.getCurrentBasket(); +} /** * Make a request to Adyen to get shipping methods */ function callGetShippingMethods(req, res, next) { try { - const { address, currentPaymentData, paymentMethodType } = JSON.parse( - req.body, - ); - const currentBasket = BasketMgr.getCurrentBasket(); + const { address, currentPaymentData, paymentMethodType, basketId } = + JSON.parse(req.form.data); + const currentBasket = getBasket(basketId); if (!currentBasket) { res.json({ error: true,