Latest 0.2.1
Homepage https://github.com/MaxKramer/SwiftLuhn
License MIT
Platforms ios 8.0, requires ARC
Authors

Swift
Circle CI
Version
Carthage compatible
License
Platform

This is a port of the Luhn Algorithm, generally used for validating debit/credit card details, written in Swift.

SwiftLuhn’s HEAD is written in Swift 3. If your project doesn’t support Swift 3 yet, please use the 0.1.0 tag.

Objective-C port can be found at https://github.com/MaxKramer/ObjectiveLuhn.

Usage

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

SwiftLuhn is available through CocoaPods or Carthage (with or without using our pre-built binary).

To install it, simply add the following line to your Podfile:

pod "SwiftLuhn" # use '0.1.0' for Swift 2.X

Or alternatively the following line to your Cartfile:

github "MaxKramer/SwiftLuhn"

Usage

Validation

Call the class method which will throws an exception SwiftLuhn.CardError if the card is invalid.

let cardNumber = "378282246310005"
do {
    try SwiftLuhn.performLuhnAlgorithm(with: cardNumber)
    // process payment
}
catch {
    // invalid, alert user
}

Alternatively, you can use the String category:

let isValid = cardNumber.isValidCardNumber()

Card Types

You can also get the type of the card being used which will be one of:

Card Type
American Express
Visa
Mastercard
Discover
Diner’s Club
JCB
do {
    let cardType = try SwiftLuhn.cardType(of: cardNumber)
}
catch {
    // card is invalid
}

Unit Tests

To run the unit test suite, please open the example project and hit CMD + U.

Sources

Name Website Reason
Paypal http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm List of valid credit card numbers for the unit tests

Author

Max Kramer, [email protected], @maxkramer

License

SwiftLuhn is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SwiftLuhn",
    "version": "0.2.1",
    "summary": "SwiftLuhn is a port of the Luhn algorithm, used for validating debit/credit card details.",
    "description": "This is a port of the Luhn Algorithm, generally used for validating debit/credit card details, to Swift.nnObjective-C port can be found at [https://github.com/MaxKramer/ObjectiveLuhn](https://github.com/MaxKramer/ObjectiveLuhn).",
    "homepage": "https://github.com/MaxKramer/SwiftLuhn",
    "license": "MIT",
    "authors": {
        "Max Kramer": "[email protected]"
    },
    "source": {
        "git": "https://github.com/maxkramer/SwiftLuhn.git",
        "tag": "0.2.1"
    },
    "social_media_url": "https://twitter.com/maxkramer",
    "platforms": {
        "ios": "8.0"
    },
    "requires_arc": true,
    "source_files": "SwiftLuhn/Classes/**/*",
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This