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

Related tags

Payments SwiftLuhn
Overview

SwiftLuhn

Swift CircleCI Version Carthage compatible License Platform

Warning! This repository is no longer maintained.

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 4. If your project doesn't support Swift 4 yet, please use the 0.2.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 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.2.1' for Swift 3.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.

Comments
  • Release version 0.2 on Cocoapods

    Release version 0.2 on Cocoapods

    Hi,

    On https://cocoapods.org the version available is 0.1 that is not compatible with Swift 3.

    Will you release the new version on cocoapods?

    At this moment I'm using the library pointing directly to your tag 0.2 on github.

    Thanks!

    opened by leobarrospereira 2
  • Invalid error

    Invalid error

    What did you do?

    When I check my mastercard debit card it gives me invalid error

    Please replace this with what you did.

    What did you expect to happen?

    Please replace this with what you expected to happen.

    What actually happened instead?

    Please replace this with what happened instead.

    Environment

    List the software versions you're using:

    • Xcode Version: ?.? (????) (Open Xcode; In menubar: Xcode > About Xcode)
    • Swift Version: ?.? (Open Xcode Preferences; Components > Toolchains. If none, use Xcode Default.)

    Please also mention which package manager you used and its version. Delete the other package managers in this list:

    • Cocoapods: ?.?.? (Use pod --version in Terminal)
    • Carthage: ?.? (Use carthage version in Terminal)
    • Swift Package Manager ?.?.? (swiftpm-???) (Use swift build --version in Terminal)
    invalid 
    opened by yildirimatcioglu 1
  • Can't detect card type

    Can't detect card type

    Hi I have problems when using this source if let cardType = cardNumber.suggestedCardType() { print("Fond Card Type") } But when I input card number is: 5555 5555 5555 4444 This code can't get card type

    Please help me.

    Thanks,

    opened by hoangtam101 1
  • Maestro card

    Maestro card

    1. Added validation for maestro card and rupay card
    2. Added a suggested card type (which ignores the length of card number) should be used while typing in the number.
    opened by gauravkeshre 1
  • 0.1.1 Tag not working

    0.1.1 Tag not working

    Hello there,

    For some reason I have this error on pod install

    [!] Unable to satisfy the following requirements:

    • SwiftLuhn (~> 0.1.1) required by Podfile

    None of your spec sources contain a spec satisfying the dependency: SwiftLuhn (~> 0.1.1).

    and in the pod file I have this:

    pod 'SwiftLuhn', '~> 0.1.1'
    

    What is happening? Broken tag?

    opened by RodolfoAntonici 1
  • Swift 3

    Swift 3

    Introducing Swift 3 support.

    Continuous integration is currently failing because CircleCI doesn't yet support Xcode 8 or Swift 3. I've opened a PR so that it can be manually tested by the community in the mean time. Unit tests are succeeding locally, so I don't have any further issues in terms of merging into master, however I'd appreciate some confirmations that this is indeed working as expected.

    Cheers, Max.

    enhancement help wanted 
    opened by maxkramer 0
  • Support Unionpay

    Support Unionpay

    The PR should summarize what was changed and why. Here are some questions to help you if you're not sure:

    • What behavior was changed? Added Unionpay card type.

    Checklist - While not every PR needs it, new features should consider this list:

    • [x] Does this have tests?
    • [ ] Does this have documentation?
    • [ ] Does this break the public API (Requires major version bump)?
    • [x] Is this a new feature (Requires minor version bump)?
    opened by limsangjin12 1
  • Added Brazilian Cards(Elo, Hipercard and Aura)

    Added Brazilian Cards(Elo, Hipercard and Aura)

    The PR should summarize what was changed and why. Here are some questions to help you if you're not sure:

    • What behavior was changed? Added Brazilian Cards, Converted to Swift 4.2
    • What code was refactored / updated to support this change? SwiftLuhn.swift added Elo, Hipercard and Aura
    • What issues are related to this PR? Or why was this change introduced?

    Checklist - While not every PR needs it, new features should consider this list:

    • [x] Does this have tests?
    • [x] Does this have documentation?
    • [ ] Does this break the public API (Requires major version bump)?
    • [x] Is this a new feature (Requires minor version bump)?
    opened by andre991 1
  • Card type detection fixed

    Card type detection fixed

    Hi @maxkramer

    The PR fixes the issue #18

    The caseIterable protocol works for enumerations without associated values, so I have added a simple solution to get the count of all enum cases:

    Please review.

    opened by nagarro-mohitpadalia 6
  • Maestro and Rupay card type detection support

    Maestro and Rupay card type detection support

    Can we have the card type detection for maestro and rupay cards.

    looks like on line 119 (SwiftLuhn.swift)

    for i in CardType.amex.rawValue...CardType.jcb.rawValue {
    
    }
    

    This iteration should go upto CardType.mir to support the card validation for maestro and rupay.

    bug help wanted 
    opened by nagarro-mohitpadalia 1
  •     public class func cardType

    public class func cardType

    do { let cardType = try SwiftLuhn.cardType(for: cc_number_to_check!) switch cardType{ case .visa:

    using this code can't use cardType.

    'cardType' is inaccessible due to 'internal' protection level

    i added public:

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

    and it works

    opened by alexookah 1
Releases(1.0.0)
Owner
Max Kramer
Engineering Leader // ex-BCG DV
Max Kramer
Luhn Credit Card Validation Algorithm

Luhn Algorithm This is a port of the Luhn Algorithm, generally used for validating Credit Card details, to Objective-C (iOS). Swift port can be found

Max Kramer 126 Sep 7, 2022
card.io provides fast, easy credit card scanning in mobile apps

card.io SDK for iOS card.io provides fast, easy credit card scanning in mobile apps. NEW!!! card.io is now an open-source project! As of December 2014

card.io 2.3k Nov 20, 2022
Easily integrate Credit Card payments module in iOS App. Swift 4.0

MFCard Buy me a coffee MFCard is an awesome looking Credit Card input & validation control. Written in Swift 3. YOoo, Now MFCard is on Swift 5. Swift

MobileFirst 361 Nov 20, 2022
A credit card scanner for iOS written in Swift

DGCardScanner A credit card scanner Requirements iOS 13.0+ Swift 5.5+ Xcode 10.0+ Installation SPM File > Add Packages > https://github.com/donggyushi

donggyu 9 Jun 24, 2022
Easy to use iOS library with components for input of Credit Card data.

AnimatedCardInput This library allows you to drop into your project two easily customisable, animated components that will make input of Credit Card i

Netguru 39 Oct 16, 2022
A credit card reader and parser for iOS Using Native Vision/VisionKit

card-reader-ios A credit card reader and parser for iOS Using Native Vision/VisionKit May-14-2021.00-43-17.mp4 Instructions Hold camera up to a card a

Khalid Asad 103 Nov 7, 2022
CreditCardForm is iOS framework that allows developers to create the UI which replicates an actual Credit Card.

CreditCardForm CreditCardForm is iOS framework that allows developers to create the UI which replicates an actual Credit Card. Fixed typo use CreditCa

Orazz 1.4k Nov 20, 2022
TextFieldFormatter - Simple Text Formatter (Credit Card Number, Phone Number, Serial Number etc.)

TextFieldFormatter Simple Text Formatter (Credit Card Number, Phone Number, Seri

Anıl ORUÇ 4 Apr 4, 2022
Ios-card-transition - iOS CocoaPod to create beautiful card transitions

CSCardTransition CSCardTransition is a small library allowing you to create wond

Creastel 12 Oct 31, 2022
Card Decks is a small utility application for your iPhone, iPod touch and iPad which brings you simple, configurable, colored, multi-line text cards that are grouped into card decks

Card Decks is a small utility application for your iPhone, iPod touch and iPad which brings you simple, configurable, colored, multi-line text cards that are grouped into card decks.

Arne Harren 40 Nov 24, 2022
Accept credit cards and PayPal in your iOS app

Important: PayPal Mobile SDKs are Deprecated. The APIs powering them will remain operational long enough for merchants to migrate, but the SDKs themse

PayPal 972 Nov 4, 2022
Ruby Gem for Rails - Easy iTunes In-App Purchase Receipt validation, including auto-renewable subscriptions

Monza is a ruby gem that makes In-App Purchase receipt and Auto-Renewable subscription validation easy. You should always validate receipts on the ser

Gabriel 156 Jun 12, 2022
Bank Card Generator with Swift using SnapKit DSL 💳

iCard BankCard & CreditCard generator with Swift 3 using SnapKit DSL iCard is a simple tool for generate Credit & Bank Card , it represent cards as UI

Emrah Korkmaz 333 Nov 9, 2022
Welcome Busines Card Built With Swift

BusinessCard Welcome Busines Card Main screen Contacts screen More info screen

Konstantin Ryabtsev 0 Dec 28, 2021
A card viewer demo for Idolmaster Millionlive Theater Days written in Swift UI

Misaki Gallery A Millionlive's cards gallery to demostrate Swift UI. All api and

Spike 0 Dec 20, 2021
Wizards-pal - Life counter for card games,built with swift

Life counter for card games Made this app in the weekend to help ma boys with ht

Vladimir Ignatov 2 Oct 28, 2022
A Payment Card UI & Validator for iOS

Description Caishen provides an easy-to-use text field to ask users for payment card information and to validate the input. It serves a similar purpos

Prolific Interactive 760 Aug 4, 2022
A study card application built with SwiftUI and Combine

StudyCards Description A study card application. I built this application to get

Matt Manion 2 Sep 26, 2022
Code1System Card Moudle

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

null 1 Aug 4, 2022