Skip to content

Commit

Permalink
Create payment instruments for each giftcard (#857)
Browse files Browse the repository at this point in the history
* fix: parsing giftcards to reduce complexity

* fix: remove unused imports; use basket giftcards check

* fix: added new file path in jest config

* fix: removed unused imports

* fix: helpers jest path
  • Loading branch information
amihajlovski committed Mar 24, 2023
1 parent 20d1149 commit 3028364
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 85 deletions.
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 = {
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;

0 comments on commit 3028364

Please sign in to comment.