forked from aliuygur/is
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credit.go
151 lines (115 loc) · 2.83 KB
/
credit.go
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
package is
import (
"strconv"
)
// CreditCard check if the string is a credit card number.
// For all special cases see: http://www.regular-expressions.info/creditcard.html
func CreditCard(s string) bool {
return luhn(stripNonNumeric(s))
}
// VisaCard verifies Visa credit card number.
// All Visa card numbers start with a 4.
// New cards have 16 digits. Old cards have 13.
func VisaCard(s string) bool {
if len(s) == 0 {
return false
}
s = stripNonNumeric(s)
if len(s) != 13 && len(s) != 16 {
return false
}
if s[0] != '4' {
return false
}
return luhn(s)
}
// MasterCard verifies Mastercard credit card number.
// MasterCard numbers either start with the numbers 51 through 55
// or with the numbers 2221 through 2720. All have 16 digits.
// There are Diners Club cards that begin with 5 and have 16 digits.
// These are a joint venture between Diners Club and MasterCard,
// and should be processed like a MasterCard.
func MasterCard(s string) bool {
if len(s) == 0 {
return false
}
s = stripNonNumeric(s)
if len(s) != 16 {
return false
}
ft, _ := strconv.Atoi(s[0:2])
ff, _ := strconv.Atoi(s[0:4])
if s[0:1] != "5" && (55 < ft || ft < 51) && (2720 < ff || ft < 2221) {
return false
}
return luhn(s)
}
// AmericanExpressCard verifies AmericanExpress credit card number.
// American Express card numbers start with 34 or 37 and have 15 digits.
func AmericanExpressCard(s string) bool {
if len(s) == 0 {
return false
}
s = stripNonNumeric(s)
if len(s) != 15 {
return false
}
if s[0:2] != "34" && s[0:2] != "37" {
return false
}
return luhn(s)
}
// DinersClubCard verifies DinersClub credit card number.
// Diners Club card numbers begin with 300 through 305, 36 or 38.
// All have 14 digits.
func DinersClubCard(s string) bool {
if len(s) == 0 {
return false
}
s = stripNonNumeric(s)
if len(s) != 14 {
return false
}
ft, _ := strconv.Atoi(s[0:3])
if s[0:2] != "36" && s[0:2] != "38" && (305 < ft || ft < 300) {
return false
}
return luhn(s)
}
// DiscoverCard verifies Discover credit card number.
// Discover card numbers begin with 6011 or 65. All have 16 digits.
func DiscoverCard(s string) bool {
if len(s) == 0 {
return false
}
s = stripNonNumeric(s)
if len(s) != 16 {
return false
}
if s[0:2] != "65" && s[0:4] != "6011" {
return false
}
return luhn(s)
}
// JCBCard verifies JCB credit card number.
// JCB cards beginning with 2131 or 1800 have 15 digits.
// JCB cards beginning with 35 have 16 digits.
func JCBCard(s string) bool {
if len(s) == 0 {
return false
}
s = stripNonNumeric(s)
if len(s) != 15 && len(s) != 16 {
return false
}
if s[0:4] != "2131" && s[0:4] != "1800" && s[0:2] != "35" {
return false
}
if (s[0:4] == "2131" || s[0:4] == "1800") && len(s) != 15 {
return false
}
if s[0:2] == "35" && len(s) != 16 {
return false
}
return luhn(s)
}