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