@trap_stevo/merchtide
Version:
Empowering the future of digital commerce, this API delivers the ultimate solution for building and managing online superstores. Seamlessly integrating product management, payment processing, and order tracking, it offers unmatched flexibility and scalabi
130 lines (125 loc) • 6 kB
JavaScript
;
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
/**
* PaymentUtilityManager for payment-related utilities.
*/
var PaymentUtilityManager = /*#__PURE__*/function () {
function PaymentUtilityManager() {
var inputBaseCurrency = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "usd";
_classCallCheck(this, PaymentUtilityManager);
this.baseCurrency = inputBaseCurrency;
}
/**
* Converts a human-readable amount into cents.
* @param {number|string} amount - The amount in a human-readable format (e.g., "10.50", "$10.50").
* @param {string} currencySymbol - Optional currency symbol to remove (e.g., "$", "₹", "€").
* @returns {number} The amount in cents (e.g., 1050).
*/
return _createClass(PaymentUtilityManager, [{
key: "formatPaymentData",
value:
/**
* Validates and formats payment data.
* @param {object} paymentData - The payment details.
* @param {string} paymentData.currency - The currency to use.
* @param {number} paymentData.amount - The amount in dollars.
* @returns {object} The formatted payment data.
*/
function formatPaymentData(_ref) {
var _ref$currency = _ref.currency,
currency = _ref$currency === void 0 ? this.baseCurrency : _ref$currency,
amount = _ref.amount;
if (!currency || typeof currency !== "string") {
console.log("Invalid currency provided.");
return null;
}
var amountInCents = this.convertToCents(amount);
return {
currency: currency,
amountInCents: amountInCents
};
}
/**
* Calculates the total price for a list of products.
* @param {Array} products - The list of products.
* @param {object} options - Additional options (e.g., discounts, taxes).
* @returns {number} The total amount in dollars.
*/
}, {
key: "calculateTotalPrice",
value: function calculateTotalPrice(products) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
if (!Array.isArray(products) || products.length === 0) {
throw new Error("Products empty.");
}
var _options$discount = options.discount,
discount = _options$discount === void 0 ? 0 : _options$discount,
_options$taxRate = options.taxRate,
taxRate = _options$taxRate === void 0 ? 0 : _options$taxRate;
var subtotal = products.reduce(function (total, product) {
if (!product.price || typeof product.price !== "number") {
throw new Error("Invalid price for product ~ ".concat(product.name));
}
return total + product.price;
}, 0);
var discountAmount = subtotal * (discount / 100);
var taxAmount = (subtotal - discountAmount) * (taxRate / 100);
return subtotal - discountAmount + taxAmount;
}
/**
* Prepares metadata for payment intents from products.
* @param {Array} products - The list of products.
* @returns {object} The metadata object.
*/
}, {
key: "prepareProductMetadata",
value: function prepareProductMetadata(products) {
return {
productDetails: JSON.stringify(products.map(function (_ref2) {
var id = _ref2.id,
name = _ref2.name,
quantity = _ref2.quantity;
return {
id: id,
name: name,
quantity: quantity
};
}))
};
}
}], [{
key: "convertToCents",
value: function convertToCents(amount) {
var currencySymbol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "";
if (typeof amount === "string") {
amount = amount.replace(new RegExp("[".concat(currencySymbol, ",]"), "g"), "").trim();
amount = parseFloat(amount);
}
if (isNaN(amount) || amount <= 0) {
throw new Error("Invalid amount provided.");
}
return Math.round(amount * 100);
}
/**
* Formats a numeric amount back to human-readable format.
* @param {number} amountInCents - The amount in cents (e.g., 1050).
* @param {string} currencySymbol - The currency symbol to prefix (e.g., "$", "₹").
* @returns {string} The formatted amount (e.g., "$10.50").
*/
}, {
key: "convertAmountToMoneyDisplay",
value: function convertAmountToMoneyDisplay(amountInCents) {
var currencySymbol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "$";
if (typeof amountInCents !== "number" || amountInCents < 0) {
throw new Error("Invalid amount provided.");
}
return "".concat(currencySymbol).concat((amountInCents / 100).toFixed(2));
}
}]);
}();
module.exports = PaymentUtilityManager;