From 4dde3687cbf339ac62c98cc4b56892c5f18b72ac Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 17 Aug 2023 10:53:21 +0200 Subject: [PATCH 1/3] feat:mpp-532 support payment and authorization expiration --- .../int_alma/cartridge/controllers/Alma.js | 20 ++++++++++ .../scripts/helpers/almaPaymentHelper.js | 25 +++++++++++- .../scripts/helpers/almaPaymentHelperTest.js | 39 +++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/cartridges/int_alma/cartridge/controllers/Alma.js b/cartridges/int_alma/cartridge/controllers/Alma.js index c7332622..3742e245 100644 --- a/cartridges/int_alma/cartridge/controllers/Alma.js +++ b/cartridges/int_alma/cartridge/controllers/Alma.js @@ -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('Authorization is expired'); + OrderMgr.failOrder(order, true); + }); + + 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); diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 79f79b1e..e0987cc5 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -441,6 +441,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); + + return paymentObj.authorization_expires_at < today.toUTCString(); +} + module.exports = { orderStatusEquals: orderStatusEquals, @@ -459,5 +480,7 @@ module.exports = { setOrderMerchantReference: setOrderMerchantReference, capturePayment: capturePayment, isAvailableForManualCapture: isAvailableForManualCapture, - cancelAlmaPayment: cancelAlmaPayment + cancelAlmaPayment: cancelAlmaPayment, + isPaymentExpired: isPaymentExpired, + isPaymentAuthorizationExpired: isPaymentAuthorizationExpired }; diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 909e9f9b..afb1aec7 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -9,6 +9,25 @@ var resolvedPaymentData = require('../../../../mocks/helpers/almaPaymentHelpers' var service = require('../../../../mocks/helpers/almaPaymentHelpers').service; var setHttpReturnStatusCode = require('../../../../mocks/helpers/almaPaymentHelpers').setHttpReturnStatusCode; +var paymentObjExpired = { + expired_at: 'Thu, 17 Aug 2023 08:08:06 GMT' +}; + +var paymentObjNotExpired = { + expired_at: null +}; + +var paymentAuthorizationExpired = { + authorization_expires_at: 'Thu, 17 Aug 2023 08:08:06 GMT' +}; + +var timeElapsed = Date.now(); +var today = new Date(timeElapsed); +today = today.setHours(today.getHours() + 1); +var paymentAuthorizationNotExpired = { + authorization_expires_at: today +}; + describe('almaPaymentHelper', function () { describe('Build payment data', function () { it('payment data for pnx', function () { @@ -82,4 +101,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)); + }); + }); }); From f9295265769bd99fb7ba3c45003fa692b87b779e Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Mon, 25 Sep 2023 17:20:09 +0200 Subject: [PATCH 2/3] =?UTF-8?q?cancel=20order=20on=20payment=E2=80=99s=20a?= =?UTF-8?q?uth=20expired=20on=20IPN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cartridges/int_alma/cartridge/controllers/Alma.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cartridges/int_alma/cartridge/controllers/Alma.js b/cartridges/int_alma/cartridge/controllers/Alma.js index 3742e245..17a0e40f 100644 --- a/cartridges/int_alma/cartridge/controllers/Alma.js +++ b/cartridges/int_alma/cartridge/controllers/Alma.js @@ -82,8 +82,8 @@ function affectOrder(paymentObj, order) { if (almaPaymentHelper.isPaymentAuthorizationExpired(paymentObj)) { Transaction.wrap(function () { - order.trackOrderChange('Authorization is expired'); - OrderMgr.failOrder(order, true); + order.trackOrderChange('Payment’s authorization is expired'); + OrderMgr.cancelOrder(order); }); logger.warn('Authorization for the payment: {0} is expired', [paymentObj.id]); From 029fcbdca2eb9c3b95efdfeb6ca02c585e8e89bd Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 27 Sep 2023 11:00:55 +0200 Subject: [PATCH 3/3] fix authorization expired date test --- .../cartridge/scripts/helpers/almaPaymentHelper.js | 4 ++-- .../scripts/helpers/almaPaymentHelperTest.js | 13 ++++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index e0987cc5..f553ff7f 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -458,8 +458,8 @@ function isPaymentExpired(paymentObj) { function isPaymentAuthorizationExpired(paymentObj) { var timeElapsed = Date.now(); var today = new Date(timeElapsed); - - return paymentObj.authorization_expires_at < today.toUTCString(); + var authorizationExpiresAtDate = new Date(paymentObj.authorization_expires_at); + return authorizationExpiresAtDate.getTime() < today.getTime(); } diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index afb1aec7..64030cd4 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -17,15 +17,18 @@ 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: 'Thu, 17 Aug 2023 08:08:06 GMT' + authorization_expires_at: todayExpired }; -var timeElapsed = Date.now(); -var today = new Date(timeElapsed); -today = today.setHours(today.getHours() + 1); +var timeElapsedNotEpired = Date.now(); +var todayNotExpired = new Date(timeElapsedNotEpired); +todayNotExpired = todayNotExpired.setHours(todayNotExpired.getHours() + 1); var paymentAuthorizationNotExpired = { - authorization_expires_at: today + authorization_expires_at: todayNotExpired }; describe('almaPaymentHelper', function () {