diff --git a/payment/index.d.ts b/payment/index.d.ts new file mode 100644 index 0000000000..e635a19028 --- /dev/null +++ b/payment/index.d.ts @@ -0,0 +1,87 @@ +// Type definitions for payment 2.1 +// Project: https://github.com/jessepollak/payment#readme +// Definitions by: Alexandre Paré +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface Fns { + /** + * Validates a card number: + * * Validates numbers + * * Validates Luhn algorithm + * * Validates length + */ + validateCardNumber(cardNumber: string): boolean; + /** + * Validates a card expiry: + * * Validates numbers + * * Validates in the future + * * Supports year shorthand + * * Supports formatted as formatCardExpiry input value + */ + validateCardExpiry(monthYear: string, year?: string): boolean; + /** + * Validates a card CVC: + * * Validates number + * * Validates length to 4 + */ + validateCardCVC(cvc: string, type: string): boolean; + /** + * Returns a card type. Either: + * * visa + * * mastercard + * * discover + * * amex + * * jcb + * * dinersclub + * * maestro + * * laser + * * unionpay + * * elo + * + * The function will return null if the card type can't be determined. + */ + cardType(cardNumber: string): string; + /** + * Parses a credit card expiry in the form of MM/YYYY, returning an object containing the `month` and `year`. + * Shorthand years, such as `13` are also supported (and converted into the longhand, e.g. `2013`). + */ + cardExpiryVal(monthYear: string | HTMLInputElement): MonthYear; +} + +interface MonthYear { + month: number; + year: number; +} + +declare var Payment: { + /** + * Formats card numbers: + * * Includes a space between every 4 digits + * * Restricts input to numbers + * * Limits to 16 numbers + * * Supports American Express formatting + * * Adds a class of the card type (e.g. 'visa') to the input + */ + formatCardNumber(elem: HTMLInputElement): HTMLInputElement; + /** + * Formats card expiry: + * * Includes a / between the month and year + * * Restricts input to numbers + * * Restricts length + */ + formatCardExpiry(elem: HTMLInputElement): HTMLInputElement; + /** + * Formats card CVC: + * * Restricts length to 4 numbers + * * Restricts input to numbers + */ + formatCardCVC(elem: HTMLInputElement): HTMLInputElement; + /** + * General numeric input restriction. + */ + restrictNumeric(elem: HTMLInputElement): HTMLInputElement; + + fns: Fns; +}; + +export = Payment; diff --git a/payment/payment-tests.ts b/payment/payment-tests.ts new file mode 100644 index 0000000000..5601067419 --- /dev/null +++ b/payment/payment-tests.ts @@ -0,0 +1,16 @@ +import * as Payment from "payment"; + +var input = document.getElementById('input') as HTMLInputElement; + +Payment.restrictNumeric(input); +Payment.formatCardNumber(input); +Payment.formatCardExpiry(input); +Payment.formatCardCVC(input); + +var card = "1234 5678 9012 3456"; + +var cardType = Payment.fns.cardType(card); +Payment.fns.validateCardNumber(card); +Payment.fns.validateCardExpiry("1 / 20"); +Payment.fns.validateCardExpiry("1", "20"); +Payment.fns.validateCardCVC("123", cardType); \ No newline at end of file diff --git a/payment/tsconfig.json b/payment/tsconfig.json new file mode 100644 index 0000000000..0b2323bef2 --- /dev/null +++ b/payment/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "payment-tests.ts" + ] +} diff --git a/payment/tslint.json b/payment/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/payment/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" }