| 1 | 
jonen | 
1.1 | 
<? | 
| 2 | 
  | 
  | 
/* | 
| 3 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 4 | 
  | 
  | 
    | Copyright (c) 2000 J.A.Greant                                        | | 
| 5 | 
  | 
  | 
    | (See end of file for usage notes and licensing information.)         | | 
| 6 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 7 | 
  | 
  | 
*/ | 
| 8 | 
  | 
  | 
 | 
| 9 | 
  | 
  | 
class CreditCard | 
| 10 | 
  | 
  | 
{ | 
| 11 | 
  | 
  | 
    function clean_no ($cc_no) | 
| 12 | 
  | 
  | 
    { | 
| 13 | 
  | 
  | 
        // Remove non-numeric characters from $cc_no | 
| 14 | 
  | 
  | 
        return ereg_replace ('[^0-9]+', '', $cc_no); | 
| 15 | 
  | 
  | 
    } | 
| 16 | 
  | 
  | 
 | 
| 17 | 
  | 
  | 
    function identify ($cc_no) | 
| 18 | 
  | 
  | 
    { | 
| 19 | 
  | 
  | 
         $cc_no = CreditCard::clean_no ($cc_no); | 
| 20 | 
  | 
  | 
 | 
| 21 | 
  | 
  | 
        // Get card type based on prefix and length of card number | 
| 22 | 
  | 
  | 
        if (ereg ('^4(.{12}|.{15})$', $cc_no)) | 
| 23 | 
  | 
  | 
            return 'Visa';  | 
| 24 | 
  | 
  | 
        if (ereg ('^5[1-5].{14}$', $cc_no)) | 
| 25 | 
  | 
  | 
            return 'Mastercard'; | 
| 26 | 
  | 
  | 
        if (ereg ('^3[47].{13}$', $cc_no)) | 
| 27 | 
  | 
  | 
            return 'American Express'; | 
| 28 | 
  | 
  | 
        if (ereg ('^3(0[0-5].{11}|[68].{12})$', $cc_no)) | 
| 29 | 
  | 
  | 
            return 'Diners Club/Carte Blanche'; | 
| 30 | 
  | 
  | 
        if (ereg ('^6011.{12}$', $cc_no)) | 
| 31 | 
  | 
  | 
            return 'Discover Card'; | 
| 32 | 
  | 
  | 
        if (ereg ('^(3.{15}|(2131|1800).{11})$', $cc_no)) | 
| 33 | 
  | 
  | 
            return 'JCB'; | 
| 34 | 
  | 
  | 
        if (ereg ('^2(014|149).{11})$', $cc_no)) | 
| 35 | 
  | 
  | 
            return 'enRoute'; | 
| 36 | 
  | 
  | 
 | 
| 37 | 
  | 
  | 
        return 'unknown'; | 
| 38 | 
  | 
  | 
    } | 
| 39 | 
  | 
  | 
 | 
| 40 | 
  | 
  | 
    function validate ($cc_no) | 
| 41 | 
  | 
  | 
    { | 
| 42 | 
  | 
  | 
        // Reverse and clean the number | 
| 43 | 
  | 
  | 
        $cc_no = strrev (CreditCard::clean_no ($cc_no)); | 
| 44 | 
  | 
  | 
         | 
| 45 | 
  | 
  | 
        // VALIDATION ALGORITHM | 
| 46 | 
  | 
  | 
        // Loop through the number one digit at a time | 
| 47 | 
  | 
  | 
        // Double the value of every second digit (starting from the right) | 
| 48 | 
  | 
  | 
        // Concatenate the new values with the unaffected digits | 
| 49 | 
  | 
  | 
        for ($ndx = 0; $ndx < strlen ($cc_no); ++$ndx) | 
| 50 | 
  | 
  | 
            $digits .= ($ndx % 2) ? $cc_no[$ndx] * 2 : $cc_no[$ndx]; | 
| 51 | 
  | 
  | 
         | 
| 52 | 
  | 
  | 
        // Add all of the single digits together | 
| 53 | 
  | 
  | 
        for ($ndx = 0; $ndx < strlen ($digits); ++$ndx) | 
| 54 | 
  | 
  | 
            $sum += $digits[$ndx]; | 
| 55 | 
  | 
  | 
 | 
| 56 | 
  | 
  | 
        // Valid card numbers will be transformed into a multiple of 10 | 
| 57 | 
  | 
  | 
        return ($sum % 10) ? FALSE : TRUE; | 
| 58 | 
  | 
  | 
    } | 
| 59 | 
  | 
  | 
 | 
| 60 | 
  | 
  | 
    function check ($cc_no) | 
| 61 | 
  | 
  | 
    { | 
| 62 | 
  | 
  | 
        $valid = CreditCard::validate ($cc_no); | 
| 63 | 
  | 
  | 
        $type  = CreditCard::identify ($cc_no); | 
| 64 | 
  | 
  | 
        return array ($valid, $type, 'valid' => $valid, 'type' => $type); | 
| 65 | 
  | 
  | 
    } | 
| 66 | 
  | 
  | 
} | 
| 67 | 
  | 
  | 
 | 
