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 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
6 changes: 5 additions & 1 deletion jest/sfccPathSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,4 +252,8 @@ jest.mock('*/cartridge/scripts/adyenLevelTwoThreeData', () => {

jest.mock('*/cartridge/scripts/adyenCustomLogs', () => {
return require('../src/cartridges/int_adyen_overlay/cartridge/scripts/adyenCustomLogs');
}, {virtual: true});
}, {virtual: true});

jest.mock('*/cartridge/scripts/util/giftCardsHelper', () => {
return require('../src/cartridges/int_adyen_overlay/cartridge/scripts/util/giftCardsHelper');
}, {virtual: true});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const constants = require('*/cartridge/adyenConstants/constants');
const collections = require('*/cartridge/scripts/util/collections');
const AdyenHelper = require('*/cartridge/scripts/util/adyenHelper');
const AdyenLogs = require('*/cartridge/scripts/adyenCustomLogs');
const GiftCardsHelper = require('*/cartridge/scripts/util/giftCardsHelper');

const expressMethods = ['applepay', 'amazonpay'];

Expand Down Expand Up @@ -78,57 +79,34 @@ 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);
});
GiftCardsHelper.createGiftCardPaymentInstrument(
giftCard,
divideBy,
order,
);
});
}
}

function handleCancellation(res, next, reqDataObj) {
Expand Down Expand Up @@ -211,6 +189,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 +208,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
@@ -1,9 +1,9 @@
/* ### Custom Adyen cartridge start ### */
const adyenHelpers = require('*/cartridge/scripts/checkout/adyenHelpers');
const AdyenHelper = require('*/cartridge/scripts/util/adyenHelper');
const GiftCardsHelper = require('*/cartridge/scripts/util/giftCardsHelper');
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 +130,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 +149,7 @@ function placeOrder(req, res, next) {
Transaction.wrap(() => {
mainPaymentInstrument.paymentTransaction.setAmount(formattedAmount); //update amount from order total to PM total
});
createGiftCardPM(giftCard, divideBy);
GiftCardsHelper.createGiftCardPaymentInstrument(giftCard, divideBy, order);
});
}
/* ### Custom Adyen cartridge end ### */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
* Adyen Salesforce Commerce Cloud
* Copyright (c) 2022 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*/
const constants = require('*/cartridge/adyenConstants/constants');
const Transaction = require('dw/system/Transaction');
//script includes
const PaymentMgr = require('dw/order/PaymentMgr');
const Money = require('dw/value/Money');

let giftCardsHelper = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

giftCardsHelper could be initialized as const

createGiftCardPaymentInstrument(parsedGiftCardObj, divideBy, order) {
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;
})
}
};

module.exports = giftCardsHelper;