Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Dankort support #106

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ console.log(ambiguousCards[2].niceType); // 'Maestro'

| Key | Type | Description |
| ---------- | -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `niceType` | `String` | A pretty printed representation of the card brand.<br/>- `Visa`<br />- `Mastercard`<br />- `American Express`<br />- `Diners Club`<br />- `Discover`<br />- `JCB`<br />- `UnionPay`<br />- `Maestro`<br />- `Mir`<br />- `Elo`<br />- `Hiper`<br />- `Hipercard` |
| `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.<br/>- `visa`<br />- `mastercard`<br />- `american-express`<br />- `diners-club`<br />- `discover`<br />- `jcb`<br />- `unionpay`<br />- `maestro`<br />- `mir`<br /> - `elo`<br /> - `hiper`<br /> - `hipercard` |
| `niceType` | `String` | A pretty printed representation of the card brand.<br/>- `Visa`<br />- `Mastercard`<br />- `American Express`<br />- `Diners Club`<br />- `Discover`<br />- `JCB`<br />- `UnionPay`<br />- `Maestro`<br />- `Mir`<br />- `Elo`<br />- `Hiper`<br />- `Hipercard`<br />- `Dankort` |
| `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.<br/>- `visa`<br />- `mastercard`<br />- `american-express`<br />- `diners-club`<br />- `discover`<br />- `jcb`<br />- `unionpay`<br />- `maestro`<br />- `mir`<br /> - `elo`<br /> - `hiper`<br /> - `hipercard`<br /> - `dankort` |
| `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](#code) below. |
Expand All @@ -58,6 +58,7 @@ If no card types are found, this returns an empty array.
Named variables are provided for each of the supported card types:

- `AMERICAN_EXPRESS`
- `DANKORT`
- `DINERS_CLUB`
- `DISCOVER`
- `ELO`
Expand Down Expand Up @@ -88,6 +89,7 @@ Card brands provide different nomenclature for their security codes as well as v
| `Elo` | `CVE` | 3 |
| `Hiper` | `CVC` | 3 |
| `Hipercard` | `CVC` | 4 |
| `Dankort` | `CVC` | 3 |

A full response for a `Visa` card will look like this:

Expand Down Expand Up @@ -146,6 +148,8 @@ card is _either_ a Visa or an Elo card. Once the card number becomes `401178`,
the modules sees that an exact match for the ELO bin has been made, and the module reports
that the card can only be an Elo card.

The above rule is also applied for identifying Dankort cards, which start with `5019`.

#### Adding Card Types

You can add additional card brands not supportted by the the module with `addCard`. Pass in the configuration object.
Expand Down Expand Up @@ -195,7 +199,8 @@ Adding new cards puts them at the bottom of the priority for testing. Priority i
creditCardType.types.ELO,
creditCardType.types.MIR,
creditCardType.types.HIPER,
creditCardType.types.HIPERCARD
creditCardType.types.HIPERCARD,
creditCardType.types.DANKORT
];
```

Expand Down
18 changes: 12 additions & 6 deletions __tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ describe('creditCardType', () => {
['637609', 'hiper'],
['637599', 'hiper'],
['637612', 'hiper'],
['637568', 'hiper']
['637568', 'hiper'],

['5019', 'dankort']
])('Matches %s to brand %s', (number, type) => {
const actual = creditCardType(number);

Expand Down Expand Up @@ -183,13 +185,15 @@ describe('creditCardType', () => {
'elo',
'mir',
'hiper',
'hipercard'
'hipercard',
'dankort'
]
],
['2', ['mastercard', 'jcb', 'mir']],
['3', ['american-express', 'diners-club', 'jcb']],
['5', ['mastercard', 'maestro', 'elo']],
['50', ['maestro', 'elo']],
['5', ['mastercard', 'maestro', 'elo', 'dankort']],
['50', ['maestro', 'elo', 'dankort']],
['501', ['maestro', 'dankort']],
['6', ['discover', 'unionpay', 'maestro', 'elo', 'hiper', 'hipercard']],
['60', ['discover', 'maestro', 'hipercard']],
['601', ['discover', 'maestro']],
Expand Down Expand Up @@ -262,7 +266,8 @@ describe('creditCardType', () => {
['Diners Club', '30569309025904', {size: 3, name: 'CVV'}],
['UnionPay', '6220558812340000', {size: 3, name: 'CVN'}],
['Maestro', '6304000000000000', {size: 3, name: 'CVC'}],
['Mir', '2200000000000000', {size: 3, name: 'CVP2'}]
['Mir', '2200000000000000', {size: 3, name: 'CVP2'}],
['Dankort', '501912345678', {size: 3, name: 'CVC'}]
])('returns security codes for %s', (brand, number, code) => {
const parsedCode = creditCardType(number)[0].code;

Expand All @@ -280,7 +285,8 @@ describe('creditCardType', () => {
['Visa', '4', {type: 'visa', lengths: [16, 18, 19]}],
['Mastercard', '54', {type: 'mastercard', lengths: [16]}],
['JCB', '35', {type: 'jcb', lengths: [16, 17, 18, 19]}],
['Mir', '220', {type: 'mir', lengths: [16, 17, 18, 19]}]
['Mir', '220', {type: 'mir', lengths: [16, 17, 18, 19]}],
['Dankort', '5019', {type: 'dankort', lengths: [16]}]
])('returns lengths for %s', (brand, number, meta) => {
const cardType = creditCardType(number)[0];

Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var cardNames = {
ELO: 'elo',
MIR: 'mir',
HIPER: 'hiper',
HIPERCARD: 'hipercard'
HIPERCARD: 'hipercard',
DANKORT: 'dankort'
};

var ORIGINAL_TEST_ORDER = [
Expand All @@ -36,7 +37,8 @@ var ORIGINAL_TEST_ORDER = [
cardNames.ELO,
cardNames.MIR,
cardNames.HIPER,
cardNames.HIPERCARD
cardNames.HIPERCARD,
cardNames.DANKORT
];

testOrder = clone(ORIGINAL_TEST_ORDER);
Expand Down
16 changes: 15 additions & 1 deletion lib/card-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ var cardTypes = {
type: 'maestro',
patterns: [
493698,
[500000, 506698],
[5000, 5018],
[502000, 506698],
Comment on lines +137 to +138
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I split the patterns here to make 5019 Dankort-exclusive. This didn't affect the other unit tests for Maestro.

[506779, 508999],
[56, 59],
63,
Expand Down Expand Up @@ -227,6 +228,19 @@ var cardTypes = {
name: 'CVC',
size: 3
}
},
dankort: {
niceType: 'Dankort',
type: 'dankort',
patterns: [
5019
],
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
}
};

Expand Down