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

Create payment instruments for each giftcard #857

Merged
merged 6 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -78,57 +78,30 @@ function failOrder(order) {
}

function handleGiftCardPayment(currentBasket, order) {
// Check if gift card was used
const divideBy = AdyenHelper.getDivisorForCurrency(
currentBasket.totalGrossPrice,
);
const parsedGiftCardObj = JSON.parse(session.privacy.giftCardResponse);
const remainingAmount = {
value: parsedGiftCardObj.remainingAmount.value,
currency: parsedGiftCardObj.remainingAmount.currency,
};
const formattedAmount = new Money(
remainingAmount.value,
remainingAmount.currency,
).divide(divideBy);
const mainPaymentInstrument = order.getPaymentInstruments(
AdyenHelper.getOrderMainPaymentInstrumentType(order),
)[0];
// update amount from order total to PM total
Transaction.wrap(() => {
mainPaymentInstrument.paymentTransaction.setAmount(formattedAmount);
});

const paidGiftcardAmount = {
value: parsedGiftCardObj.value,
currency: parsedGiftCardObj.currency,
};
const formattedGiftcardAmount = new Money(
paidGiftcardAmount.value,
paidGiftcardAmount.currency,
).divide(divideBy);
Transaction.wrap(() => {
const giftcardPM = order.createPaymentInstrument(
constants.METHOD_ADYEN_COMPONENT,
formattedGiftcardAmount,
);
const { paymentProcessor } = PaymentMgr.getPaymentMethod(
giftcardPM.paymentMethod,
);
giftcardPM.paymentTransaction.paymentProcessor = paymentProcessor;
giftcardPM.custom.adyenPaymentMethod = parsedGiftCardObj.brand;
giftcardPM.custom[`${constants.OMS_NAMESPACE}_Adyen_Payment_Method`] =
parsedGiftCardObj.brand;
giftcardPM.custom.Adyen_Payment_Method_Variant =
parsedGiftCardObj.paymentMethod.brand;
giftcardPM.custom[
`${constants.OMS_NAMESPACE}_Adyen_Payment_Method_Variant`
] = parsedGiftCardObj.paymentMethod.brand;
giftcardPM.paymentTransaction.custom.Adyen_log =
session.privacy.giftCardResponse;
giftcardPM.paymentTransaction.custom.Adyen_pspReference =
parsedGiftCardObj.giftCardpspReference;
});
const giftCards = currentBasket.custom?.adyenGiftCards
? JSON.parse(currentBasket.custom.adyenGiftCards)
: null;
if (giftCards) {
const mainPaymentInstrument = order.getPaymentInstruments(
AdyenHelper.getOrderMainPaymentInstrumentType(order),
)[0];
giftCards.forEach((giftCard) => {
const divideBy = AdyenHelper.getDivisorForCurrency(
mainPaymentInstrument.paymentTransaction.getAmount(),
);
const amount = {
value: giftCard.remainingAmount.value,
currency: giftCard.remainingAmount.currency,
};
const formattedAmount = new Money(amount.value, amount.currency).divide(
divideBy,
);
Transaction.wrap(() => {
mainPaymentInstrument.paymentTransaction.setAmount(formattedAmount);
});
AdyenHelper.createGiftCardPM(giftCard, divideBy, order);
});
}
}

