Skip to content

Commit

Permalink
Merge pull request #88 from alma/fix/develop_pr
Browse files Browse the repository at this point in the history
Fixe some comments for the PR develop -> main
  • Loading branch information
joyet-simon authored Oct 13, 2023
2 parents 2fa777a + 0608002 commit 1e1a422
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

var pkg = require('../../../package.json');

var Capture = {
var CAPTURE = {
toCapture: {
code: 'ToCapture'
},
total: {
code: 'Captured',
description: 'Total Capture'
Expand All @@ -16,6 +19,8 @@ var Capture = {
description: 'Unable to capture'
}
};
var MAX_INSTALLMENTS_COUNT_FOR_PNX = 4;
var MAX_DEFERRED_DAYS_FOR_PNX = 0;

/**
* Allow to check an order status
Expand Down Expand Up @@ -343,7 +348,7 @@ function cancelAlmaPayment(params) {
* @return {boolean} is available
*/
function isAvailableForManualCapture(isManualCaptureEnabled, installmentsCount, deferredDays) {
return isManualCaptureEnabled && installmentsCount <= 4 && deferredDays <= 0;
return isManualCaptureEnabled && installmentsCount <= MAX_INSTALLMENTS_COUNT_FOR_PNX && deferredDays <= MAX_DEFERRED_DAYS_FOR_PNX;
}

/**
Expand Down Expand Up @@ -498,7 +503,7 @@ module.exports = {
capturePayment: capturePayment,
isAvailableForManualCapture: isAvailableForManualCapture,
cancelAlmaPayment: cancelAlmaPayment,
Capture: Capture,
CAPTURE: CAPTURE,
isPaymentExpired: isPaymentExpired,
isPaymentAuthorizationExpired: isPaymentAuthorizationExpired
};
12 changes: 9 additions & 3 deletions cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ var almaWidgetHelper = require('*/cartridge/scripts/helpers/almaWidgetHelper');
var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper');
var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper');

var CAPTURE_METHOD = {
automatic: 'automatic',
manual: 'manual'
};

/**
* Calls /me/fee-plans and fetch the available plans for the current merchant
* @returns {array} plans
Expand Down Expand Up @@ -185,14 +190,14 @@ function getFormattedPlans(plans) {

Object.keys(plans[paymentMethod]).forEach(function (keys) {
paymentMethodPlans.push(plans[paymentMethod][keys]);
plans[paymentMethod][keys].captureMethod = 'automatic';
plans[paymentMethod][keys].captureMethod = CAPTURE_METHOD.automatic;

if (almaPaymentHelper.isAvailableForManualCapture(
almaConfigHelper.isDeferredCaptureEnable(),
plans[paymentMethod][keys].installments_count,
plans[paymentMethod][keys].deferred_days
)) {
plans[paymentMethod][keys].captureMethod = 'manual';
plans[paymentMethod][keys].captureMethod = CAPTURE_METHOD.manual;
}

if (!plans[paymentMethod][keys].properties) {
Expand Down Expand Up @@ -257,5 +262,6 @@ module.exports = {
getAllowedPlans: getAllowedPlans,
getPlansForWidget: getPlansForWidget,
getPlansForCheckout: getPlansForCheckout,
getFormattedPlans: getFormattedPlans
getFormattedPlans: getFormattedPlans,
CAPTURE_METHOD: CAPTURE_METHOD
};
58 changes: 34 additions & 24 deletions cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,52 @@
'use strict';

var OrderMgr = require('dw/order/OrderMgr');
var Order = require('dw/order/Order');
var Status = require('dw/system/Status');
var almaOrderHelper = require('*/cartridge/scripts/helpers/almaOrderHelper');
var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper');
var Logger = require('dw/system/Logger');

/**
* Try to make a capture while we have an order
* @param {Object} orders orders
* @param {Object} CAPTURE string constante for capture
*/
function makeCaptureWhileHaveAnOrder(orders, CAPTURE) {
var Logger = require('dw/system/Logger');
while (orders.hasNext()) {
var order = orders.next();
var params = { external_id: order.custom.almaPaymentId };
var amount = order.getTotalGrossPrice().value;
var captureType = CAPTURE.total;
var partialCaptureAmount = almaOrderHelper.getPartialCaptureAmount(order);
try {
if (partialCaptureAmount) {
captureType = CAPTURE.partial;
amount = partialCaptureAmount;
}
params.amount = amount * 100;
var capture = almaPaymentHelper.capturePayment(params);
almaOrderHelper.setAlmaDeferredCaptureFields(order, captureType.code, amount);
Logger.info(captureType.description + ' payment: order id: {0} - payment id: {1} - capture id : {2}', [order.orderNo, order.custom.almaPaymentId, capture.id]);
} catch (e) {
almaOrderHelper.setAlmaDeferredCaptureFields(order, CAPTURE.failed.code);
Logger.warn(CAPTURE.failed.description + ' payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]);
}
}
}

exports.execute = function () {
var Capture = almaPaymentHelper.Capture;
var OrderMgr = require('dw/order/OrderMgr');
var CAPTURE = almaPaymentHelper.CAPTURE;
var orders = OrderMgr.searchOrders(
"custom.ALMA_Deferred_Capture_Status='ToCapture' and status != {0} and status != {1}",
'custom.ALMA_Deferred_Capture_Status={0} and status != {1} and status != {2}',
null,
CAPTURE.toCapture.code,
Order.ORDER_STATUS_FAILED,
Order.ORDER_STATUS_CANCELLED
);

if (orders.count > 0) {
while (orders.hasNext()) {
var order = orders.next();
var params = { external_id: order.custom.almaPaymentId };
var amount = order.getTotalGrossPrice().value;
var captureType = Capture.total;
var partialCaptureAmount = almaOrderHelper.getPartialCaptureAmount(order);
try {
if (partialCaptureAmount) {
captureType = Capture.partial;
amount = partialCaptureAmount;
}
params.amount = amount * 100;
var capture = almaPaymentHelper.capturePayment(params);
almaOrderHelper.setAlmaDeferredCaptureFields(order, captureType.code, amount);
Logger.info(captureType.description + ' payment: order id: {0} - payment id: {1} - capture id : {2}', [order.orderNo, order.custom.almaPaymentId, capture.id]);
} catch (e) {
almaOrderHelper.setAlmaDeferredCaptureFields(order, Capture.failed.code);
Logger.warn(Capture.failed.description + ' payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]);
}
}
makeCaptureWhileHaveAnOrder(orders, CAPTURE);
}

return new Status(Status.OK);
Expand Down
61 changes: 35 additions & 26 deletions cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,43 @@ function refundPaymentForOrder(order) {
}
}

/**
* Cancel a payment when a refund is made on an order with the status ToCapture
* @param {Object} orderItem order
* @param {Object} Logger logger
*/
function cancelDeferredCapturePaymentForARefund(orderItem, Logger) {
var Transaction = require('dw/system/Transaction');
var AlmaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper');
var amount = 0;
var deferredStatus = 'ToCapture';
if (orderItem.custom.almaRefundType.toString() === 'Total') {
var params = { external_id: orderItem.custom.almaPaymentId };
amount = orderItem.getTotalGrossPrice().value;
AlmaPaymentHelper.cancelAlmaPayment(params);
deferredStatus = 'Canceled';
}

if (orderItem.custom.almaRefundType.toString() === 'Partial') {
Logger.info('Partial refund is not yet implemented with deferred payment - order id {0}', [orderItem.orderNo]);
}

/* jshint loopfunc: true */
// eslint-disable-next-line no-loop-func
Transaction.wrap(function () {
orderItem.custom.ALMA_Deferred_Capture_Status = deferredStatus;
// eslint-disable-next-line no-param-reassign
orderItem.custom.almaRefundedAmount = amount;
// eslint-disable-next-line no-param-reassign
orderItem.custom.almaWantedRefundAmount = 0;
// eslint-disable-next-line no-param-reassign
orderItem.custom.almaRefundType = null;
});
}

exports.execute = function () {
var Logger = require('dw/system/Logger');
var Status = require('dw/system/Status');
var Transaction = require('dw/system/Transaction');
var AlmaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper');
var orders = getOrdersRefunded();

Logger.info('[INFO][ALMA refund] job launched for: ' + orders.count + ' orders.');
Expand All @@ -52,30 +84,7 @@ exports.execute = function () {
if (isOrderToBeRefund(orderItem)) {
try {
if (orderItem.custom.ALMA_Deferred_Capture_Status === 'ToCapture') {
var amount = 0;
var deferredStatus = 'ToCapture';
if (orderItem.custom.almaRefundType.toString() === 'Total') {
var params = { external_id: orderItem.custom.almaPaymentId };
amount = orderItem.getTotalGrossPrice().value;
AlmaPaymentHelper.cancelAlmaPayment(params);
deferredStatus = 'Canceled';
}

if (orderItem.custom.almaRefundType.toString() === 'Partial') {
Logger.info('Partial refund is not yet implemented with deferred payment - order id {0}', [orderItem.orderNo]);
}

/* jshint loopfunc: true */
// eslint-disable-next-line no-loop-func
Transaction.wrap(function () {
orderItem.custom.ALMA_Deferred_Capture_Status = deferredStatus;
// eslint-disable-next-line no-param-reassign
orderItem.custom.almaRefundedAmount = amount;
// eslint-disable-next-line no-param-reassign
orderItem.custom.almaWantedRefundAmount = 0;
// eslint-disable-next-line no-param-reassign
orderItem.custom.almaRefundType = null;
});
cancelDeferredCapturePaymentForARefund(orderItem, Logger);
} else {
refundPaymentForOrder(orderItem);
}
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/steps/CapturePaymentOrders.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ var proxyquire = require('proxyquire')

var sinon = require('sinon');
var almaConfigHelpers = require('../helpers/almaConfigHelpers').almaConfigHelpers;
var Capture = require('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper').Capture;
var CAPTURE = require('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper').CAPTURE;
var OrderMgr = {
searchOrders: sinon.stub()
.returns({})
};

var almaPaymentHelper = {
capturePayment: sinon.stub(),
Capture: Capture
CAPTURE: CAPTURE
};

var almaOrderHelper = {
Expand Down
20 changes: 10 additions & 10 deletions test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ var plansDeferred = {
};
var currencyCode = 'EUR';

describe('almaCheckoutHelpers', function () {
describe('formatPlanForCheckout', function () {
it('check in page', function () {
describe('AlmaCheckoutHelpers', function () {
describe('FormatPlanForCheckout', function () {
it('Check value of the fields in_page depending of payment method', function () {
setCustomPreferenceValue(true);
var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plan, currencyCode);
assert.equal(checkoutData.in_page, true);
Expand All @@ -91,7 +91,7 @@ describe('almaCheckoutHelpers', function () {
assert.equal(checkoutData.in_page, false);
});

it('check selector', function () {
it('Check field selector depending of the plan', function () {
var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plan, currencyCode);
assert.equal(checkoutData.selector, 'ALMA_general_3_0');

Expand All @@ -103,7 +103,7 @@ describe('almaCheckoutHelpers', function () {
assert.equal(checkoutData.selector, 'ALMA_general_1_15');
});

it('check properties for pnx', function () {
it('Properties for pnx is well formed', function () {
setIsAvailableForManualCapture(true);
var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plan, currencyCode);
assert.deepEqual(checkoutData.properties, {
Expand All @@ -121,7 +121,7 @@ describe('almaCheckoutHelpers', function () {
});
});

it('check properties for credit', function () {
it('Properties for credit is well formed', function () {
setIsAvailableForManualCapture(false);
var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(planCredit, currencyCode);
assert.deepEqual(checkoutData.properties, {
Expand All @@ -139,7 +139,7 @@ describe('almaCheckoutHelpers', function () {
});
});

it('check properties for deffered', function () {
it('Properties for deferred is well formed', function () {
setIsAvailableForManualCapture(false);
var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plansDeferred, currencyCode);
assert.deepEqual(checkoutData.properties, {
Expand All @@ -157,18 +157,18 @@ describe('almaCheckoutHelpers', function () {
});
});

it('check payment method PNX', function () {
it('Check payment method for PNX', function () {
var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plan, currencyCode);
assert.equal(checkoutData.payment_method, 'ALMA_PNX'
);
});

it('check payment method CREDIT', function () {
it('check payment method for CREDIT', function () {
var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(planCredit, currencyCode);
assert.equal(checkoutData.payment_method, 'ALMA_CREDIT');
});

it('check payment method DEFERRED', function () {
it('Check payment method for DEFERRED', function () {
var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plansDeferred, currencyCode);
assert.equal(checkoutData.payment_method, 'ALMA_DEFERRED');
});
Expand Down
4 changes: 2 additions & 2 deletions test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var setCustomPreferenceValue = require('../../../../mocks/helpers/almaConfigHelp

describe('Get back-office variables', function () {
describe('Deferred Capture', function () {
it('should call site preferences with deferred capture key', function () {
it('Should call site preferences with deferred capture key', function () {
setCustomPreferenceValue(true);
almaConfigHelpers.isDeferredCaptureEnable();

Expand All @@ -18,7 +18,7 @@ describe('Get back-office variables', function () {
.getCustomPreferenceValue
.calledWith('ALMA_Deferred_Capture_Activation'));
});
it('should call site preferences and return default value', function () {
it('Should call site preferences and return default value', function () {
setCustomPreferenceValue(false);
assert.equal(false, almaConfigHelpers.isDeferredCaptureEnable());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var baseReturn =

var baseBasket = basketMock.getCurrentBasket();
describe('Construct eligibility payload', function () {
it('return a empty array for a null current bask', function () {
it('Return a empty array for a null current bask', function () {
var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', null, deferredCaptureDisabled);
// eslint-disable-next-line no-unused-expressions
expect(params).to.be.an('array').that.is.empty;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('Alma order helper', function () {
});
});

it('For payment transaction.wrap is called twice first for payment ID Second for Deferred Status', function () {
it('Payment transaction.wrap is called twice; first for payment ID, then for Deferred Status', function () {
createNewTransaction();
almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, true);
sinon.assert.calledTwice(transaction.wrap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var paymentAuthorizationNotExpired = {

describe('almaPaymentHelper', function () {
describe('Build payment data', function () {
it('payment data for pnx', function () {
it('payment data for pnx is well formed', function () {
setIsAvailableForInpage(true);
setCustomPreferenceValue(true);

Expand Down
6 changes: 3 additions & 3 deletions test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ var formattedPlansForCheckoutExpected = require('../../../../mocks/data/plans').
var plansForCheckout = require('../../../../mocks/data/plans').plansForCheckout;


describe('almaPlanHelper', function () {
describe('AlmaPlanHelper', function () {
describe('Format plan Helper', function () {
it('should return an array for a given plans', function () {
it('Check array returned by getFormattedPlans for a given plans is well formed', function () {
setIsDeferredCaptureEnable(false);
var formattedPlans = almaPlanHelperMock.getFormattedPlans(plansForCheckout);
expect(formattedPlans).to.deep.equal(formattedPlansForCheckoutExpected);
});

it('should return have a captureMethod property for deferred capture', function () {
it('Return must have a captureMethod property for deferred capture', function () {
setIsDeferredCaptureEnable(true);
var formattedPlans = almaPlanHelperMock.getFormattedPlans(plansForCheckout);
var captureMethod = '';
Expand Down
Loading

0 comments on commit 1e1a422

Please sign in to comment.