1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! card-validate detects and validates credit card numbers (type of card, number length and
//! Luhn checksum).

#![feature(inclusive_range, inclusive_range_syntax, range_contains)]

#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate luhnmod10;

use std::ops::RangeInclusive;
use regex::Regex;

/// Card type. Maps recognized cards, and validates their pattern and length.
#[derive(PartialEq)]
pub enum Type {
    // Debit
    VisaElectron,
    Maestro,
    Forbrugsforeningen,
    Dankort,

    // Credit
    Visa,
    MasterCard,
    Amex,
    DinersClub,
    Discover,
    UnionPay,
    JCB,

    // Unknown
    Other,
}

/// Validate error. Maps possible validation errors (eg. card number format invalid).
#[derive(Debug)]
pub enum ValidateError {
    InvalidFormat,
}

impl Type {
    pub fn name(&self) -> String {
        match *self {
            Type::VisaElectron => "visaelectron",
            Type::Maestro => "maestro",
            Type::Forbrugsforeningen => "forbrugsforeningen",
            Type::Dankort => "dankort",
            Type::Visa => "visa",
            Type::MasterCard => "mastercard",
            Type::Amex => "amex",
            Type::DinersClub => "dinersclub",
            Type::Discover => "discover",
            Type::UnionPay => "unionpay",
            Type::JCB => "jcb",
            Type::Other => "other",
        }.to_string()
    }

    pub fn pattern<'a>(&self) -> &'a Regex {
        // The card formats have been copied from: https://github.com/faaez/creditcardutils/\
        //   blob/master/src/creditcardutils.coffee
        lazy_static! {
            static ref VISAELECTRON_PATTERN_REGEX: Regex = Regex::new(
                r"^4(026|17500|405|508|844|91[37])").unwrap();
            static ref MAESTRO_PATTERN_REGEX: Regex = Regex::new(
                r"^(5(018|0[23]|[68])|6(39|7))").unwrap();
            static ref FORBRUGSFORENINGEN_PATTERN_REGEX: Regex = Regex::new(r"^600").unwrap();
            static ref DANKORT_PATTERN_REGEX: Regex = Regex::new(r"^5019").unwrap();
            static ref VISA_PATTERN_REGEX: Regex = Regex::new(r"^4").unwrap();
            static ref MASTERCARD_PATTERN_REGEX: Regex = Regex::new(r"^(5[1-5]|2[2-7])").unwrap();
            static ref AMEX_PATTERN_REGEX: Regex = Regex::new(r"^3[47]").unwrap();
            static ref DINERSCLUB_PATTERN_REGEX: Regex = Regex::new(r"^3[0689]").unwrap();
            static ref DISCOVER_PATTERN_REGEX: Regex = Regex::new(r"^6([045]|22)").unwrap();
            static ref UNIONPAY_PATTERN_REGEX: Regex = Regex::new(r"^(62|88)").unwrap();
            static ref JCB_PATTERN_REGEX: Regex = Regex::new(r"^35").unwrap();
            static ref OTHER_PATTERN_REGEX: Regex = Regex::new(r"^[0-9]+$").unwrap();
        }

        match *self {
            Type::VisaElectron => &*VISAELECTRON_PATTERN_REGEX,
            Type::Maestro => &*MAESTRO_PATTERN_REGEX,
            Type::Forbrugsforeningen => &*FORBRUGSFORENINGEN_PATTERN_REGEX,
            Type::Dankort => &*DANKORT_PATTERN_REGEX,
            Type::Visa => &*VISA_PATTERN_REGEX,
            Type::MasterCard => &*MASTERCARD_PATTERN_REGEX,
            Type::Amex => &*AMEX_PATTERN_REGEX,
            Type::DinersClub => &*DINERSCLUB_PATTERN_REGEX,
            Type::Discover => &*DISCOVER_PATTERN_REGEX,
            Type::UnionPay => &*UNIONPAY_PATTERN_REGEX,
            Type::JCB => &*JCB_PATTERN_REGEX,
            Type::Other => &*OTHER_PATTERN_REGEX,
        }
    }

    pub fn length<'a>(&self) -> RangeInclusive<usize> {
        match *self {
            Type::VisaElectron => (16...16),
            Type::Maestro => (12...19),
            Type::Forbrugsforeningen => (16...16),
            Type::Dankort => (16...16),
            Type::Visa => (13...16),
            Type::MasterCard => (16...16),
            Type::Amex => (15...15),
            Type::DinersClub => (14...14),
            Type::Discover => (16...16),
            Type::JCB => (16...16),
            Type::UnionPay => (16...19),
            Type::Other => (12...19),
        }
    }

    pub fn valid(&self) -> bool {
        *self != Type::Other
    }

    fn all() -> Vec<Type> {
        // Debit cards must come first, since they have more specific patterns than their\
        //   credit-card equivalents.
        vec![
            Type::VisaElectron,
            Type::Maestro,
            Type::Forbrugsforeningen,
            Type::Dankort,
            Type::Visa,
            Type::MasterCard,
            Type::Amex,
            Type::DinersClub,
            Type::Discover,
            Type::UnionPay,
            Type::JCB,
        ]
    }
}

/// Card validation utility. Used to validate a provided card number (length and Luhn checksum).
pub struct Validate {
    pub card_type: Type,
    pub valid: bool,
    pub length_valid: bool,
    pub luhn_valid: bool,
}

impl Validate {
    pub fn from(card_number: &str) -> Result<Validate, ValidateError> {
        let card_type = Validate::evaluate_type(&card_number)?;
        let length_valid = Validate::is_length_valid(&card_number, &card_type);
        let luhn_valid = Validate::is_luhn_valid(&card_number);
        let valid = length_valid && luhn_valid && card_type.valid();

        Ok(Validate {
            card_type: card_type,
            valid: valid,
            length_valid: length_valid,
            luhn_valid: luhn_valid,
        })
    }

    fn evaluate_type(card_number: &str) -> Result<Type, ValidateError> {
        // Validate overall card number structure
        if Type::Other.pattern().is_match(&card_number) {
            for card in Type::all() {
                // Validate brand-specific card number structure
                if card.pattern().is_match(&card_number) {
                    return Ok(card);
                }
            }

            Ok(Type::Other)
        } else {
            Err(ValidateError::InvalidFormat)
        }
    }

    fn is_length_valid(card_number: &str, card_type: &Type) -> bool {
        card_type.length().contains(card_number.len())
    }

    fn is_luhn_valid(card_number: &str) -> bool {
        luhnmod10::valid(&card_number)
    }
}