-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCardParser.swift
208 lines (157 loc) · 5.55 KB
/
CardParser.swift
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
//
// CardParser.swift
//
// Created by Jason Clark on 6/28/16.
// Copyright © 2016 Raizlabs. All rights reserved.
//
import Foundation
//MARK: - CardType
enum CardType {
case amex
case diners
case discover
case jcb
case masterCard
case visa
static let allValues: [CardType] = [.visa, .masterCard, .amex, .diners, .discover, .jcb]
private var validationRequirements: ValidationRequirement {
let prefix: [PrefixContainable], length: [Int]
switch self {
/* // IIN prefixes and length requriements retreived from https://en.wikipedia.org/wiki/Bank_card_number on June 28, 2016 */
case .amex: prefix = ["34", "37"]
length = [15]
case .diners: prefix = ["300"..."305", "309", "36", "38"..."39"]
length = [14]
case .discover: prefix = ["6011", "65", "644"..."649", "622126"..."622925"]
length = [16]
case .jcb: prefix = ["3528"..."3589"]
length = [16]
case .masterCard: prefix = ["51"..."55", "2221"..."2720"]
length = [16]
case .visa: prefix = ["4"]
length = [13, 16, 19]
}
return ValidationRequirement(prefixes: prefix, lengths: length)
}
var segmentGroupings: [Int] {
switch self {
case .amex: return [4, 6, 5]
case .diners: return [4, 6, 4]
default: return [4, 4, 4, 4]
}
}
var maxLength: Int {
return validationRequirements.lengths.max() ?? 16
}
var cvvLength: Int {
switch self {
case .amex: return 4
default: return 3
}
}
func isValid(_ accountNumber: String) -> Bool {
return validationRequirements.isValid(accountNumber) && CardType.luhnCheck(accountNumber)
}
func isPrefixValid(_ accountNumber: String) -> Bool {
return validationRequirements.isPrefixValid(accountNumber)
}
}
fileprivate extension CardType {
struct ValidationRequirement {
let prefixes: [PrefixContainable]
let lengths: [Int]
func isValid(_ accountNumber: String) -> Bool {
return isLengthValid(accountNumber) && isPrefixValid(accountNumber)
}
func isPrefixValid(_ accountNumber: String) -> Bool {
guard prefixes.count > 0 else { return true }
return prefixes.contains { $0.hasCommonPrefix(with: accountNumber) }
}
func isLengthValid(_ accountNumber: String) -> Bool {
guard lengths.count > 0 else { return true }
return lengths.contains { accountNumber.length == $0 }
}
}
// from: https://gist.github.com/cwagdev/635ce973e8e86da0403a
static func luhnCheck(_ cardNumber: String) -> Bool {
var sum = 0
let reversedCharacters = cardNumber.characters.reversed().map { String($0) }
for (idx, element) in reversedCharacters.enumerated() {
guard let digit = Int(element) else { return false }
switch ((idx % 2 == 1), digit) {
case (true, 9): sum += 9
case (true, 0...8): sum += (digit * 2) % 9
default: sum += digit
}
}
return sum % 10 == 0
}
}
//MARK: - CardState
enum CardState {
case identified(CardType)
case indeterminate([CardType])
case invalid
}
extension CardState: Equatable {}
func ==(lhs: CardState, rhs: CardState) -> Bool {
switch (lhs, rhs) {
case (.invalid, .invalid): return true
case (let .indeterminate(cards1), let .indeterminate(cards2)): return cards1 == cards2
case (let .identified(card1), let .identified(card2)): return card1 == card2
default: return false
}
}
extension CardState {
init(fromNumber number: String) {
if let card = CardType.allValues.first(where: { $0.isValid(number) }) {
self = .identified(card)
}
else {
self = .invalid
}
}
init(fromPrefix prefix: String) {
let possibleTypes = CardType.allValues.filter { $0.isPrefixValid(prefix) }
if possibleTypes.count >= 2 {
self = .indeterminate(possibleTypes)
}
else if possibleTypes.count == 1, let card = possibleTypes.first {
self = .identified(card)
}
else {
self = .invalid
}
}
}
//MARK: - PrefixContainable
fileprivate protocol PrefixContainable {
func hasCommonPrefix(with text: String) -> Bool
}
extension ClosedRange: PrefixContainable {
func hasCommonPrefix(with text: String) -> Bool {
//cannot include Where clause in protocol conformance, so have to ensure Bound == String :(
guard let lower = lowerBound as? String, let upper = upperBound as? String else { return false }
let trimmedRange: ClosedRange<String> = {
let length = text.length
let trimmedStart = lower.prefix(length)
let trimmedEnd = upper.prefix(length)
return trimmedStart...trimmedEnd
}()
let trimmedText = text.prefix(trimmedRange.lowerBound.characters.count)
return trimmedRange ~= trimmedText
}
}
extension String: PrefixContainable {
func hasCommonPrefix(with text: String) -> Bool {
return hasPrefix(text) || text.hasPrefix(self)
}
}
fileprivate extension String {
func prefix(_ maxLength: Int) -> String {
return String(characters.prefix(maxLength))
}
var length: Int {
return characters.count
}
}