Skip to content

Commit

Permalink
Merge pull request #86 from alma/develop
Browse files Browse the repository at this point in the history
Release v4.5.0
  • Loading branch information
joyet-simon authored Oct 13, 2023
2 parents c027733 + 11ec724 commit 8c61f99
Show file tree
Hide file tree
Showing 56 changed files with 2,083 additions and 177 deletions.
2 changes: 0 additions & 2 deletions .github/CODEOWNERS

This file was deleted.

9 changes: 9 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"node": true,
"esversion": 8,
"expr": true,
"predef": [
"describe",
"it"
]
}
61 changes: 61 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# 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

- 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:
- [email protected]
- [email protected]
- [email protected]
- [email protected]

# - 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|develop)$$"
5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

v4.5.0
------
* Feat: add Deferred Capture feature.
* Chore: add pre-commit tool.

v4.4.0
------
* Feat: In Page checkout
Expand Down
74 changes: 55 additions & 19 deletions cartridges/int_alma/cartridge/controllers/Alma.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -84,6 +70,26 @@ function affectOrder(paymentObj, order) {
throw new Error(reason);
}

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

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

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

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

var isOnShipmentPaymentEnabled = require('*/cartridge/scripts/helpers/almaOnShipmentHelper').isOnShipmentPaymentEnabled;
var paymentStatus = isOnShipmentPaymentEnabled(paymentObj.installments_count) ? Order.PAYMENT_STATUS_NOTPAID : Order.PAYMENT_STATUS_PAID;
acceptOrder(order, paymentStatus);
Expand Down Expand Up @@ -115,6 +121,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 {
Expand All @@ -128,9 +135,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
Expand Down Expand Up @@ -269,11 +283,19 @@ 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/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)
getLocale(req),
isDeferredCapture
);

try {
Expand Down Expand Up @@ -325,10 +347,20 @@ 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/almaConfigHelper');
var orderHelper = require('*/cartridge/scripts/helpers/almaOrderHelper');

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)
getLocale(req),
isDeferredCapture
);

try {
Expand All @@ -337,7 +369,8 @@ server.get(

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({
Expand Down Expand Up @@ -368,10 +401,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();
});
Expand Down
6 changes: 5 additions & 1 deletion cartridges/int_alma/cartridge/controllers/Checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -60,14 +62,16 @@ server.append('Begin', function (req, res, next) {
return;
}

var isDeferredCaptureEnabled = almaConfigHelper.isDeferredCaptureEnable();

var viewData = Object.assign(
res.getViewData(),
almaConfigInfo,
getAlmaUrls(),
{
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)
Expand Down
52 changes: 25 additions & 27 deletions cartridges/int_alma/cartridge/scripts/helpers/almaCheckoutHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ 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 almaPaymentHelper = require('*/cartridge/scripts/helpers/almaPaymentHelper');

var ALMA_PNX_ID = 'ALMA_PNX';
var ALMA_CREDIT_ID = 'ALMA_CREDIT';
Expand All @@ -17,12 +19,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;
}

/**
Expand Down Expand Up @@ -70,7 +68,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),
Expand Down Expand Up @@ -131,6 +130,15 @@ function getPaymentInstallments(plan, currencyCode) {
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) + ' ' +
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)
;
}
// installment payment
return formatCurrency(plan.payment_plan[0].purchase_amount / 100, currencyCode) + ' ' +
Resource.msg(getPropertyCategory(plan) + '.installments', 'alma', null) + ' ' +
Expand Down Expand Up @@ -166,24 +174,17 @@ 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
* @param {Object} plan plan
* @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);
Expand All @@ -205,9 +206,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;
}

Expand All @@ -224,7 +223,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,
Expand All @@ -237,7 +236,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,
Expand All @@ -250,7 +249,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,
Expand All @@ -263,7 +262,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,
Expand All @@ -280,6 +279,5 @@ function formatPlanForCheckout(plan, currencyCode) {
module.exports = {
formatPlanForCheckout: formatPlanForCheckout,
getPlanPaymentMethodID: getPlanPaymentMethodID,
isAvailableForInpage: isAvailableForInpage,
isInpageActivated: isInpageActivated
isAvailableForInpage: isAvailableForInpage
};
Loading

0 comments on commit 8c61f99

Please sign in to comment.