From 30cf663ab9f190dcb926ca1892fcc5021985cf7e Mon Sep 17 00:00:00 2001 From: aleksandarm Date: Mon, 2 Dec 2024 11:21:32 +0100 Subject: [PATCH] feat: redirect to confirmation page --- .../adyen_checkout/renderGenericComponent.js | 3 +- .../client/default/js/googlePayExpress.js | 137 +++++++++++++----- .../handlePaymentFromComponent.js | 9 +- .../cartridge/adyen/utils/adyenHelper.js | 3 + 4 files changed, 106 insertions(+), 46 deletions(-) diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGenericComponent.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGenericComponent.js index 7f02b9482..99c4d77f4 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGenericComponent.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/adyen_checkout/renderGenericComponent.js @@ -217,8 +217,7 @@ function setGiftCardContainerVisibility() { } export async function initializeCheckout() { - const paymentMethods = await getPaymentMethods(); - const paymentMethodsResponse = await paymentMethods.json(); + const paymentMethodsResponse = await getPaymentMethods(); const giftCardsData = await fetchGiftCards(); setCheckoutConfiguration(paymentMethodsResponse); diff --git a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/googlePayExpress.js b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/googlePayExpress.js index 76e400cf7..590a21008 100644 --- a/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/googlePayExpress.js +++ b/src/cartridges/app_adyen_SFRA/cartridge/client/default/js/googlePayExpress.js @@ -151,6 +151,35 @@ function getShippingOptionsParameters(selectedShippingMethod) { }; } +function handleAuthorised(response) { + console.log('handleAuthorised'); + document.querySelector('#result').value = JSON.stringify({ + pspReference: response.fullResponse?.pspReference, + resultCode: response.fullResponse?.resultCode, + paymentMethod: response.fullResponse?.paymentMethod + ? response.fullResponse.paymentMethod + : response.fullResponse?.additionalData?.paymentMethod, + donationToken: response.fullResponse?.donationToken, + amount: response.fullResponse?.amount, + }); + document.querySelector('#showConfirmationForm').submit(); +} + +function handleError() { + document.querySelector('#result').value = JSON.stringify({ + error: true, + }); + document.querySelector('#showConfirmationForm').submit(); +} + +function handleGooglePayResponse(response) { + if (response.resultCode === 'Authorised') { + handleAuthorised(response); + } else { + handleError(); + } +} + function paymentFromComponent(data) { $.ajax({ url: window.paymentFromComponentURL, @@ -163,6 +192,13 @@ function paymentFromComponent(data) { success(response) { helpers.createShowConfirmationForm(window.showConfirmationAction); helpers.setOrderFormData(response); + document.querySelector('#additionalDetailsHidden').value = JSON.stringify( + { + ...data, + ...response, + }, + ); + handleGooglePayResponse(response); }, }); } @@ -179,6 +215,59 @@ async function initializeCheckout(paymentMethodsResponse) { }); } +async function onShippingAddressChange( + resolve, + reject, + shippingAddress, + paymentDataRequestUpdate, +) { + shippingMethodsData = await getShippingMethods( + shippingAddress, + temporaryBasketId, + reject, + ); + if (shippingMethodsData?.shippingMethods?.length) { + const selectedShippingMethod = shippingMethodsData.shippingMethods[0]; + const newCalculation = await selectShippingMethod( + selectedShippingMethod, + temporaryBasketId, + reject, + ); + if (newCalculation?.grandTotalAmount) { + paymentDataRequestUpdate.newShippingOptionParameters = + getShippingOptionsParameters(selectedShippingMethod); + paymentDataRequestUpdate.newTransactionInfo = + getTransactionInfo(newCalculation); + } else { + reject(); + } + } else { + reject(); + } +} + +async function onShippingOptionChange( + reject, + shippingOptionData, + paymentDataRequestUpdate, +) { + const shippingMethods = shippingMethodsData?.shippingMethods; + const matchingShippingMethod = shippingMethods.find( + (sm) => sm.ID === shippingOptionData.id, + ); + const newCalculation = await selectShippingMethod( + matchingShippingMethod, + temporaryBasketId, + reject, + ); + if (newCalculation?.grandTotalAmount) { + paymentDataRequestUpdate.newTransactionInfo = + getTransactionInfo(newCalculation); + } else { + reject(); + } +} + async function init(paymentMethodsResponse) { initializeCheckout(paymentMethodsResponse) .then(async () => { @@ -212,9 +301,7 @@ async function init(paymentMethodsResponse) { configuration: googlePayConfig, callbackIntents: ['SHIPPING_ADDRESS', 'SHIPPING_OPTION'], amount: JSON.parse(window.basketAmount), - onError: (err) => console.log(err), onAuthorized: async (data) => { - console.log(data); const componentData = googlePayButton.data; const stateData = { paymentMethod: componentData.paymentMethod, @@ -223,9 +310,7 @@ async function init(paymentMethodsResponse) { const customer = formatCustomerObject(data); paymentFromComponent({ ...stateData, customer }); }, - onSubmit: async () => { - console.log('onsubmit'); - }, + onSubmit: async () => {}, paymentDataCallbacks: { onPaymentDataChanged(intermediatePaymentData) { // eslint-disable-next-line no-async-promise-executor @@ -237,48 +322,20 @@ async function init(paymentMethodsResponse) { callbackTrigger === CALLBACK_TRIGGERS.INITIALIZE || callbackTrigger === CALLBACK_TRIGGERS.SHIPPING_ADDRESS ) { - shippingMethodsData = await getShippingMethods( - shippingAddress, - temporaryBasketId, + await onShippingAddressChange( + resolve, reject, + shippingAddress, + paymentDataRequestUpdate, ); - if (shippingMethodsData?.shippingMethods?.length) { - const selectedShippingMethod = - shippingMethodsData.shippingMethods[0]; - const newCalculation = await selectShippingMethod( - selectedShippingMethod, - temporaryBasketId, - reject, - ); - if (newCalculation?.grandTotalAmount) { - paymentDataRequestUpdate.newShippingOptionParameters = - getShippingOptionsParameters(selectedShippingMethod); - paymentDataRequestUpdate.newTransactionInfo = - getTransactionInfo(newCalculation); - } else { - reject(); - } - } else { - reject(); - } } if (callbackTrigger === CALLBACK_TRIGGERS.SHIPPING_OPTION) { - const shippingMethods = shippingMethodsData?.shippingMethods; - const matchingShippingMethod = shippingMethods.find( - (sm) => sm.ID === shippingOptionData.id, - ); - const newCalculation = await selectShippingMethod( - matchingShippingMethod, - temporaryBasketId, + await onShippingOptionChange( reject, + shippingOptionData, + paymentDataRequestUpdate, ); - if (newCalculation?.grandTotalAmount) { - paymentDataRequestUpdate.newTransactionInfo = - getTransactionInfo(newCalculation); - } else { - reject(); - } } resolve(paymentDataRequestUpdate); diff --git a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/showConfirmation/handlePaymentFromComponent.js b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/showConfirmation/handlePaymentFromComponent.js index 51bd060ab..ffc2991b4 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/showConfirmation/handlePaymentFromComponent.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/adyen/scripts/showConfirmation/handlePaymentFromComponent.js @@ -132,10 +132,11 @@ function handlePayment(stateData, order, options) { let finalResult; if (!hasStateData) { if ( - result && - (JSON.stringify(result).indexOf('amazonpay') > -1 || - JSON.stringify(result).indexOf('applepay') > -1 || - JSON.stringify(result).indexOf('cashapp') > -1) + (result && + (JSON.stringify(result).indexOf('amazonpay') > -1 || + JSON.stringify(result).indexOf('applepay') > -1 || + JSON.stringify(result).indexOf('cashapp') > -1)) || + JSON.stringify(result).indexOf('googlepay') > -1 ) { finalResult = JSON.parse(result); } else { diff --git a/src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/adyenHelper.js b/src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/adyenHelper.js index 4feb36364..97d2fa38d 100644 --- a/src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/adyenHelper.js +++ b/src/cartridges/int_adyen_SFRA/cartridge/adyen/utils/adyenHelper.js @@ -634,6 +634,9 @@ let adyenHelperObj = { case 'paypal': methodName = 'PayPal'; break; + case 'googlepay': + methodName = 'Google Pay'; + break; default: methodName = paymentMethod; }