Skip to content

Instantly share code, notes, and snippets.

@r-martins
Created February 21, 2024 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r-martins/a6ac7fa65804daaa493213182c1ee4d7 to your computer and use it in GitHub Desktop.
Save r-martins/a6ac7fa65804daaa493213182c1ee4d7 to your computer and use it in GitHub Desktop.
Extending jQuery creditCard
/*region extending cards and card types from jqueryPayment to support new types*/
const typesPagBank = [
{
title: 'MasterCard',
type: 'mastercard',
pattern: '^(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}$',
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
},
{
title: 'American Express',
type: 'amex',
pattern: '^3([47]\\d*)?$',
isAmex: true,
gaps: [4, 10],
lengths: [15],
code: {
name: 'CID',
size: 4
}
},
{
title: 'Diners',
type: 'dinnersclub',
pattern: '^(3(0[0-5]|095|6|[8-9]))\\d*$',
gaps: [4, 10],
lengths: [14, 16, 17, 18, 19],
code: {
name: 'CVV',
size: 3
}
},
{
title: 'Elo',
type: 'elo',
pattern: '^((451416)|(509091)|(636368)|(636297)|(504175)|(438935)|(40117[8-9])|(45763[1-2])|' +
'(457393)|(431274)|(50990[0-2])|(5099[7-9][0-9])|(50996[4-9])|(509[1-8][0-9][0-9])|' +
'(5090(0[0-2]|0[4-9]|1[2-9]|[24589][0-9]|3[1-9]|6[0-46-9]|7[0-24-9]))|' +
'(5067(0[0-24-8]|1[0-24-9]|2[014-9]|3[0-379]|4[0-9]|5[0-3]|6[0-5]|7[0-8]))|' +
'(6504(0[5-9]|1[0-9]|2[0-9]|3[0-9]))|' +
'(6504(8[5-9]|9[0-9])|6505(0[0-9]|1[0-9]|2[0-9]|3[0-8]))|' +
'(6505(4[1-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9]|9[0-8]))|' +
'(6507(0[0-9]|1[0-8]))|(65072[0-7])|(6509(0[1-9]|1[0-9]|20))|' +
'(6516(5[2-9]|6[0-9]|7[0-9]))|(6550(0[0-9]|1[0-9]))|' +
'(6550(2[1-9]|3[0-9]|4[0-9]|5[0-8])))\\d*$',
gaps: [4, 8, 12],
lengths: [16],
code: {
name: 'CVC',
size: 3
}
},
{
title: 'Hipercard',
type: 'hipercard',
pattern: '^((606282)|(637095)|(637568)|(637599)|(637609)|(637612))\\d*$',
gaps: [4, 8, 12],
lengths: [13, 16],
code: {
name: 'CVC',
size: 3
}
},
{
title: 'Aura',
type: 'aura',
pattern: '^5078\\d*$',
gaps: [4, 8, 12],
lengths: [19],
code: {
name: 'CVC',
size: 3
}
}];
//cardsFriendly é usado apenas para permitir que o setCardType remova os cartões incorretos, mas não para verificar
const cardsFriendly = [
{type: 'elo', patterns: [], length: [], cssLength: [], format: '', luhn: false},
{type: 'aura', patterns: [], length: [], cssLength: [], format: '', luhn: false},
{type: 'hipercard', patterns: [], length: [], cssLength: [], format: '', luhn: false},
]
$.extend($.payment.cardsPagBank, typesPagBank);
$.extend($.payment.cards, cardsFriendly);
// método original
const originalCardType = $.payment.cardType;
// Estendemos a cardType do jqueryPayment
$.extend($.payment, {
cardType: function(num) {
// Tentamos buscar no nosso array de cartões
let cardTypes = getCardTypes(num);
if (cardTypes.length > 0) {
return cardTypes[0].type;
}
// Se não encontrarmos, retornamos o resultado original
return originalCardType.call(this, num);
}
});
/**
* Retorna o tipo de cartão
* @param cardNumber
* @returns {*|*[]}
*/
let getCardTypes = function (cardNumber) {
//remove spaces
cardNumber = cardNumber.replace(/\s/g, '');
let result = [];
if ($.isEmptyObject(cardNumber)) {
return result;
}
if (cardNumber === '') {
return $.extend(true, [], $.payment.cardsPagBank);
}
for (let i = 0; i < typesPagBank.length; i++) {
let value = typesPagBank[i];
if (new RegExp(value.pattern).test(cardNumber)) {
result.push($.extend(true, {}, value));
}
}
return result.slice(-1);
}
/*endregion*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment