-
Notifications
You must be signed in to change notification settings - Fork 37
/
creditcard.go
260 lines (219 loc) · 6.18 KB
/
creditcard.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package creditcard
import (
"errors"
"strconv"
)
// Card holds generic information about the credit card
type Card struct {
Number, Cvv, Month, Year string
Company Company
}
// Company holds a short and long names of who has issued the credit card
type Company struct {
Short, Long string
}
type digits [6]int
// At returns the digits from the start to the given length
func (d *digits) At(i int) int {
return d[i-1]
}
// LastFour returns the last four digits of the credit card's number
func (c *Card) LastFour() (string, error) {
if len(c.Number) < 4 {
return "", errors.New("Credit card number is not long enough")
}
return c.Number[len(c.Number)-4 : len(c.Number)], nil
}
// LastFourDigits as an alias for LastFour
func (c *Card) LastFourDigits() (string, error) {
return c.LastFour()
}
// Wipe returns the credit card with false/nullified/generic information
func (c *Card) Wipe() {
c.Cvv, c.Number, c.Month, c.Year = "0000", "0000000000000000", "01", "1970"
}
// Validate returns nil or an error describing why the credit card didn't validate
// this method checks for expiration date, CCV/CVV and the credit card's numbers.
// For allowing test cards to go through, simply pass true (bool) as the first argument
func (c *Card) Validate(allowTestNumbers ...bool) error {
err := c.ValidateExpiration()
if err != nil {
return err
}
err = c.ValidateCVV()
if err != nil {
return err
}
switch c.Number {
// test cards: https://stripe.com/docs/testing
case "4242424242424242",
"4012888888881881",
"4000056655665556",
"5555555555554444",
"5200828282828210",
"5105105105105100",
"378282246310005",
"371449635398431",
"6011111111111117",
"6011000990139424",
"30569309025904",
"38520000023237",
"3530111333300000",
"3566002020360505",
"4111111111111111",
"4916909992637469",
"4000111111111115",
"2223000048400011",
"6035227716427021":
if len(allowTestNumbers) > 0 && allowTestNumbers[0] {
return nil
}
return errors.New("Test numbers are not allowed")
}
valid := c.ValidateNumber()
if !valid {
return errors.New("Invalid credit card number")
}
return nil
}
// validates the credit card's expiration date
func (c *Card) ValidateExpiration() error {
var year, month int
var err error
timeNow := timeNowCaller()
if len(c.Year) < 3 {
year, err = strconv.Atoi(strconv.Itoa(timeNow.UTC().Year())[:2] + c.Year)
if err != nil {
return errors.New("Invalid year")
}
} else {
year, err = strconv.Atoi(c.Year)
if err != nil {
return errors.New("Invalid year")
}
}
month, err = strconv.Atoi(c.Month)
if err != nil {
return errors.New("Invalid month")
}
if month < 1 || 12 < month {
return errors.New("Invalid month")
}
if year < timeNowCaller().UTC().Year() {
return errors.New("Credit card has expired")
}
if year == timeNowCaller().UTC().Year() && month < int(timeNowCaller().UTC().Month()) {
return errors.New("Credit card has expired")
}
return nil
}
// validates the length of the card's CVV value
func (c *Card) ValidateCVV() error {
if len(c.Cvv) < 3 || len(c.Cvv) > 4 {
return errors.New("Invalid CVV")
}
return nil
}
// Method returns an error from MethodValidate() or returns the
// credit card with it's company / issuer attached to it
func (c *Card) Method() error {
company, err := c.MethodValidate()
if err != nil {
return err
}
c.Company = company
return nil
}
// MethodValidate adds/checks/verifies the credit card's company / issuer
func (c *Card) MethodValidate() (Company, error) {
var err error
ccLen := len(c.Number)
ccDigits := digits{}
for i := 0; i < 6; i++ {
if i < ccLen {
ccDigits[i], err = strconv.Atoi(c.Number[:i+1])
if err != nil {
return Company{"", ""}, errors.New("Unknown credit card method")
}
}
}
switch {
case isAmex(ccDigits):
return Company{"amex", "American Express"}, nil
case isBankCard(ccDigits):
return Company{"bankcard", "Bankcard"}, nil
case isCabal(ccDigits):
return Company{"cabal", "Cabal"}, nil
case isUnionPay(ccDigits):
return Company{"china unionpay", "China UnionPay"}, nil
case isDinersClubCarteBlanche(ccDigits, ccLen):
return Company{"diners club carte blanche", "Diners Club Carte Blanche"}, nil
case isDinersClubEnroute(ccDigits):
return Company{"diners club enroute", "Diners Club enRoute"}, nil
case isDinersClubInternational(ccDigits, ccLen):
return Company{"diners club international", "Diners Club International"}, nil
case isDiscover(ccDigits):
return Company{"discover", "Discover"}, nil
// Elo must be checked before interpayment
case isElo(ccDigits):
return Company{"elo", "Elo"}, nil
case isHipercard(ccDigits):
return Company{"hipercard", "Hipercard"}, nil
case isInterpayment(ccDigits, ccLen):
return Company{"interpayment", "InterPayment"}, nil
case isInstapayment(ccDigits, ccLen):
return Company{"instapayment", "InstaPayment"}, nil
case isJCB(ccDigits):
return Company{"jcb", "JCB"}, nil
case isNaranja(ccDigits):
return Company{"naranja", "Naranja"}, nil
case isMaestro(c, ccDigits):
return Company{"maestro", "Maestro"}, nil
case isDankort(ccDigits):
return Company{"dankort", "Dankort"}, nil
case isMasterCard(ccDigits):
return Company{"mastercard", "MasterCard"}, nil
case isVisaElectron(ccDigits):
return Company{"visa electron", "Visa Electron"}, nil
case isVisa(ccDigits):
return Company{"visa", "Visa"}, nil
case isAura(ccDigits):
return Company{"aura", "Aura"}, nil
default:
return Company{"", ""}, errors.New("Unknown credit card method")
}
}
// Luhn algorithm
// http://en.wikipedia.org/wiki/Luhn_algorithm
// ValidateNumber will check the credit card's number against the Luhn algorithm
func (c *Card) ValidateNumber() bool {
var sum int
var alternate bool
numberLen := len(c.Number)
if numberLen < 13 || numberLen > 19 {
return false
}
for i := numberLen - 1; i > -1; i-- {
mod, _ := strconv.Atoi(string(c.Number[i]))
if alternate {
mod *= 2
if mod > 9 {
mod = (mod % 10) + 1
}
}
alternate = !alternate
sum += mod
}
return sum%10 == 0
}
func matchesValue(number int, numbers []int) bool {
for _, v := range numbers {
if v == number {
return true
}
}
return false
}
func isInBetween(n, min, max int) bool {
return n >= min && n <= max
}