UNPKG

bc-checkout-sdk

Version:

BetterCommerce's Checkout NodeJS SDK enables BC client applications to integrate with Checkout merchant API system. It publishes an interface to interact with [Checkout API](https://api-reference.checkout.com/#operation/getPaymentDetails/) endpoints.

46 lines (45 loc) 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sanitizeAmount = exports.countDecimals = void 0; /** * Counts the number of decimal places of a given number. * @param {number} value The number to count the decimal places of. * @returns {number} The number of decimal places of the given number. */ const countDecimals = (value) => { if (Math.floor(value.valueOf()) === value.valueOf()) return 0; { return value.toString().split(".")[1].length || 0; } }; exports.countDecimals = countDecimals; /** * Sanitizes the given amount by converting it to an integer representation of the lowest currency unit. * * This function handles amounts with up to two decimal places. If the amount has more than two decimals, * it is rounded to two decimal places before conversion. The sanitized amount is returned as an integer * by multiplying the value by 100 to handle the currency in the lowest denomination (e.g., cents for USD). * * @param value - The monetary amount to be sanitized. * @returns The sanitized amount as an integer. */ const sanitizeAmount = (value) => { let amount = 0; if (value) { const decimals = (0, exports.countDecimals)(value); if (decimals > 2) { amount = Number.parseFloat(value.toFixed(2)) * 100; } else { if (decimals == 0) { amount = value * 100; } else { amount = parseInt((value * 100).toFixed(0)); } } } return amount; }; exports.sanitizeAmount = sanitizeAmount;