|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +// CreditCardValidator 信用卡号验证器 |
| 11 | +type CreditCardValidator struct { |
| 12 | + FieldName string // 字段名称 |
| 13 | +} |
| 14 | + |
| 15 | +// NewCreditCardValidator 创建一个新的信用卡号验证器 |
| 16 | +func NewCreditCardValidator(fieldName string) *CreditCardValidator { |
| 17 | + return &CreditCardValidator{ |
| 18 | + FieldName: fieldName, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// Validate 验证信用卡号格式 |
| 23 | +func (v *CreditCardValidator) Validate() func(string) error { |
| 24 | + return func(s string) error { |
| 25 | + // 移除所有空格和连字符 |
| 26 | + s = strings.ReplaceAll(s, " ", "") |
| 27 | + s = strings.ReplaceAll(s, "-", "") |
| 28 | + |
| 29 | + // 验证长度(13-19位) |
| 30 | + if len(s) < 13 || len(s) > 19 { |
| 31 | + return fmt.Errorf("信用卡号长度必须在13-19位之间") |
| 32 | + } |
| 33 | + |
| 34 | + // 验证是否都是数字 |
| 35 | + for _, c := range s { |
| 36 | + if c < '0' || c > '9' { |
| 37 | + return fmt.Errorf("信用卡号只能包含数字") |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + return nil |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +// ValidateCardType 验证信用卡号卡组织 |
| 46 | +func (v *CreditCardValidator) ValidateCardType() func(string) error { |
| 47 | + return func(s string) error { |
| 48 | + // 移除所有空格和连字符 |
| 49 | + s = strings.ReplaceAll(s, " ", "") |
| 50 | + s = strings.ReplaceAll(s, "-", "") |
| 51 | + |
| 52 | + // 定义卡组织规则 |
| 53 | + cardRules := map[string]struct { |
| 54 | + pattern string |
| 55 | + name string |
| 56 | + }{ |
| 57 | + "visa": { |
| 58 | + pattern: "^4[0-9]{12}(?:[0-9]{3})?$", |
| 59 | + name: "Visa", |
| 60 | + }, |
| 61 | + "mastercard": { |
| 62 | + pattern: "^5[1-5][0-9]{14}$", |
| 63 | + name: "MasterCard", |
| 64 | + }, |
| 65 | + "amex": { |
| 66 | + pattern: "^3[47][0-9]{13}$", |
| 67 | + name: "American Express", |
| 68 | + }, |
| 69 | + "discover": { |
| 70 | + pattern: "^6(?:011|5[0-9]{2})[0-9]{12}$", |
| 71 | + name: "Discover", |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + // 验证卡组织 |
| 76 | + for _, rule := range cardRules { |
| 77 | + matched, err := regexp.MatchString(rule.pattern, s) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("验证失败: %v", err) |
| 80 | + } |
| 81 | + if matched { |
| 82 | + return nil |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return fmt.Errorf("无效的卡组织") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// ValidateLuhn 验证信用卡号Luhn算法 |
| 91 | +func (v *CreditCardValidator) ValidateLuhn() func(string) error { |
| 92 | + return func(s string) error { |
| 93 | + // 移除所有空格和连字符 |
| 94 | + s = strings.ReplaceAll(s, " ", "") |
| 95 | + s = strings.ReplaceAll(s, "-", "") |
| 96 | + |
| 97 | + // Luhn算法验证 |
| 98 | + sum := 0 |
| 99 | + alternate := false |
| 100 | + |
| 101 | + // 从右向左遍历 |
| 102 | + for i := len(s) - 1; i >= 0; i-- { |
| 103 | + digit, _ := strconv.Atoi(string(s[i])) |
| 104 | + |
| 105 | + if alternate { |
| 106 | + digit *= 2 |
| 107 | + if digit > 9 { |
| 108 | + digit -= 9 |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + sum += digit |
| 113 | + alternate = !alternate |
| 114 | + } |
| 115 | + |
| 116 | + if sum%10 != 0 { |
| 117 | + return fmt.Errorf("Luhn算法验证失败") |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// ValidateExpiryDate 验证信用卡号有效期 |
| 125 | +func (v *CreditCardValidator) ValidateExpiryDate() func(string) error { |
| 126 | + return func(s string) error { |
| 127 | + // 验证格式(MM/YY) |
| 128 | + pattern := `^(0[1-9]|1[0-2])/([0-9]{2})$` |
| 129 | + matched, err := regexp.MatchString(pattern, s) |
| 130 | + if err != nil { |
| 131 | + return fmt.Errorf("验证失败: %v", err) |
| 132 | + } |
| 133 | + if !matched { |
| 134 | + return fmt.Errorf("无效的有效期格式") |
| 135 | + } |
| 136 | + |
| 137 | + // 验证是否过期 |
| 138 | + parts := strings.Split(s, "/") |
| 139 | + month, _ := strconv.Atoi(parts[0]) |
| 140 | + year, _ := strconv.Atoi("20" + parts[1]) |
| 141 | + |
| 142 | + // 这里只是示例,实际应该使用当前时间进行比较 |
| 143 | + if year < 2024 || (year == 2024 && month < 1) { |
| 144 | + return fmt.Errorf("信用卡已过期") |
| 145 | + } |
| 146 | + |
| 147 | + return nil |
| 148 | + } |
| 149 | +} |
0 commit comments