Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
credit-card-type
Advanced tools
The credit-card-type npm package is used to detect the type of a credit card based on its number. It can identify various card types such as Visa, MasterCard, American Express, and more. This package is useful for validating and formatting credit card information in web applications.
Detect Card Type
This feature allows you to detect the type of a credit card based on its number. The code sample demonstrates how to use the package to identify a Visa card.
const creditCardType = require('credit-card-type');
const cardInfo = creditCardType('4111111111111111');
console.log(cardInfo);
Get Card Type by Name
This feature allows you to get detailed information about a specific card type by its name. The code sample shows how to retrieve information about Visa cards.
const creditCardType = require('credit-card-type');
const visaCard = creditCardType.getTypeInfo('visa');
console.log(visaCard);
Add Custom Card Type
This feature allows you to add a custom card type to the list of recognized card types. The code sample demonstrates how to add a custom card type and detect it using the package.
const creditCardType = require('credit-card-type');
creditCardType.addCard({
niceType: 'My Custom Card',
type: 'mycustomcard',
patterns: [1234],
gaps: [4, 8, 12],
lengths: [16],
code: { name: 'CVV', size: 3 }
});
const customCardInfo = creditCardType('1234567812345678');
console.log(customCardInfo);
The card-validator package provides a comprehensive set of validation functions for credit card numbers, expiration dates, and CVV codes. It offers more extensive validation capabilities compared to credit-card-type, which focuses primarily on card type detection.
The payment package offers utilities for formatting and validating payment-related information, including credit card numbers. It provides similar functionality to credit-card-type but also includes additional features for formatting and handling other payment details.
The creditcards package is a lightweight library for validating and formatting credit card information. It offers similar card type detection capabilities as credit-card-type but also includes functions for formatting card numbers and expiration dates.
Credit Card Type provides a useful utility method for determining a credit card type from both fully qualified and partial numbers. This is not a validation library but rather a smaller component to help you build your own validation or UI library.
This library is designed for type-as-you-go detection (supports partial numbers) and is written in CommonJS so you can use it in Node, io.js, and the browser.
To install via npm:
npm install credit-card-type
To install via Bower:
bower install credit-card-type
var creditCardType = require('credit-card-type');
var visaCards = creditCardType('4111');
console.log(visaCards[0].type); // 'visa'
var ambiguousCards = creditCardType('6');
console.log(ambiguousCards.length); // 3
console.log(ambiguousCards[0].niceType); // 'Discover'
console.log(ambiguousCards[1].niceType); // 'UnionPay'
console.log(ambiguousCards[2].niceType); // 'Maestro'
creditCardType(number: String)
creditCardType
will return an array of objects, each with the following data:
Key | Type | Description |
---|---|---|
niceType | String | A pretty printed representation of the card brand. - Visa - Mastercard - American Express - Diners Club - Discover - JCB - UnionPay - Maestro - Mir - Elo |
type | String | A code-friendly presentation of the card brand (useful to class names in CSS). Please refer to Card Type "Constants" below for the list of possible values. - visa - mastercard - american-express - diners-club - discover - jcb - unionpay - maestro - mir - elo |
gaps | Array | The expected indeces of gaps in a string representation of the card number. For example, in a Visa card, 4111 1111 1111 1111 , there are expected spaces in the 4th, 8th, and 12th positions. This is useful in setting your own formatting rules. |
lengths | Array | The expected lengths of the card number as an array of strings (excluding spaces and / characters). |
code | Object | The information regarding the security code for the determined card. Learn more about the code object below. |
If no card types are found, this returns an empty array.
creditCardType.getTypeInfo(type: String)
getTypeInfo
will return a singular object (with the same structure as creditCardType
) corresponding with the specified type
, or undefined if the specified type
is invalid/unknown.
Named variables are provided for each of the supported card types:
VISA
MASTERCARD
AMERICAN_EXPRESS
DINERS_CLUB
DISCOVER
JCB
UNIONPAY
MAESTRO
MIR
ELO
code
Card brands provide different nomenclature for their security codes as well as varying lengths.
Brand | Name | Size |
---|---|---|
Visa | CVV | 3 |
Mastercard | CVC | 3 |
American Express | CID | 4 |
Diners Club | CVV | 3 |
Discover | CID | 3 |
JCB | CVV | 3 |
UnionPay | CVN | 3 |
Maestro | CVC | 3 |
Mir | CVP | 3 |
Elo | CVE | 3 |
A full response for a Visa
card will look like this:
{
niceType: 'Visa',
type: 'visa',
gaps: [ 4, 8, 12 ],
lengths: [16],
code: { name: 'CVV', size: 3 }
}
CommonJS:
var creditCardType = require('credit-card-type');
var getTypeInfo = require('credit-card-type').getTypeInfo;
var CardType = require('credit-card-type').types;
ES6:
import creditCardType, { getTypeInfo, types as CardType } from 'credit-card-type';
creditCardType(cardNumber).filter(function(card) {
return card.type == CardType.MASTERCARD || card.type == CardType.VISA;
});
You can add additional card brands not supportted by the the module with addCard
. Pass in the configuration object.
creditCardType.addCard({
niceType: 'NewCard',
type: 'new-card',
prefixPattern: /^(2|23|234)$/,
exactPattern: /^(2345)\d*$/,
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVV',
size: 3
}
});
You can also modify existing cards:
creditCardType.addCard({
niceType: 'Visa with Custom Nice Type',
type: creditCardType.types.VISA,
prefixPattern: /^(4)$/,
exactPattern: /^(4[0-1])\d*$/, // restrict to only match visas that start with 40 or 41
gaps: [4, 8, 12],
lengths: [13, 16, 19], // add support for old, deprecated 13 digit visas
code: {
name: 'CVV',
size: 3
}
});
The exactPattern
regex is checked first. If that pattern is matched, the module takes that result over the prefixPattern
. The prefixPattern
is a softer matcher for when the exact card type has not yet been determined, so multiple card types may be returned.
As an example, by default, the Visa exactPattern
matches any card number that starts with 4. If you are adding a card type that has a bin range that starts with 4, you will not only have to call addCard
with the new card type, but addCard
with Visa to alter the exactPattern
and prefixPattern
;
var visa = creditCardType.getTypeInfo(creditCardType.types.VISA);
visa.prefixPattern = /^(4)$/;
visa.exactPattern = /^(4[0-1])\d*$/; // restrict to only match visas that start with 40 or 41
creditCardType.addCard(visa);
Adding new cards puts them at the bottom of the priority for testing. Priority is determined by an array. By default, the priority looks like:
[
creditCardType.types.VISA,
creditCardType.types.MASTERCARD,
creditCardType.types.AMERICAN_EXPRESS,
creditCardType.types.DINERS_CLUB,
creditCardType.types.DISCOVER,
creditCardType.types.JCB,
creditCardType.types.UNIONPAY,
creditCardType.types.MAESTRO,
creditCardType.types.MIR,
creditCardType.types.ELO
]
You can adjust the order using changeOrder
. The number you pass in as the second argument is where the card is inserted into the array. The closer to the beginning of the array, the higher priority it has.
creditCardType.changeOrder('my-new-card', 0); // give custom card type the highest priority
creditCardType.changeOrder('my-new-card', 3); // give it a priority at position 3 in the test order array
You can also remove cards with removeCard
.
creditCardType.removeCard(creditCardType.types.VISA);
If you need to reset the modifications you have created, simply call resetModifications
:
creditCardType.resetModifications();
You can update cards with updateCard
. Pass in the card type and the configuration object. Any properties left off will inherit from the original card object.
creditCardType.updateCard(creditCardType.types.VISA, {
niceType: 'Fancy Visa',
lengths: [11, 16]
});
var visa = creditCardType.getCardInfo(creditCardType.types.VISA);
// overwritten properties
visa.niceType; // 'Fancy Visa'
visa.length; // [11, 16]
// unchanged properties
visa.gaps // [4, 8, 12]
visa.code.name // 'CVV'
If you need to reset the modifications you have created, simply call resetModifications
:
creditCardType.resetModifications();
function prettyCardNumber(cardNumber, cardType) {
var card = getTypeInfo(cardType);
if (card) {
var offsets = [].concat(0, card.gaps, cardNumber.length);
var components = [];
for (var i = 0; offsets[i] < cardNumber.length; i++) {
var start = offsets[i];
var end = Math.min(offsets[i + 1], cardNumber.length);
components.push(cardNumber.substring(start, end));
}
return components.join(' ');
}
return cardNumber;
}
prettyCardNumber('xxxxxxxxxx343', CardType.AMERICAN_EXPRESS); // 'xxxx xxxxxx 343'
We use nvm
for managing our node versions, but you do not have to. Replace any nvm
references with the tool of your choice below.
nvm install
npm install
All testing dependencies will be installed upon npm install
and the test suite executed with npm test
.
7.1.0
Elo
card typeupdateCard
method (#77)FAQs
A library for determining credit card type
The npm package credit-card-type receives a total of 290,674 weekly downloads. As such, credit-card-type popularity was classified as popular.
We found that credit-card-type demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.