-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidatePayment.js
More file actions
56 lines (43 loc) · 1.85 KB
/
validatePayment.js
File metadata and controls
56 lines (43 loc) · 1.85 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
var validator = require('validator');
//custom extensions
validator.extend('isAuthCode', function(str){
return /[A-Za-z0-9]{6}/.test(str); });
validator.extend('isLast4', function(str){
return /(\d{4})/.test(str); });
function ValPaymentObj() {
this.validator = validator;
this.payload = {};
this.message = {};
this.isValid = true;
}
ValPaymentObj.prototype.valAmount = function() {
if (!this.validator.isFloat(this.payload.amount)) {
this.message.amount = 'Invalid amount';
this.isValid = false; } };
ValPaymentObj.prototype.valPaymentType = function() {
if (!this.validator.isIn(this.payload.paymentType.toLowerCase(), ['cash', 'credit', 'ebt', 'fsa', 'gift', 'loyalty', 'debit'])) {
this.message.paymentType = 'Invalid payment type';
this.isValid = false; } };
ValPaymentObj.prototype.valCardType = function() {
if (!this.validator.isIn(this.payload.cardType.toLowerCase(), ['visa', 'mastercard', 'discover', 'amex'])) {
this.message.cardType = 'Invalid card type';
this.isValid = false; } };
ValPaymentObj.prototype.valLast4 = function() {
if (!this.validator.isLast4(this.payload.last4)) {
this.message.last4 = 'Last 4 of card invalid';
this.isValid = false; } };
ValPaymentObj.prototype.valAuthCode = function() {
if (!this.validator.isAuthCode(this.payload.authCode)) {
this.message.authCode = 'Invalid authorization code';
this.isValid = false; } };
ValPaymentObj.prototype.valPayment = function(payment) {
this.payload = payment;
this.valAmount();
this.valPaymentType();
if (this.payload.paymentType.toLowerCase() === 'credit' || this.payload.paymentType.toLowerCase() === 'debit') {
this.valCardType();
this.valLast4();
this.valAuthCode(); }
return this;
};
module.exports = ValPaymentObj;