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

Adding Brazilian cards and improve example project #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- SwiftLuhn (0.1.0)
- SwiftLuhn (0.2.1)

DEPENDENCIES:
- SwiftLuhn (from `../`)
Expand All @@ -9,7 +9,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
SwiftLuhn: d3d68f95622f904b53d72839b701247ad15c15b5
SwiftLuhn: c6aab770b8097186f906675fd970f6772220a417

PODFILE CHECKSUM: 9e1dbb60127e514c2f28a3f720501ce3568de03f

Expand Down
19 changes: 9 additions & 10 deletions Example/Pods/Local Podspecs/SwiftLuhn.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2,366 changes: 1,645 additions & 721 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Example/Pods/Target Support Files/SwiftLuhn/Info.plist

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
Expand Down Expand Up @@ -35,4 +45,4 @@
"version" : 1,
"author" : "xcode"
}
}
}
35 changes: 24 additions & 11 deletions Example/SwiftLuhn/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10116" systemVersion="15E65" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="12118" systemVersion="16E195" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="12086"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand All @@ -14,38 +18,47 @@
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Sa0-kX-WTY">
<rect key="frame" x="264" y="219" width="72.5" height="36"/>
<rect key="frame" x="151" y="210.5" width="72.5" height="36"/>
<fontDescription key="fontDescription" type="system" pointSize="30"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="378282246310005" borderStyle="roundedRect" placeholder="Card number" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="XI9-YK-h96">
<rect key="frame" x="153" y="285" width="294" height="30"/>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" placeholder="Card number" textAlignment="center" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="XI9-YK-h96">
<rect key="frame" x="87.5" y="318.5" width="200" height="30"/>
<constraints>
<constraint firstAttribute="height" constant="30" id="CLu-2d-9bX"/>
<constraint firstAttribute="width" constant="294" id="qqG-RV-dAH"/>
<constraint firstAttribute="width" constant="200" id="fRf-wq-jv8"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<textInputTraits key="textInputTraits"/>
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
<connections>
<outlet property="delegate" destination="vXZ-lx-hvc" id="kFE-16-fBb"/>
</connections>
</textField>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="bsf-xE-g9R" userLabel="Type Label">
<rect key="frame" x="163" y="278.5" width="49" height="24"/>
<fontDescription key="fontDescription" type="system" pointSize="20"/>
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="bsf-xE-g9R" firstAttribute="top" secondItem="Sa0-kX-WTY" secondAttribute="bottom" constant="32" id="6UB-BH-jxF"/>
<constraint firstItem="bsf-xE-g9R" firstAttribute="centerX" secondItem="XI9-YK-h96" secondAttribute="centerX" id="AO1-KM-wM4"/>
<constraint firstItem="Sa0-kX-WTY" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="DZA-x4-1mK"/>
<constraint firstItem="XI9-YK-h96" firstAttribute="top" secondItem="Sa0-kX-WTY" secondAttribute="bottom" constant="30" id="KDf-Sq-Zqz"/>
<constraint firstItem="XI9-YK-h96" firstAttribute="top" secondItem="bsf-xE-g9R" secondAttribute="bottom" constant="16" id="Jc1-i5-7nI"/>
<constraint firstItem="XI9-YK-h96" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" id="R1g-R3-feZ"/>
<constraint firstItem="XI9-YK-h96" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="s9a-Ys-yy8"/>
</constraints>
</view>
<connections>
<outlet property="cardTextField" destination="XI9-YK-h96" id="POh-6j-sLp"/>
<outlet property="typeLabel" destination="bsf-xE-g9R" id="OYz-33-16N"/>
<outlet property="validityLabel" destination="Sa0-kX-WTY" id="UAd-Yz-jhw"/>
</connections>
</viewController>
Expand Down
55 changes: 47 additions & 8 deletions Example/SwiftLuhn/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,47 @@ import UIKit
import SwiftLuhn

class ViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var cardTextField: UITextField!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var validityLabel: UILabel!


//src: http://www.paypalobjects.com/en_US/vhelp/paypalmanager_help/credit_card_numbers.htm
let americanExpress = "378282246310005"
let americanExpress2 = "371449635398431"
let americanExpressCorporate = "378734493671000"
let australianBankCard = "5610591081018250"
let dinersClub = "30569309025904"
let dinersClub2 = "38520000023237"
let discover = "6011111111111117"
let discover2 = "6011000990139424"
let jCB = "3530111333300000"
let jCB2 = "3566002020360505"
let masterCard = "5555555555554444"
let masterCard2 = "5105105105105100"
let visa = "4111111111111111"
let visa2 = "4012888888881881"
let visa3 = "4222222222222"

// src: http://www.dicastech.net/numeros-de-cartoes-de-credito-para-testes/
let aura = "5078601870000127985"
let aura2 = "5078601800003247449"
let eloo = "636297000045701"

// src: http://www.geradorcartaodecredito.com.br/hipercard
let hipercard = "6062825756950756"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I've added here some testing data in order to helpe testers


override func viewDidLoad() {
super.viewDidLoad()

cardTextField.text = "378282246310005"
updateLabel(cardTextField.text!.isValidCardNumber())
// Do any additional setup after loading the view, typically from a nib.

let initialValue = visa2

cardTextField.text = initialValue
updateValidityLabel(initialValue.isValidCardNumber())
updateTypeLabel(initialValue)
}

func updateLabel(_ isValid: Bool) {
private func updateValidityLabel(_ isValid: Bool) {
if isValid {
validityLabel.text = "VALID"
validityLabel.textColor = UIColor.green
Expand All @@ -32,6 +60,14 @@ class ViewController: UIViewController, UITextFieldDelegate {
validityLabel.textColor = UIColor.red
}
}

private func updateTypeLabel(_ string: String) {
if let type = string.cardType() {
typeLabel.text = type.stringValue()
} else {
typeLabel.text = "Unknown"
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Now, a type label is included on the demo

}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

Expand All @@ -40,7 +76,10 @@ class ViewController: UIViewController, UITextFieldDelegate {
let updatedString = text.replacingCharacters(in: range, with: string)

let isValid = updatedString.isValidCardNumber()
updateLabel(isValid)
updateValidityLabel(isValid)

updateTypeLabel(updatedString)

return true
}

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ You can also get the type of the card being used which will be one of:
|Discover|
|Diner's Club|
|JCB|
|Elo|
|Hipercard|
|Aura|

```swift
do {
Expand All @@ -92,6 +95,8 @@ To run the unit test suite, please open the example project and hit CMD + U.

Max Kramer, [email protected], [@maxkramer](http://twitter.com/maxkramer)

Elo, Hipercard and Aura by [@bocato](http://github.com/bocato/)

## License

SwiftLuhn is available under the MIT license. See the LICENSE file for more info.
Expand Down
4 changes: 2 additions & 2 deletions SwiftLuhn/Classes/String+SwiftLuhn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
public extension String {
public func isValidCardNumber() -> Bool {
do {
try SwiftLuhn.performLuhnAlgorithm(with: self)
try SwiftLuhn.performLuhnAlgorithm(cardNumber: self)
return true
}
catch {
Expand All @@ -20,7 +20,7 @@ public extension String {
}

public func cardType() -> SwiftLuhn.CardType? {
let cardType = try? SwiftLuhn.cardType(for: self)
let cardType = try? SwiftLuhn.cardType(cardNumber: self)
return cardType
}

Expand Down
Loading