UNPKG

payseed

Version:

A comprehensive TypeScript library for payment processing utilities - format money, validate cards, calculate fees, and more.

230 lines (165 loc) โ€ข 6.1 kB
# PaySeed ๐Ÿ’ณ > A comprehensive TypeScript library for payment processing utilities [![npm version](https://img.shields.io/npm/v/payseed.svg)](https://www.npmjs.com/package/payseed) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![TypeScript](https://img.shields.io/badge/TypeScript-Ready-blue.svg)](https://www.typescriptlang.org/) PaySeed is a lightweight, type-safe library providing essential utilities for payment processing, money formatting, credit card validation, and fee calculations. ## โœจ Features - ๐Ÿ’ฐ **Money Formatting** - Format and parse money values with multiple currency support - ๐Ÿ’ณ **Card Validation** - Validate and identify credit card brands using Luhn algorithm - ๐Ÿงฎ **Fee Calculations** - Calculate processing fees with percentage and fixed components - ๐ŸŒ **Multi-Currency** - Support for USD, EUR, GBP, JPY, CAD, AUD, CHF, CNY - ๐Ÿ”’ **Type-Safe** - Full TypeScript support with comprehensive type definitions - โšก **Zero Dependencies** - Lightweight and fast - โœ… **Well Tested** - 100% test coverage ## ๐Ÿ“ฆ Installation ```bash npm install payseed # or yarn add payseed # or pnpm add payseed ``` ## ๐Ÿš€ Quick Start ```typescript import { formatMoney, validateCardNumber, calculateProcessingFee } from 'payseed'; // Format money const money = { amount: 1234, currency: 'USD' as const }; console.log(formatMoney(money)); // "USD 12.34" // Validate credit card const isValid = validateCardNumber('4111111111111111'); console.log(isValid); // true // Calculate processing fee (2.9% + $0.30) const fee = calculateProcessingFee(10000, 2.9, 30); console.log(fee); // 320 (in cents) ``` ## ๐Ÿ“– API Reference ### Money Utilities #### `formatMoney(money: Money): string` Format money object to display string. ```typescript formatMoney({ amount: 1234, currency: 'USD' }); // "USD 12.34" ``` #### `formatCurrency(amount: number, currency: CurrencyCode, locale?: string): string` Format with locale support using Intl.NumberFormat. ```typescript formatCurrency(1234, 'USD'); // "$12.34" formatCurrency(1000, 'JPY'); // "ยฅ1,000" ``` #### `parseMoney(moneyString: string, currency: CurrencyCode): Money` Parse money string to Money object. ```typescript parseMoney('$12.34', 'USD'); // { amount: 1234, currency: 'USD' } ``` #### `toMinorUnits(amount: number, decimals?: number): number` Convert major units to minor units (e.g., dollars to cents). ```typescript toMinorUnits(10.50); // 1050 toMinorUnits(100, 0); // 100 (for JPY) ``` #### `toMajorUnits(amount: number, decimals?: number): number` Convert minor units to major units (e.g., cents to dollars). ```typescript toMajorUnits(1050); // 10.50 ``` ### Credit Card Utilities #### `validateCardNumber(cardNumber: string): boolean` Validate card number using Luhn algorithm. ```typescript validateCardNumber('4111111111111111'); // true (Valid Visa) validateCardNumber('4111-1111-1111-1111'); // true (handles formatting) ``` #### `identifyCardBrand(cardNumber: string): CardBrand` Identify credit card brand from number. ```typescript identifyCardBrand('4242424242424242'); // 'visa' identifyCardBrand('5555555555554444'); // 'mastercard' identifyCardBrand('378282246310005'); // 'amex' ``` #### `maskCardNumber(cardNumber: string): string` Mask credit card number for display. ```typescript maskCardNumber('4111111111111111'); // '************1111' maskCardNumber('378282246310005'); // '***********0005' ``` ### Fee Calculation #### `calculateFee(amount: number, percentage: number): number` Calculate simple percentage fee. ```typescript calculateFee(10000, 2.9); // 290 ``` #### `calculateProcessingFee(amount: number, percentageFee: number, fixedFee?: number): number` Calculate payment processing fee with fixed and percentage components. ```typescript calculateProcessingFee(10000, 2.9, 30); // 320 (2.9% + $0.30) ``` ### Money Arithmetic #### `addMoney(a: Money, b: Money): Money` Add two money values (must be same currency). ```typescript const sum = addMoney( { amount: 1000, currency: 'USD' }, { amount: 500, currency: 'USD' } ); // { amount: 1500, currency: 'USD' } ``` #### `subtractMoney(a: Money, b: Money): Money` Subtract two money values. ```typescript const diff = subtractMoney( { amount: 1000, currency: 'USD' }, { amount: 300, currency: 'USD' } ); // { amount: 700, currency: 'USD' } ``` #### `multiplyMoney(money: Money, factor: number): Money` Multiply money by a factor. ```typescript const doubled = multiplyMoney( { amount: 1000, currency: 'USD' }, 2 ); // { amount: 2000, currency: 'USD' } ``` ### Utility Functions #### `seedId(prefix?: string): string` Generate unique payment-related ID. ```typescript seedId(); // 'payseed_lq3x8n2k_a7b9c2' seedId('transaction'); // 'transaction_lq3x8n2k_a7b9c2' ``` #### `paymentReference(type?: 'invoice' | 'order' | 'payment'): string` Generate formatted payment reference. ```typescript paymentReference(); // 'PAY-LQ3X8N2K-A7B9C2' paymentReference('invoice'); // 'INV-LQ3X8N2K-A7B9C2' ``` #### `isZeroMoney(money: Money): boolean` Check if money amount is zero. ```typescript isZeroMoney({ amount: 0, currency: 'USD' }); // true ``` #### `isNegativeMoney(money: Money): boolean` Check if money amount is negative. ```typescript isNegativeMoney({ amount: -100, currency: 'USD' }); // true ``` ## ๐ŸŽฏ Use Cases PaySeed is perfect for: - E-commerce applications - Payment processing systems - Financial dashboards - Invoice generation - Shopping carts - Subscription billing - Any application handling money ## ๐Ÿงช Testing ```bash # Run tests pnpm test # Run tests with coverage pnpm test:ci ``` ## ๐Ÿ“ License MIT ยฉ [Your Name] ## ๐Ÿค Contributing Contributions, issues and feature requests are welcome! ## โญ Show your support Give a โญ๏ธ if this project helped you!