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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Tue Mar 11 00:42:39 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +11 -0 lines
+ fixed metadata for phpDocumentor

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

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