| 68 | 
  | 
  | 
/* | 
| 69 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 70 | 
  | 
  | 
    | FILE NAME: CreditCard.pkg                                           | | 
| 71 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 72 | 
  | 
  | 
    | Author: J.A.Greant                                                   | | 
| 73 | 
  | 
  | 
    | Email : zak@nucleus.com                                              | | 
| 74 | 
  | 
  | 
    | Date  : 2000/11/23                                                   | | 
| 75 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 76 | 
  | 
  | 
    | The CreditCard class provides methods for cleaning, validating and  | | 
| 77 | 
  | 
  | 
    | identifying the type of credit card number. The validation algorithm | | 
| 78 | 
  | 
  | 
    | and identification procedures are based on information found at:     | | 
| 79 | 
  | 
  | 
    |          http://www.beachnet.com/~hstiles/cardtype.html              | | 
| 80 | 
  | 
  | 
    |                                                                      | | 
| 81 | 
  | 
  | 
    | CreditCard::clean_no() method                                       | | 
| 82 | 
  | 
  | 
    | ------------------------------                                       | | 
| 83 | 
  | 
  | 
    | Strips all non-numeric characters from the passed value and returns  | | 
| 84 | 
  | 
  | 
    | an integer.  This method is called by the other methods in the       | | 
| 85 | 
  | 
  | 
    | CreditCard class.                                                   | | 
| 86 | 
  | 
  | 
    |                                                                      | | 
| 87 | 
  | 
  | 
    | USAGE EXAMPLE:                                                       | | 
| 88 | 
  | 
  | 
    | $cc_no ="5454 5454 5454 5454"; // Has spaces for readability         | | 
| 89 | 
  | 
  | 
    | $cleaned_cc_no = CreditCard::clean_no ($cc_no);                     | | 
| 90 | 
  | 
  | 
    | print $cleaned_cc_no; // Displays 5454545454545454                   | | 
| 91 | 
  | 
  | 
    |                                                                      | | 
| 92 | 
  | 
  | 
    | CreditCard::identify() method                                       | | 
| 93 | 
  | 
  | 
    | ------------------------------                                       | | 
| 94 | 
  | 
  | 
    | Finds the type of credit card (Mastercard, Visa, etc...) based on    | | 
| 95 | 
  | 
  | 
    | the length and format of the credit card number.  The method can     | | 
| 96 | 
  | 
  | 
    | identify American Express, Diners Club/Carte Blanche, Discover,      | | 
| 97 | 
  | 
  | 
    | enRoute, JCB, Mastercard and Visa cards.                             | | 
| 98 | 
  | 
  | 
    |                                                                      | | 
| 99 | 
  | 
  | 
    | USAGE EXAMPLE:                                                       | | 
| 100 | 
  | 
  | 
    | $cc_no ="5454 5454 5454 5454";                                       | | 
| 101 | 
  | 
  | 
    | print CreditCard::identify ($cc_no); // Returns "Mastercard"        | | 
| 102 | 
  | 
  | 
    |                                                                      | | 
| 103 | 
  | 
  | 
    | CreditCard::validate() method                                       | | 
| 104 | 
  | 
  | 
    | ------------------------------                                       | | 
| 105 | 
  | 
  | 
    | Validate a credit card number using the LUHN formula (mod 10).       | | 
| 106 | 
  | 
  | 
    | Note that many other kinds of card and account numbers are based on  | | 
| 107 | 
  | 
  | 
    | the LUHN algorith - including Canadian Social Insurance Numbers.     | | 
| 108 | 
  | 
  | 
    |                                                                      | | 
| 109 | 
  | 
  | 
    | USAGE EXAMPLE:                                                       | | 
| 110 | 
  | 
  | 
    | $cc_no ="5454 5454 5454 5454";                                       | | 
| 111 | 
  | 
  | 
    | print CreditCard::validate ($cc_no); // Returns TRUE                | | 
| 112 | 
  | 
  | 
    |                                                                      | | 
| 113 | 
  | 
  | 
    | CreditCard::check() method                                          | | 
| 114 | 
  | 
  | 
    | ---------------------------                                          | | 
| 115 | 
  | 
  | 
    | The check() method validates and identifies a credit card, returning | | 
| 116 | 
  | 
  | 
    | an array. Indexes 0 and 'valid' will contain TRUE if the card number | | 
| 117 | 
  | 
  | 
    | is valid (FALSE if it isn't), while indexes 1 and 'type' will        | | 
| 118 | 
  | 
  | 
    | the type of card (if known). The presence of the numeric keys allows | | 
| 119 | 
  | 
  | 
    | the method to be used with the list() function.                      | | 
| 120 | 
  | 
  | 
    |                                                                      | | 
| 121 | 
  | 
  | 
    | USAGE EXAMPLE:                                                       | | 
| 122 | 
  | 
  | 
    | $cc_no ="4111 1111 1111 1111";                                       | | 
| 123 | 
  | 
  | 
    | list ($valid, $type) = CreditCard::check ($cc_no);                  | | 
| 124 | 
  | 
  | 
    | print $valid; // Displays 1 (TRUE)                                   | | 
| 125 | 
  | 
  | 
    | print $type;  // Displays "Visa"                                     | | 
| 126 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 127 | 
  | 
  | 
 | 
| 128 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 129 | 
  | 
  | 
    | CVS LOG INFO                                                         | | 
| 130 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 131 | 
  | 
  | 
      $Log: credit_card_validator.pkg,v $ | 
| 132 | 
  | 
  | 
      Revision 1.2  2000/11/24 07:02:15  zak | 
| 133 | 
  | 
  | 
      Added top copyright notice | 
| 134 | 
  | 
  | 
 | 
| 135 | 
  | 
  | 
      Revision 1.1  2000/11/24 06:58:42  zak | 
| 136 | 
  | 
  | 
      Initial commit of credit card class to repository. | 
| 137 | 
  | 
  | 
      Cleaned and modified code. | 
| 138 | 
  | 
  | 
      Also rewrote significant parts of code so that I could change to the | 
| 139 | 
  | 
  | 
      more friendly BSD-style license. | 
| 140 | 
  | 
  | 
 | 
| 141 | 
  | 
  | 
 | 
| 142 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 143 | 
  | 
  | 
    | Copyright (c) 2000 J.A.Greant (jag@nucleus.com)                      | | 
| 144 | 
  | 
  | 
    | All rights reserved.                                                 | | 
| 145 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 146 | 
  | 
  | 
    | Redistribution and use in source and binary forms, with or without   | | 
| 147 | 
  | 
  | 
    | modification, is permitted provided that the following conditions    | | 
| 148 | 
  | 
  | 
    | are met:                                                             | | 
| 149 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 150 | 
  | 
  | 
    | Redistributions of source code must retain the above copyright       | | 
| 151 | 
  | 
  | 
    | notice, this list of conditions and the following disclaimer.        | | 
| 152 | 
  | 
  | 
    |                                                                      | | 
| 153 | 
  | 
  | 
    | Redistributions in binary form must reproduce the above copyright    | | 
| 154 | 
  | 
  | 
    | notice, this list of conditions and the following disclaimer in the  | | 
| 155 | 
  | 
  | 
    | documentation and/or other materials provided with the distribution. | | 
| 156 | 
  | 
  | 
    |                                                                      | | 
| 157 | 
  | 
  | 
    | Neither the name of the author nor the names of any contributors to  | | 
| 158 | 
  | 
  | 
    | this software may be used to endorse or promote products derived     | | 
| 159 | 
  | 
  | 
    | from this software without specific prior written permission.        | | 
| 160 | 
  | 
  | 
    |                                                                      | | 
| 161 | 
  | 
  | 
    | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  | | 
| 162 | 
  | 
  | 
    | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  | | 
| 163 | 
  | 
  | 
    | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    | | 
| 164 | 
  | 
  | 
    | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE       | | 
| 165 | 
  | 
  | 
    | AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,           | | 
| 166 | 
  | 
  | 
    | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | | 
| 167 | 
  | 
  | 
    | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;     | | 
| 168 | 
  | 
  | 
    | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER     | | 
| 169 | 
  | 
  | 
    | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT   | | 
| 170 | 
  | 
  | 
    | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN    | | 
| 171 | 
  | 
  | 
    | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE      | | 
| 172 | 
  | 
  | 
    | POSSIBILITY OF SUCH DAMAGE.                                          | | 
| 173 | 
  | 
  | 
    +----------------------------------------------------------------------+ | 
| 174 | 
  | 
  | 
*/ | 
| 175 | 
  | 
  | 
 | 
| 176 | 
  | 
  | 
?> |