Skip to content

Commit

Permalink
fix(SFI-1025): remove try catch for donation
Browse files Browse the repository at this point in the history
  • Loading branch information
shanikantsingh committed Dec 24, 2024
1 parent 14d8cf1 commit 12ac127
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,81 +31,75 @@ const { AdyenError } = require('*/cartridge/adyen/logs/adyenError');

// eslint-disable-next-line complexity
function donate(donationReference, donationAmount, orderToken) {
try {
if (session.privacy.orderNo !== donationReference) {
throw new AdyenError('Donation reference is invalid');
}
if (session.privacy.orderNo !== donationReference) {
throw new AdyenError('Donation reference is invalid');
}

let paymentMethodVariant;
const order = OrderMgr.getOrder(donationReference, orderToken);
const paymentInstrument = order.getPaymentInstruments(
AdyenHelper.getOrderMainPaymentInstrumentType(order),
)[0];
const donationToken =
paymentInstrument.paymentTransaction.custom.Adyen_donationToken;
const originalReference =
paymentInstrument.paymentTransaction.custom.Adyen_pspReference;
const paymentData = JSON.parse(
paymentInstrument.paymentTransaction.custom.Adyen_log,
);
const paymentCurrency =
paymentData.amount.currency || paymentData.fullResponse?.amount?.currency;
const availableDonationAmounts = AdyenHelper.getDonationAmounts();
paymentMethodVariant =
paymentData.paymentMethod?.type ||
paymentData.fullResponse?.paymentMethod?.type;
let paymentMethodVariant;
const order = OrderMgr.getOrder(donationReference, orderToken);
const paymentInstrument = order.getPaymentInstruments(
AdyenHelper.getOrderMainPaymentInstrumentType(order),
)[0];
const donationToken =
paymentInstrument.paymentTransaction.custom.Adyen_donationToken;
const originalReference =
paymentInstrument.paymentTransaction.custom.Adyen_pspReference;
const paymentData = JSON.parse(
paymentInstrument.paymentTransaction.custom.Adyen_log,
);
const paymentCurrency =
paymentData.amount.currency || paymentData.fullResponse?.amount?.currency;
const availableDonationAmounts = AdyenHelper.getDonationAmounts();
paymentMethodVariant =
paymentData.paymentMethod?.type ||
paymentData.fullResponse?.paymentMethod?.type;

// for iDeal donations, the payment method variant needs to be set to sepadirectdebit
if (paymentMethodVariant === 'ideal') {
paymentMethodVariant = 'sepadirectdebit';
}
// for Apple Pay donations, the payment method variant needs to be the brand
if (paymentMethodVariant === 'applepay') {
paymentMethodVariant =
paymentData.paymentMethod?.brand ||
paymentData.fullResponse?.paymentMethod?.brand;
}
const requestObject = {
merchantAccount: AdyenConfigs.getAdyenMerchantAccount(),
donationAccount: AdyenConfigs.getAdyenGivingCharityAccount(),
amount: donationAmount,
reference: `${AdyenConfigs.getAdyenMerchantAccount()}-${donationReference}`,
donationOriginalPspReference: originalReference,
donationToken,
paymentMethod: {
type: paymentMethodVariant,
},
shopperInteraction: constants.SHOPPER_INTERACTIONS.CONT_AUTH,
};
// for iDeal donations, the payment method variant needs to be set to sepadirectdebit
if (paymentMethodVariant === 'ideal') {
paymentMethodVariant = 'sepadirectdebit';
}
// for Apple Pay donations, the payment method variant needs to be the brand
if (paymentMethodVariant === 'applepay') {
paymentMethodVariant =
paymentData.paymentMethod?.brand ||
paymentData.fullResponse?.paymentMethod?.brand;
}
const requestObject = {
merchantAccount: AdyenConfigs.getAdyenMerchantAccount(),
donationAccount: AdyenConfigs.getAdyenGivingCharityAccount(),
amount: donationAmount,
reference: `${AdyenConfigs.getAdyenMerchantAccount()}-${donationReference}`,
donationOriginalPspReference: originalReference,
donationToken,
paymentMethod: {
type: paymentMethodVariant,
},
shopperInteraction: constants.SHOPPER_INTERACTIONS.CONT_AUTH,
};

if (
availableDonationAmounts.indexOf(parseInt(donationAmount.value, 10)) ===
-1
) {
throw new AdyenError('Donation amount is invalid');
}
if (
availableDonationAmounts.indexOf(parseInt(donationAmount.value, 10)) === -1
) {
throw new AdyenError('Donation amount is invalid');
}

if (paymentCurrency !== donationAmount.currency) {
throw new AdyenError('Donation currency is invalid');
}
if (paymentCurrency !== donationAmount.currency) {
throw new AdyenError('Donation currency is invalid');
}

const response = AdyenHelper.executeCall(
constants.SERVICE.ADYENGIVING,
requestObject,
);
const response = AdyenHelper.executeCall(
constants.SERVICE.ADYENGIVING,
requestObject,
);

Transaction.wrap(() => {
order.custom.Adyen_donationAmount = JSON.stringify(donationAmount);
// Donation token is deleted in case the donation is completed once
if (response.status === constants.DONATION_RESULT.COMPLETED) {
paymentInstrument.paymentTransaction.custom.Adyen_donationToken = null;
}
});
return response;
} catch (error) {
AdyenLogs.error_log('/donations call failed:', error);
return { error: true };
}
Transaction.wrap(() => {
order.custom.Adyen_donationAmount = JSON.stringify(donationAmount);
// Donation token is deleted in case the donation is completed once
if (response.status === constants.DONATION_RESULT.COMPLETED) {
paymentInstrument.paymentTransaction.custom.Adyen_donationToken = null;
}
});
return response;
}

function donation(req, res, next) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
const AdyenHelper = require('*/cartridge/adyen/utils/adyenHelper');
const AdyenConfigs = require('*/cartridge/adyen/utils/adyenConfigs');
const constants = require('*/cartridge/adyen/config/constants');
const { AdyenError } = require('*/cartridge/adyen/logs/adyenError');

// eslint-disable-next-line complexity
function deleteRecurringPayment(args) {
Expand All @@ -41,7 +42,7 @@ function deleteRecurringPayment(args) {
}

if (!(customerID && recurringDetailReference)) {
throw new Error('No Customer ID or RecurringDetailReference provided');
throw new AdyenError('No Customer ID or RecurringDetailReference provided');
}

const requestObject = {
Expand Down

0 comments on commit 12ac127

Please sign in to comment.