-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
172 lines (154 loc) · 4 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
*
* Card Validation Library
*
* @author Naseef O
*
*/
/**
* Cards details
* Todo -> add other cards
*/
const cardDetails = {
Visa: {
code: "Visa",
displayName: "Visa",
lengths: [16],
cvvLength: 3,
imageUrl: "./assets/visa.svg",
},
MasterCard: {
code: "MasterCard",
displayName: "MasterCard",
lengths: [16],
cvvLength: 3,
imageUrl: "./assets/mastercard.svg",
},
Amex: {
code: "Amex",
displayName: "American Express",
lengths: [15],
cvvLength: 4,
imageUrl: "./assets/amex.svg",
},
};
/**
* This method is used to identify card type
*
* @param {String} cardNumber
* @returns
*/
const identifyCardType = (cardNumber) => {
// Todo --> check to identify other cards
if (!cardNumber) return null;
// visa
let re = new RegExp("^4");
if (cardNumber.match(re) != null) { return "Visa"; }
// Mastercard
// Updated for Mastercard 2017 BINs expansion
if (/^(5[1-5][0-9]{14}|2(22[1-9][0-9]{12}|2[3-9][0-9]{13}|[3-6][0-9]{14}|7[0-1][0-9]{13}|720[0-9]{12}))$/.test(cardNumber)) {
return "MasterCard";
}
// AMEX
re = new RegExp("^3[47]");
if (cardNumber.match(re) != null) { return "Amex"; }
// Discover
re = new RegExp("^(6011|622(12[6-9]|1[3-9][0-9]|[2-8][0-9]{2}|9[0-1][0-9]|92[0-5]|64[4-9])|65)");
if (cardNumber.match(re) != null) { return "Discover"; }
// Diners
re = new RegExp("^36");
if (cardNumber.match(re) != null) { return "Diners"; }
// Diners - Carte Blanche
re = new RegExp("^30[0-5]");
if (cardNumber.match(re) != null) { return "Diners - Carte Blanche"; }
// JCB
re = new RegExp("^35(2[89]|[3-8][0-9])");
if (cardNumber.match(re) != null) { return "JCB"; }
// Visa Electron
re = new RegExp("^(4026|417500|4508|4844|491(3|7))");
if (cardNumber.match(re) != null) { return "Visa Electron"; }
};
/**
* This method is used to get card details from card number
*
* @param {String} cardNumber
* @returns
*/
const getCardNumberDetails = (cardNumber) => {
const cardType = identifyCardType(cardNumber);
return cardType ? cardDetails[cardType] : null;
};
/**
* This method is used to get card details from card type
*
* @param {String} cardNumber
* @returns
*/
const getCardDetailsByDetails = (cardType) => (cardType ? cardDetails[cardType] : null);
/**
* This method is used to get card default image
*
* @param {String} cardType
* @returns
*/
const getCardDefaultImageByDetails = (cardType) => {
if (cardType && cardDetails[cardType]) {
return cardDetails[cardType].defaultImageUrl;
}
return "";
};
/**
* This method is used to check the card number is valid
*
* @param {String} cardNumber
* @param {String} type
* @returns
*/
const isValidCardNumber = (cardNumber, type) => cardDetails[type]
&& cardDetails[type].lengths.includes(cardNumber.length);
/**
* Checking is valid card length by card number and card type ,
* default it's 16 for cyber-source-integration
*
* @param {*} cardNumber
* @returns
*/
const isValidCardLength = (cardNumber) => {
const valid = false;
if (cardNumber) {
const card = getCardNumberDetails(cardNumber);
if (card
&& card.lengths.includes(cardNumber.length)) {
return true;
} if (cardNumber.length === 16) {
return true;
}
}
return valid;
};
const formatCardNumber = (e, value, position) => {
const format = (val) => val.replace(/[^\dA-Z]/gi, "")
.toUpperCase()
.replace(/(.{4})/g, "$1 ")
.trim();
const countSpaces = (text) => {
const spaces = text.match(/(\s+)/g);
return spaces ? spaces.length : 0;
};
const element = e.target;
element.value = format(value);
if (position !== element.value.length) {
const beforeCaret = value.substr(0, position);
const countPrevious = countSpaces(beforeCaret);
const countCurrent = countSpaces(format(beforeCaret));
element.selectionEnd = position + (countCurrent - countPrevious);
}
};
export {
getCardNumberDetails,
isValidCardNumber,
getCardDetailsByDetails,
getCardDefaultImageByDetails,
isValidCardLength,
formatCardNumber,
};