GithubHelp home page GithubHelp logo

jonasman / swiftluhn Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maxkramer/swiftluhn

0.0 2.0 0.0 52 KB

Debit/Credit card validation port of the Luhn Algorithm in Swift

License: MIT License

Ruby 4.72% Objective-C 2.95% Shell 54.73% Swift 37.60%

swiftluhn's Introduction

SwiftLuhn

Swift Circle CI Version 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.1 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. To install it, simply add the following line to your Podfile:

pod "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
}

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.