From dde6e51e515f8b899d822f8a746db8cc5cfa08b7 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 27 Jul 2023 10:59:12 +0200 Subject: [PATCH 01/66] feat:mpp-480 deferred capture button --- .../ref/system-objecttype-extensions.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index 8c840f8..112167c 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -48,6 +48,13 @@ boolean false false + false + + + Activate deferred capture + boolean + false + false true @@ -96,6 +103,7 @@ + From 0e0d68bb530a5cec243b42d59d6e65fee3b2a735 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 27 Jul 2023 11:33:12 +0200 Subject: [PATCH 02/66] feat:mpp-480 add helper and test --- .../cartridge/scripts/helpers/almaConfigHelper.js | 10 ++++++++++ .../scripts/helpers/almaConfigHelperTest.js | 15 +++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js create mode 100644 test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js new file mode 100644 index 0000000..840a592 --- /dev/null +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js @@ -0,0 +1,10 @@ +/** + * return the deferred capture activation field value + */ +function isDeferredCaptureEnable() { + +} + +module.exports = { + isDeferredCaptureEnable: isDeferredCaptureEnable +}; diff --git a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js new file mode 100644 index 0000000..2cf8359 --- /dev/null +++ b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js @@ -0,0 +1,15 @@ +// // almaConfigHelper.js unit tests +// +// +// var assert = require('chai').assert; +// +// var almaConfigHelper = require('../../../../../cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper'); +// var +// +// // describe('Get back-office variables', function () { +// // describe('Deferred Capture', function () { +// // it('should call site preferences with deferred capture key', function () { +// // +// // }); +// // }); +// // }) From c0a76b0a345f8a59d1e790affe1dbe8227850c50 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Thu, 27 Jul 2023 17:37:15 +0200 Subject: [PATCH 03/66] test : Update sinon package and test button getter --- .../scripts/helpers/almaConfigHelper.js | 8 +++-- package.json | 3 +- test/mocks/helpers/AlmaConfigHelpers.js | 25 +++++++++++++ .../scripts/helpers/almaConfigHelperTest.js | 36 +++++++++++-------- 4 files changed, 54 insertions(+), 18 deletions(-) create mode 100644 test/mocks/helpers/AlmaConfigHelpers.js diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js index 840a592..f54ed41 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js @@ -1,8 +1,12 @@ +var Site = require('dw/system/Site'); + /** * return the deferred capture activation field value - */ + * @return {bool} the field value + * +*/ function isDeferredCaptureEnable() { - + return Site.getCurrent().getCustomPreferenceValue('ALMA_Deferred_Capture_Activation'); } module.exports = { diff --git a/package.json b/package.json index 7394bd1..1eddca9 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "license": "ISC", "homepage": "https://github.com/alma/sfcc-plugin", "devDependencies": { + "@types/chai": "^4.3.5", "chai": "^3.5.0", "chai-subset": "^1.6.0", "del": "^6.1.1", @@ -53,7 +54,7 @@ "request-promise": "^4.2.4", "sass-loader": "^7.3.1", "sgmf-scripts": "^2.4.2", - "sinon": "^1.17.7", + "sinon": "^15.2.0", "style-loader": "^0.21.0", "stylelint": "^8.4.0", "stylelint-config-standard": "^17.0.0", diff --git a/test/mocks/helpers/AlmaConfigHelpers.js b/test/mocks/helpers/AlmaConfigHelpers.js new file mode 100644 index 0000000..494a06b --- /dev/null +++ b/test/mocks/helpers/AlmaConfigHelpers.js @@ -0,0 +1,25 @@ +'use strict'; + +var proxyquire = require('proxyquire').noCallThru().noPreserveCache(); +var sinon = require('sinon'); + +var getCustomPreferenceValue = sinon.stub().returns(false); + + +var site = { + getCurrent: function () { + return { + getCustomPreferenceValue: getCustomPreferenceValue + }; + } +}; +function proxyModel() { + return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper', { + 'dw/system/Site': site + }); +} + +module.exports = { + almaConfigHelpers: proxyModel(), + site: site +}; diff --git a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js index 2cf8359..a52627c 100644 --- a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js @@ -1,15 +1,21 @@ -// // almaConfigHelper.js unit tests -// -// -// var assert = require('chai').assert; -// -// var almaConfigHelper = require('../../../../../cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper'); -// var -// -// // describe('Get back-office variables', function () { -// // describe('Deferred Capture', function () { -// // it('should call site preferences with deferred capture key', function () { -// // -// // }); -// // }); -// // }) +'use strict'; + +// almaConfigHelper.js unit tests + +var assert = require('chai').assert; +var site = require('../../../../mocks/helpers/AlmaConfigHelpers').site; +var almaConfigHelpers = require('../../../../mocks/helpers/AlmaConfigHelpers').almaConfigHelpers; + +describe('Get back-office variables', function () { + describe('Deferred Capture', function () { + it('should call site preferences with deferred capture key', function () { + almaConfigHelpers.isDeferredCaptureEnable(); + + assert.isTrue(site.getCurrent().getCustomPreferenceValue.calledOnce); + assert.isTrue(site.getCurrent().getCustomPreferenceValue.calledWith('ALMA_Deferred_Capture_Activation')); + }); + it('should call site preferences and return default value', function () { + assert.equal(false, almaConfigHelpers.isDeferredCaptureEnable()); + }); + }); +}); From fe2c9c8410ad694eda0a1a750df190aa9595d563 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Fri, 28 Jul 2023 16:15:43 +0200 Subject: [PATCH 04/66] feat:mpp-479 capture method in payment payload intial tests --- test/mocks/dw/order/BasketMgr.js | 90 ++++++++++++++----- test/mocks/dw/web/URLUtils.js | 12 +++ test/mocks/helpers/almaAddressHelper.js | 19 ++++ ...aConfigHelpers.js => almaConfigHelpers.js} | 0 test/mocks/helpers/almaHelpers.js | 17 ++++ test/mocks/helpers/almaOnShipmentHelpers.js | 7 ++ test/mocks/helpers/almaPaymentHelpers.js | 90 +++++++++++++++++++ .../scripts/helpers/almaConfigHelperTest.js | 4 +- .../scripts/helpers/almaPaymentHelperTest.js | 24 +++++ 9 files changed, 238 insertions(+), 25 deletions(-) create mode 100644 test/mocks/dw/web/URLUtils.js create mode 100644 test/mocks/helpers/almaAddressHelper.js rename test/mocks/helpers/{AlmaConfigHelpers.js => almaConfigHelpers.js} (100%) create mode 100644 test/mocks/helpers/almaHelpers.js create mode 100644 test/mocks/helpers/almaOnShipmentHelpers.js create mode 100644 test/mocks/helpers/almaPaymentHelpers.js create mode 100644 test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js diff --git a/test/mocks/dw/order/BasketMgr.js b/test/mocks/dw/order/BasketMgr.js index e343a61..4e114b8 100644 --- a/test/mocks/dw/order/BasketMgr.js +++ b/test/mocks/dw/order/BasketMgr.js @@ -1,31 +1,75 @@ +function getAddress() { + return { + firstName: 'Amanda', + lastName: 'Jones', + address1: '65 May Lane', + address2: '', + city: 'Allston', + postalCode: '02135', + countryCode: { value: 'us' }, + phone: '617-555-1234', + stateCode: 'MA', + + setFirstName: function (firstNameInput) { + this.firstName = firstNameInput; + }, + setLastName: function (lastNameInput) { + this.lastName = lastNameInput; + }, + setAddress1: function (address1Input) { + this.address1 = address1Input; + }, + setAddress2: function (address2Input) { + this.address2 = address2Input; + }, + setCity: function (cityInput) { + this.city = cityInput; + }, + setPostalCode: function (postalCodeInput) { + this.postalCode = postalCodeInput; + }, + setStateCode: function (stateCodeInput) { + this.stateCode = stateCodeInput; + }, + setCountryCode: function (countryCodeInput) { + this.countryCode.value = countryCodeInput; + }, + setPhone: function (phoneInput) { + this.phone = phoneInput; + } + }; +} function getCurrentBasket() { return { - defaultShipment: { - shippingAddress: { - firstName: 'Amanda', - lastName: 'Jones', - address1: '65 May Lane', - address2: '', - city: 'Allston', - postalCode: '02135', - countryCode: { value: 'us' }, - phone: '617-555-1234', - stateCode: 'MA', - - setFirstName: function (firstNameInput) { this.firstName = firstNameInput; }, - setLastName: function (lastNameInput) { this.lastName = lastNameInput; }, - setAddress1: function (address1Input) { this.address1 = address1Input; }, - setAddress2: function (address2Input) { this.address2 = address2Input; }, - setCity: function (cityInput) { this.city = cityInput; }, - setPostalCode: function (postalCodeInput) { this.postalCode = postalCodeInput; }, - setStateCode: function (stateCodeInput) { this.stateCode = stateCodeInput; }, - setCountryCode: function (countryCodeInput) { this.countryCode.value = countryCodeInput; }, - setPhone: function (phoneInput) { this.phone = phoneInput; } - } + getDefaultShipment: function () { + return getAddress(); + }, + getBillingAddress: function () { + return getAddress(); + }, + getCustomer: function () { + return { + profile: 'profile_value' + }; + }, + getCustomerEmail: function () { + return 'email@email.com'; + }, + getAllProductLineItems: function () { + return { + toArray: function () { + return []; + } + }; }, totalGrossPrice: { - value: 250.00 + value: 250.00, + multiply: function () { + return { + value: 25000 + }; + } } }; } diff --git a/test/mocks/dw/web/URLUtils.js b/test/mocks/dw/web/URLUtils.js new file mode 100644 index 0000000..803e637 --- /dev/null +++ b/test/mocks/dw/web/URLUtils.js @@ -0,0 +1,12 @@ +function https() { + return ''; +} + +function http() { + return ''; +} + +module.exports = { + https: https, + http: http +}; diff --git a/test/mocks/helpers/almaAddressHelper.js b/test/mocks/helpers/almaAddressHelper.js new file mode 100644 index 0000000..b50f237 --- /dev/null +++ b/test/mocks/helpers/almaAddressHelper.js @@ -0,0 +1,19 @@ +function formatAddress() { + return { + title: 'address.jobTitle', + first_name: 'address.lastName', + last_name: 'address.firstName', + company: 'address.companyName', + line1: 'address.address1', + line2: 'address.address2', + postal_code: 'address.postalCode', + city: 'address.city', + country: 'address.countryCode.value', + state_province: 'address.stateCode', + phone: 'address.phone' + }; +} + +module.exports = { + formatAddress: formatAddress +}; diff --git a/test/mocks/helpers/AlmaConfigHelpers.js b/test/mocks/helpers/almaConfigHelpers.js similarity index 100% rename from test/mocks/helpers/AlmaConfigHelpers.js rename to test/mocks/helpers/almaConfigHelpers.js diff --git a/test/mocks/helpers/almaHelpers.js b/test/mocks/helpers/almaHelpers.js new file mode 100644 index 0000000..3b64de2 --- /dev/null +++ b/test/mocks/helpers/almaHelpers.js @@ -0,0 +1,17 @@ +function getSfccVersion() { + return '4.0.0'; +} + +function formatCustomerData() { + return { + first_name: "'shippingAddress.first_name'", + last_name: 'shippingAddress.last_name', + email: 'customerEmail', + phone: 'shippingAddress.phone' + }; +} + +module.exports = { + getSfccVersion: getSfccVersion, + formatCustomerData: formatCustomerData +}; diff --git a/test/mocks/helpers/almaOnShipmentHelpers.js b/test/mocks/helpers/almaOnShipmentHelpers.js new file mode 100644 index 0000000..a752ef4 --- /dev/null +++ b/test/mocks/helpers/almaOnShipmentHelpers.js @@ -0,0 +1,7 @@ +function isOnShipmentPaymentEnabled() { + return false; +} + +module.exports = { + isOnShipmentPaymentEnabled: isOnShipmentPaymentEnabled +}; diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js new file mode 100644 index 0000000..9e5523e --- /dev/null +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -0,0 +1,90 @@ +'use strict'; + +var proxyquire = require('proxyquire') + .noCallThru() + .noPreserveCache(); +var BasketMgr = require('../dw/order/BasketMgr'); +var URLUtils = require('../dw/web/URLUtils'); +var almaHelper = require('../helpers/almaHelpers'); +var almaCheckoutHelper = require('../helpers/almaCheckoutHelpers'); +var almaAddressHelper = require('../helpers/almaAddressHelper'); +var almaOnShipmentHelper = require('../helpers/almaOnShipmentHelpers'); + +function proxyModel() { + return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper', { + 'dw/order/BasketMgr': BasketMgr, + 'dw/web/URLUtils': URLUtils, + '*/cartridge/scripts/helpers/almaHelpers': almaHelper, + '*/cartridge/scripts/helpers/almaCheckoutHelper': almaCheckoutHelper, + '*/cartridge/scripts/helpers/almaAddressHelper': almaAddressHelper, + '*/cartridge/scripts/helpers/almaOnShipmentHelper': almaOnShipmentHelper + }); +} + +function resolvedPaymentData(installmentsCount, defferedDays, locale, origin, hasCart = false) { + var paymentData = { + payment: { + purchase_amount: 25000, + installments_count: installmentsCount, + deferred_days: defferedDays, + deferred_months: 0, + return_url: '', + ipn_callback_url: '', + customer_cancel_url: '', + locale: locale, + origin: origin, + shipping_address: { + title: 'address.jobTitle', + first_name: 'address.lastName', + last_name: 'address.firstName', + company: 'address.companyName', + line1: 'address.address1', + line2: 'address.address2', + postal_code: 'address.postalCode', + city: 'address.city', + country: 'address.countryCode.value', + state_province: 'address.stateCode', + phone: 'address.phone' + }, + billing_address: { + title: 'address.jobTitle', + first_name: 'address.lastName', + last_name: 'address.firstName', + company: 'address.companyName', + line1: 'address.address1', + line2: 'address.address2', + postal_code: 'address.postalCode', + city: 'address.city', + country: 'address.countryCode.value', + state_province: 'address.stateCode', + phone: 'address.phone' + }, + deferred: '', + deferred_description: '', + custom_data: { + cms_name: 'SFCC', + cms_version: '4.0.0', + alma_plugin_version: '4.3.1' + } + }, + customer: { + first_name: "'shippingAddress.first_name'", + last_name: 'shippingAddress.last_name', + email: 'customerEmail', + phone: 'shippingAddress.phone' + } + }; + + if (hasCart) { + paymentData.payment.cart = { + 'items': [] + }; + } + + return paymentData; +} + +module.exports = { + proxyModel: proxyModel(), + resolvedPaymentData: resolvedPaymentData +}; diff --git a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js index a52627c..29e8547 100644 --- a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js @@ -3,8 +3,8 @@ // almaConfigHelper.js unit tests var assert = require('chai').assert; -var site = require('../../../../mocks/helpers/AlmaConfigHelpers').site; -var almaConfigHelpers = require('../../../../mocks/helpers/AlmaConfigHelpers').almaConfigHelpers; +var site = require('../../../../mocks/helpers/almaConfigHelpers').site; +var almaConfigHelpers = require('../../../../mocks/helpers/almaConfigHelpers').almaConfigHelpers; describe('Get back-office variables', function () { describe('Deferred Capture', function () { diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js new file mode 100644 index 0000000..5f0c2ae --- /dev/null +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -0,0 +1,24 @@ +'use strict'; + +// almaPayment.js unit tests + +var assert = require('chai').assert; +var almaPaymentHelper = require('../../../../mocks/helpers/almaPaymentHelpers').proxyModel; +var resolvedPaymentData = require('../../../../mocks/helpers/almaPaymentHelpers').resolvedPaymentData; + +describe('almaPaymentHelper', function () { + describe('Build payment data', function () { + it('payment data for pnx', function () { + var payment = almaPaymentHelper.buildPaymentData(3, 0, 'fr_FR'); + assert.deepEqual(payment, resolvedPaymentData(3, 0, 'fr_FR', 'online_in_page')); + }); + it('payment data for deferred', function () { + var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR'); + assert.deepEqual(payment, resolvedPaymentData(1, 15, 'fr_FR', 'online')); + }); + it('payment data for credit has car property', function () { + var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR'); + assert.deepEqual(payment, resolvedPaymentData(12, 0, 'fr_FR', 'online', true)); + }); + }); +}); From 83889bf06e53d88b5257f00c47bd65f90da37561 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Fri, 28 Jul 2023 17:36:04 +0200 Subject: [PATCH 05/66] feat:mpp-479 refacto and implement capture in payment data --- .../int_alma/cartridge/controllers/Alma.js | 19 +++++---- .../scripts/helpers/almaPaymentHelper.js | 39 ++++++++++++++++++- test/mocks/helpers/almaPaymentHelpers.js | 2 +- .../scripts/helpers/almaPaymentHelperTest.js | 16 ++++++-- 4 files changed, 63 insertions(+), 13 deletions(-) diff --git a/cartridges/int_alma/cartridge/controllers/Alma.js b/cartridges/int_alma/cartridge/controllers/Alma.js index 8cb6d4b..da6083c 100644 --- a/cartridges/int_alma/cartridge/controllers/Alma.js +++ b/cartridges/int_alma/cartridge/controllers/Alma.js @@ -269,12 +269,14 @@ server.get('OrderAmount', server.middleware.https, function (req, res, next) { server.post('CreatePaymentUrl', server.middleware.https, function (req, res, next) { var getLocale = require('*/cartridge/scripts/helpers/almaHelpers').getLocale; var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); + var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfiqHelper'); - var paymentData = almaPaymentHelper.buildPaymentData( - req.querystring.installments, - req.querystring.deferred_days, - getLocale(req) - ); + var paymentData = almaPaymentHelper.getPaymentData( + req.querystring.installments, + req.querystring.deferred_days, + getLocale(req), + almaConfigHelper.isDeferredCaptureEnable() + ); try { var result = almaPaymentHelper.createPayment(paymentData); @@ -325,10 +327,13 @@ server.get( function (req, res, next) { var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var getLocale = require('*/cartridge/scripts/helpers/almaHelpers').getLocale; - var paymentData = almaPaymentHelper.buildPaymentData( + var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfiqHelper'); + + var paymentData = almaPaymentHelper.getPaymentData( req.querystring.installments, req.querystring.deferred_days, - getLocale(req) + getLocale(req), + almaConfigHelper.isDeferredCaptureEnable() ); try { diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 6609edb..27493a6 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -281,14 +281,26 @@ function createPayment(param) { return JSON.parse(httpResult.getObject().text); } +/** + * Check if manual capture is available + * @param {boolean} isManualCaptureEnabled manual capture is enabled + * @param {number} installmentsCount installments count + * @param {number} deferredDays deferred days + * @return {boolean} is available + */ +function isAvailableForManualCapture(isManualCaptureEnabled, installmentsCount, deferredDays) { + return isManualCaptureEnabled && installmentsCount <= 4 && deferredDays <= 0; +} + /** * Build the data to give to the payment endpoint * @param {number} installmentsCount number of installments to pay * @param {number} deferredDays number of days to the first payment * @param {string} locale the user locale + * @param {boolean} isManualCaptureEnabled for capture_method * @returns {Object} to give to the payment endpoint */ -function buildPaymentData(installmentsCount, deferredDays, locale) { +function buildPaymentData(installmentsCount, deferredDays, locale, isManualCaptureEnabled) { var BasketMgr = require('dw/order/BasketMgr'); var URLUtils = require('dw/web/URLUtils'); var almaHelper = require('*/cartridge/scripts/helpers/almaHelpers'); @@ -330,6 +342,10 @@ function buildPaymentData(installmentsCount, deferredDays, locale) { customer: formatCustomerData(currentBasket.getCustomer().profile, currentBasket.getCustomerEmail(), formatAddress(currentBasket.getDefaultShipment().shippingAddress)) }; + if (isAvailableForManualCapture(isManualCaptureEnabled, installmentsCount, deferredDays)) { + paymentData.payment.capture_method = 'manual'; + } + if (installmentsCount >= 5) { var products = currentBasket.getAllProductLineItems(); var items = []; @@ -383,6 +399,23 @@ function setOrderMerchantReference(pid, order) { setOrderMerchantReferenceAPI.call(param); } +/** + * return payment data + * @param {number} installments installments + * @param {number} deferredDays deferred days + * @param {string} locale locale + * @param {boolean} isManualCaptureEnabled manual capture + * @return {Object} payment data + */ +function getPaymentData(installments, deferredDays, locale, isManualCaptureEnabled) { + return buildPaymentData( + installments, + deferredDays, + locale, + isManualCaptureEnabled + ); +} + module.exports = { orderStatusEquals: orderStatusEquals, @@ -398,5 +431,7 @@ module.exports = { buildPaymentData: buildPaymentData, flagAsPotentialFraud: flagAsPotentialFraud, createOrderFromBasketUUID: createOrderFromBasketUUID, - setOrderMerchantReference: setOrderMerchantReference + setOrderMerchantReference: setOrderMerchantReference, + getPaymentData: getPaymentData, + isAvailableForManualCapture: isAvailableForManualCapture }; diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js index 9e5523e..84a613f 100644 --- a/test/mocks/helpers/almaPaymentHelpers.js +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -21,7 +21,7 @@ function proxyModel() { }); } -function resolvedPaymentData(installmentsCount, defferedDays, locale, origin, hasCart = false) { +function resolvedPaymentData(installmentsCount, defferedDays, locale, origin, hasCart = false, hasCaptureMethod = false) { var paymentData = { payment: { purchase_amount: 25000, diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 5f0c2ae..a8c6a85 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -9,16 +9,26 @@ var resolvedPaymentData = require('../../../../mocks/helpers/almaPaymentHelpers' describe('almaPaymentHelper', function () { describe('Build payment data', function () { it('payment data for pnx', function () { - var payment = almaPaymentHelper.buildPaymentData(3, 0, 'fr_FR'); + var payment = almaPaymentHelper.buildPaymentData(3, 0, 'fr_FR', false); assert.deepEqual(payment, resolvedPaymentData(3, 0, 'fr_FR', 'online_in_page')); }); it('payment data for deferred', function () { - var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR'); + var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', false); assert.deepEqual(payment, resolvedPaymentData(1, 15, 'fr_FR', 'online')); }); it('payment data for credit has car property', function () { - var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR'); + var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', false); assert.deepEqual(payment, resolvedPaymentData(12, 0, 'fr_FR', 'online', true)); }); }); + describe('Build payment data for deferred capture', function () { + it('Payment data for deferred capture has capture method', function () { + var payment = almaPaymentHelper.buildPaymentData(3, 0, 'fr_FR', true); + assert.property(payment.payment, 'capture_method'); + }); + it('Payment data for credit has no capture_method in deferred capture', function () { + var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', true); + assert.notProperty(payment.payment, 'capture_method'); + }); + }); }); From 371d480bba80ba14f75f81d5f7283a0868e7ecfa Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Fri, 28 Jul 2023 17:40:16 +0200 Subject: [PATCH 06/66] feat:mpp-479 add tests --- .../scripts/helpers/almaPaymentHelper.js | 3 +-- .../scripts/helpers/almaPaymentHelperTest.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 27493a6..71b3dec 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -432,6 +432,5 @@ module.exports = { flagAsPotentialFraud: flagAsPotentialFraud, createOrderFromBasketUUID: createOrderFromBasketUUID, setOrderMerchantReference: setOrderMerchantReference, - getPaymentData: getPaymentData, - isAvailableForManualCapture: isAvailableForManualCapture + getPaymentData: getPaymentData }; diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index a8c6a85..5ff6c88 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -22,13 +22,25 @@ describe('almaPaymentHelper', function () { }); }); describe('Build payment data for deferred capture', function () { - it('Payment data for deferred capture has capture method', function () { + it('Payment data for pnx has not capture method if deferred capture is not enabled', function () { + var payment = almaPaymentHelper.buildPaymentData(3, 0, 'fr_FR', false); + assert.notProperty(payment.payment, 'capture_method'); + }); + it('Payment data for pnx has capture method', function () { var payment = almaPaymentHelper.buildPaymentData(3, 0, 'fr_FR', true); assert.property(payment.payment, 'capture_method'); }); - it('Payment data for credit has no capture_method in deferred capture', function () { + it('Payment data for credit has no capture method in', function () { var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', true); assert.notProperty(payment.payment, 'capture_method'); }); + it('Payment data for pay now has capture method', function () { + var payment = almaPaymentHelper.buildPaymentData(1, 0, 'fr_FR', true); + assert.property(payment.payment, 'capture_method'); + }); + it('Payment data for pay later has no capture method', function () { + var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', true); + assert.notProperty(payment.payment, 'capture_method'); + }); }); }); From 2751edd49fd0f3b9784acabbb631014858a866ea Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 1 Aug 2023 11:33:11 +0200 Subject: [PATCH 07/66] feat:mpp-479 deferred capture rm get payment data --- .../int_alma/cartridge/controllers/Alma.js | 4 ++-- .../scripts/helpers/almaPaymentHelper.js | 20 +------------------ 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/cartridges/int_alma/cartridge/controllers/Alma.js b/cartridges/int_alma/cartridge/controllers/Alma.js index da6083c..eba467e 100644 --- a/cartridges/int_alma/cartridge/controllers/Alma.js +++ b/cartridges/int_alma/cartridge/controllers/Alma.js @@ -271,7 +271,7 @@ server.post('CreatePaymentUrl', server.middleware.https, function (req, res, nex var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfiqHelper'); - var paymentData = almaPaymentHelper.getPaymentData( + var paymentData = almaPaymentHelper.buildPaymentData( req.querystring.installments, req.querystring.deferred_days, getLocale(req), @@ -329,7 +329,7 @@ server.get( var getLocale = require('*/cartridge/scripts/helpers/almaHelpers').getLocale; var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfiqHelper'); - var paymentData = almaPaymentHelper.getPaymentData( + var paymentData = almaPaymentHelper.buildPaymentData( req.querystring.installments, req.querystring.deferred_days, getLocale(req), diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 71b3dec..7e22914 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -399,23 +399,6 @@ function setOrderMerchantReference(pid, order) { setOrderMerchantReferenceAPI.call(param); } -/** - * return payment data - * @param {number} installments installments - * @param {number} deferredDays deferred days - * @param {string} locale locale - * @param {boolean} isManualCaptureEnabled manual capture - * @return {Object} payment data - */ -function getPaymentData(installments, deferredDays, locale, isManualCaptureEnabled) { - return buildPaymentData( - installments, - deferredDays, - locale, - isManualCaptureEnabled - ); -} - module.exports = { orderStatusEquals: orderStatusEquals, @@ -431,6 +414,5 @@ module.exports = { buildPaymentData: buildPaymentData, flagAsPotentialFraud: flagAsPotentialFraud, createOrderFromBasketUUID: createOrderFromBasketUUID, - setOrderMerchantReference: setOrderMerchantReference, - getPaymentData: getPaymentData + setOrderMerchantReference: setOrderMerchantReference }; From 57b06922d07bcbb1e96fd92a732a684208647285 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 1 Aug 2023 16:03:36 +0200 Subject: [PATCH 08/66] feat:mpp-483 deferred capture endpoint implement --- .../int_alma/cartridge/controllers/Alma.js | 4 +-- .../scripts/helpers/almaPaymentHelper.js | 22 ++++++++++++++- .../cartridge/scripts/services/alma.js | 27 ++++++++++++++++++- test/mocks/helpers/almaPaymentHelpers.js | 17 ++++++++++-- .../scripts/helpers/almaPaymentHelperTest.js | 10 +++++++ 5 files changed, 74 insertions(+), 6 deletions(-) diff --git a/cartridges/int_alma/cartridge/controllers/Alma.js b/cartridges/int_alma/cartridge/controllers/Alma.js index eba467e..50022a1 100644 --- a/cartridges/int_alma/cartridge/controllers/Alma.js +++ b/cartridges/int_alma/cartridge/controllers/Alma.js @@ -269,7 +269,7 @@ server.get('OrderAmount', server.middleware.https, function (req, res, next) { server.post('CreatePaymentUrl', server.middleware.https, function (req, res, next) { var getLocale = require('*/cartridge/scripts/helpers/almaHelpers').getLocale; var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); - var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfiqHelper'); + var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); var paymentData = almaPaymentHelper.buildPaymentData( req.querystring.installments, @@ -327,7 +327,7 @@ server.get( function (req, res, next) { var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var getLocale = require('*/cartridge/scripts/helpers/almaHelpers').getLocale; - var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfiqHelper'); + var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); var paymentData = almaPaymentHelper.buildPaymentData( req.querystring.installments, diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 7e22914..18028eb 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -281,6 +281,25 @@ function createPayment(param) { return JSON.parse(httpResult.getObject().text); } +/** + * Calls the capture payment endpoint + * @param {Object} params to give to the payment endpoint + * @returns {Object} api response + */ +function capturePayment(params) { + var service = require('*/cartridge/scripts/services/alma'); + var httpResult = service.captures().call(params); + console.log(httpResult); + // + // if (httpResult.msg !== 'OK') { + // var e = new Error('API error : ' + httpResult.status); + // e.name = 'create_payment_error'; + // throw e; + // } + // return JSON.parse(httpResult.getObject().text); + return ''; +} + /** * Check if manual capture is available * @param {boolean} isManualCaptureEnabled manual capture is enabled @@ -414,5 +433,6 @@ module.exports = { buildPaymentData: buildPaymentData, flagAsPotentialFraud: flagAsPotentialFraud, createOrderFromBasketUUID: createOrderFromBasketUUID, - setOrderMerchantReference: setOrderMerchantReference + setOrderMerchantReference: setOrderMerchantReference, + capturePayment: capturePayment }; diff --git a/cartridges/int_alma/cartridge/scripts/services/alma.js b/cartridges/int_alma/cartridge/scripts/services/alma.js index a3337de..c8297f8 100644 --- a/cartridges/int_alma/cartridge/scripts/services/alma.js +++ b/cartridges/int_alma/cartridge/scripts/services/alma.js @@ -166,6 +166,30 @@ function setOrderMerchantReferenceAPI() { }); } +/** + * Create a capture payment + * @return {dw.svc.Service} service instances + */ +function captures() { + return LocalServiceRegistry.createService('alma', { + /** + * @param {dw.svc.HTTPService} service service + * @param {array} params parameters + * @returns {string} json parameters as string + */ + createRequest: function (service, params) { + service.setRequestMethod('POST'); + service.URL = almaHelpers.getUrl('/v1/payments/' + params.pid + 'captures'); // eslint-disable-line no-param-reassign + almaHelpers.addHeaders(service); + + return JSON.stringify(params); + }, + parseResponse: function (svc, client) { + return client; + } + }); +} + module.exports = { getPaymentDetails: getPaymentDetails, checkEligibility: checkEligibility, @@ -173,5 +197,6 @@ module.exports = { refundPayment: refundPayment, createPayment: createPayment, potentialFraud: flagAsPotentialFraud, - setOrderMerchantReferenceAPI: setOrderMerchantReferenceAPI + setOrderMerchantReferenceAPI: setOrderMerchantReferenceAPI, + captures: captures }; diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js index 84a613f..6c11338 100644 --- a/test/mocks/helpers/almaPaymentHelpers.js +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -9,7 +9,18 @@ var almaHelper = require('../helpers/almaHelpers'); var almaCheckoutHelper = require('../helpers/almaCheckoutHelpers'); var almaAddressHelper = require('../helpers/almaAddressHelper'); var almaOnShipmentHelper = require('../helpers/almaOnShipmentHelpers'); +const sinon = require('sinon'); +var call = sinon.stub(); + + +var service = { + captures: function () { + return { + call: call + }; + } +}; function proxyModel() { return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper', { 'dw/order/BasketMgr': BasketMgr, @@ -17,7 +28,8 @@ function proxyModel() { '*/cartridge/scripts/helpers/almaHelpers': almaHelper, '*/cartridge/scripts/helpers/almaCheckoutHelper': almaCheckoutHelper, '*/cartridge/scripts/helpers/almaAddressHelper': almaAddressHelper, - '*/cartridge/scripts/helpers/almaOnShipmentHelper': almaOnShipmentHelper + '*/cartridge/scripts/helpers/almaOnShipmentHelper': almaOnShipmentHelper, + '*/cartridge/scripts/services/alma': service }); } @@ -86,5 +98,6 @@ function resolvedPaymentData(installmentsCount, defferedDays, locale, origin, ha module.exports = { proxyModel: proxyModel(), - resolvedPaymentData: resolvedPaymentData + resolvedPaymentData: resolvedPaymentData, + service: service }; diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 5ff6c88..516489c 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -5,6 +5,7 @@ var assert = require('chai').assert; var almaPaymentHelper = require('../../../../mocks/helpers/almaPaymentHelpers').proxyModel; var resolvedPaymentData = require('../../../../mocks/helpers/almaPaymentHelpers').resolvedPaymentData; +var service = require('../../../../mocks/helpers/almaPaymentHelpers').service; describe('almaPaymentHelper', function () { describe('Build payment data', function () { @@ -43,4 +44,13 @@ describe('almaPaymentHelper', function () { assert.notProperty(payment.payment, 'capture_method'); }); }); + + describe('Capture endpoint', function () { + it('Capture endpoint is call with the Alma payment external_id', function () { + var params = { external_id: 'payment_12345' }; + almaPaymentHelper.capturePayment(params); + assert.isTrue(service.captures().call.calledOnce); + assert.isTrue(service.captures().call.calledWith(params)); + }); + }); }); From fff0047508a7d157d79c5fd96097b9e53726bb05 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 2 Aug 2023 10:10:31 +0200 Subject: [PATCH 09/66] feat:mpp-483 deferred capture endpoint error and tests --- .../scripts/helpers/almaPaymentHelper.js | 40 +++++++++++-------- test/mocks/helpers/almaPaymentHelpers.js | 18 +++++++-- .../scripts/helpers/almaPaymentHelperTest.js | 18 ++++++++- 3 files changed, 54 insertions(+), 22 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 18028eb..0683919 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -25,7 +25,8 @@ function getPaymentObj(almaPaymentId) { pid: almaPaymentId }; - var httpResult = service.getPaymentDetails().call(param); + var httpResult = service.getPaymentDetails() + .call(param); if (httpResult.msg !== 'OK') { throw new Error('API error'); } @@ -242,7 +243,8 @@ function createOrderFromBasket(almaPaymentMethod) { var paymentMethodErrorContext = { Alma_Payment_Method: almaPaymentMethod }; - var Logger = require('dw/system/Logger').getLogger('alma'); + var Logger = require('dw/system/Logger') + .getLogger('alma'); Logger.error('Unable to process payment: payment method not found. | {0}', [JSON.stringify(paymentMethodErrorContext)]); throw new Error('Unable to process payment: payment method not found'); } @@ -271,7 +273,8 @@ function createOrderFromBasket(almaPaymentMethod) { */ function createPayment(param) { var service = require('*/cartridge/scripts/services/alma'); - var httpResult = service.createPayment().call(param); + var httpResult = service.createPayment() + .call(param); if (httpResult.msg !== 'OK') { var e = new Error('API error : ' + httpResult.status); @@ -288,16 +291,15 @@ function createPayment(param) { */ function capturePayment(params) { var service = require('*/cartridge/scripts/services/alma'); - var httpResult = service.captures().call(params); - console.log(httpResult); - // - // if (httpResult.msg !== 'OK') { - // var e = new Error('API error : ' + httpResult.status); - // e.name = 'create_payment_error'; - // throw e; - // } - // return JSON.parse(httpResult.getObject().text); - return ''; + var httpResult = service.captures() + .call(params); + + if (httpResult.status !== 201) { + var e = new Error('API error : ' + httpResult.status); + e.name = 'capture_payment_error'; + throw e; + } + return JSON.parse(JSON.stringify(httpResult.getObject().text)); } /** @@ -343,15 +345,19 @@ function buildPaymentData(installmentsCount, deferredDays, locale, isManualCaptu installments_count: parseInt(installmentsCount, 10), deferred_days: parseInt(deferredDays, 10), deferred_months: 0, - return_url: URLUtils.http('Alma-PaymentSuccess').toString(), - ipn_callback_url: URLUtils.http('Alma-IPN').toString(), - customer_cancel_url: URLUtils.https('Alma-CustomerCancel').toString(), + return_url: URLUtils.http('Alma-PaymentSuccess') + .toString(), + ipn_callback_url: URLUtils.http('Alma-IPN') + .toString(), + customer_cancel_url: URLUtils.https('Alma-CustomerCancel') + .toString(), locale: locale, origin: origin, shipping_address: formatAddress(currentBasket.getDefaultShipment().shippingAddress), billing_address: formatAddress(currentBasket.getBillingAddress()), deferred: isEnableOnShipment ? 'trigger' : '', - deferred_description: isEnableOnShipment ? require('dw/web/Resource').msg('alma.at_shipping', 'alma', null) : '', + deferred_description: isEnableOnShipment ? require('dw/web/Resource') + .msg('alma.at_shipping', 'alma', null) : '', custom_data: { cms_name: 'SFCC', cms_version: almaHelper.getSfccVersion(), diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js index 6c11338..0481b4a 100644 --- a/test/mocks/helpers/almaPaymentHelpers.js +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -10,9 +10,19 @@ var almaCheckoutHelper = require('../helpers/almaCheckoutHelpers'); var almaAddressHelper = require('../helpers/almaAddressHelper'); var almaOnShipmentHelper = require('../helpers/almaOnShipmentHelpers'); const sinon = require('sinon'); +var call; -var call = sinon.stub(); - +function setHttpReturnStatusCode(statusCode) { + call = sinon.stub() + .returns({ + status: statusCode, + getObject: function () { + return { + text: 'object test' + }; + } + }); +} var service = { captures: function () { @@ -21,6 +31,7 @@ var service = { }; } }; + function proxyModel() { return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper', { 'dw/order/BasketMgr': BasketMgr, @@ -99,5 +110,6 @@ function resolvedPaymentData(installmentsCount, defferedDays, locale, origin, ha module.exports = { proxyModel: proxyModel(), resolvedPaymentData: resolvedPaymentData, - service: service + service: service, + setHttpReturnStatusCode: setHttpReturnStatusCode }; diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 516489c..b51ff4a 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -6,6 +6,7 @@ var assert = require('chai').assert; var almaPaymentHelper = require('../../../../mocks/helpers/almaPaymentHelpers').proxyModel; var resolvedPaymentData = require('../../../../mocks/helpers/almaPaymentHelpers').resolvedPaymentData; var service = require('../../../../mocks/helpers/almaPaymentHelpers').service; +var setHttpReturnStatusCode = require('../../../../mocks/helpers/almaPaymentHelpers').setHttpReturnStatusCode; describe('almaPaymentHelper', function () { describe('Build payment data', function () { @@ -47,10 +48,23 @@ describe('almaPaymentHelper', function () { describe('Capture endpoint', function () { it('Capture endpoint is call with the Alma payment external_id', function () { + setHttpReturnStatusCode(201); var params = { external_id: 'payment_12345' }; - almaPaymentHelper.capturePayment(params); + assert.doesNotThrow(function () { + almaPaymentHelper.capturePayment(params); + }); assert.isTrue(service.captures().call.calledOnce); - assert.isTrue(service.captures().call.calledWith(params)); + assert.isTrue(service.captures() + .call + .calledWith(params)); + }); + + it('Capture endpoint throw an error if http status code not equal 201', function () { + setHttpReturnStatusCode(400); + var params = { external_id: 'payment_12345' }; + assert.throws(function () { + almaPaymentHelper.capturePayment(params); + }); }); }); }); From 01634a272913a38ea41d34ee594307cd35fdf0f8 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 2 Aug 2023 17:25:58 +0200 Subject: [PATCH 10/66] feat:mpp-508 add flag for deferred capture in order --- .../int_alma/cartridge/controllers/Alma.js | 45 ++++++++++--------- .../scripts/helpers/almaOrderHelper.js | 29 +++++++++++- .../scripts/helpers/almaPaymentHelper.js | 3 +- .../ref/system-objecttype-extensions.xml | 8 ++++ test/mocks/helpers/almaOrderHelpers.js | 25 +++++++++++ .../scripts/helpers/almaOrderHelperTest.js | 34 ++++++++++++++ 6 files changed, 121 insertions(+), 23 deletions(-) create mode 100644 test/mocks/helpers/almaOrderHelpers.js create mode 100644 test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js diff --git a/cartridges/int_alma/cartridge/controllers/Alma.js b/cartridges/int_alma/cartridge/controllers/Alma.js index 50022a1..71d209e 100644 --- a/cartridges/int_alma/cartridge/controllers/Alma.js +++ b/cartridges/int_alma/cartridge/controllers/Alma.js @@ -38,20 +38,6 @@ function buildViewParams(paymentObj, order, localeId, reqProfile) { return viewParams; } -/** - * Synchronize order and payment details - * @param {string} pid payment id - * @param {Object} order order - * @throw Error - */ -function syncOrderAndPaymentDetails(pid, order) { - var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); - var orderHelper = require('*/cartridge/scripts/helpers/almaOrderHelper'); - - orderHelper.addPidToOrder(order, pid); - almaPaymentHelper.setOrderMerchantReference(pid, order); -} - /** * Ensure that Alma received the right amount and that SFCC order is synced * @param {Object} paymentObj the payment to describe @@ -115,6 +101,7 @@ function buildPaymentObj(pid) { server.get('PaymentSuccess', function (req, res, next) { var paymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var orderHelper = require('*/cartridge/scripts/helpers/almaOrderHelper'); + var configHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); var paymentObj = null; try { @@ -128,9 +115,16 @@ server.get('PaymentSuccess', function (req, res, next) { } var order = getOrderByAlmaPaymentId(req.querystring.pid); + var isDeferredCapture = paymentHelper.isAvailableForManualCapture( + configHelper.isDeferredCaptureEnable(), + paymentObj.installments_count, + paymentObj.deferred_days + ); + if (!order) { order = paymentHelper.createOrderFromBasket(req.querystring.alma_payment_method); - syncOrderAndPaymentDetails(req.querystring.pid, order); + orderHelper.addAlmaDataToOrder(req.querystring.pid, order, isDeferredCapture); + paymentHelper.setOrderMerchantReference(req.querystring.pid, order); } // we probably should throw an error if we don't have an order @@ -272,11 +266,11 @@ server.post('CreatePaymentUrl', server.middleware.https, function (req, res, nex var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); var paymentData = almaPaymentHelper.buildPaymentData( - req.querystring.installments, - req.querystring.deferred_days, - getLocale(req), - almaConfigHelper.isDeferredCaptureEnable() - ); + req.querystring.installments, + req.querystring.deferred_days, + getLocale(req), + almaConfigHelper.isDeferredCaptureEnable() + ); try { var result = almaPaymentHelper.createPayment(paymentData); @@ -328,6 +322,8 @@ server.get( var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var getLocale = require('*/cartridge/scripts/helpers/almaHelpers').getLocale; var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); + var orderHelper = require('*/cartridge/scripts/helpers/almaOrderHelper'); + var configHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); var paymentData = almaPaymentHelper.buildPaymentData( req.querystring.installments, @@ -336,13 +332,20 @@ server.get( almaConfigHelper.isDeferredCaptureEnable() ); + var isDeferredCapture = almaPaymentHelper.isAvailableForManualCapture( + configHelper.isDeferredCaptureEnable(), + req.querystring.installments, + req.querystring.deferred_days + ); + try { var almaPayment = almaPaymentHelper.createPayment(paymentData); var order = getOrderByAlmaPaymentId(almaPayment.id); if (!order) { order = almaPaymentHelper.createOrderFromBasket(req.querystring.alma_payment_method); - syncOrderAndPaymentDetails(almaPayment.id, order); + orderHelper.addAlmaDataToOrder(almaPayment.id, order, isDeferredCapture); + almaPaymentHelper.setOrderMerchantReference(almaPayment.id, order); } res.setStatusCode(200); res.json({ diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js index 9a37124..c4a55a2 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js @@ -26,8 +26,35 @@ function addAlmaPaymentDetails(order, payDetail) { }); } +/** + * Add alma deferred capture in order + * @param {Order} order order + * @param {string} deferredCapture payDetail + */ +function setAlmaDeferredCapture(order, deferredCapture) { + Transaction.wrap(function () { + // eslint-disable-next-line no-param-reassign + order.custom.ALMA_Deferred_Capture = deferredCapture; + }); +} + +/** + * Add Alma data to order + * @param {string} pid payment id + * @param {Object} order order + * @param {boolean} isDeferredCapture is deferred capture + * @throw Error + */ +function addAlmaDataToOrder(pid, order, isDeferredCapture) { + addPidToOrder(order, pid); + if (isDeferredCapture) { + setAlmaDeferredCapture(order, 'true'); + } +} + module.exports = { addPidToOrder: addPidToOrder, - addAlmaPaymentDetails: addAlmaPaymentDetails + addAlmaPaymentDetails: addAlmaPaymentDetails, + addAlmaDataToOrder: addAlmaDataToOrder }; diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 0683919..0838b3c 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -440,5 +440,6 @@ module.exports = { flagAsPotentialFraud: flagAsPotentialFraud, createOrderFromBasketUUID: createOrderFromBasketUUID, setOrderMerchantReference: setOrderMerchantReference, - capturePayment: capturePayment + capturePayment: capturePayment, + isAvailableForManualCapture: isAvailableForManualCapture }; diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index 112167c..e2be989 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -14,12 +14,20 @@ false 0 + + ALMA Deferred Capture + string + false + true + 0 + Alma + diff --git a/test/mocks/helpers/almaOrderHelpers.js b/test/mocks/helpers/almaOrderHelpers.js new file mode 100644 index 0000000..24c9e56 --- /dev/null +++ b/test/mocks/helpers/almaOrderHelpers.js @@ -0,0 +1,25 @@ +'use strict'; +var sinon = require('sinon'); +var proxyquire = require('proxyquire') + .noCallThru() + .noPreserveCache(); +var stubCallback = sinon.stub(); +var transaction = { + wrap: stubCallback +}; + +function createNewTransaction() { + stubCallback.reset(); +} + +function proxyModel() { + return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper', { + 'dw/system/Transaction': transaction + }); +} + +module.exports = { + almaOrderHelpers: proxyModel(), + transaction: transaction, + createNewTransaction: createNewTransaction +}; diff --git a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js new file mode 100644 index 0000000..606f1bc --- /dev/null +++ b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js @@ -0,0 +1,34 @@ +'use strict'; + +// almaOrderHelper unit tests + +var assert = require('chai').assert; +var almaOrderHelper = require('../../../../mocks/helpers/almaOrderHelpers').almaOrderHelpers; +var transaction = require('../../../../mocks/helpers/almaOrderHelpers').transaction; +var createNewTransaction = require('../../../../mocks/helpers/almaOrderHelpers').createNewTransaction; +var order = { + custom: { + almaPaymentId: '', + ALMA_Deferred_Capture: '' + } +}; +describe('Alma order helper', function () { + it('Add Alma data to order does not throw an error', function () { + createNewTransaction(); + assert.doesNotThrow(function () { + almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, true); + }); + }); + it('For a non deferred capture payment transaction is called once', function () { + createNewTransaction(); + almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, false); + // Wrap is called twice for one save + assert.isTrue(transaction.wrap.calledOnce); + }); + it('For a deferred capture payment transaction is called twice', function () { + createNewTransaction(); + almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, true); + // Wrap is called twice for one save + assert.isTrue(transaction.wrap.calledTwice); + }); +}); From 22975247204cbb0163ee3ed160cd4dec271d3387 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 3 Aug 2023 09:20:07 +0200 Subject: [PATCH 11/66] feat:mpp-508 rm some comment --- test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js index 606f1bc..6c5c6ed 100644 --- a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js @@ -22,13 +22,11 @@ describe('Alma order helper', function () { it('For a non deferred capture payment transaction is called once', function () { createNewTransaction(); almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, false); - // Wrap is called twice for one save assert.isTrue(transaction.wrap.calledOnce); }); it('For a deferred capture payment transaction is called twice', function () { createNewTransaction(); almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, true); - // Wrap is called twice for one save assert.isTrue(transaction.wrap.calledTwice); }); }); From 8b734c14fcc7a61fadde7dcf7d5e5dea88c2057f Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 3 Aug 2023 09:33:32 +0200 Subject: [PATCH 12/66] feat:mpp-508 modify condition for manual capture --- .../int_alma/cartridge/controllers/Alma.js | 23 +++++++++++-------- .../scripts/helpers/almaPaymentHelper.js | 2 +- .../scripts/helpers/almaPaymentHelperTest.js | 4 ++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/cartridges/int_alma/cartridge/controllers/Alma.js b/cartridges/int_alma/cartridge/controllers/Alma.js index 71d209e..c733262 100644 --- a/cartridges/int_alma/cartridge/controllers/Alma.js +++ b/cartridges/int_alma/cartridge/controllers/Alma.js @@ -265,11 +265,17 @@ server.post('CreatePaymentUrl', server.middleware.https, function (req, res, nex var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); + var isDeferredCapture = almaPaymentHelper.isAvailableForManualCapture( + almaConfigHelper.isDeferredCaptureEnable(), + req.querystring.installments, + req.querystring.deferred_days + ); + var paymentData = almaPaymentHelper.buildPaymentData( req.querystring.installments, req.querystring.deferred_days, getLocale(req), - almaConfigHelper.isDeferredCaptureEnable() + isDeferredCapture ); try { @@ -323,19 +329,18 @@ server.get( var getLocale = require('*/cartridge/scripts/helpers/almaHelpers').getLocale; var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); var orderHelper = require('*/cartridge/scripts/helpers/almaOrderHelper'); - var configHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); - var paymentData = almaPaymentHelper.buildPaymentData( + var isDeferredCapture = almaPaymentHelper.isAvailableForManualCapture( + almaConfigHelper.isDeferredCaptureEnable(), req.querystring.installments, - req.querystring.deferred_days, - getLocale(req), - almaConfigHelper.isDeferredCaptureEnable() + req.querystring.deferred_days ); - var isDeferredCapture = almaPaymentHelper.isAvailableForManualCapture( - configHelper.isDeferredCaptureEnable(), + var paymentData = almaPaymentHelper.buildPaymentData( req.querystring.installments, - req.querystring.deferred_days + req.querystring.deferred_days, + getLocale(req), + isDeferredCapture ); try { diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 0838b3c..f0558ad 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -367,7 +367,7 @@ function buildPaymentData(installmentsCount, deferredDays, locale, isManualCaptu customer: formatCustomerData(currentBasket.getCustomer().profile, currentBasket.getCustomerEmail(), formatAddress(currentBasket.getDefaultShipment().shippingAddress)) }; - if (isAvailableForManualCapture(isManualCaptureEnabled, installmentsCount, deferredDays)) { + if (isManualCaptureEnabled) { paymentData.payment.capture_method = 'manual'; } diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index b51ff4a..87045a9 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -33,7 +33,7 @@ describe('almaPaymentHelper', function () { assert.property(payment.payment, 'capture_method'); }); it('Payment data for credit has no capture method in', function () { - var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', true); + var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', false); assert.notProperty(payment.payment, 'capture_method'); }); it('Payment data for pay now has capture method', function () { @@ -41,7 +41,7 @@ describe('almaPaymentHelper', function () { assert.property(payment.payment, 'capture_method'); }); it('Payment data for pay later has no capture method', function () { - var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', true); + var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', false); assert.notProperty(payment.payment, 'capture_method'); }); }); From 64d2bb8ea3908d521ad26b186f866b70a896ad36 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 3 Aug 2023 09:48:22 +0200 Subject: [PATCH 13/66] feat:mpp-508 rename isManualCapture --- .../int_alma/cartridge/scripts/helpers/almaPaymentHelper.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index f0558ad..6f09077 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -318,10 +318,10 @@ function isAvailableForManualCapture(isManualCaptureEnabled, installmentsCount, * @param {number} installmentsCount number of installments to pay * @param {number} deferredDays number of days to the first payment * @param {string} locale the user locale - * @param {boolean} isManualCaptureEnabled for capture_method + * @param {boolean} isManualCapture for capture_method * @returns {Object} to give to the payment endpoint */ -function buildPaymentData(installmentsCount, deferredDays, locale, isManualCaptureEnabled) { +function buildPaymentData(installmentsCount, deferredDays, locale, isManualCapture) { var BasketMgr = require('dw/order/BasketMgr'); var URLUtils = require('dw/web/URLUtils'); var almaHelper = require('*/cartridge/scripts/helpers/almaHelpers'); @@ -367,7 +367,7 @@ function buildPaymentData(installmentsCount, deferredDays, locale, isManualCaptu customer: formatCustomerData(currentBasket.getCustomer().profile, currentBasket.getCustomerEmail(), formatAddress(currentBasket.getDefaultShipment().shippingAddress)) }; - if (isManualCaptureEnabled) { + if (isManualCapture) { paymentData.payment.capture_method = 'manual'; } From aab1dbc1038b6e9ba430194e50ada274d23437cf Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 3 Aug 2023 12:14:51 +0200 Subject: [PATCH 14/66] feat:mpp-484 deferred capture job + tests --- .../scripts/helpers/almaOrderHelper.js | 2 +- .../scripts/steps/CheckDeferredCapture.js | 16 +++++++ test/mocks/steps/CheckDeferredCaptures.js | 29 +++++++++++++ .../scripts/steps/CheckDeferredCaptureTest.js | 43 +++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture.js create mode 100644 test/mocks/steps/CheckDeferredCaptures.js create mode 100644 test/unit/int_alma/scripts/steps/CheckDeferredCaptureTest.js diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js index c4a55a2..0c74b5e 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js @@ -27,7 +27,7 @@ function addAlmaPaymentDetails(order, payDetail) { } /** - * Add alma deferred capture in order + * Set alma deferred capture in order * @param {Order} order order * @param {string} deferredCapture payDetail */ diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture.js b/cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture.js new file mode 100644 index 0000000..07469bd --- /dev/null +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture.js @@ -0,0 +1,16 @@ +'use strict'; + +var OrderMgr = require('dw/order/OrderMgr'); +var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); + +exports.execute = function () { + var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='true'", null); + + if (orders.count > 0) { + while (orders.hasNext()) { + var order = orders.next(); + var params = { external_id: order.almaPaymentId }; + almaPaymentHelper.capturePayment(params); + } + } +}; diff --git a/test/mocks/steps/CheckDeferredCaptures.js b/test/mocks/steps/CheckDeferredCaptures.js new file mode 100644 index 0000000..dd95edf --- /dev/null +++ b/test/mocks/steps/CheckDeferredCaptures.js @@ -0,0 +1,29 @@ +'use strict'; + +var proxyquire = require('proxyquire') + .noCallThru() + .noPreserveCache(); + +var sinon = require('sinon'); + +var OrderMgr = { + searchOrders: sinon.stub() + .returns({}) +}; + +var almaPaymentHelper = { + capturePayment: sinon.stub() +}; + +function proxyModel() { + return proxyquire('../../../cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture', { + 'dw/order/OrderMgr': OrderMgr, + '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper + }); +} + +module.exports = { + CheckDeferredCapture: proxyModel(), + OrderMgr: OrderMgr, + almaPaymentHelper: almaPaymentHelper +}; diff --git a/test/unit/int_alma/scripts/steps/CheckDeferredCaptureTest.js b/test/unit/int_alma/scripts/steps/CheckDeferredCaptureTest.js new file mode 100644 index 0000000..d55adf8 --- /dev/null +++ b/test/unit/int_alma/scripts/steps/CheckDeferredCaptureTest.js @@ -0,0 +1,43 @@ +// CheckDeferredCapture.js unit tests + +var OrderMgr = require('../../../../mocks/steps/CheckDeferredCaptures').OrderMgr; +var almaPaymentHelper = require('../../../../mocks/steps/CheckDeferredCaptures').almaPaymentHelper; +var assert = require('chai').assert; +var checkDeferredCapture = require('../../../../mocks/steps/CheckDeferredCaptures').CheckDeferredCapture; + +var sinon = require('sinon'); + +var order = { + almaPaymentId: 'payment_12345' +}; + +var hasNext = sinon.stub(); +hasNext.onCall(0) + .returns(true); +hasNext.onCall(1) + .returns(false); + +var orders = { + count: 1, + hasNext: hasNext, + next: function () { + return order; + } +}; + + +describe('Deferred capture job', function () { + it('Should get orders with deferred capture', function () { + checkDeferredCapture.execute(); + assert.isTrue(OrderMgr.searchOrders.calledOnce); + assert.isTrue(OrderMgr.searchOrders.calledWith("custom.ALMA_Deferred_Capture='true'", null)); + }); + + it('Should call Capture for orders with deferred capture', function () { + OrderMgr.searchOrders = sinon.stub().returns(orders); + checkDeferredCapture.execute(); + var params = { external_id: 'payment_12345' }; + assert.isTrue(almaPaymentHelper.capturePayment.calledOnce); + assert.isTrue(almaPaymentHelper.capturePayment.calledWith(params)); + }); +}); From b133bfa1a0ce62ff84afd6cc095e76637359e410 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 3 Aug 2023 18:19:27 +0200 Subject: [PATCH 15/66] feat:mpp-484 implement capture at shipping job --- .../scripts/helpers/almaOrderHelper.js | 5 +- .../scripts/helpers/almaPaymentHelper.js | 5 +- .../cartridge/scripts/services/alma.js | 2 +- .../scripts/steps/CapturePaymentOrders.js | 35 ++++++ .../scripts/steps/CheckDeferredCapture.js | 16 --- site_preference_builder/jobs.js | 17 ++- .../ref/jobs/jobDeferredCapture.xml | 39 +++++++ .../ref/system-objecttype-extensions.xml | 4 +- test/mocks/steps/CapturePaymentOrders.js | 49 ++++++++ test/mocks/steps/CheckDeferredCaptures.js | 29 ----- .../scripts/helpers/almaPaymentHelperTest.js | 2 +- .../scripts/steps/CapturePaymentOrdersTest.js | 105 ++++++++++++++++++ .../scripts/steps/CheckDeferredCaptureTest.js | 43 ------- 13 files changed, 252 insertions(+), 99 deletions(-) create mode 100644 cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js delete mode 100644 cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture.js create mode 100644 site_preference_builder/ref/jobs/jobDeferredCapture.xml create mode 100644 test/mocks/steps/CapturePaymentOrders.js delete mode 100644 test/mocks/steps/CheckDeferredCaptures.js create mode 100644 test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js delete mode 100644 test/unit/int_alma/scripts/steps/CheckDeferredCaptureTest.js diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js index 0c74b5e..9f681a5 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js @@ -48,7 +48,7 @@ function setAlmaDeferredCapture(order, deferredCapture) { function addAlmaDataToOrder(pid, order, isDeferredCapture) { addPidToOrder(order, pid); if (isDeferredCapture) { - setAlmaDeferredCapture(order, 'true'); + setAlmaDeferredCapture(order, 'toBeCaptured'); } } @@ -56,5 +56,6 @@ function addAlmaDataToOrder(pid, order, isDeferredCapture) { module.exports = { addPidToOrder: addPidToOrder, addAlmaPaymentDetails: addAlmaPaymentDetails, - addAlmaDataToOrder: addAlmaDataToOrder + addAlmaDataToOrder: addAlmaDataToOrder, + setAlmaDeferredCapture: setAlmaDeferredCapture }; diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 6f09077..5bfd847 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -288,18 +288,19 @@ function createPayment(param) { * Calls the capture payment endpoint * @param {Object} params to give to the payment endpoint * @returns {Object} api response + * @throws Error */ function capturePayment(params) { var service = require('*/cartridge/scripts/services/alma'); var httpResult = service.captures() .call(params); - if (httpResult.status !== 201) { + if (httpResult.status !== 'OK') { var e = new Error('API error : ' + httpResult.status); e.name = 'capture_payment_error'; throw e; } - return JSON.parse(JSON.stringify(httpResult.getObject().text)); + return JSON.parse(httpResult.getObject().text); } /** diff --git a/cartridges/int_alma/cartridge/scripts/services/alma.js b/cartridges/int_alma/cartridge/scripts/services/alma.js index c8297f8..b4b672f 100644 --- a/cartridges/int_alma/cartridge/scripts/services/alma.js +++ b/cartridges/int_alma/cartridge/scripts/services/alma.js @@ -179,7 +179,7 @@ function captures() { */ createRequest: function (service, params) { service.setRequestMethod('POST'); - service.URL = almaHelpers.getUrl('/v1/payments/' + params.pid + 'captures'); // eslint-disable-line no-param-reassign + service.URL = almaHelpers.getUrl('/v1/payments/' + params.external_id + '/captures'); // eslint-disable-line no-param-reassign almaHelpers.addHeaders(service); return JSON.stringify(params); diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js new file mode 100644 index 0000000..dac37b2 --- /dev/null +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -0,0 +1,35 @@ +'use strict'; + +var OrderMgr = require('dw/order/OrderMgr'); +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') + .getLogger('alma'); + +exports.execute = function () { + var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='toBeCaptured'", null); + var errors = []; + + if (orders.count > 0) { + while (orders.hasNext()) { + var order = orders.next(); + var params = { external_id: order.custom.almaPaymentId }; + + try { + var capture = almaPaymentHelper.capturePayment(params); + almaOrderHelper.setAlmaDeferredCapture(order, capture.id); + logger.warn('Capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); + } catch (e) { + logger.warn('Unable to capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); + errors.push(e); + } + } + } + + if (errors.length > 0) { + return new Status(Status.ERROR); + } + + return new Status(Status.OK); +}; diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture.js b/cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture.js deleted file mode 100644 index 07469bd..0000000 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture.js +++ /dev/null @@ -1,16 +0,0 @@ -'use strict'; - -var OrderMgr = require('dw/order/OrderMgr'); -var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); - -exports.execute = function () { - var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='true'", null); - - if (orders.count > 0) { - while (orders.hasNext()) { - var order = orders.next(); - var params = { external_id: order.almaPaymentId }; - almaPaymentHelper.capturePayment(params); - } - } -}; diff --git a/site_preference_builder/jobs.js b/site_preference_builder/jobs.js index 892a112..166ce0b 100644 --- a/site_preference_builder/jobs.js +++ b/site_preference_builder/jobs.js @@ -3,6 +3,7 @@ const builder = require('./customSitePrefBuilder'); const INPUT_JOB_SHIPMENT_FILE = './site_preference_builder/ref/jobs/jobShipment.xml'; const INPUT_JOB_REFUND_FILE = './site_preference_builder/ref/jobs/jobRefund.xml'; +const INPUT_JOB_DEFERRED_CAPTURE_FILE = './site_preference_builder/ref/jobs/jobDeferredCapture.xml'; const INPUT_JOBS_FILE = './site_preference_builder/ref/jobs/jobs.xml'; const OUTPUT_JOB_SHIPMENT_FILE = './metadata/site_template/jobs.xml'; @@ -17,17 +18,28 @@ exports.merchantHasOnShipment = merchantHasOnShipment; const getJobShipment = async (plans, siteName) => { if (merchantHasOnShipment(plans)) { - const jobShipment = readFileSync(INPUT_JOB_SHIPMENT_FILE).toString(); + const jobShipment = readFileSync(INPUT_JOB_SHIPMENT_FILE) + .toString(); const onShipmentJob = await builder.xmlToJson(jobShipment.replace('[[SITENAME]]', siteName)); allJobs.job.push(onShipmentJob.job); } }; +const getJobDeferredCapture = async (siteName) => { + const jobDeferredCapture = readFileSync(INPUT_JOB_DEFERRED_CAPTURE_FILE) + .toString(); + const deferredCaptureJob = await builder.xmlToJson( + jobDeferredCapture.replace('[[SITENAME]]', siteName) + ); + allJobs.job.push(deferredCaptureJob.job); +}; + const getJobRefund = async (toggleRefund, siteName) => { if (toggleRefund === 'off') { return; } - const jobRefund = readFileSync(INPUT_JOB_REFUND_FILE).toString(); + const jobRefund = readFileSync(INPUT_JOB_REFUND_FILE) + .toString(); const onRefundJob = await builder.xmlToJson(jobRefund.replace('[[SITENAME]]', siteName)); allJobs.job.push(onRefundJob.job); }; @@ -35,6 +47,7 @@ const getJobRefund = async (toggleRefund, siteName) => { exports.writeJobsFile = async (toggleRefund, plans, siteName) => { await getJobShipment(plans, siteName); await getJobRefund(toggleRefund, siteName); + await getJobDeferredCapture(siteName); const jobs = readFileSync(INPUT_JOBS_FILE); const jobsContent = await builder.xmlToJson(jobs); diff --git a/site_preference_builder/ref/jobs/jobDeferredCapture.xml b/site_preference_builder/ref/jobs/jobDeferredCapture.xml new file mode 100644 index 0000000..5f721a9 --- /dev/null +++ b/site_preference_builder/ref/jobs/jobDeferredCapture.xml @@ -0,0 +1,39 @@ + + Capture payment for orders which are on deferred capture + + + + + + + + + + + int_alma/cartridge/scripts/steps/CapturePaymentOrders.js + execute + true + + + + + + + + + 2021-11-24Z + 22:36:18.000Z + 5m + + Thursday + Wednesday + Friday + Sunday + Tuesday + Monday + Saturday + + + + + diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index e2be989..10fe149 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -15,11 +15,9 @@ 0 - ALMA Deferred Capture string false - true - 0 + false diff --git a/test/mocks/steps/CapturePaymentOrders.js b/test/mocks/steps/CapturePaymentOrders.js new file mode 100644 index 0000000..5ca5ea7 --- /dev/null +++ b/test/mocks/steps/CapturePaymentOrders.js @@ -0,0 +1,49 @@ +'use strict'; + +var proxyquire = require('proxyquire') + .noCallThru() + .noPreserveCache(); + +var sinon = require('sinon'); + +var OrderMgr = { + searchOrders: sinon.stub() + .returns({}) +}; + +var almaPaymentHelper = { + capturePayment: sinon.stub() +}; + +var almaOrderHelper = { + setAlmaDeferredCapture: sinon.stub() +}; + +var warnStub = sinon.stub(); + +var logger = { + getLogger: function () { + return { + warn: warnStub, + info: function () {} + }; + } +}; + +function proxyModel() { + return proxyquire('../../../cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders', { + 'dw/order/OrderMgr': OrderMgr, + 'dw/system/Logger': logger, + 'dw/system/Status': sinon.mock(), + '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper, + '*/cartridge/scripts/helpers/almaOrderHelper': almaOrderHelper + }); +} + +module.exports = { + CapturePaymentOrders: proxyModel(), + OrderMgr: OrderMgr, + almaPaymentHelper: almaPaymentHelper, + almaOrderHelper: almaOrderHelper, + warnStub: warnStub +}; diff --git a/test/mocks/steps/CheckDeferredCaptures.js b/test/mocks/steps/CheckDeferredCaptures.js deleted file mode 100644 index dd95edf..0000000 --- a/test/mocks/steps/CheckDeferredCaptures.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict'; - -var proxyquire = require('proxyquire') - .noCallThru() - .noPreserveCache(); - -var sinon = require('sinon'); - -var OrderMgr = { - searchOrders: sinon.stub() - .returns({}) -}; - -var almaPaymentHelper = { - capturePayment: sinon.stub() -}; - -function proxyModel() { - return proxyquire('../../../cartridges/int_alma/cartridge/scripts/steps/CheckDeferredCapture', { - 'dw/order/OrderMgr': OrderMgr, - '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper - }); -} - -module.exports = { - CheckDeferredCapture: proxyModel(), - OrderMgr: OrderMgr, - almaPaymentHelper: almaPaymentHelper -}; diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 87045a9..86ab995 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -48,7 +48,7 @@ describe('almaPaymentHelper', function () { describe('Capture endpoint', function () { it('Capture endpoint is call with the Alma payment external_id', function () { - setHttpReturnStatusCode(201); + setHttpReturnStatusCode('OK'); var params = { external_id: 'payment_12345' }; assert.doesNotThrow(function () { almaPaymentHelper.capturePayment(params); diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js new file mode 100644 index 0000000..9a0d45b --- /dev/null +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -0,0 +1,105 @@ +// CapturePaymentOrders.js unit tests +var sinon = require('sinon'); + +var OrderMgr = require('../../../../mocks/steps/CapturePaymentOrders').OrderMgr; +var almaPaymentHelper = require('../../../../mocks/steps/CapturePaymentOrders').almaPaymentHelper; +var CapturePaymentOrders = require('../../../../mocks/steps/CapturePaymentOrders').CapturePaymentOrders; +var almaOrderHelper = require('../../../../mocks/steps/CapturePaymentOrders').almaOrderHelper; +var warnStub = require('../../../../mocks/steps/CapturePaymentOrders').warnStub; + +function hasNextFactory(count) { + var hasNext = sinon.stub(); + + for (var i = 0; i <= count; i++) { + if (i === count) { + hasNext.onCall(i) + .returns(false); + } else { + hasNext.onCall(i) + .returns(true); + } + } + return hasNext; +} + +function nextFactory(count) { + var next = sinon.stub(); + + for (var i = 0; i <= count; i++) { + next.onCall(i) + .returns({ + custom: { + almaPaymentId: 'payment_' + i + } + }); + } + return next; +} + +function mockOrderFactory(count) { + return { + count: count, + hasNext: hasNextFactory(count), + next: nextFactory(count) + }; +} + + +describe('Deferred capture job', function () { + beforeEach(function () { + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(1)); + almaPaymentHelper.capturePayment = sinon.stub() + .returns({ amount: 10000, id: 'capture_id' }); + }); + + afterEach(function () { + sinon.reset(); + }); + + it('Should get orders with deferred capture', function () { + CapturePaymentOrders.execute(); + + sinon.assert.calledOnce(OrderMgr.searchOrders); + sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='toBeCaptured'", null); + }); + + it('Should not call Capture where their is no order', function () { + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(0)); + + CapturePaymentOrders.execute(); + + sinon.assert.notCalled(almaPaymentHelper.capturePayment); + }); + + it('Should call Capture for orders with deferred capture', function () { + var count = 10; + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(count)); + + CapturePaymentOrders.execute(); + + sinon.assert.callCount(almaPaymentHelper.capturePayment, count); + for (var i = 0; i < count; i++) { + sinon.assert.calledWith(almaPaymentHelper.capturePayment.getCall(i), { external_id: 'payment_' + i }); + } + }); + + it('Should set capture ID when capture is validated', function () { + CapturePaymentOrders.execute(); + + sinon.assert.calledOnce(almaOrderHelper.setAlmaDeferredCapture); + }); + + it('Should not call setAlmaDeferredCapture when capture throw an error', function () { + almaPaymentHelper.capturePayment = sinon.stub() + .throws(); + + CapturePaymentOrders.execute(); + + sinon.assert.calledOnce(warnStub); + sinon.assert.notCalled(almaOrderHelper.setAlmaDeferredCapture); + }); +}); + diff --git a/test/unit/int_alma/scripts/steps/CheckDeferredCaptureTest.js b/test/unit/int_alma/scripts/steps/CheckDeferredCaptureTest.js deleted file mode 100644 index d55adf8..0000000 --- a/test/unit/int_alma/scripts/steps/CheckDeferredCaptureTest.js +++ /dev/null @@ -1,43 +0,0 @@ -// CheckDeferredCapture.js unit tests - -var OrderMgr = require('../../../../mocks/steps/CheckDeferredCaptures').OrderMgr; -var almaPaymentHelper = require('../../../../mocks/steps/CheckDeferredCaptures').almaPaymentHelper; -var assert = require('chai').assert; -var checkDeferredCapture = require('../../../../mocks/steps/CheckDeferredCaptures').CheckDeferredCapture; - -var sinon = require('sinon'); - -var order = { - almaPaymentId: 'payment_12345' -}; - -var hasNext = sinon.stub(); -hasNext.onCall(0) - .returns(true); -hasNext.onCall(1) - .returns(false); - -var orders = { - count: 1, - hasNext: hasNext, - next: function () { - return order; - } -}; - - -describe('Deferred capture job', function () { - it('Should get orders with deferred capture', function () { - checkDeferredCapture.execute(); - assert.isTrue(OrderMgr.searchOrders.calledOnce); - assert.isTrue(OrderMgr.searchOrders.calledWith("custom.ALMA_Deferred_Capture='true'", null)); - }); - - it('Should call Capture for orders with deferred capture', function () { - OrderMgr.searchOrders = sinon.stub().returns(orders); - checkDeferredCapture.execute(); - var params = { external_id: 'payment_12345' }; - assert.isTrue(almaPaymentHelper.capturePayment.calledOnce); - assert.isTrue(almaPaymentHelper.capturePayment.calledWith(params)); - }); -}); From e89ad6f4b8e6b142f709d694a61418fbc95c69a9 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Thu, 3 Aug 2023 23:35:49 +0200 Subject: [PATCH 16/66] fix: test and mock --- .../cartridge/scripts/helpers/almaPaymentHelper.js | 1 - .../cartridge/scripts/steps/CapturePaymentOrders.js | 7 +++---- test/mocks/helpers/almaPaymentHelpers.js | 2 +- test/mocks/steps/CapturePaymentOrders.js | 8 ++------ .../int_alma/scripts/helpers/almaPaymentHelperTest.js | 4 +--- 5 files changed, 7 insertions(+), 15 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 5bfd847..3c42c40 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -294,7 +294,6 @@ function capturePayment(params) { var service = require('*/cartridge/scripts/services/alma'); var httpResult = service.captures() .call(params); - if (httpResult.status !== 'OK') { var e = new Error('API error : ' + httpResult.status); e.name = 'capture_payment_error'; diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index dac37b2..790658e 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -4,8 +4,7 @@ var OrderMgr = require('dw/order/OrderMgr'); 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') - .getLogger('alma'); +var Logger = require('dw/system/Logger'); exports.execute = function () { var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='toBeCaptured'", null); @@ -19,9 +18,9 @@ exports.execute = function () { try { var capture = almaPaymentHelper.capturePayment(params); almaOrderHelper.setAlmaDeferredCapture(order, capture.id); - logger.warn('Capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); + Logger.info('Capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); } catch (e) { - logger.warn('Unable to capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); + Logger.warn('Unable to capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); errors.push(e); } } diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js index 0481b4a..3b68d29 100644 --- a/test/mocks/helpers/almaPaymentHelpers.js +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -18,7 +18,7 @@ function setHttpReturnStatusCode(statusCode) { status: statusCode, getObject: function () { return { - text: 'object test' + text: '{"amount":10000, "id":"1234567890"}' }; } }); diff --git a/test/mocks/steps/CapturePaymentOrders.js b/test/mocks/steps/CapturePaymentOrders.js index 5ca5ea7..34cb63a 100644 --- a/test/mocks/steps/CapturePaymentOrders.js +++ b/test/mocks/steps/CapturePaymentOrders.js @@ -22,12 +22,8 @@ var almaOrderHelper = { var warnStub = sinon.stub(); var logger = { - getLogger: function () { - return { - warn: warnStub, - info: function () {} - }; - } + warn: warnStub, + info: function () {} }; function proxyModel() { diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 86ab995..7ccd373 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -50,9 +50,7 @@ describe('almaPaymentHelper', function () { it('Capture endpoint is call with the Alma payment external_id', function () { setHttpReturnStatusCode('OK'); var params = { external_id: 'payment_12345' }; - assert.doesNotThrow(function () { - almaPaymentHelper.capturePayment(params); - }); + assert.deepEqual(almaPaymentHelper.capturePayment(params), { amount: 10000, id: '1234567890' }); assert.isTrue(service.captures().call.calledOnce); assert.isTrue(service.captures() .call From 3ac2f0c37670d8e68d9e29246bd1b7e3843a43ef Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Thu, 3 Aug 2023 23:59:33 +0200 Subject: [PATCH 17/66] feat: add status filter for searchOrders --- .../int_alma/cartridge/scripts/steps/CapturePaymentOrders.js | 2 +- test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index 790658e..d0f1a79 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -7,7 +7,7 @@ var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper') var Logger = require('dw/system/Logger'); exports.execute = function () { - var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='toBeCaptured'", null); + var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='toBeCaptured' and status != 8 and status != 6", null); var errors = []; if (orders.count > 0) { diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js index 9a0d45b..a55aa16 100644 --- a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -61,7 +61,8 @@ describe('Deferred capture job', function () { CapturePaymentOrders.execute(); sinon.assert.calledOnce(OrderMgr.searchOrders); - sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='toBeCaptured'", null); + // status : 8 failed - 6 canceled + sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='toBeCaptured' and status != 8 and status != 6", null); }); it('Should not call Capture where their is no order', function () { From 84edb1c1a68dcfe87e35e0e44abf1d5342a420a7 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Fri, 4 Aug 2023 11:22:49 +0200 Subject: [PATCH 18/66] typo: to capture flag --- .../int_alma/cartridge/scripts/helpers/almaOrderHelper.js | 2 +- .../int_alma/cartridge/scripts/steps/CapturePaymentOrders.js | 2 +- test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js index 9f681a5..9d8ef8d 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js @@ -48,7 +48,7 @@ function setAlmaDeferredCapture(order, deferredCapture) { function addAlmaDataToOrder(pid, order, isDeferredCapture) { addPidToOrder(order, pid); if (isDeferredCapture) { - setAlmaDeferredCapture(order, 'toBeCaptured'); + setAlmaDeferredCapture(order, 'toCapture'); } } diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index d0f1a79..4df53c1 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -7,7 +7,7 @@ var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper') var Logger = require('dw/system/Logger'); exports.execute = function () { - var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='toBeCaptured' and status != 8 and status != 6", null); + var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='toCapture' and status != 8 and status != 6", null); var errors = []; if (orders.count > 0) { diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js index a55aa16..ea55968 100644 --- a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -62,7 +62,7 @@ describe('Deferred capture job', function () { sinon.assert.calledOnce(OrderMgr.searchOrders); // status : 8 failed - 6 canceled - sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='toBeCaptured' and status != 8 and status != 6", null); + sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='toCapture' and status != 8 and status != 6", null); }); it('Should not call Capture where their is no order', function () { From e3c070ad3a2c37a3471e5fa3ad6eb9eabc373374 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Fri, 4 Aug 2023 12:39:09 +0200 Subject: [PATCH 19/66] feat: capture flag externally managed --- site_preference_builder/ref/system-objecttype-extensions.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index 10fe149..6b34c8d 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -17,7 +17,7 @@ string false - false + true @@ -109,7 +109,7 @@ - + From c41b158873a53845aef1d49aad6d485566c5fb85 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Fri, 4 Aug 2023 15:19:07 +0200 Subject: [PATCH 20/66] feat: capture flag name --- site_preference_builder/ref/system-objecttype-extensions.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index 6b34c8d..a0aaea8 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -15,6 +15,7 @@ 0 + Capture ID string false true @@ -109,7 +110,7 @@ - + From 91700b8e4edcda56b509b85665b65d1b7e537520 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Fri, 4 Aug 2023 16:27:02 +0200 Subject: [PATCH 21/66] feat: Change capture wording and add constant --- .../cartridge/scripts/helpers/almaOrderHelper.js | 2 ++ .../cartridge/scripts/steps/CapturePaymentOrders.js | 12 +++++++++--- .../ref/system-objecttype-extensions.xml | 2 +- test/mocks/steps/CapturePaymentOrders.js | 5 +++++ .../int_alma/scripts/helpers/almaOrderHelperTest.js | 11 ++++------- .../scripts/steps/CapturePaymentOrdersTest.js | 11 ++++++++++- 6 files changed, 31 insertions(+), 12 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js index 9d8ef8d..f030713 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js @@ -49,6 +49,8 @@ function addAlmaDataToOrder(pid, order, isDeferredCapture) { addPidToOrder(order, pid); if (isDeferredCapture) { setAlmaDeferredCapture(order, 'toCapture'); + } else { + setAlmaDeferredCapture(order, 'AutoCapture'); } } diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index 4df53c1..f409ded 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -1,13 +1,19 @@ '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'); exports.execute = function () { - var orders = OrderMgr.searchOrders("custom.ALMA_Deferred_Capture='toCapture' and status != 8 and status != 6", null); + var orders = OrderMgr.searchOrders( + "custom.ALMA_Deferred_Capture='toCapture' and status != {0} and status != {1}", + null, + Order.ORDER_STATUS_FAILED, + Order.ORDER_STATUS_CANCELLED + ); var errors = []; if (orders.count > 0) { @@ -17,8 +23,8 @@ exports.execute = function () { try { var capture = almaPaymentHelper.capturePayment(params); - almaOrderHelper.setAlmaDeferredCapture(order, capture.id); - Logger.info('Capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); + almaOrderHelper.setAlmaDeferredCapture(order, 'Captured'); + Logger.info('Capture payment: order id: {0} - payment id: {1} - capture id : {2}', [order.orderNo, order.custom.almaPaymentId, capture.id]); } catch (e) { Logger.warn('Unable to capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); errors.push(e); diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index a0aaea8..71e4b15 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -15,7 +15,7 @@ 0 - Capture ID + Capture Status string false true diff --git a/test/mocks/steps/CapturePaymentOrders.js b/test/mocks/steps/CapturePaymentOrders.js index 34cb63a..06166a0 100644 --- a/test/mocks/steps/CapturePaymentOrders.js +++ b/test/mocks/steps/CapturePaymentOrders.js @@ -25,10 +25,15 @@ var logger = { warn: warnStub, info: function () {} }; +var Order = { + ORDER_STATUS_FAILED: 8, + ORDER_STATUS_CANCELLED: 6 +}; function proxyModel() { return proxyquire('../../../cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders', { 'dw/order/OrderMgr': OrderMgr, + 'dw/order/Order': Order, 'dw/system/Logger': logger, 'dw/system/Status': sinon.mock(), '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper, diff --git a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js index 6c5c6ed..043ece4 100644 --- a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js @@ -3,6 +3,7 @@ // almaOrderHelper unit tests var assert = require('chai').assert; +var sinon = require('sinon'); var almaOrderHelper = require('../../../../mocks/helpers/almaOrderHelpers').almaOrderHelpers; var transaction = require('../../../../mocks/helpers/almaOrderHelpers').transaction; var createNewTransaction = require('../../../../mocks/helpers/almaOrderHelpers').createNewTransaction; @@ -19,14 +20,10 @@ describe('Alma order helper', function () { almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, true); }); }); - it('For a non deferred capture payment transaction is called once', function () { - createNewTransaction(); - almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, false); - assert.isTrue(transaction.wrap.calledOnce); - }); - it('For a deferred capture payment transaction is called twice', function () { + + it('For payment transaction.wrap is called twice first for payment ID Second for Deferred Status', function () { createNewTransaction(); almaOrderHelper.addAlmaDataToOrder('payment_fake_id', order, true); - assert.isTrue(transaction.wrap.calledTwice); + sinon.assert.calledTwice(transaction.wrap); }); }); diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js index ea55968..4ce334f 100644 --- a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -62,7 +62,7 @@ describe('Deferred capture job', function () { sinon.assert.calledOnce(OrderMgr.searchOrders); // status : 8 failed - 6 canceled - sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='toCapture' and status != 8 and status != 6", null); + sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='toCapture' and status != {0} and status != {1}", null, 8, 6); }); it('Should not call Capture where their is no order', function () { @@ -91,6 +91,15 @@ describe('Deferred capture job', function () { CapturePaymentOrders.execute(); sinon.assert.calledOnce(almaOrderHelper.setAlmaDeferredCapture); + sinon.assert.calledWith( + almaOrderHelper.setAlmaDeferredCapture, + { + custom: { + almaPaymentId: 'payment_0' + } + }, + 'Captured' + ); }); it('Should not call setAlmaDeferredCapture when capture throw an error', function () { From 4a3fc259997f1c1906cb6184047abbb499b0fda0 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 7 Aug 2023 13:45:38 +0200 Subject: [PATCH 22/66] test: add test to refund job before deferred payment. --- test/mocks/steps/CheckRefundMocks.js | 39 +++++ .../int_alma/scripts/steps/CheckRefundTest.js | 134 ++++++++++++++++++ 2 files changed, 173 insertions(+) create mode 100644 test/mocks/steps/CheckRefundMocks.js create mode 100644 test/unit/int_alma/scripts/steps/CheckRefundTest.js diff --git a/test/mocks/steps/CheckRefundMocks.js b/test/mocks/steps/CheckRefundMocks.js new file mode 100644 index 0000000..45e2525 --- /dev/null +++ b/test/mocks/steps/CheckRefundMocks.js @@ -0,0 +1,39 @@ +var proxyquire = require('proxyquire') + .noCallThru() + .noPreserveCache(); + +var sinon = require('sinon'); + +var OrderMgr = { + searchOrders: sinon.stub() + .returns({}) +}; + +var warnStub = sinon.stub(); +var logger = { + warn: warnStub, + info: function () {} +}; + +var refundHelper = { + refundPaymentForOrder: sinon.stub() +}; + + +function proxyModel() { + return proxyquire( + '../../../cartridges/int_alma/cartridge/scripts/steps/CheckRefund', + { + 'dw/system/Logger': logger, + 'dw/system/Status': sinon.mock(), + 'dw/order/OrderMgr': OrderMgr, + '*/cartridge/scripts/helpers/almaRefundHelper': refundHelper + } + ); +} + +module.exports = { + CheckRefund: proxyModel(), + OrderMgr: OrderMgr, + refundHelper: refundHelper +}; diff --git a/test/unit/int_alma/scripts/steps/CheckRefundTest.js b/test/unit/int_alma/scripts/steps/CheckRefundTest.js new file mode 100644 index 0000000..9f405ac --- /dev/null +++ b/test/unit/int_alma/scripts/steps/CheckRefundTest.js @@ -0,0 +1,134 @@ +var sinon = require('sinon'); + +var OrderMgr = require('../../../../mocks/steps/CheckRefundMocks').OrderMgr; +var CheckRefund = require('../../../../mocks/steps/CheckRefundMocks').CheckRefund; +var refundHelper = require('../../../../mocks/steps/CheckRefundMocks').refundHelper; + +function orderFactory(count, refundType, partialRefundAmount) { + return { + custom: { + almaPaymentId: 'payment_' + count, + almaRefundType: { + toString: function () { + return refundType; + } + }, + almaWantedRefundAmount: partialRefundAmount + }, + totalGrossPrice: { + value: 10000 + } + }; +} +function hasNextFactory(count) { + var hasNext = sinon.stub(); + + for (var i = 0; i <= count; i++) { + if (i === count) { + hasNext.onCall(i) + .returns(false); + } else { + hasNext.onCall(i) + .returns(true); + } + } + return hasNext; +} + +function nextFactory(count, refundType, partialRefundAmount) { + var next = sinon.stub(); + + for (var i = 0; i <= count; i++) { + next.onCall(i) + .returns(orderFactory(i, refundType, partialRefundAmount)); + } + return next; +} + +function mockOrderFactory(count, refundType, partialRefundAmount) { + return { + count: count, + hasNext: hasNextFactory(count), + next: nextFactory(count, refundType, partialRefundAmount) + }; +} +describe('Refund job test', function () { + afterEach(function () { + sinon.reset(); + }); + it('should call refund once per order for Total Refund', function () { + var count = 3; + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(count, 'Total', 10000)); + + CheckRefund.execute(); + sinon.assert.callCount(refundHelper.refundPaymentForOrder, count); + }); + it('should call refund for Total Refund with good params', function () { + var count = 1; + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(count, 'Total')); + + CheckRefund.execute(); + sinon.assert.callCount(refundHelper.refundPaymentForOrder, count); + sinon.assert.calledWith( + refundHelper.refundPaymentForOrder, + sinon.match( + { + custom: { + almaPaymentId: 'payment_0' + }, + totalGrossPrice: { + value: 10000 + } + } + ) + ); + }); + it('should call partial refund with good params', function () { + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(1, 'Partial', 3000)); + + CheckRefund.execute(); + + sinon.assert.calledWith( + refundHelper.refundPaymentForOrder, + sinon.match( + { + custom: { + almaPaymentId: 'payment_0', + almaWantedRefundAmount: 3000 + }, + totalGrossPrice: { + value: 10000 + } + } + ), + 3000 + ); + }); + it('should be not call for a partial refund with negative amount', function () { + var count = 1; + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(count, 'Partial', -10000)); + + CheckRefund.execute(); + sinon.assert.notCalled(refundHelper.refundPaymentForOrder); + }); + it('should be not call for a partial refund with not a valid amount', function () { + var count = 1; + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(count, 'Partial', 'azertyui')); + + CheckRefund.execute(); + sinon.assert.notCalled(refundHelper.refundPaymentForOrder); + }); + it('should be not call for a partial refund with an amount upper than the order amount', function () { + var count = 1; + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(count, 'Partial', 1000000)); + + CheckRefund.execute(); + sinon.assert.notCalled(refundHelper.refundPaymentForOrder); + }); +}); From c888c76f1c8208df66094d1aba9ce29bbc5b5035 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 7 Aug 2023 14:28:36 +0200 Subject: [PATCH 23/66] test: add cancel payment Endpoint --- .../scripts/helpers/almaPaymentHelper.js | 20 +++++++++++++- .../cartridge/scripts/services/alma.js | 27 ++++++++++++++++++- test/mocks/helpers/almaPaymentHelpers.js | 5 ++++ .../scripts/helpers/almaPaymentHelperTest.js | 17 ++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 3c42c40..79f79b1 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -301,6 +301,23 @@ function capturePayment(params) { } return JSON.parse(httpResult.getObject().text); } +/** + * Calls the cancel payment endpoint + * @param {Object} params to give to the payment endpoint + * @returns {Object} api response + * @throws Error + */ +function cancelAlmaPayment(params) { + var service = require('*/cartridge/scripts/services/alma'); + var httpResult = service.cancelAlmaPayment() + .call(params); + if (httpResult.status !== 'OK') { + var e = new Error('API error : ' + httpResult.status); + e.name = 'cancel_payment_error'; + throw e; + } + return JSON.parse(httpResult.getObject().text); +} /** * Check if manual capture is available @@ -441,5 +458,6 @@ module.exports = { createOrderFromBasketUUID: createOrderFromBasketUUID, setOrderMerchantReference: setOrderMerchantReference, capturePayment: capturePayment, - isAvailableForManualCapture: isAvailableForManualCapture + isAvailableForManualCapture: isAvailableForManualCapture, + cancelAlmaPayment: cancelAlmaPayment }; diff --git a/cartridges/int_alma/cartridge/scripts/services/alma.js b/cartridges/int_alma/cartridge/scripts/services/alma.js index b4b672f..f7d6732 100644 --- a/cartridges/int_alma/cartridge/scripts/services/alma.js +++ b/cartridges/int_alma/cartridge/scripts/services/alma.js @@ -190,6 +190,30 @@ function captures() { }); } +/** + * Cancel an alma payment + * @return {dw.svc.Service} service instances + */ +function cancelAlmaPayment() { + return LocalServiceRegistry.createService('alma', { + /** + * @param {dw.svc.HTTPService} service service + * @param {array} params parameters + * @returns {string} json parameters as string + */ + createRequest: function (service, params) { + service.setRequestMethod('PUT'); + service.URL = almaHelpers.getUrl('/v1/payments/' + params.external_id + '/cancel'); // eslint-disable-line no-param-reassign + almaHelpers.addHeaders(service); + + return JSON.stringify(params); + }, + parseResponse: function (svc, client) { + return client; + } + }); +} + module.exports = { getPaymentDetails: getPaymentDetails, checkEligibility: checkEligibility, @@ -198,5 +222,6 @@ module.exports = { createPayment: createPayment, potentialFraud: flagAsPotentialFraud, setOrderMerchantReferenceAPI: setOrderMerchantReferenceAPI, - captures: captures + captures: captures, + cancelAlmaPayment: cancelAlmaPayment }; diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js index 3b68d29..c4b46dc 100644 --- a/test/mocks/helpers/almaPaymentHelpers.js +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -29,6 +29,11 @@ var service = { return { call: call }; + }, + cancelAlmaPayment: function () { + return { + call: call + }; } }; diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 7ccd373..909e9f9 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -3,6 +3,7 @@ // almaPayment.js unit tests var assert = require('chai').assert; +var sinon = require('sinon'); var almaPaymentHelper = require('../../../../mocks/helpers/almaPaymentHelpers').proxyModel; var resolvedPaymentData = require('../../../../mocks/helpers/almaPaymentHelpers').resolvedPaymentData; var service = require('../../../../mocks/helpers/almaPaymentHelpers').service; @@ -65,4 +66,20 @@ describe('almaPaymentHelper', function () { }); }); }); + describe('Cancel Payment', function () { + it('should call cancel service with good params', function () { + setHttpReturnStatusCode('OK'); + var params = { external_id: 'payment_12345' }; + almaPaymentHelper.cancelAlmaPayment(params); + sinon.assert.calledOnce(service.cancelAlmaPayment().call); + sinon.assert.calledWith(service.cancelAlmaPayment().call, params); + }); + it('Cancel endpoint throw an error if http status code not equal 204', function () { + setHttpReturnStatusCode(400); + var params = { external_id: 'payment_12345' }; + assert.throws(function () { + almaPaymentHelper.cancelAlmaPayment(params); + }); + }); + }); }); From 6eecfe1b8da6798f4af15b39dd3a182313b14b19 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 7 Aug 2023 15:18:29 +0200 Subject: [PATCH 24/66] feat: cancel and refund for deferred payment --- .../cartridge/scripts/steps/CheckRefund.js | 8 +++ test/mocks/steps/CheckRefundMocks.js | 10 ++- .../int_alma/scripts/steps/CheckRefundTest.js | 67 +++++++++++++++---- 3 files changed, 69 insertions(+), 16 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index 7329f60..312219a 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -42,6 +42,7 @@ function refundPaymentForOrder(order) { exports.execute = function () { var Logger = require('dw/system/Logger'); var Status = require('dw/system/Status'); + var AlmaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var orders = getOrdersRefunded(); var errors = []; @@ -51,6 +52,13 @@ exports.execute = function () { var orderItem = orders.next(); if (isOrderToBeRefund(orderItem)) { try { + if (orderItem.custom.ALMA_Deferred_Capture === 'ToCapture' && orderItem.custom.almaRefundType.toString() === 'Total') { + var params = { external_id: orderItem.custom.almaPaymentId }; + AlmaPaymentHelper.cancelAlmaPayment(params); + } + if (orderItem.custom.ALMA_Deferred_Capture === 'ToCapture' && orderItem.custom.almaRefundType.toString() === 'Partial') { + Logger.warn('Partial refund is not yet implemented with deferred payment - order id {0}', [orderItem.currentOrderNo()]); + } refundPaymentForOrder(orderItem); } catch (e) { Logger.error('[ERROR][ALMA refund] : ' + e); diff --git a/test/mocks/steps/CheckRefundMocks.js b/test/mocks/steps/CheckRefundMocks.js index 45e2525..79ab164 100644 --- a/test/mocks/steps/CheckRefundMocks.js +++ b/test/mocks/steps/CheckRefundMocks.js @@ -19,6 +19,9 @@ var refundHelper = { refundPaymentForOrder: sinon.stub() }; +var almaPaymentHelper = { + cancelAlmaPayment: sinon.stub() +}; function proxyModel() { return proxyquire( @@ -27,7 +30,8 @@ function proxyModel() { 'dw/system/Logger': logger, 'dw/system/Status': sinon.mock(), 'dw/order/OrderMgr': OrderMgr, - '*/cartridge/scripts/helpers/almaRefundHelper': refundHelper + '*/cartridge/scripts/helpers/almaRefundHelper': refundHelper, + '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper } ); } @@ -35,5 +39,7 @@ function proxyModel() { module.exports = { CheckRefund: proxyModel(), OrderMgr: OrderMgr, - refundHelper: refundHelper + refundHelper: refundHelper, + almaPaymentHelper: almaPaymentHelper, + logger: logger }; diff --git a/test/unit/int_alma/scripts/steps/CheckRefundTest.js b/test/unit/int_alma/scripts/steps/CheckRefundTest.js index 9f405ac..85d86d4 100644 --- a/test/unit/int_alma/scripts/steps/CheckRefundTest.js +++ b/test/unit/int_alma/scripts/steps/CheckRefundTest.js @@ -3,8 +3,10 @@ var sinon = require('sinon'); var OrderMgr = require('../../../../mocks/steps/CheckRefundMocks').OrderMgr; var CheckRefund = require('../../../../mocks/steps/CheckRefundMocks').CheckRefund; var refundHelper = require('../../../../mocks/steps/CheckRefundMocks').refundHelper; +var almaPaymentHelper = require('../../../../mocks/steps/CheckRefundMocks').almaPaymentHelper; +var logger = require('../../../../mocks/steps/CheckRefundMocks').logger; -function orderFactory(count, refundType, partialRefundAmount) { +function orderFactory(count, refundType, partialRefundAmount, capture) { return { custom: { almaPaymentId: 'payment_' + count, @@ -13,10 +15,14 @@ function orderFactory(count, refundType, partialRefundAmount) { return refundType; } }, - almaWantedRefundAmount: partialRefundAmount + almaWantedRefundAmount: partialRefundAmount, + ALMA_Deferred_Capture: capture }, totalGrossPrice: { value: 10000 + }, + currentOrderNo: function () { + return 'order_id'; } }; } @@ -35,21 +41,21 @@ function hasNextFactory(count) { return hasNext; } -function nextFactory(count, refundType, partialRefundAmount) { +function nextFactory(count, refundType, partialRefundAmount, capture) { var next = sinon.stub(); for (var i = 0; i <= count; i++) { next.onCall(i) - .returns(orderFactory(i, refundType, partialRefundAmount)); + .returns(orderFactory(i, refundType, partialRefundAmount, capture)); } return next; } -function mockOrderFactory(count, refundType, partialRefundAmount) { +function mockOrderFactory(count, refundType, partialRefundAmount, capture) { return { count: count, hasNext: hasNextFactory(count), - next: nextFactory(count, refundType, partialRefundAmount) + next: nextFactory(count, refundType, partialRefundAmount, capture) }; } describe('Refund job test', function () { @@ -59,18 +65,17 @@ describe('Refund job test', function () { it('should call refund once per order for Total Refund', function () { var count = 3; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Total', 10000)); + .returns(mockOrderFactory(count, 'Total', 10000, 'AutoCapture')); CheckRefund.execute(); sinon.assert.callCount(refundHelper.refundPaymentForOrder, count); }); it('should call refund for Total Refund with good params', function () { - var count = 1; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Total')); + .returns(mockOrderFactory(1, 'Total', null, 'AutoCapture')); CheckRefund.execute(); - sinon.assert.callCount(refundHelper.refundPaymentForOrder, count); + sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); sinon.assert.calledWith( refundHelper.refundPaymentForOrder, sinon.match( @@ -87,7 +92,7 @@ describe('Refund job test', function () { }); it('should call partial refund with good params', function () { OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(1, 'Partial', 3000)); + .returns(mockOrderFactory(1, 'Partial', 3000, 'AutoCapture')); CheckRefund.execute(); @@ -110,7 +115,7 @@ describe('Refund job test', function () { it('should be not call for a partial refund with negative amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Partial', -10000)); + .returns(mockOrderFactory(count, 'Partial', -10000, 'AutoCapture')); CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); @@ -118,7 +123,7 @@ describe('Refund job test', function () { it('should be not call for a partial refund with not a valid amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Partial', 'azertyui')); + .returns(mockOrderFactory(count, 'Partial', 'azertyui', 'AutoCapture')); CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); @@ -126,9 +131,43 @@ describe('Refund job test', function () { it('should be not call for a partial refund with an amount upper than the order amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Partial', 1000000)); + .returns(mockOrderFactory(count, 'Partial', 1000000, 'AutoCapture')); CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); }); + describe('Deferred payment', function () { + it('should call refund for an order whose payment is in auto capture', function () { + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(1, 'Total', null, 'AutoCapture')); + CheckRefund.execute(); + sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); + }); + it('should call refund for an order whose payment is already captured for a total refund', function () { + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(1, 'Total', null, 'Captured')); + CheckRefund.execute(); + sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); + }); + it('should call refund for an order whose payment is already captured for a partial refund', function () { + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(1, 'Partial', 3000, 'Captured')); + CheckRefund.execute(); + sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); + }); + it('should call cancel for an order whose payment is ToCapture for a total refund', function () { + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(1, 'Total', null, 'ToCapture')); + CheckRefund.execute(); + sinon.assert.calledOnce(almaPaymentHelper.cancelAlmaPayment); + sinon.assert.calledWith(almaPaymentHelper.cancelAlmaPayment, { external_id: 'payment_0' }); + }); + it('should not call cancel for an order whose payment is toCapture for a Partial refund and write a error log', function () { + OrderMgr.searchOrders = sinon.stub() + .returns(mockOrderFactory(1, 'Partial', 3000, 'ToCapture')); + CheckRefund.execute(); + sinon.assert.notCalled(almaPaymentHelper.cancelAlmaPayment); + sinon.assert.calledWith(logger.warn, 'Partial refund is not yet implemented with deferred payment - order id {0}', ['order_id']); + }); + }); }); From 3110a78650b2449ac074fdccd9282faf6bcd6547 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 7 Aug 2023 16:03:19 +0200 Subject: [PATCH 25/66] feat: reset refund admin for after cancel job --- .../cartridge/scripts/steps/CheckRefund.js | 30 +++++++++++++++---- test/mocks/steps/CheckRefundMocks.js | 14 +++++---- .../int_alma/scripts/steps/CheckRefundTest.js | 8 ++++- 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index 312219a..5bbfa3e 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -42,6 +42,7 @@ function refundPaymentForOrder(order) { 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(); var errors = []; @@ -52,12 +53,29 @@ exports.execute = function () { var orderItem = orders.next(); if (isOrderToBeRefund(orderItem)) { try { - if (orderItem.custom.ALMA_Deferred_Capture === 'ToCapture' && orderItem.custom.almaRefundType.toString() === 'Total') { - var params = { external_id: orderItem.custom.almaPaymentId }; - AlmaPaymentHelper.cancelAlmaPayment(params); - } - if (orderItem.custom.ALMA_Deferred_Capture === 'ToCapture' && orderItem.custom.almaRefundType.toString() === 'Partial') { - Logger.warn('Partial refund is not yet implemented with deferred payment - order id {0}', [orderItem.currentOrderNo()]); + if (orderItem.custom.ALMA_Deferred_Capture === 'ToCapture') { + var amount = 0; + + if (orderItem.custom.almaRefundType.toString() === 'Total') { + var params = { external_id: orderItem.custom.almaPaymentId }; + amount = orderItem.getTotalGrossPrice(); + + AlmaPaymentHelper.cancelAlmaPayment(params); + } + + if (orderItem.custom.almaRefundType.toString() === 'Partial') { + Logger.info('Partial refund is not yet implemented with deferred payment - order id {0}', [orderItem.currentOrderNo()]); + } + + // eslint-disable-next-line no-loop-func + Transaction.wrap(function () { + // 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; + }); } refundPaymentForOrder(orderItem); } catch (e) { diff --git a/test/mocks/steps/CheckRefundMocks.js b/test/mocks/steps/CheckRefundMocks.js index 79ab164..cc017e0 100644 --- a/test/mocks/steps/CheckRefundMocks.js +++ b/test/mocks/steps/CheckRefundMocks.js @@ -9,10 +9,8 @@ var OrderMgr = { .returns({}) }; -var warnStub = sinon.stub(); var logger = { - warn: warnStub, - info: function () {} + info: sinon.stub() }; var refundHelper = { @@ -23,6 +21,10 @@ var almaPaymentHelper = { cancelAlmaPayment: sinon.stub() }; +var transaction = { + wrap: sinon.stub() +}; + function proxyModel() { return proxyquire( '../../../cartridges/int_alma/cartridge/scripts/steps/CheckRefund', @@ -31,7 +33,8 @@ function proxyModel() { 'dw/system/Status': sinon.mock(), 'dw/order/OrderMgr': OrderMgr, '*/cartridge/scripts/helpers/almaRefundHelper': refundHelper, - '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper + '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper, + 'dw/system/Transaction': transaction } ); } @@ -41,5 +44,6 @@ module.exports = { OrderMgr: OrderMgr, refundHelper: refundHelper, almaPaymentHelper: almaPaymentHelper, - logger: logger + logger: logger, + transaction: transaction }; diff --git a/test/unit/int_alma/scripts/steps/CheckRefundTest.js b/test/unit/int_alma/scripts/steps/CheckRefundTest.js index 85d86d4..83544f9 100644 --- a/test/unit/int_alma/scripts/steps/CheckRefundTest.js +++ b/test/unit/int_alma/scripts/steps/CheckRefundTest.js @@ -5,6 +5,7 @@ var CheckRefund = require('../../../../mocks/steps/CheckRefundMocks').CheckRefun var refundHelper = require('../../../../mocks/steps/CheckRefundMocks').refundHelper; var almaPaymentHelper = require('../../../../mocks/steps/CheckRefundMocks').almaPaymentHelper; var logger = require('../../../../mocks/steps/CheckRefundMocks').logger; +var transaction = require('../../../../mocks/steps/CheckRefundMocks').transaction; function orderFactory(count, refundType, partialRefundAmount, capture) { return { @@ -23,6 +24,9 @@ function orderFactory(count, refundType, partialRefundAmount, capture) { }, currentOrderNo: function () { return 'order_id'; + }, + getTotalGrossPrice: function () { + return 10000; } }; } @@ -161,13 +165,15 @@ describe('Refund job test', function () { CheckRefund.execute(); sinon.assert.calledOnce(almaPaymentHelper.cancelAlmaPayment); sinon.assert.calledWith(almaPaymentHelper.cancelAlmaPayment, { external_id: 'payment_0' }); + sinon.assert.calledOnce(transaction.wrap); }); it('should not call cancel for an order whose payment is toCapture for a Partial refund and write a error log', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Partial', 3000, 'ToCapture')); CheckRefund.execute(); sinon.assert.notCalled(almaPaymentHelper.cancelAlmaPayment); - sinon.assert.calledWith(logger.warn, 'Partial refund is not yet implemented with deferred payment - order id {0}', ['order_id']); + sinon.assert.calledWith(logger.info, 'Partial refund is not yet implemented with deferred payment - order id {0}', ['order_id']); + sinon.assert.calledOnce(transaction.wrap); }); }); }); From b175babf2628fa143a00abad177756a16ecbce31 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 7 Aug 2023 16:49:09 +0200 Subject: [PATCH 26/66] feat: add else for capture --- .../int_alma/cartridge/scripts/steps/CheckRefund.js | 7 ++++--- test/unit/int_alma/scripts/steps/CheckRefundTest.js | 8 +++----- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index 5bbfa3e..e4d36ff 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -53,7 +53,7 @@ exports.execute = function () { var orderItem = orders.next(); if (isOrderToBeRefund(orderItem)) { try { - if (orderItem.custom.ALMA_Deferred_Capture === 'ToCapture') { + if (orderItem.custom.ALMA_Deferred_Capture === 'toCapture') { var amount = 0; if (orderItem.custom.almaRefundType.toString() === 'Total') { @@ -64,7 +64,7 @@ exports.execute = function () { } if (orderItem.custom.almaRefundType.toString() === 'Partial') { - Logger.info('Partial refund is not yet implemented with deferred payment - order id {0}', [orderItem.currentOrderNo()]); + Logger.info('Partial refund is not yet implemented with deferred payment - order id {0}', [orderItem.orderNo]); } // eslint-disable-next-line no-loop-func @@ -76,8 +76,9 @@ exports.execute = function () { // eslint-disable-next-line no-param-reassign orderItem.custom.almaRefundType = null; }); + } else { + refundPaymentForOrder(orderItem); } - refundPaymentForOrder(orderItem); } catch (e) { Logger.error('[ERROR][ALMA refund] : ' + e); errors.push(e); diff --git a/test/unit/int_alma/scripts/steps/CheckRefundTest.js b/test/unit/int_alma/scripts/steps/CheckRefundTest.js index 83544f9..72ed24d 100644 --- a/test/unit/int_alma/scripts/steps/CheckRefundTest.js +++ b/test/unit/int_alma/scripts/steps/CheckRefundTest.js @@ -22,9 +22,7 @@ function orderFactory(count, refundType, partialRefundAmount, capture) { totalGrossPrice: { value: 10000 }, - currentOrderNo: function () { - return 'order_id'; - }, + orderNo: 'order_id', getTotalGrossPrice: function () { return 10000; } @@ -161,7 +159,7 @@ describe('Refund job test', function () { }); it('should call cancel for an order whose payment is ToCapture for a total refund', function () { OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(1, 'Total', null, 'ToCapture')); + .returns(mockOrderFactory(1, 'Total', null, 'toCapture')); CheckRefund.execute(); sinon.assert.calledOnce(almaPaymentHelper.cancelAlmaPayment); sinon.assert.calledWith(almaPaymentHelper.cancelAlmaPayment, { external_id: 'payment_0' }); @@ -169,7 +167,7 @@ describe('Refund job test', function () { }); it('should not call cancel for an order whose payment is toCapture for a Partial refund and write a error log', function () { OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(1, 'Partial', 3000, 'ToCapture')); + .returns(mockOrderFactory(1, 'Partial', 3000, 'toCapture')); CheckRefund.execute(); sinon.assert.notCalled(almaPaymentHelper.cancelAlmaPayment); sinon.assert.calledWith(logger.info, 'Partial refund is not yet implemented with deferred payment - order id {0}', ['order_id']); From 752e658151502192ae6a0ca9d560e9ab67375d0c Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 7 Aug 2023 17:25:07 +0200 Subject: [PATCH 27/66] feat: Add no call refund in test for cancel --- test/unit/int_alma/scripts/steps/CheckRefundTest.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/unit/int_alma/scripts/steps/CheckRefundTest.js b/test/unit/int_alma/scripts/steps/CheckRefundTest.js index 72ed24d..b37f488 100644 --- a/test/unit/int_alma/scripts/steps/CheckRefundTest.js +++ b/test/unit/int_alma/scripts/steps/CheckRefundTest.js @@ -161,6 +161,7 @@ describe('Refund job test', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Total', null, 'toCapture')); CheckRefund.execute(); + sinon.assert.notCalled(refundHelper.refundPaymentForOrder); sinon.assert.calledOnce(almaPaymentHelper.cancelAlmaPayment); sinon.assert.calledWith(almaPaymentHelper.cancelAlmaPayment, { external_id: 'payment_0' }); sinon.assert.calledOnce(transaction.wrap); @@ -169,6 +170,7 @@ describe('Refund job test', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Partial', 3000, 'toCapture')); CheckRefund.execute(); + sinon.assert.notCalled(refundHelper.refundPaymentForOrder); sinon.assert.notCalled(almaPaymentHelper.cancelAlmaPayment); sinon.assert.calledWith(logger.info, 'Partial refund is not yet implemented with deferred payment - order id {0}', ['order_id']); sinon.assert.calledOnce(transaction.wrap); From f6819ebe06cb58a2df1836e6e70b6f4be5745d9d Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 7 Aug 2023 17:25:27 +0200 Subject: [PATCH 28/66] feat: change deferred status after cancel --- cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index e4d36ff..3da0463 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -55,12 +55,12 @@ exports.execute = function () { try { if (orderItem.custom.ALMA_Deferred_Capture === 'toCapture') { var amount = 0; - + var deferredStatus = 'toCapture'; if (orderItem.custom.almaRefundType.toString() === 'Total') { var params = { external_id: orderItem.custom.almaPaymentId }; - amount = orderItem.getTotalGrossPrice(); - + amount = orderItem.getTotalGrossPrice().value; AlmaPaymentHelper.cancelAlmaPayment(params); + deferredStatus = 'Canceled'; } if (orderItem.custom.almaRefundType.toString() === 'Partial') { @@ -69,6 +69,7 @@ exports.execute = function () { // eslint-disable-next-line no-loop-func Transaction.wrap(function () { + orderItem.custom.ALMA_Deferred_Capture = deferredStatus; // eslint-disable-next-line no-param-reassign orderItem.custom.almaRefundedAmount = amount; // eslint-disable-next-line no-param-reassign From 78fbf7fdf35f64302156fd51a906308593625eb1 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 8 Aug 2023 14:25:51 +0200 Subject: [PATCH 29/66] feat:mpp-523 change BO alma deferred capture value --- .../scripts/helpers/almaOrderHelper.js | 4 ++-- .../scripts/steps/CapturePaymentOrders.js | 9 ++------- .../cartridge/scripts/steps/CheckRefund.js | 4 ++-- .../ref/jobs/jobDeferredCapture.xml | 16 --------------- .../ref/system-objecttype-extensions.xml | 15 ++++++++++++-- .../scripts/steps/CapturePaymentOrdersTest.js | 12 +++++++++-- .../int_alma/scripts/steps/CheckRefundTest.js | 20 +++++++++---------- 7 files changed, 39 insertions(+), 41 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js index f030713..6659cef 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js @@ -48,9 +48,9 @@ function setAlmaDeferredCapture(order, deferredCapture) { function addAlmaDataToOrder(pid, order, isDeferredCapture) { addPidToOrder(order, pid); if (isDeferredCapture) { - setAlmaDeferredCapture(order, 'toCapture'); + setAlmaDeferredCapture(order, 'ToCapture'); } else { - setAlmaDeferredCapture(order, 'AutoCapture'); + setAlmaDeferredCapture(order, 'Captured'); } } diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index f409ded..9797d60 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -9,12 +9,11 @@ var Logger = require('dw/system/Logger'); exports.execute = function () { var orders = OrderMgr.searchOrders( - "custom.ALMA_Deferred_Capture='toCapture' and status != {0} and status != {1}", + "custom.ALMA_Deferred_Capture='ToCapture' and status != {0} and status != {1}", null, Order.ORDER_STATUS_FAILED, Order.ORDER_STATUS_CANCELLED ); - var errors = []; if (orders.count > 0) { while (orders.hasNext()) { @@ -26,15 +25,11 @@ exports.execute = function () { almaOrderHelper.setAlmaDeferredCapture(order, 'Captured'); Logger.info('Capture payment: order id: {0} - payment id: {1} - capture id : {2}', [order.orderNo, order.custom.almaPaymentId, capture.id]); } catch (e) { + almaOrderHelper.setAlmaDeferredCapture(order, 'Failed'); Logger.warn('Unable to capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); - errors.push(e); } } } - if (errors.length > 0) { - return new Status(Status.ERROR); - } - return new Status(Status.OK); }; diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index 3da0463..6f96dba 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -53,9 +53,9 @@ exports.execute = function () { var orderItem = orders.next(); if (isOrderToBeRefund(orderItem)) { try { - if (orderItem.custom.ALMA_Deferred_Capture === 'toCapture') { + if (orderItem.custom.ALMA_Deferred_Capture === 'ToCapture') { var amount = 0; - var deferredStatus = 'toCapture'; + var deferredStatus = 'ToCapture'; if (orderItem.custom.almaRefundType.toString() === 'Total') { var params = { external_id: orderItem.custom.almaPaymentId }; amount = orderItem.getTotalGrossPrice().value; diff --git a/site_preference_builder/ref/jobs/jobDeferredCapture.xml b/site_preference_builder/ref/jobs/jobDeferredCapture.xml index 5f721a9..736da52 100644 --- a/site_preference_builder/ref/jobs/jobDeferredCapture.xml +++ b/site_preference_builder/ref/jobs/jobDeferredCapture.xml @@ -19,21 +19,5 @@ - - - 2021-11-24Z - 22:36:18.000Z - 5m - - Thursday - Wednesday - Friday - Sunday - Tuesday - Monday - Saturday - - - diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index 71e4b15..ca1c9d4 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -16,9 +16,20 @@ Capture Status - string + enum-of-string false - true + false + + + ToCapture + + + Captured + + + Failed + + diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js index 4ce334f..44345b7 100644 --- a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -62,7 +62,7 @@ describe('Deferred capture job', function () { sinon.assert.calledOnce(OrderMgr.searchOrders); // status : 8 failed - 6 canceled - sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='toCapture' and status != {0} and status != {1}", null, 8, 6); + sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='ToCapture' and status != {0} and status != {1}", null, 8, 6); }); it('Should not call Capture where their is no order', function () { @@ -109,7 +109,15 @@ describe('Deferred capture job', function () { CapturePaymentOrders.execute(); sinon.assert.calledOnce(warnStub); - sinon.assert.notCalled(almaOrderHelper.setAlmaDeferredCapture); + sinon.assert.calledWith( + almaOrderHelper.setAlmaDeferredCapture, + { + custom: { + almaPaymentId: 'payment_0' + } + }, + 'Failed' + ); }); }); diff --git a/test/unit/int_alma/scripts/steps/CheckRefundTest.js b/test/unit/int_alma/scripts/steps/CheckRefundTest.js index b37f488..fa626db 100644 --- a/test/unit/int_alma/scripts/steps/CheckRefundTest.js +++ b/test/unit/int_alma/scripts/steps/CheckRefundTest.js @@ -67,14 +67,14 @@ describe('Refund job test', function () { it('should call refund once per order for Total Refund', function () { var count = 3; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Total', 10000, 'AutoCapture')); + .returns(mockOrderFactory(count, 'Total', 10000, 'Captured')); CheckRefund.execute(); sinon.assert.callCount(refundHelper.refundPaymentForOrder, count); }); it('should call refund for Total Refund with good params', function () { OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(1, 'Total', null, 'AutoCapture')); + .returns(mockOrderFactory(1, 'Total', null, 'Captured')); CheckRefund.execute(); sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); @@ -94,7 +94,7 @@ describe('Refund job test', function () { }); it('should call partial refund with good params', function () { OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(1, 'Partial', 3000, 'AutoCapture')); + .returns(mockOrderFactory(1, 'Partial', 3000, 'Captured')); CheckRefund.execute(); @@ -117,7 +117,7 @@ describe('Refund job test', function () { it('should be not call for a partial refund with negative amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Partial', -10000, 'AutoCapture')); + .returns(mockOrderFactory(count, 'Partial', -10000, 'Captured')); CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); @@ -125,7 +125,7 @@ describe('Refund job test', function () { it('should be not call for a partial refund with not a valid amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Partial', 'azertyui', 'AutoCapture')); + .returns(mockOrderFactory(count, 'Partial', 'azertyui', 'Captured')); CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); @@ -133,7 +133,7 @@ describe('Refund job test', function () { it('should be not call for a partial refund with an amount upper than the order amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(count, 'Partial', 1000000, 'AutoCapture')); + .returns(mockOrderFactory(count, 'Partial', 1000000, 'Captured')); CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); @@ -141,7 +141,7 @@ describe('Refund job test', function () { describe('Deferred payment', function () { it('should call refund for an order whose payment is in auto capture', function () { OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(1, 'Total', null, 'AutoCapture')); + .returns(mockOrderFactory(1, 'Total', null, 'Captured')); CheckRefund.execute(); sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); }); @@ -159,16 +159,16 @@ describe('Refund job test', function () { }); it('should call cancel for an order whose payment is ToCapture for a total refund', function () { OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(1, 'Total', null, 'toCapture')); + .returns(mockOrderFactory(1, 'Total', null, 'ToCapture')); CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); sinon.assert.calledOnce(almaPaymentHelper.cancelAlmaPayment); sinon.assert.calledWith(almaPaymentHelper.cancelAlmaPayment, { external_id: 'payment_0' }); sinon.assert.calledOnce(transaction.wrap); }); - it('should not call cancel for an order whose payment is toCapture for a Partial refund and write a error log', function () { + it('should not call cancel for an order whose payment is ToCapture for a Partial refund and write a error log', function () { OrderMgr.searchOrders = sinon.stub() - .returns(mockOrderFactory(1, 'Partial', 3000, 'toCapture')); + .returns(mockOrderFactory(1, 'Partial', 3000, 'ToCapture')); CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); sinon.assert.notCalled(almaPaymentHelper.cancelAlmaPayment); From a0e7d04b4b0d02a52fc112662dca2f1c742c2fd6 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 8 Aug 2023 15:12:34 +0200 Subject: [PATCH 30/66] feat:mpp-523 disable error on refund job --- cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index 6f96dba..095869f 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -45,7 +45,6 @@ exports.execute = function () { var Transaction = require('dw/system/Transaction'); var AlmaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var orders = getOrdersRefunded(); - var errors = []; Logger.info('[INFO][ALMA refund] job launched for: ' + orders.count + ' orders.'); if (orders.count > 0) { @@ -82,15 +81,10 @@ exports.execute = function () { } } catch (e) { Logger.error('[ERROR][ALMA refund] : ' + e); - errors.push(e); } } } } - if (errors.length > 0) { - return new Status(Status.ERROR); - } - return new Status(Status.OK); }; From 4dde3687cbf339ac62c98cc4b56892c5f18b72ac Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 17 Aug 2023 10:53:21 +0200 Subject: [PATCH 31/66] 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 c733262..3742e24 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 79f79b1..e0987cc 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 909e9f9..afb1aec 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 0f7b5d3196e97405f8444224ea8e7a2c9b9574a4 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Fri, 18 Aug 2023 15:33:58 +0200 Subject: [PATCH 32/66] add pre-commit config file --- .pre-commit-config.yaml | 98 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..85974fb --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,98 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: check-added-large-files + args: ["--maxkb=1024"] + - id: detect-private-key + - id: check-merge-conflict +# - id: end-of-file-fixer +# exclude: | +# (?x)( +# ^static/vendors| +# .min.js$| +# .po$| +# .md$| +# .drawio.svg$| +# ^tmp/| +# ^.test_durations$ +# ) +# - id: trailing-whitespace +# args: ["--markdown-linebreak-ext=md"] +# +# - repo: local +# hooks: +# - id: check-po +# name: Check .po files for errors +# language: python +# entry: python ./tools/check_po.py +# types: [text, pofile] +# exclude: | +# (?x)( +# /fr/| +# /pt_PT/ +# ) +# stages: [commit] +# additional_dependencies: [Babel] + + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v3.0.0-alpha.4 + hooks: + - id: prettier + files: "(^.*\\.js$)" + + - repo: https://github.com/pre-commit/mirrors-eslint + rev: v8.29.0 + hooks: + - id: eslint + files: "(^.*\\.js$)"# *.js, *.jsx, *.ts and *.tsx + types: [file] + args: + - --fix + + - repo: https://github.com/thibaudcolas/pre-commit-stylelint + rev: v14.16.0 + hooks: + - id: stylelint + files: "(^cartridges/int_alma/cartridge/static/default/css/.*\\.css$)" + args: + - --config + - dashboard/frontend/stylelint.config.cjs + additional_dependencies: + - stylelint@15.10.2 + - prettier@2.8.8 + - stylelint-config-standard@32.0.0 + - stylelint-prettier@3.0.0 + +# - repo: https://github.com/returntocorp/semgrep +# rev: v1.27.0 +# hooks: +# - id: semgrep +# args: +# - --error +# - --config +# - semgrep/rules/ +# - --metrics +# - "off" +# - --quiet +# - --disable-version-check +# - --skip-unknown-extensions +# pass_filenames: false # Passing files explicitely override semgremignore + + - repo: https://github.com/alma/pre-commit-hooks + rev: 1.1.2 + hooks: + - id: check-branch-name + args: + - "-r^((chore|ci|dependabot|devx|docs|feature|fix|hotfix|hotfix-backport|infra|other|perf|refactor|security|test)\/.+|(snyk)-.+|main|HEAD)$$" +# +# - repo: local +# hooks: +# - id: forbid-translation-changes +# name: Forbit manual changes to translation files +# language: python +# entry: python ./tools/forbid_translation_changes.py +# files: "(^.*\\.(mo|po|pot)$)" + From fd365633bc81b2308b85792a5c6ab218f3791074 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Fri, 18 Aug 2023 16:59:20 +0200 Subject: [PATCH 33/66] add jshint to pre-commit config file --- .jshintrc | 3 ++ .pre-commit-config.yaml | 94 +++++++++++++---------------------------- 2 files changed, 32 insertions(+), 65 deletions(-) create mode 100644 .jshintrc diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..80fc4c0 --- /dev/null +++ b/.jshintrc @@ -0,0 +1,3 @@ +{ + "esversion": 8 +} \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 85974fb..44833dd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,64 +8,37 @@ repos: args: ["--maxkb=1024"] - id: detect-private-key - id: check-merge-conflict -# - id: end-of-file-fixer -# exclude: | -# (?x)( -# ^static/vendors| -# .min.js$| -# .po$| -# .md$| -# .drawio.svg$| -# ^tmp/| -# ^.test_durations$ -# ) -# - id: trailing-whitespace -# args: ["--markdown-linebreak-ext=md"] +### +# - repo: https://github.com/pre-commit/mirrors-jshint +# rev: v2.13.6 +# hooks: +# - id: jshint +# files: "(^.*\\.js$)" # -# - repo: local +# - repo: https://github.com/pre-commit/mirrors-eslint +# rev: v8.29.0 # hooks: -# - id: check-po -# name: Check .po files for errors -# language: python -# entry: python ./tools/check_po.py -# types: [text, pofile] -# exclude: | -# (?x)( -# /fr/| -# /pt_PT/ -# ) -# stages: [commit] -# additional_dependencies: [Babel] - - - repo: https://github.com/pre-commit/mirrors-prettier - rev: v3.0.0-alpha.4 - hooks: - - id: prettier - files: "(^.*\\.js$)" - - - repo: https://github.com/pre-commit/mirrors-eslint - rev: v8.29.0 - hooks: - - id: eslint - files: "(^.*\\.js$)"# *.js, *.jsx, *.ts and *.tsx - types: [file] - args: - - --fix - - - repo: https://github.com/thibaudcolas/pre-commit-stylelint - rev: v14.16.0 - hooks: - - id: stylelint - files: "(^cartridges/int_alma/cartridge/static/default/css/.*\\.css$)" - args: - - --config - - dashboard/frontend/stylelint.config.cjs - additional_dependencies: - - stylelint@15.10.2 - - prettier@2.8.8 - - stylelint-config-standard@32.0.0 - - stylelint-prettier@3.0.0 - +# - id: eslint +# files: "(^cartridges/int_alma/cartridge/.*\\.js$)"# *.js, *.jsx, *.ts and *.tsx +# types: [file] +# args: +# - --fix +# +# - repo: https://github.com/thibaudcolas/pre-commit-stylelint +# rev: v14.16.0 +# hooks: +# - id: stylelint +# files: "(^cartridges/int_alma/cartridge/static/default/css/.*\\.css$)" +# args: +# - --config +# - .stylelintrc.json +# additional_dependencies: +# - stylelint@15.10.2 +# - prettier@2.8.8 +# - stylelint-config-standard@32.0.0 +# - stylelint-prettier@3.0.0 +### + # - repo: https://github.com/returntocorp/semgrep # rev: v1.27.0 # hooks: @@ -87,12 +60,3 @@ repos: - id: check-branch-name args: - "-r^((chore|ci|dependabot|devx|docs|feature|fix|hotfix|hotfix-backport|infra|other|perf|refactor|security|test)\/.+|(snyk)-.+|main|HEAD)$$" -# -# - repo: local -# hooks: -# - id: forbid-translation-changes -# name: Forbit manual changes to translation files -# language: python -# entry: python ./tools/forbid_translation_changes.py -# files: "(^.*\\.(mo|po|pot)$)" - From a5931ddea19874af066f8d23ddfd536f8f6f7325 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 1 Aug 2023 14:41:43 +0200 Subject: [PATCH 34/66] feat:mpp-486 deferred capture refacto + tests --- .../scripts/helpers/almaCheckoutHelper.js | 22 +++++-------------- .../scripts/helpers/almaConfigHelper.js | 11 +++++++++- .../scripts/helpers/almaPaymentHelper.js | 3 ++- test/mocks/helpers/almaCheckoutHelpers.js | 8 ++++++- test/mocks/helpers/almaConfigHelpers.js | 13 ++++++++--- test/mocks/helpers/almaPaymentHelpers.js | 15 +++++++++++-- .../scripts/helpers/almaCheckoutHelperTest.js | 4 +++- .../scripts/helpers/almaConfigHelperTest.js | 3 +++ .../scripts/helpers/almaPaymentHelperTest.js | 12 ++++++++-- 9 files changed, 64 insertions(+), 27 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js index f6a432c..03641ab 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js @@ -4,6 +4,7 @@ var Resource = require('dw/web/Resource'); var formatCurrency = require('*/cartridge/scripts/util/formatting').formatCurrency; var isOnShipmentPaymentEnabled = require('*/cartridge/scripts/helpers/almaOnShipmentHelper').isOnShipmentPaymentEnabled; var PaymentMgr = require('dw/order/PaymentMgr'); +var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); var ALMA_PNX_ID = 'ALMA_PNX'; var ALMA_CREDIT_ID = 'ALMA_CREDIT'; @@ -166,16 +167,6 @@ function isAvailableForInpage(installmentsCount, deferredDays) { return installmentsCount <= 4 && deferredDays <= 0; } -/** - * Returns true if the merchant want in-page payment - * @returns {boolean} if we can use inpage - */ -function isInpageActivated() { - var Site = require('dw/system/Site'); - - return Site.getCurrent().getCustomPreferenceValue('ALMA_Inpage_Payment'); -} - /** * Return true if plan is activated * @param {Object} paymentMethod payment method @@ -224,7 +215,7 @@ function formatPlanForCheckout(plan, currencyCode) { var formatPlan = {}; if (plan.installments_count < 5 && planIsActivated(PaymentMgr.getPaymentMethod(ALMA_PNX_ID), plan)) { formatPlan = { - in_page: isAvailableForInpage(plan.installments_count, plan.deferred_days) && isInpageActivated(), + in_page: isAvailableForInpage(plan.installments_count, plan.deferred_days) && almaConfigHelper.isInpageActivated(), selector: getSelectorNameFromPlan(plan), installments_count: plan.installments_count, deferred_days: plan.deferred_days, @@ -237,7 +228,7 @@ function formatPlanForCheckout(plan, currencyCode) { } if (plan.installments_count >= 5 && planIsActivated(PaymentMgr.getPaymentMethod(ALMA_CREDIT_ID), plan)) { formatPlan = { - in_page: isAvailableForInpage(plan.installments_count, plan.deferred_days) && isInpageActivated(), + in_page: isAvailableForInpage(plan.installments_count, plan.deferred_days) && almaConfigHelper.isInpageActivated(), selector: getSelectorNameFromPlan(plan), installments_count: plan.installments_count, deferred_days: plan.deferred_days, @@ -250,7 +241,7 @@ function formatPlanForCheckout(plan, currencyCode) { } if (plan.deferred_days > 0 && planIsActivated(PaymentMgr.getPaymentMethod(ALMA_DEFERRED_ID), plan)) { formatPlan = { - in_page: isAvailableForInpage(plan.installments_count, plan.deferred_days) && isInpageActivated(), + in_page: isAvailableForInpage(plan.installments_count, plan.deferred_days) && almaConfigHelper.isInpageActivated(), selector: getSelectorNameFromPlan(plan), installments_count: plan.installments_count, deferred_days: plan.deferred_days, @@ -263,7 +254,7 @@ function formatPlanForCheckout(plan, currencyCode) { } if (plan.installments_count === 1 && plan.deferred_days === 0 && planIsActivated(PaymentMgr.getPaymentMethod(ALMA_PAY_NOW_ID), plan)) { formatPlan = { - in_page: isAvailableForInpage(plan.installments_count, plan.deferred_days) && isInpageActivated(), + in_page: isAvailableForInpage(plan.installments_count, plan.deferred_days) && almaConfigHelper.isInpageActivated(), selector: getSelectorNameFromPlan(plan), installments_count: plan.installments_count, deferred_days: plan.deferred_days, @@ -280,6 +271,5 @@ function formatPlanForCheckout(plan, currencyCode) { module.exports = { formatPlanForCheckout: formatPlanForCheckout, getPlanPaymentMethodID: getPlanPaymentMethodID, - isAvailableForInpage: isAvailableForInpage, - isInpageActivated: isInpageActivated + isAvailableForInpage: isAvailableForInpage }; diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js index f54ed41..d0aad0c 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js @@ -9,6 +9,15 @@ function isDeferredCaptureEnable() { return Site.getCurrent().getCustomPreferenceValue('ALMA_Deferred_Capture_Activation'); } +/** + * Returns true if the merchant want in-page payment + * @returns {boolean} if we can use inpage + */ +function isInpageActivated() { + return Site.getCurrent().getCustomPreferenceValue('ALMA_Inpage_Payment'); +} + module.exports = { - isDeferredCaptureEnable: isDeferredCaptureEnable + isDeferredCaptureEnable: isDeferredCaptureEnable, + isInpageActivated: isInpageActivated }; diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 79f79b1..72ba8fc 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -343,13 +343,14 @@ function buildPaymentData(installmentsCount, deferredDays, locale, isManualCaptu var URLUtils = require('dw/web/URLUtils'); var almaHelper = require('*/cartridge/scripts/helpers/almaHelpers'); var almaCheckoutHelper = require('*/cartridge/scripts/helpers/almaCheckoutHelper'); + var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); var formatAddress = require('*/cartridge/scripts/helpers/almaAddressHelper').formatAddress; var isOnShipmentPaymentEnabled = require('*/cartridge/scripts/helpers/almaOnShipmentHelper').isOnShipmentPaymentEnabled; var formatCustomerData = require('*/cartridge/scripts/helpers/almaHelpers').formatCustomerData; var origin = 'online'; - if (almaCheckoutHelper.isAvailableForInpage(installmentsCount, deferredDays) && almaCheckoutHelper.isInpageActivated()) { + if (almaCheckoutHelper.isAvailableForInpage(installmentsCount, deferredDays) && almaConfigHelper.isInpageActivated()) { origin = 'online_in_page'; } diff --git a/test/mocks/helpers/almaCheckoutHelpers.js b/test/mocks/helpers/almaCheckoutHelpers.js index 0edd76c..3952c20 100644 --- a/test/mocks/helpers/almaCheckoutHelpers.js +++ b/test/mocks/helpers/almaCheckoutHelpers.js @@ -2,6 +2,8 @@ var proxyquire = require('proxyquire').noCallThru().noPreserveCache(); +var almaConfigHelpers = require('./almaConfigHelpers').almaConfigHelpers; +var setCustomPreferenceValue = require('./almaConfigHelpers').setCustomPreferenceValue; var resource = { msg: function (param1) { @@ -62,8 +64,12 @@ function proxyModel() { } }, 'dw/system/Site': site, + '*/cartridge/scripts/helpers/almaConfigHelper': almaConfigHelpers, 'dw/order/PaymentMgr': paymentMgr }); } -module.exports = proxyModel(); +module.exports = { + almaCheckoutHelpers: proxyModel(), + setCustomPreferenceValue: setCustomPreferenceValue +}; diff --git a/test/mocks/helpers/almaConfigHelpers.js b/test/mocks/helpers/almaConfigHelpers.js index 494a06b..fdf17f5 100644 --- a/test/mocks/helpers/almaConfigHelpers.js +++ b/test/mocks/helpers/almaConfigHelpers.js @@ -1,9 +1,14 @@ 'use strict'; -var proxyquire = require('proxyquire').noCallThru().noPreserveCache(); +var proxyquire = require('proxyquire') + .noCallThru() + .noPreserveCache(); var sinon = require('sinon'); -var getCustomPreferenceValue = sinon.stub().returns(false); +var getCustomPreferenceValue; +function setCustomPreferenceValue(value) { + getCustomPreferenceValue = sinon.stub().returns(value); +} var site = { @@ -13,6 +18,7 @@ var site = { }; } }; + function proxyModel() { return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper', { 'dw/system/Site': site @@ -21,5 +27,6 @@ function proxyModel() { module.exports = { almaConfigHelpers: proxyModel(), - site: site + site: site, + setCustomPreferenceValue: setCustomPreferenceValue }; diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js index c4b46dc..3bcd1ef 100644 --- a/test/mocks/helpers/almaPaymentHelpers.js +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -9,8 +9,11 @@ var almaHelper = require('../helpers/almaHelpers'); var almaCheckoutHelper = require('../helpers/almaCheckoutHelpers'); var almaAddressHelper = require('../helpers/almaAddressHelper'); var almaOnShipmentHelper = require('../helpers/almaOnShipmentHelpers'); +var almaConfigHelpers = require('./almaConfigHelpers').almaConfigHelpers; + const sinon = require('sinon'); var call; +var isAvailableForInpage; function setHttpReturnStatusCode(statusCode) { call = sinon.stub() @@ -37,6 +40,12 @@ var service = { } }; + +function setIsAvailableForInpage(value) { + isAvailableForInpage = value; +} + + function proxyModel() { return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper', { 'dw/order/BasketMgr': BasketMgr, @@ -45,7 +54,8 @@ function proxyModel() { '*/cartridge/scripts/helpers/almaCheckoutHelper': almaCheckoutHelper, '*/cartridge/scripts/helpers/almaAddressHelper': almaAddressHelper, '*/cartridge/scripts/helpers/almaOnShipmentHelper': almaOnShipmentHelper, - '*/cartridge/scripts/services/alma': service + '*/cartridge/scripts/services/alma': service, + '*/cartridge/scripts/helpers/almaConfigHelper': almaConfigHelpers }); } @@ -116,5 +126,6 @@ module.exports = { proxyModel: proxyModel(), resolvedPaymentData: resolvedPaymentData, service: service, - setHttpReturnStatusCode: setHttpReturnStatusCode + setHttpReturnStatusCode: setHttpReturnStatusCode, + setIsAvailableForInpage: setIsAvailableForInpage }; diff --git a/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js b/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js index e5abd96..bc45635 100644 --- a/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js @@ -2,7 +2,8 @@ var assert = require('chai').assert; -var almaCheckoutHelpers = require('../../../../mocks/helpers/almaCheckoutHelpers'); +var almaCheckoutHelpers = require('../../../../mocks/helpers/almaCheckoutHelpers').almaCheckoutHelpers; +var setCustomPreferenceValue = require('../../../../mocks/helpers/almaCheckoutHelpers').setCustomPreferenceValue; var plan = { installments_count: 3, @@ -75,6 +76,7 @@ var currencyCode = 'EUR'; describe('almaCheckoutHelpers', function () { describe('formatPlanForCheckout', function () { it('check in page', function () { + setCustomPreferenceValue(true); var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plan, currencyCode); assert.equal(checkoutData.in_page, true); diff --git a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js index 29e8547..1733761 100644 --- a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js @@ -5,16 +5,19 @@ var assert = require('chai').assert; var site = require('../../../../mocks/helpers/almaConfigHelpers').site; var almaConfigHelpers = require('../../../../mocks/helpers/almaConfigHelpers').almaConfigHelpers; +var setCustomPreferenceValue = require('../../../../mocks/helpers/almaConfigHelpers').setCustomPreferenceValue; describe('Get back-office variables', function () { describe('Deferred Capture', function () { it('should call site preferences with deferred capture key', function () { + setCustomPreferenceValue(true); almaConfigHelpers.isDeferredCaptureEnable(); assert.isTrue(site.getCurrent().getCustomPreferenceValue.calledOnce); assert.isTrue(site.getCurrent().getCustomPreferenceValue.calledWith('ALMA_Deferred_Capture_Activation')); }); it('should call site preferences and return default value', function () { + setCustomPreferenceValue(false); assert.equal(false, almaConfigHelpers.isDeferredCaptureEnable()); }); }); diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 909e9f9..2733d58 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -8,18 +8,26 @@ var almaPaymentHelper = require('../../../../mocks/helpers/almaPaymentHelpers'). var resolvedPaymentData = require('../../../../mocks/helpers/almaPaymentHelpers').resolvedPaymentData; var service = require('../../../../mocks/helpers/almaPaymentHelpers').service; var setHttpReturnStatusCode = require('../../../../mocks/helpers/almaPaymentHelpers').setHttpReturnStatusCode; +var setIsAvailableForInpage = require('../../../../mocks/helpers/almaPaymentHelpers').setIsAvailableForInpage; +var setCustomPreferenceValue = require('../../../../mocks/helpers/almaConfigHelpers').setCustomPreferenceValue; describe('almaPaymentHelper', function () { describe('Build payment data', function () { it('payment data for pnx', function () { + setIsAvailableForInpage(true); + setCustomPreferenceValue(true); + var payment = almaPaymentHelper.buildPaymentData(3, 0, 'fr_FR', false); assert.deepEqual(payment, resolvedPaymentData(3, 0, 'fr_FR', 'online_in_page')); }); it('payment data for deferred', function () { + setIsAvailableForInpage(false); var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', false); assert.deepEqual(payment, resolvedPaymentData(1, 15, 'fr_FR', 'online')); }); it('payment data for credit has car property', function () { + setIsAvailableForInpage(false); + var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', false); assert.deepEqual(payment, resolvedPaymentData(12, 0, 'fr_FR', 'online', true)); }); @@ -34,7 +42,7 @@ describe('almaPaymentHelper', function () { assert.property(payment.payment, 'capture_method'); }); it('Payment data for credit has no capture method in', function () { - var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', false); + var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', true); assert.notProperty(payment.payment, 'capture_method'); }); it('Payment data for pay now has capture method', function () { @@ -42,7 +50,7 @@ describe('almaPaymentHelper', function () { assert.property(payment.payment, 'capture_method'); }); it('Payment data for pay later has no capture method', function () { - var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', false); + var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', true); assert.notProperty(payment.payment, 'capture_method'); }); }); From 62dd6a490a25df0d4ccf42781a3fe68fc5acdded Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 1 Aug 2023 15:18:32 +0200 Subject: [PATCH 35/66] feat:mpp-486 deferred capture frontend wording --- .../scripts/helpers/almaCheckoutHelper.js | 23 +++++++--- .../templates/resources/alma.properties | 1 + .../templates/resources/alma_de_DE.properties | 2 + .../templates/resources/alma_es_ES.properties | 2 + .../templates/resources/alma_fr_FR.properties | 2 + .../templates/resources/alma_it_IT.properties | 4 +- .../templates/resources/alma_nl_NL.properties | 2 + .../templates/resources/alma_pt_PT.properties | 2 + test/mocks/helpers/almaCheckoutHelpers.js | 20 +++++++-- .../scripts/helpers/almaCheckoutHelperTest.js | 42 ++++++++++++++++++- .../scripts/helpers/almaConfigHelperTest.js | 4 +- 11 files changed, 92 insertions(+), 12 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js index 03641ab..a50bad6 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js @@ -5,6 +5,7 @@ var formatCurrency = require('*/cartridge/scripts/util/formatting').formatCurren var isOnShipmentPaymentEnabled = require('*/cartridge/scripts/helpers/almaOnShipmentHelper').isOnShipmentPaymentEnabled; var PaymentMgr = require('dw/order/PaymentMgr'); var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); +var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); var ALMA_PNX_ID = 'ALMA_PNX'; var ALMA_CREDIT_ID = 'ALMA_CREDIT'; @@ -23,7 +24,7 @@ function getSelectorNameFromPlan(plan) { + plan.installments_count + '_' // by how many days is the payment defered + plan.deferred_days - ; + ; } /** @@ -71,7 +72,8 @@ function getCreditInfo(plan, currencyCode) { var costOfCredit = formatCurrency(plan.customer_total_cost_amount / 100, currencyCode); var purchaseAmount = formatCurrency(plan.purchaseAmount, currencyCode); var totalCost = formatCurrency(plan.purchaseAmount + (plan.customer_total_cost_amount / 100), currencyCode); - var rate = Math.round(plan.annual_interest_rate / 100).toString() + '.' + (plan.annual_interest_rate % 100) + '%'; + var rate = Math.round(plan.annual_interest_rate / 100) + .toString() + '.' + (plan.annual_interest_rate % 100) + '%'; return { basket_cost: Resource.msgf('alma.credit.basket_cost', 'alma', null, purchaseAmount), amount: Resource.msgf('alma.credit.cost_of_credit', 'alma', null, costOfCredit), @@ -130,14 +132,22 @@ function getPaymentInstallments(plan, currencyCode) { Resource.msg(getPropertyCategory(plan) + '.installments.onshipment', 'alma', null) + ' ' + getInstallmentCountAfterFirst(plan) + formatCurrency(plan.payment_plan[1].purchase_amount / 100, currencyCode) - ; + ; + } + // on deferred capture + if (almaPaymentHelper.isAvailableForManualCapture(almaConfigHelper.isDeferredCaptureEnable(), plan.installments_count, plan.deferred_days)) { + return formatCurrency(plan.payment_plan[0].purchase_amount / 100, currencyCode) + ' ' + + Resource.msg(getPropertyCategory(plan) + '.installments.deferred_capture', 'alma', null) + ' ' + + getInstallmentCountAfterFirst(plan) + + formatCurrency(plan.payment_plan[1].purchase_amount / 100, currencyCode) + ; } // installment payment return formatCurrency(plan.payment_plan[0].purchase_amount / 100, currencyCode) + ' ' + Resource.msg(getPropertyCategory(plan) + '.installments', 'alma', null) + ' ' + getInstallmentCountAfterFirst(plan) + formatCurrency(plan.payment_plan[1].purchase_amount / 100, currencyCode) - ; + ; } /** @@ -174,7 +184,10 @@ function isAvailableForInpage(installmentsCount, deferredDays) { * @returns {boolean} payment method ID */ function planIsActivated(paymentMethod, plan) { - var almaActivated = paymentMethod.getCustom().almaActivated.trim().split('|'); + var almaActivated = paymentMethod.getCustom() + .almaActivated + .trim() + .split('|'); return almaActivated.some(function (element) { return element.includes(plan.installments_count) || element.includes(plan.deferred_days); diff --git a/cartridges/int_alma/cartridge/templates/resources/alma.properties b/cartridges/int_alma/cartridge/templates/resources/alma.properties index b55595e..7c2d9c0 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma.properties @@ -31,6 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Pay in {0} installments with credit card alma.pay.in_x_installment.installments=today, then alma.pay.in_x_installment.installments.onshipment=at shipping, then +alma.pay.in_x_installment.installments.deferred_capture=at order processing, then alma.pay.in_x_installment_after_x_days=Pay in {0} installments in {1} days alma.pay.in_x_installment_after_x_days.img=x{0}+{1} diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_de_DE.properties b/cartridges/int_alma/cartridge/templates/resources/alma_de_DE.properties index 11963b9..0dac823 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_de_DE.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_de_DE.properties @@ -31,6 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Zahlung in {0} Raten mit Kreditkarte alma.pay.in_x_installment.installments=heute, dann alma.pay.in_x_installment.installments.onshipment=beim Versand, dann +alma.pay.in_x_installment.installments.deferred_capture=bei der Auftragsabwicklung, dann alma.pay.in_x_installment_after_x_days=Zahlung in {0} Raten in {1} Tagen alma.pay.in_x_installment_after_x_days.img=x{0}+{1} @@ -43,3 +44,4 @@ alma.inpage_on_close_message=Wenn Sie das Alma-Popup schließen, haben Sie den Z alma.pay.now.installments=Heute alma.pay.now=Jetzt mit Kreditkarte bezahlen alma.pay.now.description=Schnelle und sichere Zahlungen + diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_es_ES.properties b/cartridges/int_alma/cartridge/templates/resources/alma_es_ES.properties index 59267fd..775a40e 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_es_ES.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_es_ES.properties @@ -31,6 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Pagar en {0} plazos con tarjeta bancaria alma.pay.in_x_installment.installments=hoy, después alma.pay.in_x_installment.installments.onshipment=al envío, después +alma.pay.in_x_installment.installments.deferred_capture=en la tramitación del pedido, entonces alma.pay.in_x_installment_after_x_days=Pagar en {0} plazos en {1} días alma.pay.in_x_installment_after_x_days.img=x{0}+{1} @@ -43,3 +44,4 @@ alma.inpage_on_close_message=Al cerrar la ventana emergente Alma, ha cancelado e alma.pay.now.installments=Hoy alma.pay.now=Pague ahora con tarjeta de crédito alma.pay.now.description=Pagos rápidos y seguros + diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_fr_FR.properties b/cartridges/int_alma/cartridge/templates/resources/alma_fr_FR.properties index 32fc7ad..a74aa01 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_fr_FR.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_fr_FR.properties @@ -31,6 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Payer en {0} fois par carte de crédit alma.pay.in_x_installment.installments=aujourd'hui, puis alma.pay.in_x_installment.installments.onshipment=à l'expédition puis +alma.pay.in_x_installment.installments.deferred_capture=au traitement de la commande, puis alma.pay.in_x_installment_after_x_days=Payer en {0} fois dans {1} jours alma.pay.in_x_installment_after_x_days.img=x{0}+{1} @@ -43,3 +44,4 @@ alma.inpage_on_close_message=En fermant la popup Alma, vous avez annulé le proc alma.pay.now.installments=Aujourd'hui alma.pay.now=Payer maintenant par carte de crédit alma.pay.now.description=Paiements rapides et sécurisés + diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_it_IT.properties b/cartridges/int_alma/cartridge/templates/resources/alma_it_IT.properties index 3a307bb..ebef5b6 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_it_IT.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_it_IT.properties @@ -31,6 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Paga in {0} rate con carta di credito alma.pay.in_x_installment.installments=oggi, poi alma.pay.in_x_installment.installments.onshipment=alla spedizione, poi +alma.pay.in_x_installment.installments.deferred_capture=al momento dell'elaborazione dell'ordine, poi alma.pay.in_x_installment_after_x_days=Paga in {0} rate tra {1} giorni alma.pay.in_x_installment_after_x_days.img=x{0}+{1} @@ -41,5 +42,6 @@ alma.inpage_on_failure_message=Alma non è in grado di elaborare il pagamento. R alma.inpage_on_close_message=Chiudendo il popup Alma, avete annullato il processo di pagamento. alma.pay.now.installments=Oggi -alma.pay.now=Paga ora con carta di credito +alma.pay.now=Paga ora con la carta alma.pay.now.description=Pagamenti rapidi e sicuri + diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_nl_NL.properties b/cartridges/int_alma/cartridge/templates/resources/alma_nl_NL.properties index 5062308..498435e 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_nl_NL.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_nl_NL.properties @@ -31,6 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Betaal in {0} termijnen met kredietkaart alma.pay.in_x_installment.installments=vandaag, dan alma.pay.in_x_installment.installments.onshipment=bij verzending, daarna +alma.pay.in_x_installment.installments.deferred_capture=bij het verwerken van de bestelling, dan alma.pay.in_x_installment_after_x_days=Betaal in {0} termijnen in {1} dagen alma.pay.in_x_installment_after_x_days.img=x{0}+{1} @@ -43,3 +44,4 @@ alma.inpage_on_close_message=Door de Alma popup te sluiten, heeft u het betaling alma.pay.now.installments=Vandaag alma.pay.now=Betaal nu met creditcard alma.pay.now.description=Snelle en veilige betalingen + diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_pt_PT.properties b/cartridges/int_alma/cartridge/templates/resources/alma_pt_PT.properties index ae681f6..dc725bb 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_pt_PT.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_pt_PT.properties @@ -31,6 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Pagar em {0} prestações com cartão de crédito alma.pay.in_x_installment.installments=hoje, e depois alma.pay.in_x_installment.installments.onshipment=na expedição, e depois +alma.pay.in_x_installment.installments.deferred_capture=no processamento da encomenda, depois alma.pay.in_x_installment_after_x_days=Pagar em {0} prestações em {1} dias alma.pay.in_x_installment_after_x_days.img=x{0}+{1} @@ -43,3 +44,4 @@ alma.inpage_on_close_message=Ao fechar o pop-up Alma, cancelou o processo de pag alma.pay.now.installments=Hoje alma.pay.now=Pagar agora com cartão de crédito alma.pay.now.description=Pagamentos rápidos e seguros + diff --git a/test/mocks/helpers/almaCheckoutHelpers.js b/test/mocks/helpers/almaCheckoutHelpers.js index 3952c20..28e9817 100644 --- a/test/mocks/helpers/almaCheckoutHelpers.js +++ b/test/mocks/helpers/almaCheckoutHelpers.js @@ -1,7 +1,9 @@ 'use strict'; -var proxyquire = require('proxyquire').noCallThru().noPreserveCache(); +var proxyquire = require('proxyquire') + .noCallThru() + .noPreserveCache(); var almaConfigHelpers = require('./almaConfigHelpers').almaConfigHelpers; var setCustomPreferenceValue = require('./almaConfigHelpers').setCustomPreferenceValue; @@ -49,6 +51,12 @@ var paymentMgr = { } }; +var isAvailableForManualCapture; + +function setIsAvailableForManualCapture(value) { + isAvailableForManualCapture = value; +} + function proxyModel() { return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper', { @@ -65,11 +73,17 @@ function proxyModel() { }, 'dw/system/Site': site, '*/cartridge/scripts/helpers/almaConfigHelper': almaConfigHelpers, - 'dw/order/PaymentMgr': paymentMgr + 'dw/order/PaymentMgr': paymentMgr, + '*/cartridge/scripts/helpers/almaPaymentHelper': { + isAvailableForManualCapture: function () { + return isAvailableForManualCapture; + } + } }); } module.exports = { almaCheckoutHelpers: proxyModel(), - setCustomPreferenceValue: setCustomPreferenceValue + setCustomPreferenceValue: setCustomPreferenceValue, + setIsAvailableForManualCapture: setIsAvailableForManualCapture }; diff --git a/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js b/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js index bc45635..aa730ac 100644 --- a/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js @@ -4,6 +4,7 @@ var assert = require('chai').assert; var almaCheckoutHelpers = require('../../../../mocks/helpers/almaCheckoutHelpers').almaCheckoutHelpers; var setCustomPreferenceValue = require('../../../../mocks/helpers/almaCheckoutHelpers').setCustomPreferenceValue; +var setIsAvailableForManualCapture = require('../../../../mocks/helpers/almaCheckoutHelpers').setIsAvailableForManualCapture; var plan = { installments_count: 3, @@ -99,7 +100,8 @@ describe('almaCheckoutHelpers', function () { assert.equal(checkoutData.selector, 'ALMA_general_1_15'); }); - it('check properties', function () { + it('check properties for pnx', function () { + setIsAvailableForManualCapture(true); var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plan, currencyCode); assert.deepEqual(checkoutData.properties, { title: 'alma.pay.in_x_installment', @@ -112,7 +114,43 @@ describe('almaCheckoutHelpers', function () { basket_cost: 'alma.credit.basket_cost', total_cost: 'alma.credit.total_cost' }, - payment_installments: '117 alma.pay.in_x_installment.installments 2x116' + payment_installments: '117 alma.pay.in_x_installment.installments.deferred_capture 2x116' + }); + }); + + it('check properties for credit', function () { + setIsAvailableForManualCapture(false); + var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(planCredit, currencyCode); + assert.deepEqual(checkoutData.properties, { + title: 'alma.pay.in_x_installment', + img: 'alma.pay.in_x_installment.img', + description: 'alma.pay.in_x_installment.description', + fees: 'alma.not_fee', + credit: { + amount: 'alma.credit.cost_of_credit', + rate: 'alma.credit.fixed_apr', + basket_cost: 'alma.credit.basket_cost', + total_cost: 'alma.credit.total_cost' + }, + payment_installments: '60 alma.pay.in_x_installment.installments 5x60' + }); + }); + + it('check properties for deffered', function () { + setIsAvailableForManualCapture(false); + var checkoutData = almaCheckoutHelpers.formatPlanForCheckout(plansDeferred, currencyCode); + assert.deepEqual(checkoutData.properties, { + title: 'alma.pay.after_x_days', + img: 'alma.pay.after_x_days.img', + description: 'alma.pay.after_x_days.description', + fees: 'alma.not_fee', + credit: { + amount: 'alma.credit.cost_of_credit', + rate: 'alma.credit.fixed_apr', + basket_cost: 'alma.credit.basket_cost', + total_cost: 'alma.credit.total_cost' + }, + payment_installments: 'alma.pay.after_x_days.installments' }); }); diff --git a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js index 1733761..5b608ce 100644 --- a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js @@ -14,7 +14,9 @@ describe('Get back-office variables', function () { almaConfigHelpers.isDeferredCaptureEnable(); assert.isTrue(site.getCurrent().getCustomPreferenceValue.calledOnce); - assert.isTrue(site.getCurrent().getCustomPreferenceValue.calledWith('ALMA_Deferred_Capture_Activation')); + assert.isTrue(site.getCurrent() + .getCustomPreferenceValue + .calledWith('ALMA_Deferred_Capture_Activation')); }); it('should call site preferences and return default value', function () { setCustomPreferenceValue(false); From 10d300e1eb7de099fade1c4d6d25a94fe8d10be2 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 4 Sep 2023 12:03:39 +0200 Subject: [PATCH 36/66] fix: unit test on payment data building --- test/mocks/helpers/almaPaymentHelpers.js | 2 +- test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js index 3bcd1ef..cf23410 100644 --- a/test/mocks/helpers/almaPaymentHelpers.js +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -6,7 +6,7 @@ var proxyquire = require('proxyquire') var BasketMgr = require('../dw/order/BasketMgr'); var URLUtils = require('../dw/web/URLUtils'); var almaHelper = require('../helpers/almaHelpers'); -var almaCheckoutHelper = require('../helpers/almaCheckoutHelpers'); +var almaCheckoutHelper = require('../helpers/almaCheckoutHelpers').almaCheckoutHelpers; var almaAddressHelper = require('../helpers/almaAddressHelper'); var almaOnShipmentHelper = require('../helpers/almaOnShipmentHelpers'); var almaConfigHelpers = require('./almaConfigHelpers').almaConfigHelpers; diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 2733d58..55734b2 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -42,7 +42,7 @@ describe('almaPaymentHelper', function () { assert.property(payment.payment, 'capture_method'); }); it('Payment data for credit has no capture method in', function () { - var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', true); + var payment = almaPaymentHelper.buildPaymentData(12, 0, 'fr_FR', false); assert.notProperty(payment.payment, 'capture_method'); }); it('Payment data for pay now has capture method', function () { @@ -50,7 +50,7 @@ describe('almaPaymentHelper', function () { assert.property(payment.payment, 'capture_method'); }); it('Payment data for pay later has no capture method', function () { - var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', true); + var payment = almaPaymentHelper.buildPaymentData(1, 15, 'fr_FR', false); assert.notProperty(payment.payment, 'capture_method'); }); }); From 4a2c8d1d781d4b6b33fd8cfe706c7f3d7c94b507 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 4 Sep 2023 16:24:37 +0200 Subject: [PATCH 37/66] refactor: refactor eligibility helper for test. --- .../scripts/helpers/almaEligibilityHelper.js | 24 ++++++-- .../helpers/almaEligibilityHelperMocks.js | 28 +++++++++ .../helpers/almaEligibilityHelperTest.js | 58 +++++++++++++++++++ 3 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 test/mocks/helpers/almaEligibilityHelperMocks.js create mode 100644 test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js index 8b397cb..f1f640c 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js @@ -29,17 +29,16 @@ function callEligibility(param) { /** - * Get eligibility for a given set of plan, a locale and a basket + * Get eligibility params for a given set of plan, a locale and a basket * @param {Object} plansForEligibility Alma plans * @param {string} locale e.g. 'fr_FR' * @param {dw.order.Basket} currentBasket current basket * @returns {array} of eligibility plan Object */ -function getEligibility(plansForEligibility, locale, currentBasket) { +function getParams(plansForEligibility, locale, currentBasket) { if (currentBasket === null) { return []; } - var purchaseAmount = Math.round(currentBasket.totalGrossPrice.multiply(100).value); var billingAddress = formatAddress(currentBasket.getBillingAddress(), currentBasket.getCustomerEmail()); var shippingAddress = formatAddress( @@ -47,16 +46,29 @@ function getEligibility(plansForEligibility, locale, currentBasket) { currentBasket.getCustomerEmail() ); - var param = { + var params = { purchase_amount: purchaseAmount, queries: plansForEligibility, locale: locale, billing_address: billingAddress, shipping_address: shippingAddress }; - return callEligibility(param); + return params; +} + +/** + * Get eligibility for a given set of plan, a locale and a basket + * @param {Object} plansForEligibility Alma plans + * @param {string} locale e.g. 'fr_FR' + * @param {dw.order.Basket} currentBasket current basket + * @returns {array} of eligibility plan Object + */ +function getEligibility(plansForEligibility, locale, currentBasket) { + var params = getParams(plansForEligibility, locale, currentBasket); + return callEligibility(params); } module.exports = { - getEligibility: getEligibility + getEligibility: getEligibility, + getParams: getParams }; diff --git a/test/mocks/helpers/almaEligibilityHelperMocks.js b/test/mocks/helpers/almaEligibilityHelperMocks.js new file mode 100644 index 0000000..292c813 --- /dev/null +++ b/test/mocks/helpers/almaEligibilityHelperMocks.js @@ -0,0 +1,28 @@ +'use strict'; + +var proxyquire = require('proxyquire').noCallThru().noPreserveCache(); +var sinon = require('sinon'); +var almaAddressHelper = require('../../mocks/helpers/almaAddressHelper'); + +var logger = { + getLogger: function () { + return { + info: sinon.stub() + }; + } +}; + + +function proxyModel() { + return proxyquire( + '../../../cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper', + { + 'dw/system/Logger': logger, + '*/cartridge/scripts/helpers/almaAddressHelper': almaAddressHelper + } + ); +} +module.exports = { + almaEligibilityHelperMocks: proxyModel(), + logger: logger +}; diff --git a/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js new file mode 100644 index 0000000..9c6a25e --- /dev/null +++ b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js @@ -0,0 +1,58 @@ +'use strict'; + +// almaEligibilityHelper.js unit tests + +var assert = require('chai').assert; +var expect = require('chai').expect; +var almaEligibilityHelperMocks = require('../../../../mocks/helpers/almaEligibilityHelperMocks').almaEligibilityHelperMocks; +var basketMock = require('../../../../mocks/dw/order/BasketMgr'); + +var baseReturn = + { + purchase_amount: 25000, + queries: [], + locale: 'fr_FR', + billing_address: { + title: 'address.jobTitle', + first_name: 'address.lastName', + last_name: 'address.firstName', + company: 'address.companyName', + line1: 'address.address1', + line2: 'address.address2', + postal_code: 'address.postalCode', + city: 'address.city', + country: 'address.countryCode.value', + state_province: 'address.stateCode', + phone: 'address.phone' + }, + shipping_address: { + title: 'address.jobTitle', + first_name: 'address.lastName', + last_name: 'address.firstName', + company: 'address.companyName', + line1: 'address.address1', + line2: 'address.address2', + postal_code: 'address.postalCode', + city: 'address.city', + country: 'address.countryCode.value', + state_province: 'address.stateCode', + phone: 'address.phone' + } + }; + +var baseBasket = basketMock.getCurrentBasket(); +describe('Construct eligibility payload', function () { + it('return a empty array for a null current bask', function () { + var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', null); + // eslint-disable-next-line no-unused-expressions + expect(params).to.be.an('array').that.is.empty; + }); + it('Return full eligibility payload for a basket', function () { + var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', baseBasket); + assert.deepEqual( + params, + baseReturn + ); + }); +}); + From ffa38891cc9699be4f442b4c7e67dc2b2c551d3d Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Mon, 4 Sep 2023 17:14:02 +0200 Subject: [PATCH 38/66] feature: add default value for capture_method to automatic --- .../cartridge/scripts/helpers/almaEligibilityHelper.js | 3 ++- .../unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js index f1f640c..2b24e48 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js @@ -51,7 +51,8 @@ function getParams(plansForEligibility, locale, currentBasket) { queries: plansForEligibility, locale: locale, billing_address: billingAddress, - shipping_address: shippingAddress + shipping_address: shippingAddress, + capture_method: 'automatic' }; return params; } diff --git a/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js index 9c6a25e..2b46965 100644 --- a/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js @@ -37,7 +37,8 @@ var baseReturn = country: 'address.countryCode.value', state_province: 'address.stateCode', phone: 'address.phone' - } + }, + capture_method: 'automatic' }; var baseBasket = basketMock.getCurrentBasket(); From 6c3a50302bbc107184195cb1755b6ce52ae8e38d Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Tue, 5 Sep 2023 14:43:52 +0200 Subject: [PATCH 39/66] feature: add capture_method in eligibility calls --- cartridges/int_alma/cartridge/controllers/Alma.js | 5 ++++- .../int_alma/cartridge/controllers/Checkout.js | 6 +++++- .../scripts/helpers/almaEligibilityHelper.js | 11 ++++++++--- .../cartridge/scripts/helpers/almaPlanHelper.js | 10 ++++++---- .../scripts/helpers/almaEligibilityHelperTest.js | 14 ++++++++++++-- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/cartridges/int_alma/cartridge/controllers/Alma.js b/cartridges/int_alma/cartridge/controllers/Alma.js index c733262..1fdda03 100644 --- a/cartridges/int_alma/cartridge/controllers/Alma.js +++ b/cartridges/int_alma/cartridge/controllers/Alma.js @@ -381,10 +381,13 @@ server.get('Plans', var almaPlanHelper = require('*/cartridge/scripts/helpers/almaPlanHelper'); var BasketMgr = require('dw/order/BasketMgr'); + var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); + + var isDeferredCaptureEnabled = almaConfigHelper.isDeferredCaptureEnable(); var currentBasket = BasketMgr.getCurrentBasket(); res.json({ - plans: almaPlanHelper.getPlansForCheckout(getLocale(req), currentBasket) + plans: almaPlanHelper.getPlansForCheckout(getLocale(req), currentBasket, isDeferredCaptureEnabled) }); return next(); }); diff --git a/cartridges/int_alma/cartridge/controllers/Checkout.js b/cartridges/int_alma/cartridge/controllers/Checkout.js index 1e74db3..c5e8658 100644 --- a/cartridges/int_alma/cartridge/controllers/Checkout.js +++ b/cartridges/int_alma/cartridge/controllers/Checkout.js @@ -45,6 +45,8 @@ server.append('Begin', function (req, res, next) { var almaPlanHelper = require('*/cartridge/scripts/helpers/almaPlanHelper'); var almaHelpers = require('*/cartridge/scripts/helpers/almaHelpers'); var almaConfigInfo = getAlmaInfo(); + var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); + var BasketMgr = require('dw/order/BasketMgr'); var currentBasket = BasketMgr.getCurrentBasket(); @@ -60,6 +62,8 @@ server.append('Begin', function (req, res, next) { return; } + var isDeferredCaptureEnabled = almaConfigHelper.isDeferredCaptureEnable(); + var viewData = Object.assign( res.getViewData(), almaConfigInfo, @@ -67,7 +71,7 @@ server.append('Begin', function (req, res, next) { { currencyCode: currentBasket.currencyCode, purchase_amount: Math.round(currentBasket.totalGrossPrice.multiply(100).value), - plans: almaPlanHelper.getPlansForCheckout(getLocale(req), currentBasket), + plans: almaPlanHelper.getPlansForCheckout(getLocale(req), currentBasket, isDeferredCaptureEnabled), inpage_on_close_message: Resource.msg('alma.inpage_on_close_message', 'alma', null), inpage_on_failure_message: Resource.msg('alma.inpage_on_failure_message', 'alma', null), locale: getLocale(req) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js index 2b24e48..21a75c4 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaEligibilityHelper.js @@ -33,9 +33,10 @@ function callEligibility(param) { * @param {Object} plansForEligibility Alma plans * @param {string} locale e.g. 'fr_FR' * @param {dw.order.Basket} currentBasket current basket + * @param {bool} isDeferredCaptureEnabled deferred capture is enabled * @returns {array} of eligibility plan Object */ -function getParams(plansForEligibility, locale, currentBasket) { +function getParams(plansForEligibility, locale, currentBasket, isDeferredCaptureEnabled) { if (currentBasket === null) { return []; } @@ -54,6 +55,9 @@ function getParams(plansForEligibility, locale, currentBasket) { shipping_address: shippingAddress, capture_method: 'automatic' }; + if (isDeferredCaptureEnabled) { + params.capture_method = 'manual'; + } return params; } @@ -62,10 +66,11 @@ function getParams(plansForEligibility, locale, currentBasket) { * @param {Object} plansForEligibility Alma plans * @param {string} locale e.g. 'fr_FR' * @param {dw.order.Basket} currentBasket current basket + * @param {bool} isDeferredCaptureEnabled deferred capture is enabled * @returns {array} of eligibility plan Object */ -function getEligibility(plansForEligibility, locale, currentBasket) { - var params = getParams(plansForEligibility, locale, currentBasket); +function getEligibility(plansForEligibility, locale, currentBasket, isDeferredCaptureEnabled) { + var params = getParams(plansForEligibility, locale, currentBasket, isDeferredCaptureEnabled); return callEligibility(params); } diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js index 68e4603..14203a0 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js @@ -141,10 +141,11 @@ function getFeePlansBoFormat(feePlans) { * @param {string} locale locale * @param {Object} currentBasket currentBasket * @param {array} plans plans + * @param {bool} isDeferredCaptureEnabled deferred capture is enabled * @returns {array} eligible plans */ -function buildEligiblePlans(purchaseAmount, feePlans, locale, currentBasket, plans) { - var plansEligible = almaEligibilityHelper.getEligibility(feePlans, locale, currentBasket); +function buildEligiblePlans(purchaseAmount, feePlans, locale, currentBasket, plans, isDeferredCaptureEnabled) { + var plansEligible = almaEligibilityHelper.getEligibility(feePlans, locale, currentBasket, isDeferredCaptureEnabled); if (!Array.isArray(plansEligible)) { plansEligible = [plansEligible]; } @@ -218,9 +219,10 @@ function getFormattedPlans(plans) { * Get data to initialize widget in cart and product detail * @param {string} locale e.g. "fr_FR" * @param {dw.order.Basket} currentBasket current basket + * @param {bool} isDeferredCaptureEnabled deferred capture is enabled * @returns {array} eligible data */ -function getPlansForCheckout(locale, currentBasket) { +function getPlansForCheckout(locale, currentBasket, isDeferredCaptureEnabled) { var feePlans = getFeePlans(); feePlans = getFeePlansBoFormat(feePlans); @@ -239,7 +241,7 @@ function getPlansForCheckout(locale, currentBasket) { return filterWithMerchantConfig(feePlan, purchaseAmount); }); - plans = buildEligiblePlans(purchaseAmount, feePlans, locale, currentBasket, plans); + plans = buildEligiblePlans(purchaseAmount, feePlans, locale, currentBasket, plans, isDeferredCaptureEnabled); return getFormattedPlans(plans); } diff --git a/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js index 2b46965..b615945 100644 --- a/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js @@ -6,6 +6,8 @@ var assert = require('chai').assert; var expect = require('chai').expect; var almaEligibilityHelperMocks = require('../../../../mocks/helpers/almaEligibilityHelperMocks').almaEligibilityHelperMocks; var basketMock = require('../../../../mocks/dw/order/BasketMgr'); +var deferredCaptureEnabled = true; +var deferredCaptureDisabled = false; var baseReturn = { @@ -44,12 +46,20 @@ var baseReturn = var baseBasket = basketMock.getCurrentBasket(); describe('Construct eligibility payload', function () { it('return a empty array for a null current bask', function () { - var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', null); + var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', null, deferredCaptureDisabled); // eslint-disable-next-line no-unused-expressions expect(params).to.be.an('array').that.is.empty; }); it('Return full eligibility payload for a basket', function () { - var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', baseBasket); + var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', baseBasket, deferredCaptureDisabled); + assert.deepEqual( + params, + baseReturn + ); + }); + it('Return eligibility payload with capture method manual ', function () { + var params = almaEligibilityHelperMocks.getParams([], 'fr_FR', baseBasket, deferredCaptureEnabled); + baseReturn.capture_method = 'manual'; assert.deepEqual( params, baseReturn From 8969333b8a0eded8b888af72608dd882c8ba9e84 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Tue, 5 Sep 2023 16:53:02 +0200 Subject: [PATCH 40/66] feature: Change wording on checkout with eligibility result --- .../int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js | 3 ++- .../int_alma/cartridge/templates/resources/alma.properties | 2 +- .../cartridge/templates/resources/alma_de_DE.properties | 2 +- .../cartridge/templates/resources/alma_es_ES.properties | 2 +- .../cartridge/templates/resources/alma_fr_FR.properties | 2 +- .../cartridge/templates/resources/alma_it_IT.properties | 2 +- .../cartridge/templates/resources/alma_nl_NL.properties | 2 +- .../cartridge/templates/resources/alma_pt_PT.properties | 2 +- 8 files changed, 9 insertions(+), 8 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js index a50bad6..3cdbf39 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js @@ -137,7 +137,8 @@ function getPaymentInstallments(plan, currencyCode) { // on deferred capture if (almaPaymentHelper.isAvailableForManualCapture(almaConfigHelper.isDeferredCaptureEnable(), plan.installments_count, plan.deferred_days)) { return formatCurrency(plan.payment_plan[0].purchase_amount / 100, currencyCode) + ' ' + - Resource.msg(getPropertyCategory(plan) + '.installments.deferred_capture', 'alma', null) + ' ' + + plan.payment_plan[0].localized_due_date + + Resource.msg(getPropertyCategory(plan) + '.installments.then', 'alma', null) + ' ' + getInstallmentCountAfterFirst(plan) + formatCurrency(plan.payment_plan[1].purchase_amount / 100, currencyCode) ; diff --git a/cartridges/int_alma/cartridge/templates/resources/alma.properties b/cartridges/int_alma/cartridge/templates/resources/alma.properties index 7c2d9c0..64bf996 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma.properties @@ -31,7 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Pay in {0} installments with credit card alma.pay.in_x_installment.installments=today, then alma.pay.in_x_installment.installments.onshipment=at shipping, then -alma.pay.in_x_installment.installments.deferred_capture=at order processing, then +alma.pay.in_x_installment.installments.then=, then alma.pay.in_x_installment_after_x_days=Pay in {0} installments in {1} days alma.pay.in_x_installment_after_x_days.img=x{0}+{1} diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_de_DE.properties b/cartridges/int_alma/cartridge/templates/resources/alma_de_DE.properties index 0dac823..7ab7e51 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_de_DE.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_de_DE.properties @@ -31,7 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Zahlung in {0} Raten mit Kreditkarte alma.pay.in_x_installment.installments=heute, dann alma.pay.in_x_installment.installments.onshipment=beim Versand, dann -alma.pay.in_x_installment.installments.deferred_capture=bei der Auftragsabwicklung, dann +alma.pay.in_x_installment.installments.then=, dann alma.pay.in_x_installment_after_x_days=Zahlung in {0} Raten in {1} Tagen alma.pay.in_x_installment_after_x_days.img=x{0}+{1} diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_es_ES.properties b/cartridges/int_alma/cartridge/templates/resources/alma_es_ES.properties index 775a40e..8b049fc 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_es_ES.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_es_ES.properties @@ -31,7 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Pagar en {0} plazos con tarjeta bancaria alma.pay.in_x_installment.installments=hoy, después alma.pay.in_x_installment.installments.onshipment=al envío, después -alma.pay.in_x_installment.installments.deferred_capture=en la tramitación del pedido, entonces +alma.pay.in_x_installment.installments.then=, entonces alma.pay.in_x_installment_after_x_days=Pagar en {0} plazos en {1} días alma.pay.in_x_installment_after_x_days.img=x{0}+{1} diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_fr_FR.properties b/cartridges/int_alma/cartridge/templates/resources/alma_fr_FR.properties index a74aa01..8826ec8 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_fr_FR.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_fr_FR.properties @@ -31,7 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Payer en {0} fois par carte de crédit alma.pay.in_x_installment.installments=aujourd'hui, puis alma.pay.in_x_installment.installments.onshipment=à l'expédition puis -alma.pay.in_x_installment.installments.deferred_capture=au traitement de la commande, puis +alma.pay.in_x_installment.installments.then=, puis alma.pay.in_x_installment_after_x_days=Payer en {0} fois dans {1} jours alma.pay.in_x_installment_after_x_days.img=x{0}+{1} diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_it_IT.properties b/cartridges/int_alma/cartridge/templates/resources/alma_it_IT.properties index ebef5b6..1e5ae7c 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_it_IT.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_it_IT.properties @@ -31,7 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Paga in {0} rate con carta di credito alma.pay.in_x_installment.installments=oggi, poi alma.pay.in_x_installment.installments.onshipment=alla spedizione, poi -alma.pay.in_x_installment.installments.deferred_capture=al momento dell'elaborazione dell'ordine, poi +alma.pay.in_x_installment.installments.then=, allora alma.pay.in_x_installment_after_x_days=Paga in {0} rate tra {1} giorni alma.pay.in_x_installment_after_x_days.img=x{0}+{1} diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_nl_NL.properties b/cartridges/int_alma/cartridge/templates/resources/alma_nl_NL.properties index 498435e..c975aa7 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_nl_NL.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_nl_NL.properties @@ -31,7 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Betaal in {0} termijnen met kredietkaart alma.pay.in_x_installment.installments=vandaag, dan alma.pay.in_x_installment.installments.onshipment=bij verzending, daarna -alma.pay.in_x_installment.installments.deferred_capture=bij het verwerken van de bestelling, dan +alma.pay.in_x_installment.installments.then=, dan alma.pay.in_x_installment_after_x_days=Betaal in {0} termijnen in {1} dagen alma.pay.in_x_installment_after_x_days.img=x{0}+{1} diff --git a/cartridges/int_alma/cartridge/templates/resources/alma_pt_PT.properties b/cartridges/int_alma/cartridge/templates/resources/alma_pt_PT.properties index dc725bb..63160bf 100644 --- a/cartridges/int_alma/cartridge/templates/resources/alma_pt_PT.properties +++ b/cartridges/int_alma/cartridge/templates/resources/alma_pt_PT.properties @@ -31,7 +31,7 @@ alma.pay.in_x_installment.img=x{0} alma.pay.in_x_installment.description=Pagar em {0} prestações com cartão de crédito alma.pay.in_x_installment.installments=hoje, e depois alma.pay.in_x_installment.installments.onshipment=na expedição, e depois -alma.pay.in_x_installment.installments.deferred_capture=no processamento da encomenda, depois +alma.pay.in_x_installment.installments.then=, então alma.pay.in_x_installment_after_x_days=Pagar em {0} prestações em {1} dias alma.pay.in_x_installment_after_x_days.img=x{0}+{1} From 4ecd6120d34b695ffe336da3d63f3228c04901b5 Mon Sep 17 00:00:00 2001 From: Francois-Gomis Date: Tue, 5 Sep 2023 17:01:35 +0200 Subject: [PATCH 41/66] test: fix checkout test --- .../scripts/helpers/almaCheckoutHelperTest.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js b/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js index aa730ac..17a6561 100644 --- a/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js @@ -14,15 +14,18 @@ var plan = { payment_plan: [ { customer_fee: 0, - purchase_amount: 11700 + purchase_amount: 11700, + localized_due_date: 'Order processing' }, { customer_fee: 0, - purchase_amount: 11600 + purchase_amount: 11600, + localized_due_date: '1 months later' }, { customer_fee: 0, - purchase_amount: 11600 + purchase_amount: 11600, + localized_due_date: '2 months later' } ] }; @@ -114,7 +117,7 @@ describe('almaCheckoutHelpers', function () { basket_cost: 'alma.credit.basket_cost', total_cost: 'alma.credit.total_cost' }, - payment_installments: '117 alma.pay.in_x_installment.installments.deferred_capture 2x116' + payment_installments: '117 Order processingalma.pay.in_x_installment.installments.then 2x116' }); }); From ebc5a30ae04dcf71d355a3aa61676a65acba2e93 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 6 Sep 2023 09:15:47 +0200 Subject: [PATCH 42/66] feat: pre-commit hook config --- .pre-commit-config.yaml | 59 ++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 44833dd..a9b9344 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,36 +8,35 @@ repos: args: ["--maxkb=1024"] - id: detect-private-key - id: check-merge-conflict -### -# - repo: https://github.com/pre-commit/mirrors-jshint -# rev: v2.13.6 -# hooks: -# - id: jshint -# files: "(^.*\\.js$)" -# -# - repo: https://github.com/pre-commit/mirrors-eslint -# rev: v8.29.0 -# hooks: -# - id: eslint -# files: "(^cartridges/int_alma/cartridge/.*\\.js$)"# *.js, *.jsx, *.ts and *.tsx -# types: [file] -# args: -# - --fix -# -# - repo: https://github.com/thibaudcolas/pre-commit-stylelint -# rev: v14.16.0 -# hooks: -# - id: stylelint -# files: "(^cartridges/int_alma/cartridge/static/default/css/.*\\.css$)" -# args: -# - --config -# - .stylelintrc.json -# additional_dependencies: -# - stylelint@15.10.2 -# - prettier@2.8.8 -# - stylelint-config-standard@32.0.0 -# - stylelint-prettier@3.0.0 -### + + - repo: https://github.com/pre-commit/mirrors-jshint + rev: v2.13.6 + hooks: + - id: jshint + files: "(^.*\\.js$)" + + - repo: https://github.com/pre-commit/mirrors-eslint + rev: v8.29.0 + hooks: + - id: eslint + files: "(^cartridges/int_alma/cartridge/.*\\.js$)"# *.js, *.jsx, *.ts and *.tsx + types: [file] + args: + - --fix + + - repo: https://github.com/thibaudcolas/pre-commit-stylelint + rev: v14.16.0 + hooks: + - id: stylelint + files: "(^cartridges/int_alma/cartridge/static/default/css/.*\\.css$)" + args: + - --config + - .stylelintrc.json + additional_dependencies: + - stylelint@15.10.2 + - prettier@2.8.8 + - stylelint-config-standard@32.0.0 + - stylelint-prettier@3.0.0 # - repo: https://github.com/returntocorp/semgrep # rev: v1.27.0 From d6dc26202876a89157256838e00137e50f936a5a Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 6 Sep 2023 09:16:18 +0200 Subject: [PATCH 43/66] feat: pre-commit hook stylelint --- .../int_alma/cartridge/static/default/css/almaContent.css | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cartridges/int_alma/cartridge/static/default/css/almaContent.css b/cartridges/int_alma/cartridge/static/default/css/almaContent.css index 33bb340..7ca6408 100644 --- a/cartridges/int_alma/cartridge/static/default/css/almaContent.css +++ b/cartridges/int_alma/cartridge/static/default/css/almaContent.css @@ -1,5 +1,4 @@ .alma-content .pay-type { - /* background: #cccccc7a;*/ border-radius: 5px; margin-top: 10px; cursor: pointer; @@ -42,11 +41,11 @@ } .alma-content .pay-type p { - color: var(--color-grey6); + color: var(--color-grey6); } .alma-content .pay-type .no-margin { - margin-bottom: 0px; + margin-bottom: 0; } .alma-content .loading { @@ -86,7 +85,8 @@ .alma-lds-ring div:nth-child(3) { animation-delay: -0.15s; } -.alma-error-message{ + +.alma-error-message { color: red; font-size: small; text-align: center; From 95160bd1a9e62dbb97ca6a9742aacefca4b4d609 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 6 Sep 2023 10:32:44 +0200 Subject: [PATCH 44/66] feat: pre-commit hook jshint --- .jshintrc | 7 ++++- .../scripts/helpers/almaCheckoutHelper.js | 12 ++------ .../scripts/helpers/almaOnShipmentHelper.js | 5 ++-- .../scripts/helpers/almaPaymentHelper.js | 1 + .../scripts/helpers/almaPlanHelper.js | 10 ++----- .../cartridge/scripts/steps/CheckRefund.js | 8 +++--- test/mocks/helpers/almaOrderHelpers.js | 1 + test/util.js | 28 ++++++++++--------- 8 files changed, 35 insertions(+), 37 deletions(-) diff --git a/.jshintrc b/.jshintrc index 80fc4c0..9e36a80 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,3 +1,8 @@ { - "esversion": 8 + "node": true, + "esversion": 8, + "predef": [ + "describe", + "it" + ] } \ No newline at end of file diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js index f6a432c..eea75ce 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js @@ -17,12 +17,8 @@ var paymentMethodId = ''; * @returns {string} the custom site preference name */ function getSelectorNameFromPlan(plan) { - return 'ALMA_general_' - // number of installments (p1x, p3x, p4x, ....) - + plan.installments_count + '_' - // by how many days is the payment defered - + plan.deferred_days - ; + // ALMA_general + number of installments (p1x, p3x, p4x, ....) + by how many days is the payment defered + return 'ALMA_general_' + plan.installments_count + '_' + plan.deferred_days; } /** @@ -205,9 +201,7 @@ function getPlanPaymentMethodID(plan) { if (plan.deferred_days > 0 && planIsActivated(PaymentMgr.getPaymentMethod(ALMA_DEFERRED_ID), plan)) { paymentMethodId = ALMA_DEFERRED_ID; } - if (plan.installments_count === 1 - && plan.deferred_days === 0 - && planIsActivated(PaymentMgr.getPaymentMethod(ALMA_PAY_NOW_ID), plan)) { + if (plan.installments_count === 1 && plan.deferred_days === 0 && planIsActivated(PaymentMgr.getPaymentMethod(ALMA_PAY_NOW_ID), plan)) { paymentMethodId = ALMA_PAY_NOW_ID; } diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaOnShipmentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaOnShipmentHelper.js index 42840a9..ad5d77f 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaOnShipmentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaOnShipmentHelper.js @@ -18,9 +18,8 @@ function isOnShipmentPaymentEnabled(installmentsCount, deferredDays) { return plan.installments_count === parseInt(installmentsCount, 10) && plan.deferred_days === parseInt(deferredDays, 10); }); - return Site.getCurrent().getCustomPreferenceValue('ALMA_On_Shipment_Payment') - && requestedPlan !== null - && requestedPlan.deferred_trigger_limit_days !== null; + return Site.getCurrent() + .getCustomPreferenceValue('ALMA_On_Shipment_Payment') && requestedPlan !== null && requestedPlan.deferred_trigger_limit_days !== null; } module.exports = { diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index 79f79b1..052520c 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -1,4 +1,5 @@ 'use strict'; + var pkg = require('../../../package.json'); /** diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js index 68e4603..081cb23 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js @@ -27,11 +27,8 @@ function getFeePlans() { * @returns {string} site preference name */ function getSitePrefNameFromPlan(plan) { - return 'ALMA_general_' - // number of installments (p1x, p3x, p4x, ....) - + plan.installments_count + '_' - // by how many days is the payment deferred - + plan.deferred_days + // ALMA_general + number of installments (p1x, p3x, p4x, ....) + by how many days is the payment deferred + return 'ALMA_general_' + plan.installments_count + '_' + plan.deferred_days ; } @@ -77,8 +74,7 @@ function applyMerchantConfig(currentPlan) { * @returns {Object} plan any alma plan */ function filterWithMerchantConfig(plan, purchaseAmount) { - return (purchaseAmount > (plan.min_display_amount / 100)) - && (purchaseAmount < (plan.max_display_amount / 100)); + return (purchaseAmount > (plan.min_display_amount / 100)) && (purchaseAmount < (plan.max_display_amount / 100)); } /** diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index 095869f..f43c4cd 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -7,10 +7,9 @@ */ function isOrderToBeRefund(order) { return ( - order.custom.almaRefundType.toString() === 'Total' - || (order.custom.almaRefundType.toString() === 'Partial' - && order.custom.almaWantedRefundAmount > 0 - && order.custom.almaWantedRefundAmount < order.totalGrossPrice.value) + order.custom.almaRefundType.toString() === 'Total' || ( + order.custom.almaRefundType.toString() === 'Partial' && order.custom.almaWantedRefundAmount > 0 && order.custom.almaWantedRefundAmount < order.totalGrossPrice.value + ) ); } @@ -66,6 +65,7 @@ exports.execute = function () { 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 = deferredStatus; diff --git a/test/mocks/helpers/almaOrderHelpers.js b/test/mocks/helpers/almaOrderHelpers.js index 24c9e56..e17f3e1 100644 --- a/test/mocks/helpers/almaOrderHelpers.js +++ b/test/mocks/helpers/almaOrderHelpers.js @@ -1,4 +1,5 @@ 'use strict'; + var sinon = require('sinon'); var proxyquire = require('proxyquire') .noCallThru() diff --git a/test/util.js b/test/util.js index 19e2a9f..409717b 100644 --- a/test/util.js +++ b/test/util.js @@ -1,4 +1,5 @@ 'use strict'; + var ArrayList = require('./mocks/dw.util.Collection'); module.exports = function toProductMock(mock) { @@ -8,20 +9,21 @@ module.exports = function toProductMock(mock) { var result = {}; if (typeof mock === 'object') { - Object.keys(mock).forEach(function (item) { - if (typeof mock[item] === 'object') { - if (mock[item] && mock[item].type === 'function') { - var innerMock = typeof mock[item].return !== 'undefined' - ? toProductMock(mock[item].return) - : toProductMock(mock[item]); - result[item] = function () { return innerMock; }; - } else { - result[item] = toProductMock(mock[item]); + Object.keys(mock) + .forEach(function (item) { + if (typeof mock[item] === 'object') { + if (mock[item] && mock[item].type === 'function') { + var innerMock = typeof mock[item].return !== 'undefined' ? toProductMock(mock[item].return) : toProductMock(mock[item]); + result[item] = function () { + return innerMock; + }; + } else { + result[item] = toProductMock(mock[item]); + } + } else if (item !== 'function' || item !== 'return') { + result[item] = mock[item]; } - } else if (item !== 'function' || item !== 'return') { - result[item] = mock[item]; - } - }); + }); } else { result = mock; } From f0bc7f7964f13542533dab7820f43e1e35c34837 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 20 Sep 2023 09:34:23 +0200 Subject: [PATCH 45/66] finish pre-commit implementation --- .jshintrc | 1 + .../cartridge/scripts/helpers/almaCheckoutHelper.js | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.jshintrc b/.jshintrc index 9e36a80..4d781b8 100644 --- a/.jshintrc +++ b/.jshintrc @@ -1,6 +1,7 @@ { "node": true, "esversion": 8, + "expr": true, "predef": [ "describe", "it" diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js index 1fb163e..eaa841b 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js @@ -128,7 +128,7 @@ function getPaymentInstallments(plan, currencyCode) { Resource.msg(getPropertyCategory(plan) + '.installments.onshipment', 'alma', null) + ' ' + getInstallmentCountAfterFirst(plan) + formatCurrency(plan.payment_plan[1].purchase_amount / 100, currencyCode) - ; + ; } // on deferred capture if (almaPaymentHelper.isAvailableForManualCapture(almaConfigHelper.isDeferredCaptureEnable(), plan.installments_count, plan.deferred_days)) { @@ -137,14 +137,14 @@ function getPaymentInstallments(plan, currencyCode) { Resource.msg(getPropertyCategory(plan) + '.installments.then', 'alma', null) + ' ' + getInstallmentCountAfterFirst(plan) + formatCurrency(plan.payment_plan[1].purchase_amount / 100, currencyCode) - ; + ; } // installment payment return formatCurrency(plan.payment_plan[0].purchase_amount / 100, currencyCode) + ' ' + Resource.msg(getPropertyCategory(plan) + '.installments', 'alma', null) + ' ' + getInstallmentCountAfterFirst(plan) + formatCurrency(plan.payment_plan[1].purchase_amount / 100, currencyCode) - ; + ; } /** From 2916da2f455e868eaab8b1fede42f8e691ac879f Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 21 Sep 2023 16:52:44 +0200 Subject: [PATCH 46/66] add captureMethod in plan for checkout --- .../scripts/helpers/almaPlanHelper.js | 20 +- .../static/default/js/almaCheckout.js | 9 +- .../almaContent/almaContent.isml | 1 + test/mocks/data/plans.js | 475 ++++++++++++++++++ test/mocks/helpers/almaPlanHelperMock.js | 58 +++ .../scripts/helpers/almaPlanHelperTest.js | 37 ++ 6 files changed, 593 insertions(+), 7 deletions(-) create mode 100644 test/mocks/data/plans.js create mode 100644 test/mocks/helpers/almaPlanHelperMock.js create mode 100644 test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js index 154ceb4..33211e1 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js @@ -6,6 +6,8 @@ var almaUtilsHelpers = require('*/cartridge/scripts/helpers/almaUtilsHelper'); var almaEligibilityHelper = require('*/cartridge/scripts/helpers/almaEligibilityHelper'); var almaCheckoutHelper = require('*/cartridge/scripts/helpers/almaCheckoutHelper'); var almaWidgetHelper = require('*/cartridge/scripts/helpers/almaWidgetHelper'); +var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper'); +var almaConfigHelper = require('*/cartridge/scripts/helpers/almaConfigHelper'); /** * Calls /me/fee-plans and fetch the available plans for the current merchant @@ -138,7 +140,7 @@ function getFeePlansBoFormat(feePlans) { * @param {Object} currentBasket currentBasket * @param {array} plans plans * @param {bool} isDeferredCaptureEnabled deferred capture is enabled - * @returns {array} eligible plans + * @returns {Object} eligible plans */ function buildEligiblePlans(purchaseAmount, feePlans, locale, currentBasket, plans, isDeferredCaptureEnabled) { var plansEligible = almaEligibilityHelper.getEligibility(feePlans, locale, currentBasket, isDeferredCaptureEnabled); @@ -171,7 +173,7 @@ function buildEligiblePlans(purchaseAmount, feePlans, locale, currentBasket, pla /** * Get plans formatted for front integration - * @param {array} plans plans before format + * @param {Object} plans plans before format * @returns {array} formatted plans */ function getFormattedPlans(plans) { @@ -183,6 +185,16 @@ function getFormattedPlans(plans) { Object.keys(plans[paymentMethod]).forEach(function (keys) { paymentMethodPlans.push(plans[paymentMethod][keys]); + plans[paymentMethod][keys].captureMethod = 'automatic'; + + if (almaPaymentHelper.isAvailableForManualCapture( + almaConfigHelper.isDeferredCaptureEnable(), + plans[paymentMethod][keys].installments_count, + plans[paymentMethod][keys].deferred_days + )) { + plans[paymentMethod][keys].captureMethod = 'manual'; + } + if (!plans[paymentMethod][keys].properties) { plans[paymentMethod][keys].properties = { title: '', @@ -207,7 +219,6 @@ function getFormattedPlans(plans) { formattedPaymentMethod.plans = paymentMethodPlans; formattedPlans.push(formattedPaymentMethod); }); - return formattedPlans; } @@ -245,5 +256,6 @@ function getPlansForCheckout(locale, currentBasket, isDeferredCaptureEnabled) { module.exports = { getAllowedPlans: getAllowedPlans, getPlansForWidget: getPlansForWidget, - getPlansForCheckout: getPlansForCheckout + getPlansForCheckout: getPlansForCheckout, + getFormattedPlans: getFormattedPlans }; diff --git a/cartridges/int_alma/cartridge/static/default/js/almaCheckout.js b/cartridges/int_alma/cartridge/static/default/js/almaCheckout.js index f4d0b9a..c3ac44c 100644 --- a/cartridges/int_alma/cartridge/static/default/js/almaCheckout.js +++ b/cartridges/int_alma/cartridge/static/default/js/almaCheckout.js @@ -135,7 +135,7 @@ window.addEventListener('DOMContentLoaded', window.location = body.url; } - async function inPageInitialize(inPageContainer, installments_count) { + async function inPageInitialize(inPageContainer, installments_count, captureMethod) { return Alma.InPage.initialize( { merchantId: almaContext.merchantId, @@ -143,7 +143,8 @@ window.addEventListener('DOMContentLoaded', installmentsCount: installments_count, selector: "#" + inPageContainer, locale: almaContext.locale.slice(0, 2), - environment: almaContext.almaMode + environment: almaContext.almaMode, + captureMethod: captureMethod } ); } @@ -179,11 +180,13 @@ window.addEventListener('DOMContentLoaded', var deferred_days = parseInt(t.getAttribute('data-deferred-days')); var alma_payment_method = t.getAttribute('data-alma-payment-method'); var in_page = t.getAttribute('data-in-page') === 'true'; + var captureMethod = t.getAttribute('data-captureMethod'); if (in_page) { await inPageInitialize( t.id + "-inpage", - installments_count + installments_count, + captureMethod ) .then(function (inPage) { var checkoutInpageCall = async function () { diff --git a/cartridges/int_alma/cartridge/templates/default/checkout/billing/paymentOptions/almaContent/almaContent.isml b/cartridges/int_alma/cartridge/templates/default/checkout/billing/paymentOptions/almaContent/almaContent.isml index e2bfda1..1d0e266 100644 --- a/cartridges/int_alma/cartridge/templates/default/checkout/billing/paymentOptions/almaContent/almaContent.isml +++ b/cartridges/int_alma/cartridge/templates/default/checkout/billing/paymentOptions/almaContent/almaContent.isml @@ -6,6 +6,7 @@ data-deferred-days="${plan.deferred_days}" data-in-page="${plan.in_page}" data-alma-payment-method="${paymentOption.ID}" + data-captureMethod="${plan.captureMethod}" >
diff --git a/test/mocks/data/plans.js b/test/mocks/data/plans.js new file mode 100644 index 0000000..16d98bb --- /dev/null +++ b/test/mocks/data/plans.js @@ -0,0 +1,475 @@ +'use strict'; + +var plansForCheckout = { + ALMA_PAY_NOW: { + ALMA_general_1_0: { + installments_count: 1, + deferred_days: 0, + payment_plans: [ + { + due_date: 1696596910, + purchase_amount: 30000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: 'Order processing', + time_delta_from_start: null + } + ] + } + }, + ALMA_DEFERRED: { + ALMA_general_1_15: { + installments_count: 1, + deferred_days: 15, + payment_plans: [ + { + due_date: 1696596910, + purchase_amount: 30000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: 'Order processing', + time_delta_from_start: null + } + ] + } + }, + ALMA_PNX: { + ALMA_general_2_0: { + installments_count: 2, + deferred_days: 0, + payment_plans: [ + { + due_date: 1695300910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: 'Order processing', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '1 months later', + time_delta_from_start: null + } + ] + }, + ALMA_general_3_0: { + installments_count: 3, + deferred_days: 0, + payment_plans: [ + { + due_date: 1695300910, + purchase_amount: 10000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: 'Order processing', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 10000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '1 months later', + time_delta_from_start: null + }, + { + due_date: 1700571310, + purchase_amount: 10000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '2 months later', + time_delta_from_start: null + } + ] + } + }, + ALMA_CREDIT: { + ALMA_general_10_0: { + installments_count: 10, + deferred_days: 0, + payment_plans: [ + { + due_date: 1695300910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: 'Order processing', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '1 months later', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '2 months later', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '3 months later', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '4 months later', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '5 months later', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '6 months later', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '7 months later', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '8 months later', + time_delta_from_start: null + }, + { + due_date: 1697892910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: '9 months later', + time_delta_from_start: null + } + ] + } + } +}; + +var formattedPlansForCheckoutExpected = [ + { + hasEligiblePaymentMethod: true, + name: 'ALMA_PAY_NOW', + plans: [ + { + installments_count: 1, + deferred_days: 0, + captureMethod: 'automatic', + payment_plans: [ + { + customer_fee: 0, + customer_interest: 0, + due_date: 1696596910, + localized_due_date: 'Order processing', + purchase_amount: 30000, + time_delta_from_start: null, + total_amount: 30000 + } + ], + properties: { + credit: { + amount: '', + basket_cost: '', + rate: '', + total_cost: '' + }, + description: '', + fees: '', + img: '', + title: '' + } + } + ] + }, + { + hasEligiblePaymentMethod: true, + name: 'ALMA_DEFERRED', + plans: [ + { + installments_count: 1, + deferred_days: 15, + captureMethod: 'automatic', + payment_plans: [ + { + customer_fee: 0, + customer_interest: 0, + due_date: 1696596910, + localized_due_date: 'Order processing', + purchase_amount: 30000, + time_delta_from_start: null, + total_amount: 30000 + } + ], + properties: { + credit: { + amount: '', + basket_cost: '', + rate: '', + total_cost: '' + }, + description: '', + fees: '', + img: '', + title: '' + } + } + ] + }, + { + hasEligiblePaymentMethod: true, + name: 'ALMA_PNX', + plans: [ + { + installments_count: 2, + deferred_days: 0, + captureMethod: 'automatic', + payment_plans: [ + { + customer_fee: 0, + customer_interest: 0, + due_date: 1695300910, + localized_due_date: 'Order processing', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '1 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + } + ], + properties: { + credit: { + amount: '', + basket_cost: '', + rate: '', + total_cost: '' + }, + description: '', + fees: '', + img: '', + title: '' + } + }, + { + installments_count: 3, + deferred_days: 0, + captureMethod: 'automatic', + payment_plans: [ + { + customer_fee: 0, + customer_interest: 0, + due_date: 1695300910, + localized_due_date: 'Order processing', + purchase_amount: 10000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '1 months later', + purchase_amount: 10000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1700571310, + localized_due_date: '2 months later', + purchase_amount: 10000, + time_delta_from_start: null, + total_amount: 30000 + } + ], + properties: { + credit: { + amount: '', + basket_cost: '', + rate: '', + total_cost: '' + }, + description: '', + fees: '', + img: '', + title: '' + } + } + ] + }, + { + hasEligiblePaymentMethod: true, + name: 'ALMA_CREDIT', + plans: [ + { + installments_count: 10, + deferred_days: 0, + captureMethod: 'automatic', + payment_plans: [ + { + customer_fee: 0, + customer_interest: 0, + due_date: 1695300910, + localized_due_date: 'Order processing', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '1 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '2 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '3 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '4 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '5 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '6 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '7 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '8 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + }, + { + customer_fee: 0, + customer_interest: 0, + due_date: 1697892910, + localized_due_date: '9 months later', + purchase_amount: 15000, + time_delta_from_start: null, + total_amount: 30000 + } + ], + properties: { + credit: { + amount: '', + basket_cost: '', + rate: '', + total_cost: '' + }, + description: '', + fees: '', + img: '', + title: '' + } + } + ] + } +]; + +module.exports = { + plansForCheckout: plansForCheckout, + formattedPlansForCheckoutExpected: formattedPlansForCheckoutExpected +}; \ No newline at end of file diff --git a/test/mocks/helpers/almaPlanHelperMock.js b/test/mocks/helpers/almaPlanHelperMock.js new file mode 100644 index 0000000..85f9330 --- /dev/null +++ b/test/mocks/helpers/almaPlanHelperMock.js @@ -0,0 +1,58 @@ +'use strict'; + +const sinon = require('sinon'); +var almaPaymentHelper = require('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper'); + +var proxyquire = require('proxyquire') + .noCallThru() + .noPreserveCache(); + + +var site = { + getCurrent: function () { + return { + getCustomPreferenceValue: function () { + return true; + } + }; + } +}; + +var logger = { + getLogger: function () { + return { + warn: sinon.stub() + }; + } +}; + +var isDeferredCaptureEnableValue; + +function setIsDeferredCaptureEnable(value) { + isDeferredCaptureEnableValue = value; +} + +var almaConfigHelper = { + isDeferredCaptureEnable: function () { + return isDeferredCaptureEnableValue; + } +}; + +function proxyModel() { + return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper', { + 'dw/system/Site': site, + 'dw/system/Logger': logger, + '*/cartridge/scripts/helpers/almaUtilsHelper': '', + '*/cartridge/scripts/helpers/almaEligibilityHelper': '', + '*/cartridge/scripts/helpers/almaCheckoutHelper': '', + '*/cartridge/scripts/helpers/almaWidgetHelper': '', + '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper, + '*/cartridge/scripts/helpers/almaConfigHelper': almaConfigHelper + }); +} + +module.exports = { + almaPlanHelperMock: proxyModel(), + setIsDeferredCaptureEnable: setIsDeferredCaptureEnable, + logger: logger +}; diff --git a/test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js new file mode 100644 index 0000000..75d49eb --- /dev/null +++ b/test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js @@ -0,0 +1,37 @@ +// planHelpers.js unit tests + +var expect = require('chai').expect; + +var almaPlanHelperMock = require('../../../../mocks/helpers/almaPlanHelperMock').almaPlanHelperMock; +var setIsDeferredCaptureEnable = require('../../../../mocks/helpers/almaPlanHelperMock').setIsDeferredCaptureEnable; +var formattedPlansForCheckoutExpected = require('../../../../mocks/data/plans').formattedPlansForCheckoutExpected; +var plansForCheckout = require('../../../../mocks/data/plans').plansForCheckout; + + +describe('almaPlanHelper', function () { + describe('Format plan Helper', function () { + it('should return an array for a given plans', 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 () { + setIsDeferredCaptureEnable(true); + var formattedPlans = almaPlanHelperMock.getFormattedPlans(plansForCheckout); + var captureMethod = ''; + formattedPlans.forEach(function (formattedPlan) { + switch (formattedPlan.name) { + case 'ALMA_PNX': + case 'ALMA_PAY_NOW': + captureMethod = 'manual'; + break; + default: + captureMethod = 'automatic'; + } + expect(formattedPlan.plans[0]).to.have.deep.property('captureMethod', captureMethod); + }); + }); + }); +}); + From edc3dc3cfbf657615cc5061a5d616322f78dd47d Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 21 Sep 2023 16:53:27 +0200 Subject: [PATCH 47/66] add captureMethod in plan for checkout --- test/mocks/data/plans.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mocks/data/plans.js b/test/mocks/data/plans.js index 16d98bb..5fe3727 100644 --- a/test/mocks/data/plans.js +++ b/test/mocks/data/plans.js @@ -472,4 +472,4 @@ var formattedPlansForCheckoutExpected = [ module.exports = { plansForCheckout: plansForCheckout, formattedPlansForCheckoutExpected: formattedPlansForCheckoutExpected -}; \ No newline at end of file +}; From 4633c4f41ed1596ee16aa112f563370bde010551 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 21 Sep 2023 17:43:04 +0200 Subject: [PATCH 48/66] make plan factory for test --- test/mocks/data/plans.js | 360 ++++----------------------------------- 1 file changed, 34 insertions(+), 326 deletions(-) diff --git a/test/mocks/data/plans.js b/test/mocks/data/plans.js index 5fe3727..25855d2 100644 --- a/test/mocks/data/plans.js +++ b/test/mocks/data/plans.js @@ -1,195 +1,61 @@ 'use strict'; + +const sinon = require('sinon'); + +function mockPlanFactory(count) { + var plans = []; + for (var i = 0; i < count; i++) { + var localizedDueDate = i + ' months later'; + if (i === 0) { + localizedDueDate = 'Order processing'; + } + var plan = { + due_date: 1695300910, + purchase_amount: 15000, + customer_fee: 0, + customer_interest: 0, + total_amount: 30000, + localized_due_date: localizedDueDate, + time_delta_from_start: null + }; + plans.push(plan); + } + return plans; +} + var plansForCheckout = { ALMA_PAY_NOW: { ALMA_general_1_0: { installments_count: 1, deferred_days: 0, - payment_plans: [ - { - due_date: 1696596910, - purchase_amount: 30000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: 'Order processing', - time_delta_from_start: null - } - ] + payment_plans: mockPlanFactory(1) } }, ALMA_DEFERRED: { ALMA_general_1_15: { installments_count: 1, deferred_days: 15, - payment_plans: [ - { - due_date: 1696596910, - purchase_amount: 30000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: 'Order processing', - time_delta_from_start: null - } - ] + payment_plans: mockPlanFactory(1) } }, ALMA_PNX: { ALMA_general_2_0: { installments_count: 2, deferred_days: 0, - payment_plans: [ - { - due_date: 1695300910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: 'Order processing', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '1 months later', - time_delta_from_start: null - } - ] + payment_plans: mockPlanFactory(2) }, ALMA_general_3_0: { installments_count: 3, deferred_days: 0, - payment_plans: [ - { - due_date: 1695300910, - purchase_amount: 10000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: 'Order processing', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 10000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '1 months later', - time_delta_from_start: null - }, - { - due_date: 1700571310, - purchase_amount: 10000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '2 months later', - time_delta_from_start: null - } - ] + payment_plans: mockPlanFactory(3) } }, ALMA_CREDIT: { ALMA_general_10_0: { installments_count: 10, deferred_days: 0, - payment_plans: [ - { - due_date: 1695300910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: 'Order processing', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '1 months later', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '2 months later', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '3 months later', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '4 months later', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '5 months later', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '6 months later', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '7 months later', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '8 months later', - time_delta_from_start: null - }, - { - due_date: 1697892910, - purchase_amount: 15000, - customer_fee: 0, - customer_interest: 0, - total_amount: 30000, - localized_due_date: '9 months later', - time_delta_from_start: null - } - ] + payment_plans: mockPlanFactory(10) } } }; @@ -203,17 +69,7 @@ var formattedPlansForCheckoutExpected = [ installments_count: 1, deferred_days: 0, captureMethod: 'automatic', - payment_plans: [ - { - customer_fee: 0, - customer_interest: 0, - due_date: 1696596910, - localized_due_date: 'Order processing', - purchase_amount: 30000, - time_delta_from_start: null, - total_amount: 30000 - } - ], + payment_plans: mockPlanFactory(1), properties: { credit: { amount: '', @@ -236,18 +92,8 @@ var formattedPlansForCheckoutExpected = [ { installments_count: 1, deferred_days: 15, + payment_plans: mockPlanFactory(1), captureMethod: 'automatic', - payment_plans: [ - { - customer_fee: 0, - customer_interest: 0, - due_date: 1696596910, - localized_due_date: 'Order processing', - purchase_amount: 30000, - time_delta_from_start: null, - total_amount: 30000 - } - ], properties: { credit: { amount: '', @@ -270,27 +116,8 @@ var formattedPlansForCheckoutExpected = [ { installments_count: 2, deferred_days: 0, + payment_plans: mockPlanFactory(2), captureMethod: 'automatic', - payment_plans: [ - { - customer_fee: 0, - customer_interest: 0, - due_date: 1695300910, - localized_due_date: 'Order processing', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '1 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - } - ], properties: { credit: { amount: '', @@ -307,36 +134,8 @@ var formattedPlansForCheckoutExpected = [ { installments_count: 3, deferred_days: 0, + payment_plans: mockPlanFactory(3), captureMethod: 'automatic', - payment_plans: [ - { - customer_fee: 0, - customer_interest: 0, - due_date: 1695300910, - localized_due_date: 'Order processing', - purchase_amount: 10000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '1 months later', - purchase_amount: 10000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1700571310, - localized_due_date: '2 months later', - purchase_amount: 10000, - time_delta_from_start: null, - total_amount: 30000 - } - ], properties: { credit: { amount: '', @@ -359,99 +158,8 @@ var formattedPlansForCheckoutExpected = [ { installments_count: 10, deferred_days: 0, + payment_plans: mockPlanFactory(10), captureMethod: 'automatic', - payment_plans: [ - { - customer_fee: 0, - customer_interest: 0, - due_date: 1695300910, - localized_due_date: 'Order processing', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '1 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '2 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '3 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '4 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '5 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '6 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '7 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '8 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - }, - { - customer_fee: 0, - customer_interest: 0, - due_date: 1697892910, - localized_due_date: '9 months later', - purchase_amount: 15000, - time_delta_from_start: null, - total_amount: 30000 - } - ], properties: { credit: { amount: '', From 3e6e91089d30dc9eb2ad20be42b0f7b640783c09 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Mon, 25 Sep 2023 11:15:18 +0200 Subject: [PATCH 49/66] Add input capture amount field in BO --- .../cartridge/scripts/helpers/almaConfigHelper.js | 11 ++++++++++- .../ref/system-objecttype-extensions.xml | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js index d0aad0c..fdf5521 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js @@ -17,7 +17,16 @@ function isInpageActivated() { return Site.getCurrent().getCustomPreferenceValue('ALMA_Inpage_Payment'); } +/** + * Return amount for partial capture + * @return {number} the amount value + */ +function getPartialCaptureAmount() { + return Site.getCurrent().getCustomPreferenceValue('ALMA_Deferred_Capture_Partial_Amount'); +} + module.exports = { isDeferredCaptureEnable: isDeferredCaptureEnable, - isInpageActivated: isInpageActivated + isInpageActivated: isInpageActivated, + getPartialCaptureAmount: getPartialCaptureAmount }; diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index ca1c9d4..3a4e48c 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -31,6 +31,12 @@ + + Amount for partial capture + double + false + true + @@ -38,6 +44,7 @@ + From dd1ebfe592bb8fd93236c5e7b24d0986542f1d26 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Mon, 25 Sep 2023 11:35:44 +0200 Subject: [PATCH 50/66] Partial capture get amount value unit test --- test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js index 5b608ce..89541d2 100644 --- a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js @@ -22,5 +22,10 @@ describe('Get back-office variables', function () { setCustomPreferenceValue(false); assert.equal(false, almaConfigHelpers.isDeferredCaptureEnable()); }); + + it('should call site preferences and return default value for partial capture amount', function () { + setCustomPreferenceValue(100); + assert.equal(100, almaConfigHelpers.getPartialCaptureAmount()); + }); }); }); From f9295265769bd99fb7ba3c45003fa692b87b779e Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Mon, 25 Sep 2023 17:20:09 +0200 Subject: [PATCH 51/66] =?UTF-8?q?cancel=20order=20on=20payment=E2=80=99s?= =?UTF-8?q?=20auth=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 3742e24..17a0e40 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 52/66] 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 e0987cc..f553ff7 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 afb1aec..64030cd 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 () { From 773e49b9ee4fcb8cbd0e0e2bc709c7e1d9d6c3a4 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 10 Oct 2023 11:41:29 +0200 Subject: [PATCH 53/66] get and set partial capture fields in Order helper --- .../scripts/helpers/almaConfigHelper.js | 11 +---- .../scripts/helpers/almaOrderHelper.js | 43 ++++++++++++++----- 2 files changed, 33 insertions(+), 21 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js index fdf5521..d0aad0c 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper.js @@ -17,16 +17,7 @@ function isInpageActivated() { return Site.getCurrent().getCustomPreferenceValue('ALMA_Inpage_Payment'); } -/** - * Return amount for partial capture - * @return {number} the amount value - */ -function getPartialCaptureAmount() { - return Site.getCurrent().getCustomPreferenceValue('ALMA_Deferred_Capture_Partial_Amount'); -} - module.exports = { isDeferredCaptureEnable: isDeferredCaptureEnable, - isInpageActivated: isInpageActivated, - getPartialCaptureAmount: getPartialCaptureAmount + isInpageActivated: isInpageActivated }; diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js index 6659cef..5220bc0 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaOrderHelper.js @@ -27,15 +27,26 @@ function addAlmaPaymentDetails(order, payDetail) { } /** - * Set alma deferred capture in order - * @param {Order} order order - * @param {string} deferredCapture payDetail + * Set alma deferred capture status in order + * @param {Object} order order + * @param {string} deferredCaptureStatus payDetail + * @param {number} [amount] amount capture */ -function setAlmaDeferredCapture(order, deferredCapture) { - Transaction.wrap(function () { - // eslint-disable-next-line no-param-reassign - order.custom.ALMA_Deferred_Capture = deferredCapture; - }); +function setAlmaDeferredCaptureFields(order, deferredCaptureStatus, amount) { + if (amount) { + Transaction.wrap(function () { + // eslint-disable-next-line no-param-reassign + order.custom.ALMA_Deferred_Capture_Status = deferredCaptureStatus; + order.custom.ALMA_Deferred_Capture_Partial_Amount_Captured = amount; + order.custom.ALMA_Deferred_Capture_Partial_Amount = null; + }); + } else { + Transaction.wrap(function () { + // eslint-disable-next-line no-param-reassign + order.custom.ALMA_Deferred_Capture_Status = deferredCaptureStatus; + order.custom.ALMA_Deferred_Capture_Partial_Amount = null; + }); + } } /** @@ -48,16 +59,26 @@ function setAlmaDeferredCapture(order, deferredCapture) { function addAlmaDataToOrder(pid, order, isDeferredCapture) { addPidToOrder(order, pid); if (isDeferredCapture) { - setAlmaDeferredCapture(order, 'ToCapture'); + setAlmaDeferredCaptureFields(order, 'ToCapture'); } else { - setAlmaDeferredCapture(order, 'Captured'); + setAlmaDeferredCaptureFields(order, 'Captured'); } } +/** + * Return amount for partial capture + * @param {Object} order order from get partial capture amount + * @return {number|null} the amount value + */ +function getPartialCaptureAmount(order) { + return order.custom.ALMA_Deferred_Capture_Partial_Amount; +} + module.exports = { addPidToOrder: addPidToOrder, addAlmaPaymentDetails: addAlmaPaymentDetails, addAlmaDataToOrder: addAlmaDataToOrder, - setAlmaDeferredCapture: setAlmaDeferredCapture + setAlmaDeferredCaptureFields: setAlmaDeferredCaptureFields, + getPartialCaptureAmount: getPartialCaptureAmount }; From 1238c95cb375102a937fc55a08e7cbe34f3f5603 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 10 Oct 2023 11:44:10 +0200 Subject: [PATCH 54/66] add partial capture and variables to simplify code --- .../scripts/helpers/almaPaymentHelper.js | 18 +++++++++++++++++- .../scripts/steps/CapturePaymentOrders.js | 18 +++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index c20efad..ad4e91d 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -2,6 +2,21 @@ var pkg = require('../../../package.json'); +var Capture = { + total: { + code: 'Captured', + description: 'Total Capture' + }, + partial: { + code: 'PartialCaptured', + description: 'Partial captured' + }, + failed: { + code: 'Failed', + description: 'Unable to capture' + } +}; + /** * Allow to check an order status * @param {dw.order.Order} order the order to check @@ -461,5 +476,6 @@ module.exports = { setOrderMerchantReference: setOrderMerchantReference, capturePayment: capturePayment, isAvailableForManualCapture: isAvailableForManualCapture, - cancelAlmaPayment: cancelAlmaPayment + cancelAlmaPayment: cancelAlmaPayment, + Capture: Capture }; diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index 9797d60..24416e6 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -8,6 +8,7 @@ var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper') var Logger = require('dw/system/Logger'); exports.execute = function () { + var Capture = almaPaymentHelper.Capture; var orders = OrderMgr.searchOrders( "custom.ALMA_Deferred_Capture='ToCapture' and status != {0} and status != {1}", null, @@ -19,14 +20,21 @@ exports.execute = function () { 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; + almaOrderHelper.setAlmaDeferredCaptureFields(order, captureType.code, amount); var capture = almaPaymentHelper.capturePayment(params); - almaOrderHelper.setAlmaDeferredCapture(order, 'Captured'); - Logger.info('Capture payment: order id: {0} - payment id: {1} - capture id : {2}', [order.orderNo, order.custom.almaPaymentId, capture.id]); + Logger.info(captureType.description + ' payment: order id: {0} - payment id: {1} - capture id : {2}', [order.orderNo, order.custom.almaPaymentId, capture.id]); } catch (e) { - almaOrderHelper.setAlmaDeferredCapture(order, 'Failed'); - Logger.warn('Unable to capture payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); + almaOrderHelper.setAlmaDeferredCaptureFields(order, Capture.failed.code); + Logger.warn(Capture.failed.description + ' payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); } } } From f4be62562afcf8d8c954a6edc8218f29d5005e1c Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 10 Oct 2023 11:45:18 +0200 Subject: [PATCH 55/66] update site pref builder for partial capture --- .../scripts/steps/CapturePaymentOrders.js | 2 +- .../cartridge/scripts/steps/CheckRefund.js | 4 ++-- .../ref/system-objecttype-extensions.xml | 18 +++++++++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index 24416e6..5628123 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -10,7 +10,7 @@ var Logger = require('dw/system/Logger'); exports.execute = function () { var Capture = almaPaymentHelper.Capture; var orders = OrderMgr.searchOrders( - "custom.ALMA_Deferred_Capture='ToCapture' and status != {0} and status != {1}", + "custom.ALMA_Deferred_Capture_Status='ToCapture' and status != {0} and status != {1}", null, Order.ORDER_STATUS_FAILED, Order.ORDER_STATUS_CANCELLED diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index f43c4cd..f7dfdea 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -51,7 +51,7 @@ exports.execute = function () { var orderItem = orders.next(); if (isOrderToBeRefund(orderItem)) { try { - if (orderItem.custom.ALMA_Deferred_Capture === 'ToCapture') { + if (orderItem.custom.ALMA_Deferred_Capture_Status === 'ToCapture') { var amount = 0; var deferredStatus = 'ToCapture'; if (orderItem.custom.almaRefundType.toString() === 'Total') { @@ -68,7 +68,7 @@ exports.execute = function () { /* jshint loopfunc: true */ // eslint-disable-next-line no-loop-func Transaction.wrap(function () { - orderItem.custom.ALMA_Deferred_Capture = deferredStatus; + 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 diff --git a/site_preference_builder/ref/system-objecttype-extensions.xml b/site_preference_builder/ref/system-objecttype-extensions.xml index 3a4e48c..e630691 100644 --- a/site_preference_builder/ref/system-objecttype-extensions.xml +++ b/site_preference_builder/ref/system-objecttype-extensions.xml @@ -14,7 +14,7 @@ false 0 - + Capture Status enum-of-string false @@ -23,6 +23,9 @@ ToCapture + + PartialCaptured + Captured @@ -35,6 +38,12 @@ Amount for partial capture double false + false + + + Amount captured + double + false true @@ -43,10 +52,13 @@ Alma - + + + Alma Deferred Capture + + - From 138e8e35d0f730396cdbae814e6b4f261566c556 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 10 Oct 2023 11:46:03 +0200 Subject: [PATCH 56/66] add and rectify unit tests for partial capture --- test/mocks/helpers/almaConfigHelpers.js | 12 ++++- test/mocks/steps/CapturePaymentOrders.js | 12 +++-- .../scripts/helpers/almaConfigHelperTest.js | 5 -- .../scripts/helpers/almaOrderHelperTest.js | 2 +- .../scripts/steps/CapturePaymentOrdersTest.js | 48 +++++++++++-------- .../int_alma/scripts/steps/CheckRefundTest.js | 2 +- 6 files changed, 49 insertions(+), 32 deletions(-) diff --git a/test/mocks/helpers/almaConfigHelpers.js b/test/mocks/helpers/almaConfigHelpers.js index fdf17f5..79c74fe 100644 --- a/test/mocks/helpers/almaConfigHelpers.js +++ b/test/mocks/helpers/almaConfigHelpers.js @@ -6,8 +6,10 @@ var proxyquire = require('proxyquire') var sinon = require('sinon'); var getCustomPreferenceValue; + function setCustomPreferenceValue(value) { - getCustomPreferenceValue = sinon.stub().returns(value); + getCustomPreferenceValue = sinon.stub() + .returns(value); } @@ -19,6 +21,13 @@ var site = { } }; +var order = { + custom: { + ALMA_Deferred_Capture_Partial_Amount: 100 + } +}; + + function proxyModel() { return proxyquire('../../../cartridges/int_alma/cartridge/scripts/helpers/almaConfigHelper', { 'dw/system/Site': site @@ -28,5 +37,6 @@ function proxyModel() { module.exports = { almaConfigHelpers: proxyModel(), site: site, + order: order, setCustomPreferenceValue: setCustomPreferenceValue }; diff --git a/test/mocks/steps/CapturePaymentOrders.js b/test/mocks/steps/CapturePaymentOrders.js index 06166a0..0ad6cbc 100644 --- a/test/mocks/steps/CapturePaymentOrders.js +++ b/test/mocks/steps/CapturePaymentOrders.js @@ -5,18 +5,21 @@ var proxyquire = require('proxyquire') .noPreserveCache(); var sinon = require('sinon'); - +var almaConfigHelpers = require('../helpers/almaConfigHelpers').almaConfigHelpers; +var Capture = require('../../../cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper').Capture; var OrderMgr = { searchOrders: sinon.stub() .returns({}) }; var almaPaymentHelper = { - capturePayment: sinon.stub() + capturePayment: sinon.stub(), + Capture: Capture }; var almaOrderHelper = { - setAlmaDeferredCapture: sinon.stub() + setAlmaDeferredCaptureFields: sinon.stub(), + getPartialCaptureAmount: sinon.stub().returns(null) }; var warnStub = sinon.stub(); @@ -37,7 +40,8 @@ function proxyModel() { 'dw/system/Logger': logger, 'dw/system/Status': sinon.mock(), '*/cartridge/scripts/helpers/almaPaymentHelper': almaPaymentHelper, - '*/cartridge/scripts/helpers/almaOrderHelper': almaOrderHelper + '*/cartridge/scripts/helpers/almaOrderHelper': almaOrderHelper, + '*/cartridge/scripts/helpers/almaConfigHelper': almaConfigHelpers }); } diff --git a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js index 89541d2..5b608ce 100644 --- a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js @@ -22,10 +22,5 @@ describe('Get back-office variables', function () { setCustomPreferenceValue(false); assert.equal(false, almaConfigHelpers.isDeferredCaptureEnable()); }); - - it('should call site preferences and return default value for partial capture amount', function () { - setCustomPreferenceValue(100); - assert.equal(100, almaConfigHelpers.getPartialCaptureAmount()); - }); }); }); diff --git a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js index 043ece4..11b4da6 100644 --- a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js @@ -10,7 +10,7 @@ var createNewTransaction = require('../../../../mocks/helpers/almaOrderHelpers') var order = { custom: { almaPaymentId: '', - ALMA_Deferred_Capture: '' + ALMA_Deferred_Capture_Status: '' } }; describe('Alma order helper', function () { diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js index 44345b7..2f72306 100644 --- a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -5,6 +5,7 @@ var OrderMgr = require('../../../../mocks/steps/CapturePaymentOrders').OrderMgr; var almaPaymentHelper = require('../../../../mocks/steps/CapturePaymentOrders').almaPaymentHelper; var CapturePaymentOrders = require('../../../../mocks/steps/CapturePaymentOrders').CapturePaymentOrders; var almaOrderHelper = require('../../../../mocks/steps/CapturePaymentOrders').almaOrderHelper; +var setPartialCaptureAmount = require('../../../../mocks/steps/CapturePaymentOrders').setPartialCaptureAmount; var warnStub = require('../../../../mocks/steps/CapturePaymentOrders').warnStub; function hasNextFactory(count) { @@ -30,7 +31,8 @@ function nextFactory(count) { .returns({ custom: { almaPaymentId: 'payment_' + i - } + }, + getTotalGrossPrice: sinon.stub().returns({ value: 100 }) }); } return next; @@ -62,7 +64,7 @@ describe('Deferred capture job', function () { sinon.assert.calledOnce(OrderMgr.searchOrders); // status : 8 failed - 6 canceled - sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture='ToCapture' and status != {0} and status != {1}", null, 8, 6); + sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture_Status='ToCapture' and status != {0} and status != {1}", null, 8, 6); }); it('Should not call Capture where their is no order', function () { @@ -83,41 +85,47 @@ describe('Deferred capture job', function () { sinon.assert.callCount(almaPaymentHelper.capturePayment, count); for (var i = 0; i < count; i++) { - sinon.assert.calledWith(almaPaymentHelper.capturePayment.getCall(i), { external_id: 'payment_' + i }); + sinon.assert.calledWith(almaPaymentHelper.capturePayment.getCall(i), { external_id: 'payment_' + i, amount: 10000 }); } }); it('Should set capture ID when capture is validated', function () { CapturePaymentOrders.execute(); - sinon.assert.calledOnce(almaOrderHelper.setAlmaDeferredCapture); - sinon.assert.calledWith( - almaOrderHelper.setAlmaDeferredCapture, - { - custom: { - almaPaymentId: 'payment_0' - } - }, - 'Captured' + sinon.assert.calledOnce(almaOrderHelper.setAlmaDeferredCaptureFields); + sinon.assert.calledWithMatch( + almaOrderHelper.setAlmaDeferredCaptureFields, + {}, + 'Captured', + 100 ); }); - it('Should not call setAlmaDeferredCapture when capture throw an error', function () { + it('Should not call setAlmaDeferredCaptureFields when capture throw an error', function () { almaPaymentHelper.capturePayment = sinon.stub() .throws(); CapturePaymentOrders.execute(); sinon.assert.calledOnce(warnStub); - sinon.assert.calledWith( - almaOrderHelper.setAlmaDeferredCapture, - { - custom: { - almaPaymentId: 'payment_0' - } - }, + sinon.assert.calledWithMatch( + almaOrderHelper.setAlmaDeferredCaptureFields, + {}, 'Failed' ); }); + + it('should get call capture with right amount and code for partial capture', function () { + almaOrderHelper.getPartialCaptureAmount = sinon.stub().returns(25); + CapturePaymentOrders.execute(); + + sinon.assert.calledOnce(almaOrderHelper.setAlmaDeferredCaptureFields); + sinon.assert.calledWithMatch( + almaOrderHelper.setAlmaDeferredCaptureFields, + {}, + 'PartialCaptured', + 25 + ); + }); }); diff --git a/test/unit/int_alma/scripts/steps/CheckRefundTest.js b/test/unit/int_alma/scripts/steps/CheckRefundTest.js index fa626db..2be6d14 100644 --- a/test/unit/int_alma/scripts/steps/CheckRefundTest.js +++ b/test/unit/int_alma/scripts/steps/CheckRefundTest.js @@ -17,7 +17,7 @@ function orderFactory(count, refundType, partialRefundAmount, capture) { } }, almaWantedRefundAmount: partialRefundAmount, - ALMA_Deferred_Capture: capture + ALMA_Deferred_Capture_Status: capture }, totalGrossPrice: { value: 10000 From e4b1baacd9c29d2b775d98f0670240aceecadee0 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 10 Oct 2023 11:46:51 +0200 Subject: [PATCH 57/66] allow Alma payment methods for country GB --- site_preference_builder/ref/payment/payment-methods.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/site_preference_builder/ref/payment/payment-methods.xml b/site_preference_builder/ref/payment/payment-methods.xml index 2d20a66..62507a7 100644 --- a/site_preference_builder/ref/payment/payment-methods.xml +++ b/site_preference_builder/ref/payment/payment-methods.xml @@ -7,6 +7,7 @@ ALMA + EUR @@ -22,6 +23,7 @@ ALMA + EUR @@ -37,6 +39,7 @@ ALMA + EUR @@ -52,6 +55,7 @@ ALMA + EUR From efa53f7b397b7c022a26cfb646be2c4a9b83f7a3 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 10 Oct 2023 11:48:05 +0200 Subject: [PATCH 58/66] rm unused function in unit tests --- test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js index 2f72306..52effeb 100644 --- a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -5,7 +5,6 @@ var OrderMgr = require('../../../../mocks/steps/CapturePaymentOrders').OrderMgr; var almaPaymentHelper = require('../../../../mocks/steps/CapturePaymentOrders').almaPaymentHelper; var CapturePaymentOrders = require('../../../../mocks/steps/CapturePaymentOrders').CapturePaymentOrders; var almaOrderHelper = require('../../../../mocks/steps/CapturePaymentOrders').almaOrderHelper; -var setPartialCaptureAmount = require('../../../../mocks/steps/CapturePaymentOrders').setPartialCaptureAmount; var warnStub = require('../../../../mocks/steps/CapturePaymentOrders').warnStub; function hasNextFactory(count) { From eb2cd20081aa601054436fb90a28b792e6189045 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Tue, 10 Oct 2023 12:03:08 +0200 Subject: [PATCH 59/66] rm github codeowners --- .github/CODEOWNERS | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS deleted file mode 100644 index 355d8d2..0000000 --- a/.github/CODEOWNERS +++ /dev/null @@ -1,2 +0,0 @@ -* simon.joyet@getalma.eu -* francois.gomis@getalma.eu From e9543ee1143e464485573439c6dd8b97ea212b6a Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 11 Oct 2023 10:48:54 +0200 Subject: [PATCH 60/66] =?UTF-8?q?set=20order=E2=80=99s=20deferred=20attrib?= =?UTF-8?q?utes=20after=20capture=20payment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../int_alma/cartridge/scripts/steps/CapturePaymentOrders.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index 5628123..afb91c8 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -29,8 +29,8 @@ exports.execute = function () { amount = partialCaptureAmount; } params.amount = amount * 100; - almaOrderHelper.setAlmaDeferredCaptureFields(order, captureType.code, amount); 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); From 688f5b8a18ae8cbbb574e56c00ff5b73b8392421 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 11 Oct 2023 15:04:35 +0200 Subject: [PATCH 61/66] update changelog --- CHANGELOG.txt | 5 +++++ cartridges/int_alma/package.json | 2 +- package.json | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 4b3c473..735269a 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,6 +1,11 @@ CHANGELOG ========= +v4.5.0 +------ +* Feat: add Deferred Capture feature. +* Chore: add pre-commit tool. + v4.3.1 ------ * Feat: add Pay Now with credit card with Alma. diff --git a/cartridges/int_alma/package.json b/cartridges/int_alma/package.json index e020ad7..aba9b71 100644 --- a/cartridges/int_alma/package.json +++ b/cartridges/int_alma/package.json @@ -1,4 +1,4 @@ { - "version": "4.3.1", + "version": "4.5.0", "hooks": "./hooks.json" } diff --git a/package.json b/package.json index 1eddca9..3b3c6aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alma-sfcc-plugin", - "version": "4.3.1", + "version": "4.5.0", "description": "Alma SFCC integration plugin", "main": "index.js", "engines": { From 831093d286737cbc42762f7ae4b1cb2773d4f871 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Wed, 11 Oct 2023 15:16:34 +0200 Subject: [PATCH 62/66] update unit test with the good alma_plugin_version --- test/mocks/helpers/almaPaymentHelpers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/mocks/helpers/almaPaymentHelpers.js b/test/mocks/helpers/almaPaymentHelpers.js index cf23410..691aa32 100644 --- a/test/mocks/helpers/almaPaymentHelpers.js +++ b/test/mocks/helpers/almaPaymentHelpers.js @@ -102,7 +102,7 @@ function resolvedPaymentData(installmentsCount, defferedDays, locale, origin, ha custom_data: { cms_name: 'SFCC', cms_version: '4.0.0', - alma_plugin_version: '4.3.1' + alma_plugin_version: '4.5.0' } }, customer: { From 9aa03984ffcb4b082e421f8b41fc37cfa6005634 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 12 Oct 2023 16:31:04 +0200 Subject: [PATCH 63/66] add some constant and rename others --- .../cartridge/scripts/helpers/almaPaymentHelper.js | 11 ++++++++--- .../cartridge/scripts/helpers/almaPlanHelper.js | 12 +++++++++--- .../cartridge/scripts/steps/CapturePaymentOrders.js | 13 +++++++------ test/mocks/steps/CapturePaymentOrders.js | 4 ++-- .../scripts/steps/CapturePaymentOrdersTest.js | 6 +++++- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index f761a3c..abac1d4 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -2,7 +2,10 @@ var pkg = require('../../../package.json'); -var Capture = { +var CAPTURE = { + toCapture: { + code: 'ToCapture' + }, total: { code: 'Captured', description: 'Total Capture' @@ -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 @@ -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; } /** @@ -498,7 +503,7 @@ module.exports = { capturePayment: capturePayment, isAvailableForManualCapture: isAvailableForManualCapture, cancelAlmaPayment: cancelAlmaPayment, - Capture: Capture, + CAPTURE: CAPTURE, isPaymentExpired: isPaymentExpired, isPaymentAuthorizationExpired: isPaymentAuthorizationExpired }; diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js index 33211e1..28f92cf 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPlanHelper.js @@ -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 @@ -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) { @@ -257,5 +262,6 @@ module.exports = { getAllowedPlans: getAllowedPlans, getPlansForWidget: getPlansForWidget, getPlansForCheckout: getPlansForCheckout, - getFormattedPlans: getFormattedPlans + getFormattedPlans: getFormattedPlans, + CAPTURE_METHOD: CAPTURE_METHOD }; diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index afb91c8..13c26ec 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -8,10 +8,11 @@ var almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper') var Logger = require('dw/system/Logger'); exports.execute = function () { - var Capture = almaPaymentHelper.Capture; + 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 ); @@ -21,11 +22,11 @@ exports.execute = function () { var order = orders.next(); var params = { external_id: order.custom.almaPaymentId }; var amount = order.getTotalGrossPrice().value; - var captureType = Capture.total; + var captureType = CAPTURE.total; var partialCaptureAmount = almaOrderHelper.getPartialCaptureAmount(order); try { if (partialCaptureAmount) { - captureType = Capture.partial; + captureType = CAPTURE.partial; amount = partialCaptureAmount; } params.amount = amount * 100; @@ -33,8 +34,8 @@ exports.execute = function () { 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]); + almaOrderHelper.setAlmaDeferredCaptureFields(order, CAPTURE.failed.code); + Logger.warn(CAPTURE.failed.description + ' payment: order id: {0}, payment id: {1}', [order.orderNo, order.custom.almaPaymentId]); } } } diff --git a/test/mocks/steps/CapturePaymentOrders.js b/test/mocks/steps/CapturePaymentOrders.js index 0ad6cbc..a3bf854 100644 --- a/test/mocks/steps/CapturePaymentOrders.js +++ b/test/mocks/steps/CapturePaymentOrders.js @@ -6,7 +6,7 @@ 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({}) @@ -14,7 +14,7 @@ var OrderMgr = { var almaPaymentHelper = { capturePayment: sinon.stub(), - Capture: Capture + CAPTURE: CAPTURE }; var almaOrderHelper = { diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js index 52effeb..40b9fe9 100644 --- a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -63,7 +63,11 @@ describe('Deferred capture job', function () { sinon.assert.calledOnce(OrderMgr.searchOrders); // status : 8 failed - 6 canceled - sinon.assert.calledWith(OrderMgr.searchOrders, "custom.ALMA_Deferred_Capture_Status='ToCapture' and status != {0} and status != {1}", null, 8, 6); + sinon.assert.calledWith(OrderMgr.searchOrders, 'custom.ALMA_Deferred_Capture_Status={0} and status != {1} and status != {2}', + null, + 'ToCapture', + 8, + 6); }); it('Should not call Capture where their is no order', function () { From 6bba0c7ada221f37a429c1bdaaa21a220e30321d Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 12 Oct 2023 16:40:54 +0200 Subject: [PATCH 64/66] extract some function --- .../scripts/steps/CapturePaymentOrders.js | 53 +++++++++------- .../cartridge/scripts/steps/CheckRefund.js | 61 +++++++++++-------- 2 files changed, 66 insertions(+), 48 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js index 13c26ec..bdd1c5c 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CapturePaymentOrders.js @@ -1,13 +1,41 @@ '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 OrderMgr = require('dw/order/OrderMgr'); var CAPTURE = almaPaymentHelper.CAPTURE; var orders = OrderMgr.searchOrders( 'custom.ALMA_Deferred_Capture_Status={0} and status != {1} and status != {2}', @@ -18,26 +46,7 @@ exports.execute = function () { ); 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); diff --git a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js index f7dfdea..d30ddac 100644 --- a/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js +++ b/cartridges/int_alma/cartridge/scripts/steps/CheckRefund.js @@ -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.'); @@ -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); } From 06080027b4f74832fa6b5a5c9104466fefcfadcc Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Thu, 12 Oct 2023 16:58:33 +0200 Subject: [PATCH 65/66] rename some unit tests --- .../scripts/helpers/almaCheckoutHelperTest.js | 20 ++++++++-------- .../scripts/helpers/almaConfigHelperTest.js | 4 ++-- .../helpers/almaEligibilityHelperTest.js | 2 +- .../scripts/helpers/almaOrderHelperTest.js | 2 +- .../scripts/helpers/almaPaymentHelperTest.js | 2 +- .../scripts/helpers/almaPlanHelperTest.js | 6 ++--- .../scripts/helpers/almaRefundHelperTest.js | 23 ++++++++----------- .../scripts/steps/CapturePaymentOrdersTest.js | 23 +++++++++++-------- .../int_alma/scripts/steps/CheckRefundTest.js | 22 +++++++++--------- 9 files changed, 52 insertions(+), 52 deletions(-) diff --git a/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js b/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js index 17a6561..d286275 100644 --- a/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaCheckoutHelperTest.js @@ -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); @@ -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'); @@ -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, { @@ -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, { @@ -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, { @@ -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'); }); diff --git a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js index 5b608ce..6951fe0 100644 --- a/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaConfigHelperTest.js @@ -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(); @@ -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()); }); diff --git a/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js index b615945..2bfd0a1 100644 --- a/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaEligibilityHelperTest.js @@ -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; diff --git a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js index 11b4da6..b0d8ba7 100644 --- a/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaOrderHelperTest.js @@ -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); diff --git a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js index 218b987..ade2351 100644 --- a/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPaymentHelperTest.js @@ -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); diff --git a/test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js b/test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js index 75d49eb..50c3ecf 100644 --- a/test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaPlanHelperTest.js @@ -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 = ''; diff --git a/test/unit/int_alma/scripts/helpers/almaRefundHelperTest.js b/test/unit/int_alma/scripts/helpers/almaRefundHelperTest.js index 05b8da6..efe5ebf 100644 --- a/test/unit/int_alma/scripts/helpers/almaRefundHelperTest.js +++ b/test/unit/int_alma/scripts/helpers/almaRefundHelperTest.js @@ -16,13 +16,13 @@ var order = { var almaRefundHelpers = require('../../../../mocks/helpers/almaRefundHelpers'); var expect = require('chai').expect; -describe('almaRefundHelpers', function () { - it('check properties', function () { +describe('AlmaRefundHelpers', function () { + it('Order refunded amount is equal to total gross price for a total refund', function () { almaRefundHelpers.refundPaymentForOrder(order); assert.equal(order.custom.almaRefundedAmount, order.getTotalGrossPrice()); }); - it('when order is null', function () { + it('Refund throw an error when order is null', function () { expect(function () { almaRefundHelpers.refundPaymentForOrder(); }) @@ -30,13 +30,13 @@ describe('almaRefundHelpers', function () { .throw('Order not found'); }); - it('partial refund', function () { + it('Order refunded amount is equal to the partial refund', function () { order.custom.almaRefundedAmount = 0; almaRefundHelpers.refundPaymentForOrder(order, 10); assert.equal(order.custom.almaRefundedAmount, 10); }); - it('partial refund with a negative amount', function () { + it('Partial refund with a negative amount throw an error', function () { expect(function () { almaRefundHelpers.refundPaymentForOrder(order, -10); }) @@ -44,7 +44,7 @@ describe('almaRefundHelpers', function () { .throw('Amount can\'t be negative.'); }); - it('partial refund with an amount > to the total price', function () { + it('Partial refund with an amount > to the total price throw an error', function () { expect(function () { almaRefundHelpers.refundPaymentForOrder(order, 5000); }) @@ -52,14 +52,14 @@ describe('almaRefundHelpers', function () { .throw('Amount can\'t be upper than order total gross price.'); }); - it('check almaRefundedAmount for 2 partial refund', function () { + it('Order refunded amount is equal to 2 partial refund', function () { order.custom.almaRefundedAmount = 0; almaRefundHelpers.refundPaymentForOrder(order, 10); almaRefundHelpers.refundPaymentForOrder(order, 40); assert.equal(order.custom.almaRefundedAmount, 50); }); - it('2 partial refund more than total gross price', function () { + it('2 partial refund more than total gross price throw an error', function () { order.custom.almaRefundedAmount = 0; almaRefundHelpers.refundPaymentForOrder(order, 250); expect(function () { @@ -69,12 +69,7 @@ describe('almaRefundHelpers', function () { .throw('Amount can\'t be upper than order total gross price less refunded amount.'); }); - it('check almaRefundedAmount for total refund', function () { - order.custom.almaRefundedAmount = 0; - almaRefundHelpers.refundPaymentForOrder(order); - assert.equal(order.custom.almaRefundedAmount, order.getTotalGrossPrice()); - }); - it('check almaWantedRefundAmount goes back to 0', function () { + it('Order refunded amount wanted goes back to 0 after refund', function () { almaRefundHelpers.refundPaymentForOrder(order); assert.equal(order.custom.almaWantedRefundAmount, 0); }); diff --git a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js index 40b9fe9..7f0e0c8 100644 --- a/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js +++ b/test/unit/int_alma/scripts/steps/CapturePaymentOrdersTest.js @@ -31,7 +31,8 @@ function nextFactory(count) { custom: { almaPaymentId: 'payment_' + i }, - getTotalGrossPrice: sinon.stub().returns({ value: 100 }) + getTotalGrossPrice: sinon.stub() + .returns({ value: 100 }) }); } return next; @@ -58,7 +59,7 @@ describe('Deferred capture job', function () { sinon.reset(); }); - it('Should get orders with deferred capture', function () { + it('Should get orders with deferred capture status equal to ToCapture', function () { CapturePaymentOrders.execute(); sinon.assert.calledOnce(OrderMgr.searchOrders); @@ -70,7 +71,7 @@ describe('Deferred capture job', function () { 6); }); - it('Should not call Capture where their is no order', function () { + it('Should not call Capture endpoint where their is no order', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(0)); @@ -79,7 +80,7 @@ describe('Deferred capture job', function () { sinon.assert.notCalled(almaPaymentHelper.capturePayment); }); - it('Should call Capture for orders with deferred capture', function () { + it('Should call Capture endpoint for orders with deferred capture', function () { var count = 10; OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(count)); @@ -88,11 +89,14 @@ describe('Deferred capture job', function () { sinon.assert.callCount(almaPaymentHelper.capturePayment, count); for (var i = 0; i < count; i++) { - sinon.assert.calledWith(almaPaymentHelper.capturePayment.getCall(i), { external_id: 'payment_' + i, amount: 10000 }); + sinon.assert.calledWith(almaPaymentHelper.capturePayment.getCall(i), { + external_id: 'payment_' + i, + amount: 10000 + }); } }); - it('Should set capture ID when capture is validated', function () { + it('Order’s deferred status is equal to Captured when capture is validated', function () { CapturePaymentOrders.execute(); sinon.assert.calledOnce(almaOrderHelper.setAlmaDeferredCaptureFields); @@ -104,7 +108,7 @@ describe('Deferred capture job', function () { ); }); - it('Should not call setAlmaDeferredCaptureFields when capture throw an error', function () { + it('Order’s deferred status is equal to Failed when capture throw an error', function () { almaPaymentHelper.capturePayment = sinon.stub() .throws(); @@ -118,8 +122,9 @@ describe('Deferred capture job', function () { ); }); - it('should get call capture with right amount and code for partial capture', function () { - almaOrderHelper.getPartialCaptureAmount = sinon.stub().returns(25); + it('Should get call capture with right amount and code for partial capture', function () { + almaOrderHelper.getPartialCaptureAmount = sinon.stub() + .returns(25); CapturePaymentOrders.execute(); sinon.assert.calledOnce(almaOrderHelper.setAlmaDeferredCaptureFields); diff --git a/test/unit/int_alma/scripts/steps/CheckRefundTest.js b/test/unit/int_alma/scripts/steps/CheckRefundTest.js index 2be6d14..eae1ca3 100644 --- a/test/unit/int_alma/scripts/steps/CheckRefundTest.js +++ b/test/unit/int_alma/scripts/steps/CheckRefundTest.js @@ -64,7 +64,7 @@ describe('Refund job test', function () { afterEach(function () { sinon.reset(); }); - it('should call refund once per order for Total Refund', function () { + it('Should call refund once per order for Total Refund', function () { var count = 3; OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(count, 'Total', 10000, 'Captured')); @@ -72,7 +72,7 @@ describe('Refund job test', function () { CheckRefund.execute(); sinon.assert.callCount(refundHelper.refundPaymentForOrder, count); }); - it('should call refund for Total Refund with good params', function () { + it('Should call refund for Total Refund with good params', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Total', null, 'Captured')); @@ -92,7 +92,7 @@ describe('Refund job test', function () { ) ); }); - it('should call partial refund with good params', function () { + it('Should call partial refund with good params', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Partial', 3000, 'Captured')); @@ -114,7 +114,7 @@ describe('Refund job test', function () { 3000 ); }); - it('should be not call for a partial refund with negative amount', function () { + it('Should be not call for a partial refund with negative amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(count, 'Partial', -10000, 'Captured')); @@ -122,7 +122,7 @@ describe('Refund job test', function () { CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); }); - it('should be not call for a partial refund with not a valid amount', function () { + it('Should be not call for a partial refund with not a valid amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(count, 'Partial', 'azertyui', 'Captured')); @@ -130,7 +130,7 @@ describe('Refund job test', function () { CheckRefund.execute(); sinon.assert.notCalled(refundHelper.refundPaymentForOrder); }); - it('should be not call for a partial refund with an amount upper than the order amount', function () { + it('Should be not call for a partial refund with an amount upper than the order amount', function () { var count = 1; OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(count, 'Partial', 1000000, 'Captured')); @@ -139,25 +139,25 @@ describe('Refund job test', function () { sinon.assert.notCalled(refundHelper.refundPaymentForOrder); }); describe('Deferred payment', function () { - it('should call refund for an order whose payment is in auto capture', function () { + it('Should call refund for an order whose payment is in auto capture', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Total', null, 'Captured')); CheckRefund.execute(); sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); }); - it('should call refund for an order whose payment is already captured for a total refund', function () { + it('Should call refund for an order whose payment is already captured for a total refund', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Total', null, 'Captured')); CheckRefund.execute(); sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); }); - it('should call refund for an order whose payment is already captured for a partial refund', function () { + it('Should call refund for an order whose payment is already captured for a partial refund', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Partial', 3000, 'Captured')); CheckRefund.execute(); sinon.assert.calledOnce(refundHelper.refundPaymentForOrder); }); - it('should call cancel for an order whose payment is ToCapture for a total refund', function () { + it('Should call cancel for an order whose payment is ToCapture for a total refund', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Total', null, 'ToCapture')); CheckRefund.execute(); @@ -166,7 +166,7 @@ describe('Refund job test', function () { sinon.assert.calledWith(almaPaymentHelper.cancelAlmaPayment, { external_id: 'payment_0' }); sinon.assert.calledOnce(transaction.wrap); }); - it('should not call cancel for an order whose payment is ToCapture for a Partial refund and write a error log', function () { + it('Should not call cancel for an order whose payment is ToCapture for a Partial refund and write a error log', function () { OrderMgr.searchOrders = sinon.stub() .returns(mockOrderFactory(1, 'Partial', 3000, 'ToCapture')); CheckRefund.execute(); From 76fa50aa60860957857f157ec2ad2e9eb7d102a1 Mon Sep 17 00:00:00 2001 From: joyet-simon Date: Fri, 13 Oct 2023 09:52:35 +0200 Subject: [PATCH 66/66] rename a constant --- .../int_alma/cartridge/scripts/helpers/almaPaymentHelper.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js index abac1d4..a36e756 100644 --- a/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js +++ b/cartridges/int_alma/cartridge/scripts/helpers/almaPaymentHelper.js @@ -20,7 +20,7 @@ var CAPTURE = { } }; var MAX_INSTALLMENTS_COUNT_FOR_PNX = 4; -var MAX_DEFERRED_DAYS_FOR_PNX = 0; +var DEFERRED_DAYS_FOR_PNX = 0; /** * Allow to check an order status @@ -348,7 +348,7 @@ function cancelAlmaPayment(params) { * @return {boolean} is available */ function isAvailableForManualCapture(isManualCaptureEnabled, installmentsCount, deferredDays) { - return isManualCaptureEnabled && installmentsCount <= MAX_INSTALLMENTS_COUNT_FOR_PNX && deferredDays <= MAX_DEFERRED_DAYS_FOR_PNX; + return isManualCaptureEnabled && installmentsCount <= MAX_INSTALLMENTS_COUNT_FOR_PNX && deferredDays <= DEFERRED_DAYS_FOR_PNX; } /**