Coder Social home page Coder Social logo

cardparser's Introduction

CardParser

Credit Card Type Parsing for Swift

CardParser is a lightweight utility for identifying major credit card types from in-progress input. This is useful for building responsive credit entry forms and validating input.

Usage

For now, just download and add CardParser.swift to your project.

The Basics

To determine the state of some in-progress text:

switch CardState(fromPrefix: cardString) {

case .identified(let card):
  //do something with card

case .indeterminate(let possibleCards):
  //do something with possibleCards

case .invalid:
  //show some validation error

}

Or if you are just concerned with the validity of some completed input:

guard CardState(fromNumber: cardNumber) != .invalid else {
    //show some validation error
}

Under The Hood

CardParser.swift contains the enums:

  • CardType
  • CardState

Card Type

CardType encapsulates credit card issuers and their validation/ formatting requirements

enum CardType {
    case amex
    case diners
    case discover
    case jcb
    case masterCard
    case visa
}

You can interact with an extracted card type to update your form:

let visa: CardType = .visa
visa.isPrefixValid("4") //true
visa.isValid("4")       //false
visa.cvvLength          //3
visa.segmentGroupings   //[4, 4, 4, 4]
visa.maxLength          //19

Card State

CardState can be leveraged to identify the CardType (or possible CardTypes) of some input.

enum CardState {
    case identified(CardType)
    case indeterminate([CardType])
    case invalid
}

Use CardState to extract state and type information

CardState(fromPrefix: "4")  //.identified(.visa)
CardState(fromPrefix: "3")  //.indeterminate([.amex, .diners, .jcb])
CardState(fromPrefix: "0")  //.invalid

For example, you may write a method to return a card image for your form:

func cardImage(forState cardState:CardState) -> UIImage? {
    switch cardState {
    case .identified(let cardType):
        switch cardType{
        case .visa:         return #imageLiteral(resourceName: "credit_cards_visa")
        case .masterCard:   return #imageLiteral(resourceName: "credit_cards_mastercard")
        case .amex:         return #imageLiteral(resourceName: "credit_cards_americanexpress")
        case .diners:       return #imageLiteral(resourceName: "credit_cards_diners")
        case .discover:     return #imageLiteral(resourceName: "credit_cards_discover")
        case .jcb:          return #imageLiteral(resourceName: "credit_cards_jcb")
        }
    case .indeterminate: return #imageLiteral(resourceName: "credit_cards_generic")
    case .invalid:      return #imageLiteral(resourceName: "credit_cards_invalid")
    }
}

Author

Jay Clark: [email protected]

License

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

cardparser's People

Contributors

zeveisenberg avatar

Watchers

 avatar  avatar

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.