Skip to content

Commit

Permalink
Support additional card types with $.payment.cards
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesreggio committed Jan 21, 2015
1 parent 87f494f commit c16dd65
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 68 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Supported card types are:
* Forbrugsforeningen
* Dankort

(Additional card types are supported by extending the `$.payment.cards` array.)

## API

### $.fn.payment('formatCardNumber')
Expand Down Expand Up @@ -172,6 +174,30 @@ $('input.cc-exp').payment('cardExpiryVal') //=> {month: 4, year: 2020}

This function doesn't perform any validation of the month or year; use `$.payment.validateCardExpiry(month, year)` for that.

### $.payment.cards

Array of objects that describe valid card types. Each object should contain the following fields:

``` javascript
{
// Card type, as returned by $.payment.cardType.
type: 'mastercard',
// Regex used to identify the card type. For the best experience, this should be
// the shortest pattern that can guarantee the card is of a particular type.
pattern: /^5[0-5]/,
// Array of valid card number lengths.
length: [16],
// Array of valid card CVC lengths.
cvcLength: [3],
// Boolean indicating whether a valid card number should satisfy the Luhn check.
luhn: true,
// Regex used to format the card number. Each match is joined with a space.
format: /(\d{1,4})/g
}
```

When identifying a card type, the array is traversed in order until the card number matches a `pattern`. For this reason, patterns with higher specificity should appear towards the beginning of the array.

## Example

Look in [`./example/index.html`](example/index.html)
Expand Down
2 changes: 1 addition & 1 deletion lib/jquery.payment.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

134 changes: 67 additions & 67 deletions src/jquery.payment.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,97 +8,97 @@ $.fn.payment = (method, args...) ->

defaultFormat = /(\d{1,4})/g

cards = [
$.payment.cards = cards = [
# Debit cards must come first, since they have more
# specific patterns than their credit-card equivalents.
{
type: 'visaelectron'
pattern: /^4(026|17500|405|508|844|91[37])/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
type: 'visaelectron'
pattern: /^4(026|17500|405|508|844|91[37])/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
{
type: 'maestro'
pattern: /^(5(018|0[23]|[68])|6(39|7))/
format: defaultFormat
length: [12..19]
cvcLength: [3]
luhn: true
type: 'maestro'
pattern: /^(5(018|0[23]|[68])|6(39|7))/
format: defaultFormat
length: [12..19]
cvcLength: [3]
luhn: true
}
{
type: 'forbrugsforeningen'
pattern: /^600/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
type: 'forbrugsforeningen'
pattern: /^600/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
{
type: 'dankort'
pattern: /^5019/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
type: 'dankort'
pattern: /^5019/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
# Credit cards
{
type: 'visa'
pattern: /^4/
format: defaultFormat
length: [13, 16]
cvcLength: [3]
luhn: true
type: 'visa'
pattern: /^4/
format: defaultFormat
length: [13, 16]
cvcLength: [3]
luhn: true
}
{
type: 'mastercard'
pattern: /^5[0-5]/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
type: 'mastercard'
pattern: /^5[0-5]/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
{
type: 'amex'
pattern: /^3[47]/
format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/
length: [15]
cvcLength: [3..4]
luhn: true
type: 'amex'
pattern: /^3[47]/
format: /(\d{1,4})(\d{1,6})?(\d{1,5})?/
length: [15]
cvcLength: [3..4]
luhn: true
}
{
type: 'dinersclub'
pattern: /^3[0689]/
format: defaultFormat
length: [14]
cvcLength: [3]
luhn: true
type: 'dinersclub'
pattern: /^3[0689]/
format: defaultFormat
length: [14]
cvcLength: [3]
luhn: true
}
{
type: 'discover'
pattern: /^6([045]|22)/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
type: 'discover'
pattern: /^6([045]|22)/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
{
type: 'unionpay'
pattern: /^(62|88)/
format: defaultFormat
length: [16..19]
cvcLength: [3]
luhn: false
type: 'unionpay'
pattern: /^(62|88)/
format: defaultFormat
length: [16..19]
cvcLength: [3]
luhn: false
}
{
type: 'jcb'
pattern: /^35/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
type: 'jcb'
pattern: /^35/
format: defaultFormat
length: [16]
cvcLength: [3]
luhn: true
}
]

Expand Down
21 changes: 21 additions & 0 deletions test/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,27 @@ describe 'jquery.payment', ->
assert.equal($.payment.cardType('3530111333300000'), 'jcb')
assert.equal($.payment.cardType('3566002020360505'), 'jcb')

describe 'Extending the card collection', ->
it 'should expose an array of standard card types', ->
cards = $.payment.cards
assert Array.isArray(cards)

visa = card for card in cards when card.type is 'visa'
assert.notEqual visa, null

it 'should support new card types', ->
wing = {
type: 'wing'
pattern: /^501818/
length: [16]
luhn: false
}
$.payment.cards.unshift wing

wingCard = '5018 1818 1818 1818'
assert.equal $.payment.cardType(wingCard), 'wing'
assert.equal $.payment.validateCardNumber(wingCard), true

describe 'formatCardNumber', ->
it 'should format cc number correctly', (done) ->
$number = $('<input type=text>').payment('formatCardNumber')
Expand Down

0 comments on commit c16dd65

Please sign in to comment.