function handleCancellation(res, next, reqDataObj) {
Expand Down Expand Up @@ -211,6 +184,11 @@ function paymentFromComponent(req, res, next) {

const order = COHelpers.createOrder(currentBasket);

// Check if gift card was used
if (currentBasket.custom?.adyenGiftCards) {
handleGiftCardPayment(currentBasket, order);
}

let result;
Transaction.wrap(() => {
result = adyenCheckout.createPaymentRequest({
Expand All @@ -225,10 +203,6 @@ function paymentFromComponent(req, res, next) {
handleRefusedResultCode(result, reqDataObj, order);
}

// Check if gift card was used
if (session.privacy.giftCardResponse) {
handleGiftCardPayment(currentBasket, order);
}
if (AdyenHelper.isApplePay(reqDataObj.paymentMethod?.type)) {
result.isApplePay = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const adyenHelpers = require('*/cartridge/scripts/checkout/adyenHelpers');
const AdyenHelper = require('*/cartridge/scripts/util/adyenHelper');
const constants = require('*/cartridge/adyenConstants/constants');
const { processPayment, isNotAdyen } = require('*/cartridge/controllers/middlewares/checkout_services/adyenCheckoutServices');
const PaymentMgr = require('dw/order/PaymentMgr');
const Money = require('dw/value/Money');
const { clearForms } = require('*/cartridge/controllers/utils/index');

Expand Down Expand Up @@ -130,33 +129,6 @@ function placeOrder(req, res, next) {
// Handles payment authorization
var handlePaymentResult = adyenHelpers.handlePayments(order);

function createGiftCardPM(parsedGiftCardObj, divideBy) {
let paymentInstrument;
const paidGiftCardAmount = {
value: parsedGiftCardObj.giftCard.amount.value,
currency: parsedGiftCardObj.giftCard.amount.currency
};
const paidGiftCardAmountFormatted = new Money(paidGiftCardAmount.value, paidGiftCardAmount.currency).divide(divideBy);
Transaction.wrap(() => {
paymentInstrument = order.createPaymentInstrument(
constants.METHOD_ADYEN_COMPONENT,
paidGiftCardAmountFormatted,
);
const { paymentProcessor } = PaymentMgr.getPaymentMethod(
paymentInstrument.paymentMethod,
);
paymentInstrument.paymentTransaction.paymentProcessor = paymentProcessor;
paymentInstrument.custom.adyenPaymentMethod = parsedGiftCardObj.giftCard.name;
paymentInstrument.custom[`${constants.OMS_NAMESPACE}_Adyen_Payment_Method`] = parsedGiftCardObj.giftCard.name;
paymentInstrument.custom.Adyen_Payment_Method_Variant = parsedGiftCardObj.giftCard.brand;
paymentInstrument.custom[
`${constants.OMS_NAMESPACE}_Adyen_Payment_Method_Variant`
] = parsedGiftCardObj.giftCard.brand;
paymentInstrument.paymentTransaction.custom.Adyen_log = JSON.stringify(parsedGiftCardObj);
paymentInstrument.paymentTransaction.custom.Adyen_pspReference = parsedGiftCardObj.giftCard.pspReference;
})
}

const mainPaymentInstrument = order.getPaymentInstruments(
AdyenHelper.getOrderMainPaymentInstrumentType(order)
)[0];
Expand All @@ -176,7 +148,7 @@ function placeOrder(req, res, next) {
Transaction.wrap(() => {
mainPaymentInstrument.paymentTransaction.setAmount(formattedAmount); //update amount from order total to PM total
});
createGiftCardPM(giftCard, divideBy);
AdyenHelper.createGiftCardPM(giftCard, divideBy, order);
});
}
/* ### Custom Adyen cartridge end ### */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const PaymentInstrument = require('dw/order/PaymentInstrument');
//script includes
const AdyenLogs = require('*/cartridge/scripts/adyenCustomLogs');
const BasketMgr = require('dw/order/BasketMgr');
const PaymentMgr = require('dw/order/PaymentMgr');
const Money = require('dw/value/Money');

/* eslint no-var: off */
var adyenHelperObj = {
Expand Down Expand Up @@ -880,6 +882,33 @@ var adyenHelperObj = {

return JSON.parse(resultObject.getText());
},

createGiftCardPM(parsedGiftCardObj, divideBy, order) {
let paymentInstrument;
amihajlovski marked this conversation as resolved.
Show resolved Hide resolved
const paidGiftCardAmount = {
value: parsedGiftCardObj.giftCard.amount.value,
currency: parsedGiftCardObj.giftCard.amount.currency
};
const paidGiftCardAmountFormatted = new Money(paidGiftCardAmount.value, paidGiftCardAmount.currency).divide(divideBy);
Transaction.wrap(() => {
paymentInstrument = order.createPaymentInstrument(
constants.METHOD_ADYEN_COMPONENT,
paidGiftCardAmountFormatted,
);
const { paymentProcessor } = PaymentMgr.getPaymentMethod(
paymentInstrument.paymentMethod,
);
paymentInstrument.paymentTransaction.paymentProcessor = paymentProcessor;
paymentInstrument.custom.adyenPaymentMethod = parsedGiftCardObj.giftCard.name;
paymentInstrument.custom[`${constants.OMS_NAMESPACE}_Adyen_Payment_Method`] = parsedGiftCardObj.giftCard.name;
paymentInstrument.custom.Adyen_Payment_Method_Variant = parsedGiftCardObj.giftCard.brand;
paymentInstrument.custom[
`${constants.OMS_NAMESPACE}_Adyen_Payment_Method_Variant`
] = parsedGiftCardObj.giftCard.brand;
paymentInstrument.paymentTransaction.custom.Adyen_log = JSON.stringify(parsedGiftCardObj);
paymentInstrument.paymentTransaction.custom.Adyen_pspReference = parsedGiftCardObj.giftCard.pspReference;
})
}
};

module.exports = adyenHelperObj;