Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepollak committed Aug 20, 2017
2 parents 96f3a03 + 35287a7 commit c95d07a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -33,6 +33,7 @@ Supported card types are:
* Laser
* UnionPay
* Elo
* Hipercard

## API

Expand Down Expand Up @@ -151,6 +152,7 @@ Returns a card type. Either:
* `laser`
* `unionpay`
* `elo`
* `hipercard`

The function will return `null` if the card type can't be determined.

Expand Down
17 changes: 14 additions & 3 deletions dist/payment.js
Expand Up @@ -137,6 +137,13 @@ var payment =
length: [13, 16, 19],
cvcLength: [3],
luhn: true
}, {
type: 'verve',
pattern: /^([506]{3})([0-9]{1,16})$/,
format: defaultFormat,
length: [19],
cvcLength: [3],
luhn: false
}
];

Expand Down Expand Up @@ -578,10 +585,14 @@ var payment =
return (ref = num.match(card.format)) != null ? ref.join(' ') : void 0;
} else {
groups = card.format.exec(num);
if (groups != null) {
groups.shift();
if (groups == null) {
return;
}
return groups != null ? groups.join(' ') : void 0;
groups.shift();
groups = groups.filter(function(n) {
return n;
});
return groups.join(' ');
}
}
};
Expand Down
19 changes: 19 additions & 0 deletions spec/index.spec.coffee
Expand Up @@ -76,6 +76,14 @@ describe 'payment', ->
assert(Payment.fns.validateCardNumber('6759649826438453'), 'maestro')
assert(Payment.fns.validateCardNumber('6759 4111 0000 0008'), 'maestro')
assert(Payment.fns.validateCardNumber('6759 6498 2643 8453'), 'maestro')
it 'should validate hipercard card types', ->
assert(Payment.fns.validateCardNumber('6062821086773091'), 'hipercard')
assert(Payment.fns.validateCardNumber('6375683647504601'), 'hipercard')
assert(Payment.fns.validateCardNumber('6370957513839696'), 'hipercard')
assert(Payment.fns.validateCardNumber('6375688248373892'), 'hipercard')
assert(Payment.fns.validateCardNumber('6012135281693108'), 'hipercard')
assert(Payment.fns.validateCardNumber('38410036464094'), 'hipercard')
assert(Payment.fns.validateCardNumber('38414050328938'), 'hipercard')

describe 'Validating a CVC', ->
it 'should fail if is empty', ->
Expand Down Expand Up @@ -227,6 +235,17 @@ describe 'payment', ->
topic = Payment.fns.cardType '4012506712121212'
assert.equal topic, 'visa'

it 'should return hipercard type', ->
assert.equal (Payment.fns.cardType '384100'), 'hipercard'
assert.equal (Payment.fns.cardType '384140'), 'hipercard'
assert.equal (Payment.fns.cardType '384160'), 'hipercard'
assert.equal (Payment.fns.cardType '6062'), 'hipercard'
assert.equal (Payment.fns.cardType '6012'), 'hipercard'

it 'should not return hipercard type', ->
topic = Payment.fns.cardType '6011'
assert.equal topic, 'discover'

it 'that is not numbers should return null', ->
topic = Payment.fns.cardType 'aoeu'
assert.equal topic, null
Expand Down
32 changes: 24 additions & 8 deletions src/index.coffee
Expand Up @@ -19,6 +19,14 @@ cards = [
cvcLength: [3],
luhn: true
}
{
type: 'hipercard',
pattern: /^(384100|384140|384160|606282|637095|637568|60(?!11))/,
format: defaultFormat,
length: [14..19],
cvcLength: [3],
luhn: true
}
{
type: 'dinersclub'
pattern: /^(36|38|30[0-5])/
Expand Down Expand Up @@ -85,7 +93,7 @@ cards = [
}
{
type: 'elo'
pattern: /^(4011|438935|45(1416|76|7393)|50(4175|6699|67|90[4-7])|63(6297|6368))/,
pattern: /^(4011(78|79)|43(1274|8935)|45(1416|7393|763(1|2))|50(4175|6699|67[0-7][0-9]|9000)|627780|63(6297|6368)|650(03([^4])|04([0-9])|05(0|1)|4(0[5-9]|3[0-9]|8[5-9]|9[0-9])|5([0-2][0-9]|3[0-8])|9([2-6][0-9]|7[0-8])|541|700|720|901)|651652|655000|655021)/,
format: defaultFormat
length: [16]
cvcLength: [3]
Expand All @@ -99,6 +107,14 @@ cards = [
cvcLength: [3]
luhn: true
}
{
type: 'verve',
pattern: /^([506]{3})([0-9]{1,16})$/,
format: defaultFormat,
length: [19],
cvcLength: [3],
luhn: false
}
]

cardFromNumber = (num) ->
Expand Down Expand Up @@ -177,8 +193,7 @@ formatCardNumber = (maxLength) -> (e) ->

# If '4242' + 4
if re.test(value)
e.preventDefault()
QJ.val(target, value + ' ' + digit)
QJ.val(target, value + ' ')
QJ.trigger(target, 'change')

formatBackCardNumber = (e) ->
Expand All @@ -198,9 +213,8 @@ formatBackCardNumber = (e) ->
e.preventDefault()
QJ.val(target, value.replace(/\d\s$/, ''))
QJ.trigger(target, 'change')
else if /\s\d?$/.test(value)
e.preventDefault()
QJ.val(target, value.replace(/\s\d?$/, ''))
else if /\s\d$/.test(value)
QJ.val(target, value.replace(/\d?$/, ''))
QJ.trigger(target, 'change')

# Format Expiry
Expand Down Expand Up @@ -457,8 +471,10 @@ class Payment
num.match(card.format)?.join(' ')
else
groups = card.format.exec(num)
groups?.shift()
groups?.join(' ')
return unless groups?
groups.shift()
groups = groups.filter((n) -> n) # Filter empty groups
groups.join(' ')
@restrictNumeric: (el) ->
QJ.on el, 'keypress', restrictNumeric
@cardExpiryVal: (el) ->
Expand Down

0 comments on commit c95d07a

Please sign in to comment.