Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #5 from gauravkeshre/maestro_card
Browse files Browse the repository at this point in the history
Maestro card
  • Loading branch information
maxkramer authored Apr 18, 2017
2 parents eb1a831 + 888b060 commit d730a23
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions SwiftLuhn/Classes/String+SwiftLuhn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public extension String {
let cardType = try? SwiftLuhn.cardType(for: self)
return cardType
}
public func suggestedCardType() -> SwiftLuhn.CardType? {
let cardType = try? SwiftLuhn.cardType(for: self, suggest: true)
return cardType
}

public func formattedCardNumber() -> String {
let numbersOnlyEquivalent = replacingOccurrences(of: "[^0-9]", with: "", options: .regularExpression, range: nil)
Expand Down
42 changes: 39 additions & 3 deletions SwiftLuhn/Classes/SwiftLuhn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ open class SwiftLuhn {
case discover
case dinersClub
case jcb
case maestro
case rupay
}

public enum CardError: Error {
case unsupported
case invalid
}

fileprivate class func regularExpression(for cardType: CardType) -> String {
switch cardType {
case .amex:
Expand All @@ -37,6 +38,32 @@ open class SwiftLuhn {
return "^5[1-5][0-9]{5,}$";
case .visa:
return "^4[0-9]{6,}$";
case .maestro:
return "^(5018|5020|5038|6304|6759|6761|6763)[0-9]{8,15}$";
case .rupay:
return "^6[0-9]{15}$";

}
}
fileprivate class func suggestionRegularExpression(for cardType: CardType) -> String {
switch cardType {
case .amex:
return "^3[47][0-9]+$";
case .dinersClub:
return "^3(?:0[0-5]|[68][0-9])[0-9]+$";
case .discover:
return "^6(?:011|5[0-9]{2})[0-9]+$";
case .jcb:
return "^(?:2131|1800|35[0-9]{3})[0-9]+$";
case .mastercard:
return "^5[1-5][0-9]+$";
case .visa:
return "^4[0-9]+$";
case .maestro:
return "^(5018|5020|5038|6304|6759|6761|6763)[0-9]+$";
case .rupay:
return "^6[0-9]+$";

}
}

Expand Down Expand Up @@ -81,12 +108,12 @@ open class SwiftLuhn {
}
}

class func cardType(for cardNumber: String) throws -> CardType {
class func cardType(for cardNumber: String, suggest: Bool = false) throws -> CardType {
var foundCardType: CardType?

for i in CardType.amex.rawValue...CardType.jcb.rawValue {
let cardType = CardType(rawValue: i)!
let regex = regularExpression(for: cardType)
let regex = suggest ? suggestionRegularExpression(for: cardType) : regularExpression(for: cardType)

let predicate = NSPredicate(format: "SELF MATCHES %@", regex)

Expand Down Expand Up @@ -119,6 +146,11 @@ public extension SwiftLuhn.CardType {
return "Diner's Club"
case .jcb:
return "JCB"
case .maestro:
return "Maestro";
case .rupay:
return "Rupay";

}
}

Expand All @@ -136,6 +168,10 @@ public extension SwiftLuhn.CardType {
self.init(rawValue: 4)
case "jcb":
self.init(rawValue: 5)
case "maestro":
self.init(rawValue: 6)
case "rupay":
self.init(rawValue: 7)
default:
return nil
}
Expand Down

0 comments on commit d730a23

Please sign in to comment.