payseed
Version:
A comprehensive TypeScript library for payment processing utilities - format money, validate cards, calculate fees, and more.
2 lines • 2.66 kB
JavaScript
function m(e){let{amount:n,currency:r}=e,t=(n/100).toFixed(2);return `${r} ${t}`}function s(e,n){if(e<0)throw new Error("Amount must be non-negative");if(n<0)throw new Error("Percentage must be non-negative");return Math.round(e*n/100)}function f(e="payseed"){let n=Math.random().toString(36).slice(2,10),r=Date.now().toString(36);return `${e}_${r}_${n}`}function c(e,n=2){return Math.round(e*Math.pow(10,n))}function i(e,n=2){return e/Math.pow(10,n)}function y(e){let n=e.replace(/\D/g,"");return /^4/.test(n)?"visa":/^5[1-5]/.test(n)?"mastercard":/^3[47]/.test(n)?"amex":/^6(?:011|5)/.test(n)?"discover":"unknown"}function p(e){let n=e.replace(/\D/g,"");if(n.length<4)return "****";let r=n.slice(-4);return "*".repeat(Math.max(n.length-4,0))+r}function d(e){let n=e.replace(/\D/g,"");if(n.length<13||n.length>19)return false;let r=0,t=false;for(let u=n.length-1;u>=0;u--){let o=parseInt(n[u],10);t&&(o*=2,o>9&&(o-=9)),r+=o,t=!t;}return r%10===0}function l(e,n,r=0){if(e<0)throw new Error("Amount must be non-negative");if(n<0)throw new Error("Percentage fee must be non-negative");if(r<0)throw new Error("Fixed fee must be non-negative");return Math.round(e*n/100)+r}function g(e,n,r="en-US"){let t=i(e,a(n));return new Intl.NumberFormat(r,{style:"currency",currency:n}).format(t)}function a(e){return e==="JPY"?0:2}function b(e,n){let r=e.replace(/[^0-9.-]/g,""),t=parseFloat(r);if(isNaN(t))throw new Error("Invalid money format");return {amount:c(t,a(n)),currency:n}}function M(e,n){if(e.currency!==n.currency)throw new Error("Cannot add money with different currencies");return {amount:e.amount+n.amount,currency:e.currency}}function x(e,n){if(e.currency!==n.currency)throw new Error("Cannot subtract money with different currencies");return {amount:e.amount-n.amount,currency:e.currency}}function w(e,n){return {amount:Math.round(e.amount*n),currency:e.currency}}function h(e){return e.amount===0}function C(e){return e.amount<0}function v(e="payment"){let n={invoice:"INV",order:"ORD",payment:"PAY"},r=Date.now().toString(36).toUpperCase(),t=Math.random().toString(36).substring(2,8).toUpperCase();return `${n[e]}-${r}-${t}`}exports.addMoney=M;exports.calculateFee=s;exports.calculateProcessingFee=l;exports.formatCurrency=g;exports.formatMoney=m;exports.getCurrencyDecimals=a;exports.identifyCardBrand=y;exports.isNegativeMoney=C;exports.isZeroMoney=h;exports.maskCardNumber=p;exports.multiplyMoney=w;exports.parseMoney=b;exports.paymentReference=v;exports.seedId=f;exports.subtractMoney=x;exports.toMajorUnits=i;exports.toMinorUnits=c;exports.validateCardNumber=d;//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map
;