Generate Luhn-valid credit card numbers with vendor-specific prefixes for testing purposes
The generated numbers have the right length, start with a real issuer identification number of the vendor and pass the Luhn check, so they get through the format validation of payment forms. They do not belong to real accounts and cannot be charged.
The package ships ES module and CommonJS builds with TypeScript declarations and has no runtime dependencies.
The package is published to GitHub Packages. Point the
@jermainee scope to its registry in your project's .npmrc and
authenticate
with a token that has the read:packages scope:
@jermainee:registry=https://npm.pkg.github.com
npm install --save-dev @jermainee/credit-card-generator
Installing straight from the repository works as well and builds the package on install:
npm install --save-dev github:jermainee/credit-card-generator
Import the CreditCardGenerator and the CreditCardVendor enum:
import CreditCardGenerator, { CreditCardVendor } from "@jermainee/credit-card-generator";With CommonJS the generator is the default export:
const { default: CreditCardGenerator, CreditCardVendor } = require("@jermainee/credit-card-generator");Generating a single credit card number:
const creditCardNumber = CreditCardGenerator.generateSingle(CreditCardVendor.VISA);
// "4716169796495547"Generating multiple credit card numbers:
const creditCardNumbers = CreditCardGenerator.generateMultiple(CreditCardVendor.MasterCard, 3);Generating a credit card number using your own preset (see ICreditCardPreset):
import CreditCardGenerator, { type ICreditCardPreset } from "@jermainee/credit-card-generator";
const preset: ICreditCardPreset = { digitCount: 19, prefixes: ["4"] };
const creditCardNumber = CreditCardGenerator.generateWithPreset(preset);digitCount is the total length of the number including the prefix and the check digit. One of the prefixes is
picked at random for every number.
Checking numbers with the Luhn algorithm:
import { calculateLuhnCheckDigit, isLuhnValid } from "@jermainee/credit-card-generator";
isLuhnValid("4111111111111111"); // true
calculateLuhnCheckDigit("411111111111111"); // 1Invalid input throws instead of producing invalid numbers: unknown vendors throw an Error, counts that are not
non-negative integers throw a RangeError and malformed presets throw a TypeError or RangeError.
| Vendor | Digits | Prefixes |
|---|---|---|
VISA |
16 | 4 |
MasterCard |
16 | 51–55 |
Amex |
15 | 34, 37 |
Diners |
16 | 300–303, 36, 38 |
Discover |
16 | 6011 |
EnRoute |
15 | 2014, 2149 |
JCB |
16 | 3528–3589 |
Voyager |
15 | 8699 |
npm ci
npm test # run the unit tests
npm run test:coverage # run the unit tests with coverage
npm run check # typecheck, test with coverage, build and lint the package
- Bump the version with
npm version <major|minor|patch> --no-git-tag-versionand describe the changes in CHANGELOG.md. - Merge the changes into
master. - Run the Node.js Package
workflow on
master. It checks and publishes the package and creates the GitHub release with the changelog entry. Publishing a GitHub release whose tag equals the package version publishes the package as well.