-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (106 loc) · 3.32 KB
/
index.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
// Node Engine v16.14.2
function isCreditCard(card) {
if (card.length >= 15 && card.length <= 19) {
return true;
}
return false;
}
function isEncryptedToken(token) {
if (token.length === 44) {
return true;
}
return false;
}
function detectCardType(cardNumber) {
// Define regular expressions for different card types
var visaPattern = /^4[0-9]{12}(?:[0-9]{3})?$/;
var mastercardPattern = /^5[1-5][0-9]{14}$/;
var amexPattern = /^3[47][0-9]{13}$/;
var discoverPattern = /^6(?:011|5[0-9]{2})[0-9]{12}$/;
var dinersClubPattern = /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/;
// Match the card number against the regular expressions
if (dinersClubPattern.test(cardNumber)) {
return "diners club";
} else if (visaPattern.test(cardNumber)) {
return "visa";
} else if (mastercardPattern.test(cardNumber)) {
return "mastercard";
} else if (amexPattern.test(cardNumber)) {
return "american express";
} else if (discoverPattern.test(cardNumber)) {
return "discover";
} else {
return "invalid";
}
}
function validateCreditCardNumber(cardNumber) {
// Remove any spaces or dashes from the card number
cardNumber = cardNumber.replace(/[^\d]/g, "");
// Check if the card number is empty or not a number
if (cardNumber === "" || isNaN(cardNumber)) {
return false;
}
// Reverse the card number and convert it to an array
var cardNumberArray = cardNumber.split("").reverse();
// Apply the Luhn algorithm
var sum = 0;
for (var i = 0; i < cardNumberArray.length; i++) {
var num = parseInt(cardNumberArray[i], 10);
if (i % 2 === 1) {
num *= 2;
if (num > 9) {
num -= 9;
}
}
sum += num;
}
// The card number is valid if the sum is divisible by 10
return sum % 10 === 0;
}
function validateCVVORCVCCode(securityCode) {
// Remove any spaces or dashes from the security code
securityCode = securityCode.replace(/[^\d]/g, "");
// Check if the security code is empty or not a number
if (securityCode === "" || isNaN(securityCode)) {
return false;
}
// Check if the security code is 3 or 4 digits long
var validLengths = [3, 4];
if (!validLengths.includes(securityCode.length)) {
return false;
}
// The security code is valid
return true;
}
function validateExpirationDate(expirationMonth, expirationYear) {
// Get the current date
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth() + 1; // Adding 1 since getMonth() returns zero-based index
// Convert the expiration month and year to numbers
var expMonth = parseInt(expirationMonth, 10);
var expYear = parseInt(expirationYear, 10);
// Check if the expiration year is in the future
if (expYear < currentYear) {
return false;
}
// Check if the expiration year is the same as the current year but the expiration month is in the past
if (expYear === currentYear && expMonth < currentMonth) {
return false;
}
// The expiration date is valid
return true;
}
// Example usage
var expirationMonth = "06";
var expirationYear = "2023";
var isValid = validateExpirationDate(expirationMonth, expirationYear);
console.log(isValid); // Output: true
module.exports = {
isCreditCard,
isEncryptedToken,
detectCardType,
validateCreditCardNumber,
validateCVVORCVCCode,
validateExpirationDate,
};