-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
109 lines (90 loc) · 2.61 KB
/
card.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
package card
import (
"fmt"
"strconv"
"strings"
"time"
)
// Card - credit card structure
type Card struct {
number []int
cvv string
month int
year int
paymentSystem PaymentSystem
cardHolder string
}
//Number - return a number of credit card
func (c *Card) Number() string {
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(c.number)), ""), "[]")
}
//MaskedNumber - return a masked credit card number like 427615xxxxxx1234
//@headlen - length of first series
//@taillen - length of last series
func (c *Card) MaskedNumber(headlen int, taillen int) string {
hl := headlen
tl := taillen
if (hl < 0) || (hl > len(c.number)) {
hl = 6
}
if (tl < 0) || (tl > len(c.number)) {
tl = 4
}
head := c.number[:hl]
tail := c.number[len(c.number)-tl:]
maskednumber := strings.Trim(strings.Join(strings.Fields(fmt.Sprint(head)), ""), "[]")
masklen := len(c.number) - len(head) - len(tail)
maskednumber = maskednumber + strings.Repeat("*", masklen)
maskednumber = maskednumber + strings.Trim(strings.Join(strings.Fields(fmt.Sprint(tail)), ""), "[]")
return maskednumber
}
//Cvv - return a cvv of credit card
func (c *Card) Cvv() string {
return c.cvv
}
//Month - return expiration month of credit card
func (c *Card) Month() int {
return c.month
}
//Year - return expiration year of credit card
func (c *Card) Year() int {
return c.year
}
//CardHolder - return cardholder of credit card
func (c *Card) CardHolder() string {
return c.cardHolder
}
//Expired - return true if card is expired and false if not
func (c *Card) Expired() bool {
now := time.Now()
expirationDate := time.Date(c.year, time.Month(c.month), 1, 0, 0, 0, 0, now.Location())
expirationDate.AddDate(0, 1, 0)
return expirationDate.Before(now)
}
//PaymentSystem holds a short and long names of who has issued the credit card
type PaymentSystem struct {
Short, Full string
}
//NewCard - create a new credit card object
func NewCard(number string, cvv string, month int, year int, cardHolder string) (*Card, error) {
length := len(number)
if (length < 14) || (length > 19) {
return nil, fmt.Errorf("Invalid number length")
}
if (month > 12) || (month < 0) || (year < 0) {
return nil, fmt.Errorf("Invalid expiration date")
}
digits := []int{}
for i := 0; i < len(number); i++ {
d, err := strconv.Atoi(string(number[i]))
if err != nil {
return nil, fmt.Errorf("Invalid characters in number")
}
digits = append(digits, d)
}
if !valid(digits) {
return nil, fmt.Errorf("Card number is not valid")
}
c := &Card{number: digits, cvv: cvv, month: month, year: year, cardHolder: cardHolder}
return c, nil
}