-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
95 lines (87 loc) · 2.86 KB
/
index.js
File metadata and controls
95 lines (87 loc) · 2.86 KB
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
const http = require('http');
const fs = require('fs');
const path = require('path');
const cardFlags = {
'Mastercard': '5277959558870483',
'Visa': '4111111111111111',
'Elo': '6362970000457013',
'American Express': '378282246310005',
'Discover': '6011111111111117',
'Hipercard': '6062825189259528',
'JCB': '3530111333300000',
'Diners Club': '30569309025904',
'Voyager': '869940697287073',
'Aura': '5078601870000127985'
};
// Exemplo de uso
const cardNumber = cardFlags['Voyager'];
if (validateCreditCardNumber(cardNumber)) {
const cardType = determineCardType(cardNumber);
console.log(`O cartão é válido e a bandeira é ${cardType}`);
} else {
console.log('Cartão Inválido');
}
function validateCreditCardNumber(cardNumber) {
let sum = 0;
let shouldDouble = false;
for (let i = cardNumber.length - 1; i >= 0; i--) {
let digit = parseInt(cardNumber.charAt(i));
if (shouldDouble) {
digit *= 2;
if (digit > 9) {
digit -= 9;
}
}
sum += digit;
shouldDouble = !shouldDouble;
}
return sum % 10 === 0;
}
function determineCardType(cardNumber) {
const cardTypes = {
Visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
Mastercard: /^5[1-5][0-9]{14}$/,
Elo: /^((636368)|(438935)|(504175)|(451416)|(636297)|(5067)|(4576)|(4011))\d+$/,
"American Express": /^3[47][0-9]{13}$/,
Discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/,
Hipercard: /^(606282\d{10}(\d{3})?)|(3841\d{15})$/,
JCB: /^(?:2131|1800|35\d{3})\d{11}$/,
"Diners Club": /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/,
Voyager: /^8699[0-9]{11}$/,
Aura: /^50[0-9]{14,17}$/
};
for (const [type, pattern] of Object.entries(cardTypes)) {
if (pattern.test(cardNumber)) {
return type;
}
}
return 'unknown';
}
const server = http.createServer((req, res) => {
if (req.url === '/' && req.method === 'GET') {
fs.readFile(path.join(__dirname, 'index.html'), (err, data) => {
if (err) {
res.writeHead(500);
res.end('Error loading index.html');
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(data);
}
});
} else if (req.url === '/card-type' && req.method === 'POST') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
const { cardNumber } = JSON.parse(body);
const cardType = determineCardType(cardNumber);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ cardType }));
});
} else {
res.writeHead(404);
res.end('Not Found');
}
});
server.listen(process.env.PORT || 3000)