Skip to content

Commit

Permalink
Fix remaining amount label if order amount is covered by giftcard (#880)
Browse files Browse the repository at this point in the history
* fix: remaining amount if order amount is covered by giftcard

* fix: removing select options
  • Loading branch information
amihajlovski committed Mar 24, 2023
1 parent 3028364 commit 8902221
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
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

0 comments on commit 8902221

Please sign in to comment.