Skip to content

Commit

Permalink
Refactored Adyen Giving (#917)
Browse files Browse the repository at this point in the history
* feat: refactored adyen giving code

* chore: updated the test snapshot to include orderToken

* chore: linting issues fixed
  • Loading branch information
zenit2001 committed Jun 1, 2023
1 parent c54d09d commit 6ab8e8b
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ function handleOnDonate(state, component) {
const donationData = {
amountValue: selectedAmount.value,
amountCurrency: selectedAmount.currency,
orderNo,
pspReference,
orderNo: window.orderNo,
orderToken: window.orderToken,
};

$.ajax({
Expand Down
5 changes: 2 additions & 3 deletions src/cartridges/int_adyen_SFRA/cartridge/controllers/Adyen.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ server.post(
* Complete a donation through adyenGiving
*/
server.post('Donate', server.middleware.https, (req /* , res, next */) => {
const { pspReference } = req.form;
const { orderNo } = req.form;
const { orderNo, orderToken } = req.form;
const donationAmount = {
value: req.form.amountValue,
currency: req.form.amountCurrency,
};
const donationResult = adyenGiving.donate(
orderNo,
donationAmount,
pspReference,
orderToken,
);

return donationResult.response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ function paymentFromComponent(req, res, next) {
handleExpressPayment(reqDataObj, currentBasket);

const order = COHelpers.createOrder(currentBasket);
session.privacy.orderNo = order.orderNo;

// Check if gift card was used
if (currentBasket.custom?.adyenGiftCards) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ function placeOrder(req, res, next) {
// Creates a new order.
var order = COHelpers.createOrder(currentBasket);
if (!order) {
session.privacy.orderNo = null;
res.json({
error: true,
errorMessage: Resource.msg('error.technical', 'checkout', null)
Expand All @@ -126,6 +127,7 @@ function placeOrder(req, res, next) {
// Cache order number in order to be able to restore cart later
req.session.privacyCache.set('currentOrderNumber', order.orderNo);
req.session.privacyCache.set('currentOrderToken', order.orderToken);
session.privacy.orderNo = order.orderNo;

// Handles payment authorization
var handlePaymentResult = adyenHelpers.handlePayments(order);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ exports[`Confirm should set view data 1`] = `
"clientKey": "mocked_client_key",
"donationAmounts": "{"currency":"EUR","values":[10,20,30]}",
"environment": "test",
"pspReference": undefined,
"orderToken": "mocked_token",
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function getOrderToken(req) {
: req.querystring.token;
}

function handleAdyenGiving(req, res, order) {
function handleAdyenGiving(req, res) {
const clientKey = AdyenConfigs.getAdyenClientKey();
const environment = AdyenHelper.getCheckoutEnvironment();
const configuredAmounts = AdyenHelper.getDonationAmounts();
Expand All @@ -23,10 +23,7 @@ function handleAdyenGiving(req, res, order) {
const charityDescription = AdyenConfigs.getAdyenGivingCharityDescription();
const adyenGivingBackgroundUrl = AdyenConfigs.getAdyenGivingBackgroundUrl();
const adyenGivingLogoUrl = AdyenConfigs.getAdyenGivingLogoUrl();

const paymentInstrument = order.getPaymentInstruments(
AdyenHelper.getOrderMainPaymentInstrumentType(order),
)[0];
const orderToken = getOrderToken(req);

const donationAmounts = {
currency: session.currency.currencyCode,
Expand All @@ -37,14 +34,13 @@ function handleAdyenGiving(req, res, order) {
clientKey,
environment,
adyenGivingAvailable: true,
pspReference:
paymentInstrument.paymentTransaction.custom.Adyen_pspReference,
donationAmounts: JSON.stringify(donationAmounts),
charityName,
charityDescription,
charityWebsite,
adyenGivingBackgroundUrl,
adyenGivingLogoUrl,
orderToken,
};
res.setViewData(viewData);
}
Expand All @@ -61,7 +57,7 @@ function confirm(req, res, next) {
AdyenHelper.getAdyenGivingConfig(order) &&
AdyenHelper.isAdyenGivingAvailable(paymentInstrument)
) {
handleAdyenGiving(req, res, order);
handleAdyenGiving(req, res);
}
}
return next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const Transaction = require('dw/system/Transaction');
function clearCustomSessionFields() {
// Clears all fields used in the 3d secure payment.
session.privacy.paymentMethod = null;
session.privacy.orderNo = null;
session.privacy.brandCode = null;
session.privacy.issuer = null;
session.privacy.adyenPaymentMethod = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
window.donateURL = "${URLUtils.https('Adyen-Donate')}";

var orderNo = '${pdict.order.orderNumber}';
var orderToken = '${pdict.adyen.orderToken}';
var donationAmounts = '${pdict.adyen.donationAmounts}'.replace(/"/g, '\"');
var pspReference = '${pdict.adyen.pspReference}';
var charityName = '${pdict.adyen.charityName}';
var charityWebsite = '${pdict.adyen.charityWebsite}';
var charityDescription = '${pdict.adyen.charityDescription}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ module.exports = {
ECOMMERCE : 'Ecommerce',
},

DONATION_RESULT : {
COMPLETED : 'completed',
},

RECURRING_PROCESSING_MODEL : {
CARD_ON_FILE : 'CardOnFile',
},
Expand Down
27 changes: 24 additions & 3 deletions src/cartridges/int_adyen_overlay/cartridge/scripts/adyenGiving.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,23 @@ const AdyenConfigs = require('*/cartridge/scripts/util/adyenConfigs');
const constants = require('*/cartridge/adyenConstants/constants');
const AdyenLogs = require('*/cartridge/scripts/adyenCustomLogs');

function donate(donationReference, donationAmount, originalReference) {
function donate(donationReference, donationAmount, orderToken) {
try {

if (session.privacy.orderNo !== donationReference){
throw new Error(`Donation reference is invalid`);
}

let paymentMethodVariant;
const order = OrderMgr.getOrder(donationReference);
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;
const availableDonationAmounts = AdyenHelper.getDonationAmounts();
paymentMethodVariant = paymentInstrument.custom.Adyen_Payment_Method_Variant;

// for iDeal donations, the payment method variant needs to be set to sepadirectdebit
Expand All @@ -54,13 +63,25 @@ function donate(donationReference, donationAmount, originalReference) {
shopperInteraction: constants.SHOPPER_INTERACTIONS.CONT_AUTH,
};

if (availableDonationAmounts.indexOf(parseInt(donationAmount.value)) === -1){
throw new Error(`Donation amount is invalid`);
}

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

const platformVersion = AdyenHelper.getApplicationInfo().externalPlatform.version;
const service = platformVersion === constants.PLATFORMS.SG ? `${constants.SERVICE.ADYENGIVING}${constants.PLATFORMS.SG}` : constants.SERVICE.ADYENGIVING;
const response = AdyenHelper.executeCall(service, requestObject);
const response = AdyenHelper.executeCall(service, requestObject);

Transaction.wrap(() => {
const order = OrderMgr.getOrder(donationReference);
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 (e) {
Expand Down

0 comments on commit 6ab8e8b

Please sign in to comment.