Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update remaining amount label if order amount is covered by giftcard #880

Merged
merged 2 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,23 @@ function getGiftCardConfig() {
giftCardSelect,
giftCardCancelButton,
giftCardAddButton,
giftCardSelectWrapper,
} = getGiftCardElements();
if (giftCardSelect) {
giftCardSelect.classList.add('invisible');
if (giftCardSelectWrapper) {
giftCardSelectWrapper.classList.add('invisible');
}
const initialPartialObject = { ...store.partialPaymentsOrderObj };
giftCardCancelButton.classList.remove('invisible');
giftCardCancelButton.addEventListener('click', () => {
store.componentsObj.giftcard.node.unmount('component_giftcard');
giftCardCancelButton.classList.add('invisible');
giftCardAddButton.style.display = 'block';
giftCardSelect.value = 'null';
store.partialPaymentsOrderObj.remainingAmountFormatted =
initialPartialObject.remainingAmountFormatted;
store.partialPaymentsOrderObj.totalDiscountedAmount =
initialPartialObject.totalDiscountedAmount;
createElementsToShowRemainingGiftCardAmount();
});
document.querySelector(
'button[value="submit-payment"]',
Expand All @@ -159,6 +166,11 @@ function getGiftCardConfig() {
giftCardsInfoMessageContainer.classList.remove(
'gift-cards-info-message-container',
);
store.partialPaymentsOrderObj.remainingAmountFormatted =
data.remainingAmountFormatted;
store.partialPaymentsOrderObj.totalDiscountedAmount =
data.totalAmountFormatted;
createElementsToShowRemainingGiftCardAmount();
resolve(data);
} else if (data.resultCode === constants.NOTENOUGHBALANCE) {
resolve(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ function attachGiftCardFormListeners() {

if (giftCardSelect) {
giftCardSelectWrapper.addEventListener('mousedown', () => {
giftCardSelect.innerHTML = '';
giftCardUl.classList.toggle('invisible');
});
}
Expand Down Expand Up @@ -149,7 +148,12 @@ function showGiftCardWarningMessage() {
}

function attachGiftCardAddButtonListener() {
const { giftCardAddButton, giftCardSelectContainer } = getGiftCardElements();
const {
giftCardAddButton,
giftCardSelectContainer,
giftCardSelectWrapper,
giftCardSelect,
} = getGiftCardElements();
if (giftCardAddButton) {
giftCardAddButton.addEventListener('click', () => {
renderGiftCardSelectForm();
Expand All @@ -160,8 +164,10 @@ function attachGiftCardAddButtonListener() {
if (giftCardWarningMessageEl) {
giftCardWarningMessageEl.style.display = 'none';
}
giftCardSelect.value = 'null';
giftCardAddButton.style.display = 'none';
giftCardSelectContainer.classList.remove('invisible');
giftCardSelectWrapper.classList.remove('invisible');
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
const BasketMgr = require('dw/order/BasketMgr');
const Money = require('dw/value/Money');
const AdyenHelper = require('*/cartridge/scripts/util/adyenHelper');
const adyenCheckout = require('*/cartridge/scripts/adyenCheckout');
const AdyenConfigs = require('*/cartridge/scripts/util/adyenConfigs');
const constants = require('*/cartridge/adyenConstants/constants');
const AdyenLogs = require('*/cartridge/scripts/adyenCustomLogs');

function getFormattedProperties(checkBalanceResponse, orderAmount) {
if (checkBalanceResponse.resultCode === 'Success') {
const remainingAmount = new Money(0, checkBalanceResponse.balance.currency);
const remainingDivideBy =
AdyenHelper.getDivisorForCurrency(remainingAmount);
const remainingAmountFormatted = remainingAmount
.divide(remainingDivideBy)
.toFormattedString();
const totalAmount = new Money(orderAmount.value, orderAmount.currency);
const totalDivideBy = AdyenHelper.getDivisorForCurrency(totalAmount);
const totalAmountFormatted = totalAmount
.divide(totalDivideBy)
.toFormattedString();
return {
remainingAmountFormatted,
totalAmountFormatted,
};
}
return {};
}

function callCheckBalance(req, res, next) {
try {
const currentBasket = BasketMgr.getCurrentBasket();
Expand Down Expand Up @@ -34,9 +56,12 @@ function callCheckBalance(req, res, next) {
paymentMethod,
};

const response = adyenCheckout.doCheckBalanceCall(checkBalanceRequest);

res.json(response);
const checkBalanceResponse =
adyenCheckout.doCheckBalanceCall(checkBalanceRequest);
res.json({
...checkBalanceResponse,
...getFormattedProperties(checkBalanceResponse, orderAmount),
});
} catch (error) {
AdyenLogs.error_log(
`Failed to check gift card balance ${error.toString()}`,
Expand Down