ccnum:   Credit Card Number Utilities
1 Introduction
2 Validation
check-credit-card-number
credit-card-number-check-digit-ok?
credit-card-number-seems-ok?
3 Formatting
write-formatted-credit-card-number
formatted-credit-card-number
4 History
5 Legal
3:0

ccnum: Credit Card Number Utilities🔗ℹ

Neil Van Dyke

 (require ccnum) package: ccnum

1 Introduction🔗ℹ

The ccnum package defines a few utilities for validating and formatting credit card numbers. Credit card numbers are represented as strings containing digits and arbitrary whitespace. The procedures are based on information gleaned from dozens of written artifacts of credit card number oral tradition. The author invites free copies of authoritative documentation.
Some references that were used:
Achtung! Do not use this library as anything other than a novelty unless you understand the code thoroughly and can invest in verifying its correctness. I will not be held liable if you say, “Hey, ccnum says the credit card number is good! So mail grandma’s jewelry to that nice man in Nigeria; we’re rich, baby!”

2 Validation🔗ℹ

The following procedures provide different ways of validating credit card numbers. Most applications will use credit-card-number-check-digit-ok? or credit-card-number-seems-ok?.

procedure

(check-credit-card-number str)

  
(or/c #f
      exact-nonnegative-integer?
      (list/c boolean?
              exact-positive-integer?
              (or/c 'american-express
                    'australian-bankcard
                    'carte-blanche
                    'diners-club
                    'discover-novus
                    'jcb
                    'mastercard
                    'visa)))
  str : string?
Performs a partial validation of the credit card number in str. If the check digit is incorrect, then #f is yielded:
> (check-credit-card-number "4408041234567890")
  #f
If the check digit is correct, but the issuer cannot be determined, then an integer representing the digit count is yielded:
> (check-credit-card-number "1234567890123452")
  16
If the check digit is correct and issuer can be determined, then a list of three elements is returned. The first element is a boolean value for whether or not the digit count matches what is known about how many digits the issuer uses for this class of cards. The second element is the digit count. The third element is a symbol loosely identifying the issuer. For example:
> (check-credit-card-number "5551 2121 9")
  (#f 9 mastercard)
> (check-credit-card-number "4408041234567893")
  (#t 16 visa)
Note: The above particular interface for return values is for historical reasons, and is not particularly good form.

procedure

(credit-card-number-check-digit-ok? str)  boolean?

  str : string?
Predicate for whether or not the check digit of credit card number str is correct.
> (credit-card-number-check-digit-ok? "4408 0412 3456 7893")
  #t
> (credit-card-number-check-digit-ok? "4408 0412 3456 7890")
  #f
> (credit-card-number-check-digit-ok? "trump")
  #f

procedure

(credit-card-number-seems-ok? str)  boolean?

  str : string?
Predicate for whether or not the credit card number str “seems” to be valid. For a credit card number to “seem” valid, the check digit must be correct, the issuer must be identified, and the digit count must match what is known about issuer digit counts. In the following example the check digit is correct, and the issuer (MasterCard) has been identified, but the digit count is too low for a MasterCard number:
> (credit-card-number-check-digit-ok? "5551 2121 9")
  #t
> (credit-card-number-seems-ok?       "5551 2121 9")
  #f

3 Formatting🔗ℹ

Two procedures are provided for formatting credit card numbers.

procedure

(write-formatted-credit-card-number str    
  port)  void?
  str : string
  port : output-port?
Writes credit card number str to output port port, using a format similar to that used on many credit cards. In the current version of this package, the format is always groups of four digits separated by single space characters, although a future version might mimic the format used by the issuer. For example:
(write-formatted-credit-card-number " 1 23 456  7890 12345 6 "
                                    (current-output-port))
Outputs:

1234 5678 9012 3456

procedure

(formatted-credit-card-number str)  string?

  str : string?
Yields a formatted string representation of credit card number str like that written by write-formatted-credit-card-number.
> (formatted-credit-card-number "1234567890123456")
  "1234 5678 9012 3456"
> (formatted-credit-card-number "  12 34 56  7890 1234 56")
  "1234 5678 9012 3456"
> (formatted-credit-card-number "123 abc")
  #f
Note that (write-formatted-credit-card-number n p) might be more efficient than (display (formatted-credit-card-number n) p).

4 History🔗ℹ

5 Legal🔗ℹ

Copyright 2004, 2005, 2009, 2012, 2016 Neil Van Dyke. This program is Free Software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See http://www.gnu.org/licenses/ for details. For other licenses and consulting, please contact the author.