Skip to content

Commit

Permalink
Merge branch 'develop' into feature/MPP-648_partial_capture
Browse files Browse the repository at this point in the history
  • Loading branch information
joyet-simon authored Oct 10, 2023
2 parents eb2cd20 + 1a306c3 commit 9d3b3b5
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
20 changes: 20 additions & 0 deletions cartridges/int_alma/cartridge/controllers/Alma.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ function affectOrder(paymentObj, order) {
throw new Error(reason);
}

if (almaPaymentHelper.isPaymentExpired(paymentObj)) {
Transaction.wrap(function () {
order.trackOrderChange('Payment is expired');
OrderMgr.failOrder(order, true);
});

logger.warn('Payment: {0} is expired', [paymentObj.id]);
throw new Error('payment_expired');
}

if (almaPaymentHelper.isPaymentAuthorizationExpired(paymentObj)) {
Transaction.wrap(function () {
order.trackOrderChange('Payment’s authorization is expired');
OrderMgr.cancelOrder(order);
});

logger.warn('Authorization for the payment: {0} is expired', [paymentObj.id]);
throw new Error('authorization_expired');
}

var isOnShipmentPaymentEnabled = require('*/cartridge/scripts/helpers/almaOnShipmentHelper').isOnShipmentPaymentEnabled;
var paymentStatus = isOnShipmentPaymentEnabled(paymentObj.installments_count) ? Order.PAYMENT_STATUS_NOTPAID : Order.PAYMENT_STATUS_PAID;
acceptOrder(order, paymentStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,27 @@ function setOrderMerchantReference(pid, order) {
setOrderMerchantReferenceAPI.call(param);
}

/**
* Check if a payment is expired
* @param {Object} paymentObj payment object
* @return {boolean} payment is expired
*/
function isPaymentExpired(paymentObj) {
return paymentObj.expired_at !== null;
}

/**
* Check if an authorization for a payment is expired
* @param {Object} paymentObj payment object
* @return {boolean} payment’s authorization is expired
*/
function isPaymentAuthorizationExpired(paymentObj) {
var timeElapsed = Date.now();
var today = new Date(timeElapsed);
var authorizationExpiresAtDate = new Date(paymentObj.authorization_expires_at);
return authorizationExpiresAtDate.getTime() < today.getTime();
}


module.exports = {
orderStatusEquals: orderStatusEquals,
Expand All @@ -477,5 +498,7 @@ module.exports = {
capturePayment: capturePayment,
isAvailableForManualCapture: isAvailableForManualCapture,
cancelAlmaPayment: cancelAlmaPayment,
Capture: Capture
Capture: Capture,
isPaymentExpired: isPaymentExpired,
isPaymentAuthorizationExpired: isPaymentAuthorizationExpired
};
42 changes: 42 additions & 0 deletions test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@ var setHttpReturnStatusCode = require('../../../../mocks/helpers/almaPaymentHelp
var setIsAvailableForInpage = require('../../../../mocks/helpers/almaPaymentHelpers').setIsAvailableForInpage;
var setCustomPreferenceValue = require('../../../../mocks/helpers/almaConfigHelpers').setCustomPreferenceValue;

var paymentObjExpired = {
expired_at: 'Thu, 17 Aug 2023 08:08:06 GMT'
};

var paymentObjNotExpired = {
expired_at: null
};

var timeElapsedExpired = Date.now();
var todayExpired = new Date(timeElapsedExpired);
todayExpired = todayExpired.setHours(todayExpired.getHours() - 1);
var paymentAuthorizationExpired = {
authorization_expires_at: todayExpired
};

var timeElapsedNotEpired = Date.now();
var todayNotExpired = new Date(timeElapsedNotEpired);
todayNotExpired = todayNotExpired.setHours(todayNotExpired.getHours() + 1);
var paymentAuthorizationNotExpired = {
authorization_expires_at: todayNotExpired
};

describe('almaPaymentHelper', function () {
describe('Build payment data', function () {
it('payment data for pnx', function () {
Expand Down Expand Up @@ -90,4 +112,24 @@ describe('almaPaymentHelper', function () {
});
});
});

describe('Payment is expired', function () {
it('Should return true if payment is expired', function () {
assert.isTrue(almaPaymentHelper.isPaymentExpired(paymentObjExpired));
});

it('Should return false if payment is not expired', function () {
assert.isFalse(almaPaymentHelper.isPaymentExpired(paymentObjNotExpired));
});
});

describe('Payment’s authorization is expired', function () {
it('Should return true if payment’s authorization is expired', function () {
assert.isTrue(almaPaymentHelper.isPaymentAuthorizationExpired(paymentAuthorizationExpired));
});

it('Should return false if payment’s authorization is not expired', function () {
assert.isFalse(almaPaymentHelper.isPaymentAuthorizationExpired(paymentAuthorizationNotExpired));
});
});
});

0 comments on commit 9d3b3b5

Please sign in to comment.