1 Introduction
2 Validation
3 Formatting
4 History
5 Legal
Version: 2:0

ccnum: Credit Card Number Utilities in Racket

Neil Van Dyke

 (require (planet neil/ccnum:2:0))

1 Introduction

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!”
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:

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?.
(check-credit-card-number str)
(or/c #f
      (list/c boolean?
              (or/c 'american-express
  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")
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")
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.
(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")
> (credit-card-number-check-digit-ok? "4408 0412 3456 7890")
> (credit-card-number-check-digit-ok? "trump")
(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")
> (credit-card-number-seems-ok?       "5551 2121 9")

3 Formatting

Two procedures are provided for formatting credit card numbers.
(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 "

1234 5678 9012 3456

(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")
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 – 2012 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.