• This repository has been archived on 20/Jan/2024
  • Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 8 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

More Repositories

1

ObjectiveLuhn

Luhn Credit Card Validation Algorithm
Objective-C
127
star
2

CINBouncyButton

UIButton, but bouncier and with your own icons.
Objective-C
25
star
3

UIImageView-MKAsynchrony

Easy to implement alternative to AFNetworking's UIImageView+AFNetworking.
Objective-C
13
star
4

Easy-JSON-for-iOS-5

This helper class allows you to easily make use of the JSON functions found in the iOS 5 SDK, by adding a JSONValue category to NSString and NSData. This is a substitute for using SBJSON and JSONKit and uses NSJSONSerialization.
Objective-C
7
star
5

SCRProfanityChecker

Checks for profanity in a given file or string.
Objective-C
6
star
6

SCRSidewaysBarGraph

A sideways bar graph for iOS.
Objective-C
5
star
7

NetworkKit

iOS Async Networking Library with Stubbed Requests
Objective-C
5
star
8

URL-Shortener

A easy to use PHP & MySQL URL Shortener.
PHP
4
star
9

TMLanguageExtractor

Extract the syntax files from Sublime Text (2|3)
Python
4
star
10

MKImageDownloader

Asynchronous Image Downloader for iOS
Objective-C
4
star
11

Timber

Timber is a lightweight and flexible logging framework written in Swift
Swift
4
star
12

BlockUIAlertViewActionSheet

UIAlertView & UIActionSheet subclasses using Blocks rather than delegate callbacks
Objective-C
3
star
13

MKReachableOperationQueue

An NSOperationQueue subclass automatically responding to changes in internet connection with thanks to Apple's Reachability.
Objective-C
3
star
14

kotlin-starter

Starter project for Spring Boot with Kotlin
Kotlin
2
star
15

NSStringDrawingSizeMethod

Small category which can easily determine the height of a string. Replaces deprecated sizeWithFont... in iOS 7.
Objective-C
2
star
16

MontyHall

Monty Hall problem in Objective-C
Objective-C
1
star
17

RTFDiskCache

Read/Write Objects to/from disk
Objective-C
1
star
18

Butler

Swift
1
star
19

dotfiles

Dotfiles
Python
1
star
20

kramer.so

My personal website
TypeScript
1
star
21

Xcode4-Templates

Personally edited Xcode 4 Objective-C Class templates
Objective-C
1
star
22

Zeus

Zeus: God of the Cloud. A basic server setup script.
Shell
1
star
23

workflow-watcher

Simple Golang utility for listening on changes to Argo workflows.
Go
1
star