Skip to content

Commit

Permalink
feat: redirect to confirmation page
Browse files Browse the repository at this point in the history
  • Loading branch information
amihajlovski committed Dec 2, 2024
1 parent 2b7e6e7 commit 30cf663
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
},
});
}
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@ let adyenHelperObj = {
case 'paypal':
methodName = 'PayPal';
break;
case 'googlepay':
methodName = 'Google Pay';
break;
default:
methodName = paymentMethod;
}
Expand Down

0 comments on commit 30cf663

Please sign in to comment.