/[cvs]/nfo/php/libs/org.netfrag.glib/Data/Validator/CreditCard.php
ViewVC logotype

Contents of /nfo/php/libs/org.netfrag.glib/Data/Validator/CreditCard.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Sun Feb 9 17:26:48 2003 UTC (21 years, 6 months ago) by joko
Branch: MAIN
+ refactored from flib/utils/

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: CreditCard.php,v $
132 Revision 1.1 2002/12/22 13:28:38 jonen
133 + first init
134 class for advanced creditcard format and type checking
135
136 Revision 1.2 2000/11/24 07:02:15 zak
137 Added top copyright notice
138
139 Revision 1.1 2000/11/24 06:58:42 zak
140 Initial commit of credit card class to repository.
141 Cleaned and modified code.
142 Also rewrote significant parts of code so that I could change to the
143 more friendly BSD-style license.
144
145
146 +----------------------------------------------------------------------+
147 | Copyright (c) 2000 J.A.Greant (jag@nucleus.com) |
148 | All rights reserved. |
149 +----------------------------------------------------------------------+
150 | Redistribution and use in source and binary forms, with or without |
151 | modification, is permitted provided that the following conditions |
152 | are met: |
153 +----------------------------------------------------------------------+
154 | Redistributions of source code must retain the above copyright |
155 | notice, this list of conditions and the following disclaimer. |
156 | |
157 | Redistributions in binary form must reproduce the above copyright |
158 | notice, this list of conditions and the following disclaimer in the |
159 | documentation and/or other materials provided with the distribution. |
160 | |
161 | Neither the name of the author nor the names of any contributors to |
162 | this software may be used to endorse or promote products derived |
163 | from this software without specific prior written permission. |
164 | |
165 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
166 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
167 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
168 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
169 | AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
170 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
171 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
172 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
173 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
174 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
175 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
176 | POSSIBILITY OF SUCH DAMAGE. |
177 +----------------------------------------------------------------------+
178 */
179
180 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed