diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/__tests__/paypalExpress.test.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/__tests__/paypalExpress.test.js index 93339a8f0..1abe660af 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/__tests__/paypalExpress.test.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/__tests__/paypalExpress.test.js @@ -28,10 +28,9 @@ describe('paypal express', () => { global.$.spinner = jest.fn(() => {return { start: start }}) - global.fetch = jest.fn().mockResolvedValueOnce({ - ok: true, - json: jest.fn(() => {return {action: {}}}) - }) + global.$.ajax = jest.fn().mockImplementation(({ success }) => { + success({ action : {}}) + }); const component = { handleError: jest.fn(), handleAction: jest.fn() @@ -46,10 +45,9 @@ describe('paypal express', () => { global.$.spinner = jest.fn(() => {return { start: start }}) - global.fetch = jest.fn().mockResolvedValueOnce({ - ok: true, - json: jest.fn(() => {return {}}) - }) + global.$.ajax = jest.fn().mockImplementation(({ success }) => { + success({}) + }); const component = { handleError: jest.fn(), handleAction: jest.fn() diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyenAccount.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyenAccount.js index b2f3b28e8..59f693060 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyenAccount.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyenAccount.js @@ -33,11 +33,16 @@ function handleAction(action) { // confirm onAdditionalDetails event and paymentsDetails response store.checkoutConfiguration.onAdditionalDetails = (state) => { + const requestData = JSON.stringify({ + data: state.data, + }); $.ajax({ type: 'POST', - url: 'Adyen-PaymentsDetails', - data: JSON.stringify({ data: state.data }), - contentType: 'application/json; charset=utf-8', + url: window.paymentsDetailsURL, + data: { + csrf_token: $('#adyen-token').val(), + data: requestData, + }, async: false, success(data) { if (data.isSuccessful) { diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyenGiving.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyenGiving.js index 6ab787e9c..ee559d513 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyenGiving.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyenGiving.js @@ -10,6 +10,7 @@ function handleOnDonate(state, component) { amountCurrency: selectedAmount.currency, orderNo: window.orderNo, orderToken: window.orderToken, + csrf_token: $('#adyen-token').val(), }; $.ajax({ diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/checkoutConfiguration.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/checkoutConfiguration.js index a2a828587..d924d4f12 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/checkoutConfiguration.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/checkoutConfiguration.js @@ -181,11 +181,14 @@ function getGiftCardConfig() { store.updateSelectedPayment(constants.GIFTCARD, 'stateData', state.data); }, onBalanceCheck: (resolve, reject, requestData) => { + const payload = { + csrf_token: $('#adyen-token').val(), + data: JSON.stringify(requestData), + }; $.ajax({ type: 'POST', url: window.checkBalanceUrl, - data: JSON.stringify(requestData), - contentType: 'application/json; charset=utf-8', + data: payload, async: false, success: (data) => { giftcardBalance = data.balance; @@ -248,8 +251,10 @@ function getGiftCardConfig() { $.ajax({ type: 'POST', url: window.partialPaymentsOrderUrl, - data: JSON.stringify(requestData), - contentType: 'application/json; charset=utf-8', + data: { + csrf_token: $('#adyen-token').val(), + data: JSON.stringify(requestData), + }, async: false, success: (data) => { if (data.resultCode === 'Success') { @@ -293,14 +298,17 @@ const actionHandler = async (action) => { }; function handleOnAdditionalDetails(state) { + const requestData = JSON.stringify({ + data: state.data, + orderToken: window.orderToken, + }); $.ajax({ type: 'POST', url: window.paymentsDetailsURL, - data: JSON.stringify({ - data: state.data, - orderToken: window.orderToken, - }), - contentType: 'application/json; charset=utf-8', + data: { + csrf_token: $('#adyen-token').val(), + data: requestData, + }, async: false, success(data) { if (!data.isFinal && typeof data.action === 'object') { diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/helpers.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/helpers.js index cd3956990..dd1d8a8d5 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/helpers.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/helpers.js @@ -33,6 +33,7 @@ function paymentFromComponent(data, component = {}) { url: window.paymentFromComponentURL, type: 'post', data: { + csrf_token: $('#adyen-token').val(), data: JSON.stringify(requestData), paymentMethod: document.querySelector('#adyenPaymentMethodName').value, }, diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/makePartialPayment.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/makePartialPayment.js index 0e656dd0e..9db360830 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/makePartialPayment.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/makePartialPayment.js @@ -7,8 +7,10 @@ function makePartialPayment(requestData) { $.ajax({ url: window.partialPaymentUrl, type: 'POST', - data: JSON.stringify(requestData), - contentType: 'application/json; charset=utf-8', + data: { + csrf_token: $('#adyen-token').val(), + data: JSON.stringify(requestData), + }, }) .done((response) => { if (response.error) { diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent.js index 2f4401f6c..e2f73a19b 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGiftcardComponent.js @@ -56,8 +56,10 @@ function removeGiftCards() { $.ajax({ type: 'POST', url: window.cancelPartialPaymentOrderUrl, - data: JSON.stringify(card), - contentType: 'application/json; charset=utf-8', + data: { + csrf_token: $('#adyen-token').val(), + data: JSON.stringify(card), + }, async: false, success(res) { const adyenPartialPaymentsOrder = document.querySelector( diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayCheckout.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayCheckout.js index ef6a2462d..4d4bc3f30 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayCheckout.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayCheckout.js @@ -42,6 +42,7 @@ function paymentFromComponent(data, component) { url: window.paymentFromComponentURL, type: 'post', data: { + csrf_token: $('#adyen-token').val(), data: JSON.stringify(requestData), paymentMethod: 'amazonpay', merchantReference: document.querySelector('#merchantReference').value, @@ -80,14 +81,17 @@ async function mountAmazonPayComponent() { }, onAdditionalDetails: (state) => { state.data.paymentMethod = 'amazonpay'; + const requestData = JSON.stringify({ + data: state.data, + orderToken: window.orderToken, + }); $.ajax({ type: 'post', url: window.paymentsDetailsURL, - data: JSON.stringify({ - data: state.data, - orderToken: window.orderToken, - }), - contentType: 'application/json; charset=utf-8', + data: { + csrf_token: $('#adyen-token').val(), + data: requestData, + }, success(data) { if (data.isSuccessful) { handleAuthorised(data); diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js index 9da564676..301e9a380 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/amazonPayExpressPart2.js @@ -5,6 +5,7 @@ function saveShopperDetails(details) { url: window.saveShopperDetailsURL, type: 'post', data: { + csrf_token: $('#adyen-token').val(), shopperDetails: JSON.stringify(details), paymentMethod: 'amazonpay', }, diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/applePayExpress.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/applePayExpress.js index ebd0c3027..2aa5954ed 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/applePayExpress.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/applePayExpress.js @@ -90,6 +90,7 @@ function callPaymentFromComponent(data, resolveApplePay, rejectApplePay) { url: window.paymentFromComponentURL, type: 'post', data: { + csrf_token: $('#adyen-token').val(), data: JSON.stringify(data), paymentMethod: APPLE_PAY, }, diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/checkoutReviewButtons.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/checkoutReviewButtons.js index 6a33eb2da..21553f9fa 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/checkoutReviewButtons.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/checkoutReviewButtons.js @@ -6,11 +6,16 @@ const helpers = require('./adyen_checkout/helpers'); * @return {undefined} */ function makeExpressPaymentDetailsCall(data) { + const csrfToken = document.querySelector( + '#showConfirmationForm input[id="adyen-token"]', + ).value; $.ajax({ type: 'POST', url: window.makeExpressPaymentDetailsCall, - data: JSON.stringify({ data }), - contentType: 'application/json; charset=utf-8', + data: { + csrf_token: csrfToken, + data: JSON.stringify({ data }), + }, async: false, success(response) { helpers.setOrderFormData(response); diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/paypalExpress.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/paypalExpress.js index 20f5bff8e..53cbc3ffd 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/paypalExpress.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/paypalExpress.js @@ -9,19 +9,26 @@ const { PAYPAL } = require('./constants'); async function callPaymentFromComponent(data, component) { try { $.spinner().start(); - const response = await fetch(window.makeExpressPaymentsCall, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', + + $.ajax({ + type: 'POST', + url: window.makeExpressPaymentsCall, + data: { + csrf_token: $('#adyen-token').val(), + data: JSON.stringify(data), + }, // Send the data as a JSON string + success(response) { + const { action, errorMessage = '' } = response; + if (action) { + component.handleAction(action); + } else { + throw new Error(errorMessage); + } + }, + error() { + component.handleError(); }, - body: JSON.stringify(data), }); - const { action, errorMessage = '' } = await response.json(); - if (response.ok && action) { - component.handleAction(action); - } else { - throw new Error(errorMessage); - } } catch (e) { component.handleError(); } @@ -33,6 +40,7 @@ async function saveShopperDetails(details, actions) { type: 'post', data: { shopperDetails: JSON.stringify(details), + csrf_token: $('#adyen-token').val(), }, success() { actions.resolve(); @@ -55,6 +63,13 @@ function redirectToReviewPage(data) { value: JSON.stringify(data), }); + $('') + .appendTo(redirect) + .attr({ + name: 'csrf_token', + value: $('#adyen-token').val(), + }); + redirect.submit(); } @@ -62,8 +77,10 @@ function makeExpressPaymentDetailsCall(data) { return $.ajax({ type: 'POST', url: window.makeExpressPaymentDetailsCall, - data: JSON.stringify({ data }), - contentType: 'application/json; charset=utf-8', + data: { + csrf_token: $('#adyen-token').val(), + data: JSON.stringify({ data }), + }, async: false, success(response) { helpers.createShowConfirmationForm(window.showConfirmationAction); diff --git a/src/cartridges/app_adyen_SFRA/cartridge/templates/default/account/payment/paymentForm.isml b/src/cartridges/app_adyen_SFRA/cartridge/templates/default/account/payment/paymentForm.isml index 5b9441f13..c917ee659 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/templates/default/account/payment/paymentForm.isml +++ b/src/cartridges/app_adyen_SFRA/cartridge/templates/default/account/payment/paymentForm.isml @@ -17,6 +17,7 @@ environment: '${pdict.adyen.environment}' }; window.redirectUrl = "${URLUtils.url('PaymentInstruments-List')}"; + window.paymentsDetailsURL = "${URLUtils.https('Adyen-PaymentsDetails')}";
diff --git a/src/cartridges/app_adyen_SFRA/cartridge/templates/default/cart/checkoutButtons.isml b/src/cartridges/app_adyen_SFRA/cartridge/templates/default/cart/checkoutButtons.isml index 630c5db42..97db6c106 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/templates/default/cart/checkoutButtons.isml +++ b/src/cartridges/app_adyen_SFRA/cartridge/templates/default/cart/checkoutButtons.isml @@ -100,3 +100,4 @@ + diff --git a/src/cartridges/app_adyen_SFRA/cartridge/templates/default/cart/checkoutReview.isml b/src/cartridges/app_adyen_SFRA/cartridge/templates/default/cart/checkoutReview.isml index a7f24beda..82526062b 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/templates/default/cart/checkoutReview.isml +++ b/src/cartridges/app_adyen_SFRA/cartridge/templates/default/cart/checkoutReview.isml @@ -1,4 +1,5 @@