-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.js
89 lines (72 loc) · 2.13 KB
/
server.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
/**
* Credit Card Check App - server.js
*/
"use strict";
// Include modules
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(express.static(__dirname + '/'));
app.use(bodyParser.json());
/**
* Handle POST request from client.
*/
app.post('/api/cc', function(req, res) {
/**
* Note: - Only credit card number is being validated
* Expiry date, Expiry year, CSV is not being validated
*/
var CCApp = {};
CCApp.number = req.body.ccnumber; // Store credit card number from the request.
/**
* Validates the credit card number using the Luhn10 algorithm.
*
* @param {Number} number Credit card number.
* @returns {Boolean}
* Credits: Online resources
*/
CCApp.validateCC = function() {
var sum = 0;
var numdigits = this.number.length;
var parity = numdigits % 2;
for(var i=0; i < numdigits; i++) {
var digit = parseInt(this.number.charAt(i));
if(i % 2 == parity) digit *= 2; // Double every other digits
if(digit > 9) digit -= 9;
sum += digit;
}
return (sum % 10) == 0;
}
/**
* Find out the card type using card length and starting seq number.
*
* @param {Number} number Credit card number.
* @returns {String}
*/
CCApp.findOutCardType = function() {
var length = this.number.length;
if ((/^(4)/.test(this.number)) && ((length == 13) || (length == 16) || (length == 19))) {
return 'Visa';
} else if ((/^3[47]/.test(this.number)) && (length == 15)) {
return 'American Express';
} else if ((/^5[1-5]/.test(this.number)) && (length == 16)) {
return 'MasterCard';
} else {
return 'Unknown';
}
}
/**
* Send response to the client after credit card validation.
* @returns {status, type} object
*/
res.send({
status: CCApp.validateCC(),
type: CCApp.findOutCardType()
});
});
/**
* Start Listening request
*/
var port = process.env.PORT || 3000;
app.listen(port);
console.log('Server is listening on port : ' + port );