@misterhomer1992/miit-bot-payment 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,245 @@
1
+ # @miit-bot/payment-utils
2
+
3
+ A TypeScript utility library for payment validation and formatting. This library provides robust validation for credit card numbers, payment amounts, CVV codes, and formatting utilities for displaying payment information.
4
+
5
+ ## Features
6
+
7
+ - 💳 Credit card validation with Luhn algorithm
8
+ - 🏦 Automatic card type detection (Visa, Mastercard, Amex, Discover)
9
+ - 💰 Payment amount validation with customizable limits
10
+ - 🔒 CVV/CVC validation
11
+ - 🎨 Card number formatting and masking
12
+ - 🌍 Currency formatting with internationalization support
13
+ - 📅 Expiry date formatting
14
+ - 🔧 Written in TypeScript with full type definitions
15
+ - ✅ Zero dependencies (dev dependencies only)
16
+
17
+ ## Installation
18
+
19
+ ```bash
20
+ npm install @miit-bot/payment-utils
21
+ ```
22
+
23
+ or
24
+
25
+ ```bash
26
+ yarn add @miit-bot/payment-utils
27
+ ```
28
+
29
+ ## Usage
30
+
31
+ ### Card Validation
32
+
33
+ ```typescript
34
+ import { validateCardNumber, CardType } from '@miit-bot/payment-utils';
35
+
36
+ // Validate a credit card number
37
+ const result = validateCardNumber('4532015112830366');
38
+ console.log(result);
39
+ // {
40
+ // isValid: true,
41
+ // cardType: 'VISA',
42
+ // message: 'Valid card number'
43
+ // }
44
+ ```
45
+
46
+ ### Amount Validation
47
+
48
+ ```typescript
49
+ import { validateAmount } from '@miit-bot/payment-utils';
50
+
51
+ // Validate payment amount
52
+ const result = validateAmount(99.99);
53
+ console.log(result);
54
+ // {
55
+ // isValid: true,
56
+ // amount: 99.99,
57
+ // message: 'Valid amount'
58
+ // }
59
+
60
+ // With custom limits
61
+ const customResult = validateAmount(150.0, 10, 100);
62
+ console.log(customResult);
63
+ // {
64
+ // isValid: false,
65
+ // amount: 150,
66
+ // message: 'Amount must not exceed 100'
67
+ // }
68
+ ```
69
+
70
+ ### CVV Validation
71
+
72
+ ```typescript
73
+ import { validateCVV, CardType } from '@miit-bot/payment-utils';
74
+
75
+ // Validate CVV
76
+ const isValid = validateCVV('123'); // true
77
+
78
+ // Validate Amex CVV (4 digits)
79
+ const isValidAmex = validateCVV('1234', CardType.AMEX); // true
80
+ ```
81
+
82
+ ### Card Formatting
83
+
84
+ ```typescript
85
+ import { formatCardNumber, maskCardNumber } from '@miit-bot/payment-utils';
86
+
87
+ // Format card number with spaces
88
+ const formatted = formatCardNumber('4532015112830366');
89
+ console.log(formatted); // '4532 0151 1283 0366'
90
+
91
+ // Format and mask
92
+ const masked = formatCardNumber('4532015112830366', true);
93
+ console.log(masked); // '**** **** **** 0366'
94
+
95
+ // Custom masking
96
+ const customMask = maskCardNumber('4532015112830366', 4, 'X');
97
+ console.log(customMask); // 'XXXXXXXXXXXX0366'
98
+ ```
99
+
100
+ ### Currency Formatting
101
+
102
+ ```typescript
103
+ import { formatCurrency } from '@miit-bot/payment-utils';
104
+
105
+ // Format as USD
106
+ const usd = formatCurrency(1234.56);
107
+ console.log(usd); // '$1,234.56'
108
+
109
+ // Format with custom locale and currency
110
+ const eur = formatCurrency(1234.56, {
111
+ locale: 'de-DE',
112
+ currency: 'EUR',
113
+ });
114
+ console.log(eur); // '1.234,56 €'
115
+ ```
116
+
117
+ ### Expiry Date Formatting
118
+
119
+ ```typescript
120
+ import { formatExpiryDate } from '@miit-bot/payment-utils';
121
+
122
+ // Format expiry date
123
+ const expiry = formatExpiryDate(12, 2025);
124
+ console.log(expiry); // '12/25'
125
+ ```
126
+
127
+ ## API Reference
128
+
129
+ ### Types
130
+
131
+ #### CardType
132
+
133
+ ```typescript
134
+ enum CardType {
135
+ VISA = 'VISA',
136
+ MASTERCARD = 'MASTERCARD',
137
+ AMEX = 'AMEX',
138
+ DISCOVER = 'DISCOVER',
139
+ UNKNOWN = 'UNKNOWN',
140
+ }
141
+ ```
142
+
143
+ #### CardValidationResult
144
+
145
+ ```typescript
146
+ interface CardValidationResult {
147
+ isValid: boolean;
148
+ cardType: CardType;
149
+ message?: string;
150
+ }
151
+ ```
152
+
153
+ #### AmountValidationResult
154
+
155
+ ```typescript
156
+ interface AmountValidationResult {
157
+ isValid: boolean;
158
+ amount?: number;
159
+ message?: string;
160
+ }
161
+ ```
162
+
163
+ ### Validators
164
+
165
+ #### validateCardNumber(cardNumber: string): CardValidationResult
166
+
167
+ Validates a credit card number using the Luhn algorithm and detects the card type.
168
+
169
+ #### validateAmount(amount: string | number, minAmount?: number, maxAmount?: number): AmountValidationResult
170
+
171
+ Validates a payment amount with optional min/max limits. Defaults: min=0.01, max=999999.99
172
+
173
+ #### validateCVV(cvv: string, cardType?: CardType): boolean
174
+
175
+ Validates a CVV/CVC code. Returns true if valid (3 digits for most cards, 4 for Amex).
176
+
177
+ ### Formatters
178
+
179
+ #### formatCardNumber(cardNumber: string, maskDigits?: boolean): string
180
+
181
+ Formats a card number with spaces. Optionally masks all but the last 4 digits.
182
+
183
+ #### formatCurrency(amount: number, options?: CurrencyFormatOptions): string
184
+
185
+ Formats a number as currency with internationalization support.
186
+
187
+ #### maskCardNumber(cardNumber: string, visibleDigits?: number, maskChar?: string): string
188
+
189
+ Masks a card number showing only the last N digits.
190
+
191
+ #### formatExpiryDate(month: number, year: number): string
192
+
193
+ Formats an expiry date to MM/YY format.
194
+
195
+ ## Development
196
+
197
+ ### Setup
198
+
199
+ ```bash
200
+ # Install dependencies
201
+ npm install
202
+
203
+ # Build the library
204
+ npm run build
205
+
206
+ # Run linter
207
+ npm run lint
208
+
209
+ # Format code
210
+ npm run format
211
+
212
+ # Watch mode for development
213
+ npm run watch
214
+ ```
215
+
216
+ ### Building
217
+
218
+ The library is built using TypeScript compiler. The build output is in the `dist` folder with:
219
+
220
+ - Compiled JavaScript files
221
+ - Type declaration files (.d.ts)
222
+ - Source maps
223
+
224
+ ### Publishing
225
+
226
+ ```bash
227
+ # Build and publish to npm
228
+ npm publish
229
+ ```
230
+
231
+ ## License
232
+
233
+ MIT
234
+
235
+ ## Contributing
236
+
237
+ Contributions are welcome! Please feel free to submit a Pull Request.
238
+
239
+ ## Author
240
+
241
+ Your Name
242
+
243
+ ## Repository
244
+
245
+ https://github.com/yourusername/miit-bot-payment
@@ -0,0 +1,31 @@
1
+ import { CurrencyFormatOptions } from './types';
2
+ /**
3
+ * Formats a credit card number for display (e.g., "1234 5678 9012 3456")
4
+ * @param cardNumber - The card number to format
5
+ * @param maskDigits - Whether to mask all but the last 4 digits
6
+ * @returns Formatted card number
7
+ */
8
+ export declare function formatCardNumber(cardNumber: string, maskDigits?: boolean): string;
9
+ /**
10
+ * Formats an amount as currency
11
+ * @param amount - The amount to format
12
+ * @param options - Formatting options
13
+ * @returns Formatted currency string
14
+ */
15
+ export declare function formatCurrency(amount: number, options?: CurrencyFormatOptions): string;
16
+ /**
17
+ * Masks a card number showing only the last N digits
18
+ * @param cardNumber - The card number to mask
19
+ * @param visibleDigits - Number of digits to show (default: 4)
20
+ * @param maskChar - Character to use for masking (default: '*')
21
+ * @returns Masked card number
22
+ */
23
+ export declare function maskCardNumber(cardNumber: string, visibleDigits?: number, maskChar?: string): string;
24
+ /**
25
+ * Formats an expiry date to MM/YY format
26
+ * @param month - Month (1-12)
27
+ * @param year - Year (full year or last 2 digits)
28
+ * @returns Formatted expiry date
29
+ */
30
+ export declare function formatExpiryDate(month: number, year: number): string;
31
+ //# sourceMappingURL=formatters.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEhD;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,UAAU,GAAE,OAAe,GAAG,MAAM,CAUxF;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,qBAA0B,GAAG,MAAM,CAc1F;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,UAAU,EAAE,MAAM,EAClB,aAAa,GAAE,MAAU,EACzB,QAAQ,GAAE,MAAY,GACrB,MAAM,CAWR;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAMpE"}
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formatCardNumber = formatCardNumber;
4
+ exports.formatCurrency = formatCurrency;
5
+ exports.maskCardNumber = maskCardNumber;
6
+ exports.formatExpiryDate = formatExpiryDate;
7
+ /**
8
+ * Formats a credit card number for display (e.g., "1234 5678 9012 3456")
9
+ * @param cardNumber - The card number to format
10
+ * @param maskDigits - Whether to mask all but the last 4 digits
11
+ * @returns Formatted card number
12
+ */
13
+ function formatCardNumber(cardNumber, maskDigits = false) {
14
+ const cleanedNumber = cardNumber.replace(/[\s-]/g, '');
15
+ if (maskDigits && cleanedNumber.length > 4) {
16
+ const lastFour = cleanedNumber.slice(-4);
17
+ const masked = '*'.repeat(cleanedNumber.length - 4);
18
+ return (masked + lastFour).replace(/(.{4})/g, '$1 ').trim();
19
+ }
20
+ return cleanedNumber.replace(/(.{4})/g, '$1 ').trim();
21
+ }
22
+ /**
23
+ * Formats an amount as currency
24
+ * @param amount - The amount to format
25
+ * @param options - Formatting options
26
+ * @returns Formatted currency string
27
+ */
28
+ function formatCurrency(amount, options = {}) {
29
+ const { locale = 'en-US', currency = 'USD', minimumFractionDigits = 2, maximumFractionDigits = 2, } = options;
30
+ return new Intl.NumberFormat(locale, {
31
+ style: 'currency',
32
+ currency,
33
+ minimumFractionDigits,
34
+ maximumFractionDigits,
35
+ }).format(amount);
36
+ }
37
+ /**
38
+ * Masks a card number showing only the last N digits
39
+ * @param cardNumber - The card number to mask
40
+ * @param visibleDigits - Number of digits to show (default: 4)
41
+ * @param maskChar - Character to use for masking (default: '*')
42
+ * @returns Masked card number
43
+ */
44
+ function maskCardNumber(cardNumber, visibleDigits = 4, maskChar = '*') {
45
+ const cleanedNumber = cardNumber.replace(/[\s-]/g, '');
46
+ if (cleanedNumber.length <= visibleDigits) {
47
+ return cleanedNumber;
48
+ }
49
+ const visiblePart = cleanedNumber.slice(-visibleDigits);
50
+ const maskedPart = maskChar.repeat(cleanedNumber.length - visibleDigits);
51
+ return maskedPart + visiblePart;
52
+ }
53
+ /**
54
+ * Formats an expiry date to MM/YY format
55
+ * @param month - Month (1-12)
56
+ * @param year - Year (full year or last 2 digits)
57
+ * @returns Formatted expiry date
58
+ */
59
+ function formatExpiryDate(month, year) {
60
+ const monthStr = month.toString().padStart(2, '0');
61
+ const yearStr = year >= 2000 ? (year % 100).toString().padStart(2, '0') : year.toString().padStart(2, '0');
62
+ return `${monthStr}/${yearStr}`;
63
+ }
64
+ //# sourceMappingURL=formatters.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.js","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":";;AAQA,4CAUC;AAQD,wCAcC;AASD,wCAeC;AAQD,4CAMC;AA5ED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,UAAkB,EAAE,aAAsB,KAAK;IAC9E,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAEvD,IAAI,UAAU,IAAI,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QAC3C,MAAM,QAAQ,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QACzC,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACpD,OAAO,CAAC,MAAM,GAAG,QAAQ,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC;IAC9D,CAAC;IAED,OAAO,aAAa,CAAC,OAAO,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC,IAAI,EAAE,CAAC;AACxD,CAAC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,MAAc,EAAE,UAAiC,EAAE;IAChF,MAAM,EACJ,MAAM,GAAG,OAAO,EAChB,QAAQ,GAAG,KAAK,EAChB,qBAAqB,GAAG,CAAC,EACzB,qBAAqB,GAAG,CAAC,GAC1B,GAAG,OAAO,CAAC;IAEZ,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;QACnC,KAAK,EAAE,UAAU;QACjB,QAAQ;QACR,qBAAqB;QACrB,qBAAqB;KACtB,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;AACpB,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,cAAc,CAC5B,UAAkB,EAClB,gBAAwB,CAAC,EACzB,WAAmB,GAAG;IAEtB,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAEvD,IAAI,aAAa,CAAC,MAAM,IAAI,aAAa,EAAE,CAAC;QAC1C,OAAO,aAAa,CAAC;IACvB,CAAC;IAED,MAAM,WAAW,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC,aAAa,CAAC,CAAC;IACxD,MAAM,UAAU,GAAG,QAAQ,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,GAAG,aAAa,CAAC,CAAC;IAEzE,OAAO,UAAU,GAAG,WAAW,CAAC;AAClC,CAAC;AAED;;;;;GAKG;AACH,SAAgB,gBAAgB,CAAC,KAAa,EAAE,IAAY;IAC1D,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IACnD,MAAM,OAAO,GACX,IAAI,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;IAE7F,OAAO,GAAG,QAAQ,IAAI,OAAO,EAAE,CAAC;AAClC,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * @miit-bot/payment-utils
3
+ * A TypeScript utility library for payment validation and formatting
4
+ */
5
+ export * from './types';
6
+ export { validateCardNumber, validateAmount, validateCVV } from './validators';
7
+ export { formatCardNumber, formatCurrency, maskCardNumber, formatExpiryDate } from './formatters';
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,cAAc,SAAS,CAAC;AAGxB,OAAO,EAAE,kBAAkB,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAG/E,OAAO,EAAE,gBAAgB,EAAE,cAAc,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ /**
3
+ * @miit-bot/payment-utils
4
+ * A TypeScript utility library for payment validation and formatting
5
+ */
6
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
7
+ if (k2 === undefined) k2 = k;
8
+ var desc = Object.getOwnPropertyDescriptor(m, k);
9
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
10
+ desc = { enumerable: true, get: function() { return m[k]; } };
11
+ }
12
+ Object.defineProperty(o, k2, desc);
13
+ }) : (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ o[k2] = m[k];
16
+ }));
17
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
18
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ exports.formatExpiryDate = exports.maskCardNumber = exports.formatCurrency = exports.formatCardNumber = exports.validateCVV = exports.validateAmount = exports.validateCardNumber = void 0;
22
+ // Export types
23
+ __exportStar(require("./types"), exports);
24
+ // Export validators
25
+ var validators_1 = require("./validators");
26
+ Object.defineProperty(exports, "validateCardNumber", { enumerable: true, get: function () { return validators_1.validateCardNumber; } });
27
+ Object.defineProperty(exports, "validateAmount", { enumerable: true, get: function () { return validators_1.validateAmount; } });
28
+ Object.defineProperty(exports, "validateCVV", { enumerable: true, get: function () { return validators_1.validateCVV; } });
29
+ // Export formatters
30
+ var formatters_1 = require("./formatters");
31
+ Object.defineProperty(exports, "formatCardNumber", { enumerable: true, get: function () { return formatters_1.formatCardNumber; } });
32
+ Object.defineProperty(exports, "formatCurrency", { enumerable: true, get: function () { return formatters_1.formatCurrency; } });
33
+ Object.defineProperty(exports, "maskCardNumber", { enumerable: true, get: function () { return formatters_1.maskCardNumber; } });
34
+ Object.defineProperty(exports, "formatExpiryDate", { enumerable: true, get: function () { return formatters_1.formatExpiryDate; } });
35
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;AAEH,eAAe;AACf,0CAAwB;AAExB,oBAAoB;AACpB,2CAA+E;AAAtE,gHAAA,kBAAkB,OAAA;AAAE,4GAAA,cAAc,OAAA;AAAE,yGAAA,WAAW,OAAA;AAExD,oBAAoB;AACpB,2CAAkG;AAAzF,8GAAA,gBAAgB,OAAA;AAAE,4GAAA,cAAc,OAAA;AAAE,4GAAA,cAAc,OAAA;AAAE,8GAAA,gBAAgB,OAAA"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Supported credit card types
3
+ */
4
+ export declare enum CardType {
5
+ VISA = "VISA",
6
+ MASTERCARD = "MASTERCARD",
7
+ AMEX = "AMEX",
8
+ DISCOVER = "DISCOVER",
9
+ UNKNOWN = "UNKNOWN"
10
+ }
11
+ /**
12
+ * Credit card validation result
13
+ */
14
+ export interface CardValidationResult {
15
+ isValid: boolean;
16
+ cardType: CardType;
17
+ message?: string;
18
+ }
19
+ /**
20
+ * Currency formatting options
21
+ */
22
+ export interface CurrencyFormatOptions {
23
+ locale?: string;
24
+ currency?: string;
25
+ minimumFractionDigits?: number;
26
+ maximumFractionDigits?: number;
27
+ }
28
+ /**
29
+ * Payment amount validation result
30
+ */
31
+ export interface AmountValidationResult {
32
+ isValid: boolean;
33
+ amount?: number;
34
+ message?: string;
35
+ }
36
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,QAAQ;IAClB,IAAI,SAAS;IACb,UAAU,eAAe;IACzB,IAAI,SAAS;IACb,QAAQ,aAAa;IACrB,OAAO,YAAY;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,QAAQ,CAAC;IACnB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,qBAAqB,CAAC,EAAE,MAAM,CAAC;IAC/B,qBAAqB,CAAC,EAAE,MAAM,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB"}
package/dist/types.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CardType = void 0;
4
+ /**
5
+ * Supported credit card types
6
+ */
7
+ var CardType;
8
+ (function (CardType) {
9
+ CardType["VISA"] = "VISA";
10
+ CardType["MASTERCARD"] = "MASTERCARD";
11
+ CardType["AMEX"] = "AMEX";
12
+ CardType["DISCOVER"] = "DISCOVER";
13
+ CardType["UNKNOWN"] = "UNKNOWN";
14
+ })(CardType || (exports.CardType = CardType = {}));
15
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":";;;AAAA;;GAEG;AACH,IAAY,QAMX;AAND,WAAY,QAAQ;IAClB,yBAAa,CAAA;IACb,qCAAyB,CAAA;IACzB,yBAAa,CAAA;IACb,iCAAqB,CAAA;IACrB,+BAAmB,CAAA;AACrB,CAAC,EANW,QAAQ,wBAAR,QAAQ,QAMnB"}
@@ -0,0 +1,23 @@
1
+ import { CardType, CardValidationResult, AmountValidationResult } from './types';
2
+ /**
3
+ * Validates a credit card number using the Luhn algorithm
4
+ * @param cardNumber - The credit card number to validate
5
+ * @returns Validation result with card type
6
+ */
7
+ export declare function validateCardNumber(cardNumber: string): CardValidationResult;
8
+ /**
9
+ * Validates a payment amount
10
+ * @param amount - The amount to validate (string or number)
11
+ * @param minAmount - Minimum allowed amount (default: 0.01)
12
+ * @param maxAmount - Maximum allowed amount (default: 999999.99)
13
+ * @returns Validation result
14
+ */
15
+ export declare function validateAmount(amount: string | number, minAmount?: number, maxAmount?: number): AmountValidationResult;
16
+ /**
17
+ * Validates a CVV/CVC code
18
+ * @param cvv - The CVV code to validate
19
+ * @param cardType - The card type (AMEX uses 4 digits, others use 3)
20
+ * @returns Whether the CVV is valid
21
+ */
22
+ export declare function validateCVV(cvv: string, cardType?: CardType): boolean;
23
+ //# sourceMappingURL=validators.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../src/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEjF;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,MAAM,GAAG,oBAAoB,CAiC3E;AA6DD;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,MAAM,EAAE,MAAM,GAAG,MAAM,EACvB,SAAS,GAAE,MAAa,EACxB,SAAS,GAAE,MAAkB,GAC5B,sBAAsB,CAyCxB;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,GAAE,QAA2B,GAAG,OAAO,CAYvF"}
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateCardNumber = validateCardNumber;
4
+ exports.validateAmount = validateAmount;
5
+ exports.validateCVV = validateCVV;
6
+ const types_1 = require("./types");
7
+ /**
8
+ * Validates a credit card number using the Luhn algorithm
9
+ * @param cardNumber - The credit card number to validate
10
+ * @returns Validation result with card type
11
+ */
12
+ function validateCardNumber(cardNumber) {
13
+ // Remove spaces and dashes
14
+ const cleanedNumber = cardNumber.replace(/[\s-]/g, '');
15
+ // Check if it contains only digits
16
+ if (!/^\d+$/.test(cleanedNumber)) {
17
+ return {
18
+ isValid: false,
19
+ cardType: types_1.CardType.UNKNOWN,
20
+ message: 'Card number must contain only digits',
21
+ };
22
+ }
23
+ // Check length (most cards are 13-19 digits)
24
+ if (cleanedNumber.length < 13 || cleanedNumber.length > 19) {
25
+ return {
26
+ isValid: false,
27
+ cardType: types_1.CardType.UNKNOWN,
28
+ message: 'Invalid card number length',
29
+ };
30
+ }
31
+ // Determine card type
32
+ const cardType = detectCardType(cleanedNumber);
33
+ // Luhn algorithm validation
34
+ const isValidLuhn = luhnCheck(cleanedNumber);
35
+ return {
36
+ isValid: isValidLuhn,
37
+ cardType,
38
+ message: isValidLuhn ? 'Valid card number' : 'Invalid card number (failed Luhn check)',
39
+ };
40
+ }
41
+ /**
42
+ * Performs Luhn algorithm check
43
+ * @param cardNumber - The card number to check
44
+ * @returns Whether the card passes the Luhn check
45
+ */
46
+ function luhnCheck(cardNumber) {
47
+ let sum = 0;
48
+ let isEven = false;
49
+ // Loop through values starting from the rightmost digit
50
+ for (let i = cardNumber.length - 1; i >= 0; i--) {
51
+ let digit = parseInt(cardNumber[i], 10);
52
+ if (isEven) {
53
+ digit *= 2;
54
+ if (digit > 9) {
55
+ digit -= 9;
56
+ }
57
+ }
58
+ sum += digit;
59
+ isEven = !isEven;
60
+ }
61
+ return sum % 10 === 0;
62
+ }
63
+ /**
64
+ * Detects the card type based on the card number
65
+ * @param cardNumber - The card number
66
+ * @returns The detected card type
67
+ */
68
+ function detectCardType(cardNumber) {
69
+ // Visa: starts with 4
70
+ if (/^4/.test(cardNumber)) {
71
+ return types_1.CardType.VISA;
72
+ }
73
+ // Mastercard: starts with 51-55 or 2221-2720
74
+ if (/^5[1-5]/.test(cardNumber) ||
75
+ /^2(22[1-9]|2[3-9]\d|[3-6]\d{2}|7[01]\d|720)/.test(cardNumber)) {
76
+ return types_1.CardType.MASTERCARD;
77
+ }
78
+ // American Express: starts with 34 or 37
79
+ if (/^3[47]/.test(cardNumber)) {
80
+ return types_1.CardType.AMEX;
81
+ }
82
+ // Discover: starts with 6011, 622126-622925, 644-649, or 65
83
+ if (/^(6011|65|64[4-9]|622)/.test(cardNumber)) {
84
+ return types_1.CardType.DISCOVER;
85
+ }
86
+ return types_1.CardType.UNKNOWN;
87
+ }
88
+ /**
89
+ * Validates a payment amount
90
+ * @param amount - The amount to validate (string or number)
91
+ * @param minAmount - Minimum allowed amount (default: 0.01)
92
+ * @param maxAmount - Maximum allowed amount (default: 999999.99)
93
+ * @returns Validation result
94
+ */
95
+ function validateAmount(amount, minAmount = 0.01, maxAmount = 999999.99) {
96
+ const parsedAmount = typeof amount === 'string' ? parseFloat(amount) : amount;
97
+ if (isNaN(parsedAmount)) {
98
+ return {
99
+ isValid: false,
100
+ message: 'Invalid amount: not a number',
101
+ };
102
+ }
103
+ if (parsedAmount < minAmount) {
104
+ return {
105
+ isValid: false,
106
+ amount: parsedAmount,
107
+ message: `Amount must be at least ${minAmount}`,
108
+ };
109
+ }
110
+ if (parsedAmount > maxAmount) {
111
+ return {
112
+ isValid: false,
113
+ amount: parsedAmount,
114
+ message: `Amount must not exceed ${maxAmount}`,
115
+ };
116
+ }
117
+ // Check for valid decimal places (max 2)
118
+ const decimalPlaces = (parsedAmount.toString().split('.')[1] || '').length;
119
+ if (decimalPlaces > 2) {
120
+ return {
121
+ isValid: false,
122
+ amount: parsedAmount,
123
+ message: 'Amount must have at most 2 decimal places',
124
+ };
125
+ }
126
+ return {
127
+ isValid: true,
128
+ amount: parsedAmount,
129
+ message: 'Valid amount',
130
+ };
131
+ }
132
+ /**
133
+ * Validates a CVV/CVC code
134
+ * @param cvv - The CVV code to validate
135
+ * @param cardType - The card type (AMEX uses 4 digits, others use 3)
136
+ * @returns Whether the CVV is valid
137
+ */
138
+ function validateCVV(cvv, cardType = types_1.CardType.UNKNOWN) {
139
+ const cleanedCVV = cvv.trim();
140
+ if (!/^\d+$/.test(cleanedCVV)) {
141
+ return false;
142
+ }
143
+ if (cardType === types_1.CardType.AMEX) {
144
+ return cleanedCVV.length === 4;
145
+ }
146
+ return cleanedCVV.length === 3;
147
+ }
148
+ //# sourceMappingURL=validators.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.js","sourceRoot":"","sources":["../src/validators.ts"],"names":[],"mappings":";;AAOA,gDAiCC;AAoED,wCA6CC;AAQD,kCAYC;AA7KD,mCAAiF;AAEjF;;;;GAIG;AACH,SAAgB,kBAAkB,CAAC,UAAkB;IACnD,2BAA2B;IAC3B,MAAM,aAAa,GAAG,UAAU,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IAEvD,mCAAmC;IACnC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,aAAa,CAAC,EAAE,CAAC;QACjC,OAAO;YACL,OAAO,EAAE,KAAK;YACd,QAAQ,EAAE,gBAAQ,CAAC,OAAO;YAC1B,OAAO,EAAE,sCAAsC;SAChD,CAAC;IACJ,CAAC;IAED,6CAA6C;IAC7C,IAAI,aAAa,CAAC,MAAM,GAAG,EAAE,IAAI,aAAa,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;QAC3D,OAAO;YACL,OAAO,EAAE,KAAK;YACd,QAAQ,EAAE,gBAAQ,CAAC,OAAO;YAC1B,OAAO,EAAE,4BAA4B;SACtC,CAAC;IACJ,CAAC;IAED,sBAAsB;IACtB,MAAM,QAAQ,GAAG,cAAc,CAAC,aAAa,CAAC,CAAC;IAE/C,4BAA4B;IAC5B,MAAM,WAAW,GAAG,SAAS,CAAC,aAAa,CAAC,CAAC;IAE7C,OAAO;QACL,OA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AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAgB,WAAW,CAAC,GAAW,EAAE,WAAqB,gBAAQ,CAAC,OAAO;IAC5E,MAAM,UAAU,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;IAE9B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,QAAQ,KAAK,gBAAQ,CAAC,IAAI,EAAE,CAAC;QAC/B,OAAO,UAAU,CAAC,MAAM,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAO,UAAU,CAAC,MAAM,KAAK,CAAC,CAAC;AACjC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@misterhomer1992/miit-bot-payment",
3
+ "version": "1.0.0",
4
+ "description": "A TypeScript utility library for payment validation and formatting",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "prepublishOnly": "npm run build",
10
+ "test": "node --test dist/**/*.test.js",
11
+ "lint": "eslint src --ext .ts",
12
+ "format": "prettier --write \"src/**/*.ts\"",
13
+ "watch": "tsc --watch",
14
+ "example": "ts-node example.ts"
15
+ },
16
+ "keywords": [
17
+ "payment",
18
+ "validation",
19
+ "formatting",
20
+ "typescript",
21
+ "utilities"
22
+ ],
23
+ "author": "",
24
+ "license": "MIT",
25
+ "devDependencies": {
26
+ "@types/node": "^20.10.0",
27
+ "@typescript-eslint/eslint-plugin": "^6.13.0",
28
+ "@typescript-eslint/parser": "^6.13.0",
29
+ "eslint": "^8.54.0",
30
+ "prettier": "^3.1.0",
31
+ "ts-node": "^10.9.2",
32
+ "typescript": "^5.3.2"
33
+ },
34
+ "files": [
35
+ "dist",
36
+ "README.md",
37
+ "LICENSE"
38
+ ],
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/misterhomer1992/miit-bot-payment.git"
42
+ },
43
+ "bugs": {
44
+ "url": "https://github.com/misterhomer1992/miit-bot-payment/issues"
45
+ },
46
+ "homepage": "https://github.com/misterhomer1992/miit-bot-payment#readme"
47
+ }