Quick Search:

View

Revision:

Diff

Diff from 485 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/osCommerce/branches/hpdl/oscommerce/includes/classes/credit_card.php

Annotated File View

hpdl
485
1 <?php
2 /*
3   $Id: $
4
5   osCommerce, Open Source E-Commerce Solutions
6   http://www.oscommerce.com
7
8   Copyright (c) 2006 osCommerce
9
10   Released under the GNU General Public License
11 */
12
13   class osC_CreditCard {
14
15 /* Private variables */
16
17     var $_owner,
18         $_number,
19         $_expiry_month,
20         $_expiry_year,
21         $_cvc,
22         $_type,
23         $_data;
24
25 /* Class constructor */
26
27     function osC_CreditCard($number = '', $exp_month = '', $exp_year = '') {
28       global $osC_Database;
29
30       if (empty($number) === false) {
31         $this->_number = ereg_replace('[^0-9]', '', $number);
32         $this->_expiry_month = (int)$exp_month;
33         $this->_expiry_year = (int)$exp_year;
34       }
35
36       $this->_data = array();
37
38       $Qcc = $osC_Database->query('select id, credit_card_name as title, pattern from :table_credit_cards where credit_card_status = "1" order by sort_order, credit_card_name');
39       $Qcc->bindTable(':table_credit_cards', TABLE_CREDIT_CARDS);
40 //      $Qcc->setCache('credit_cards');
41       $Qcc->execute();
42
43       while ($Qcc->next()) {
44         $this->_data[$Qcc->valueInt('id')] = $Qcc->toArray();
45       }
46     }
47
48 /* Public variables */
49
50     function isValid($valid_cc_types = '') {
51       if (CFG_CREDIT_CARDS_VERIFY_WITH_REGEXP == '1') {
52         if ($this->hasValidNumber() === false) {
53           return -1;
54         }
55
56         if ($this->isAccepted($valid_cc_types) === false) {
57           return -5;
58         }
59       }
60
61       if ($this->hasValidExpiryDate() === false) {
62         return -2;
63       }
64
65       if ($this->hasExpired() === true) {
66         return -3;
67       }
68
69       if ($this->hasOwner() && ($this->hasValidOwner() === false)) {
70         return -4;
71       }
72
73       return true;
74     }
75
76     function hasValidNumber() {
77       if ( (empty($this->_number) === false) && (strlen($this->_number) >= CC_NUMBER_MIN_LENGTH) ) {
78         $cardNumber = strrev($this->_number);
79         $numSum = 0;
80
81         for ($i=0, $n=strlen($cardNumber); $i<$n; $i++) {
82           $currentNum = substr($cardNumber, $i, 1);
83
84 // Double every second digit
85           if ($i % 2 == 1) {
86             $currentNum *= 2;
87           }
88
89 // Add digits of 2-digit numbers together
90           if ($currentNum > 9) {
91             $firstNum = $currentNum % 10;
92             $secondNum = ($currentNum - $firstNum) / 10;
93             $currentNum = $firstNum + $secondNum;
94           }
95
96           $numSum += $currentNum;
97         }
98
99 // If the total has no remainder it's OK
100         return ($numSum % 10 == 0);
101       }
102
103       return false;
104     }
105
106     function isAccepted($valid_cc_types) {
107       if ( (empty($valid_cc_types) === false) && (empty($this->_number) === false) && (strlen($this->_number) >= CC_NUMBER_MIN_LENGTH) ) {
108         if (is_array($valid_cc_types) === false) {
109           $valid_cc_types = explode(',', $valid_cc_types);
110         }
111
112         foreach ($this->_data as $data) {
113           if (in_array($data['id'], $valid_cc_types)) {
114             if (preg_match($data['pattern'], $this->_number) === 1) {
115               $this->_type = $data['title'];
116
117               return true;
118             }
119           }
120         }
121       }
122
123       return false;
124     }
125
126     function hasValidExpiryDate() {
127       $year = date('Y');
128
129       return ( ($this->_expiry_month > 0) && ($this->_expiry_month < 13) && ($this->_expiry_year >= $year) && ($this->_expiry_year <= ($year+10)) );
130     }
131
132     function hasExpired() {
133       return ( ($this->_expiry_year <= date('Y')) && ($this->_expiry_month < date('n')) );
134     }
135
136     function hasOwner() {
137       return (isset($this->_owner));
138     }
139
140     function hasValidOwner() {
141       return ( (empty($this->_owner) === false) && (strlen($this->_owner) >= CC_OWNER_MIN_LENGTH) );
142     }
143
144     function typeExists($id) {
145       return isset($this->_data[$id]);
146     }
147
148     function getNumber() {
149       return $this->_number;
150     }
151
152     function getSafeNumber() {
153       return str_repeat('X', strlen($this->_number)-4) . substr($this->_number, -4);
154     }
155
156     function getExpiryMonth() {
157       return str_pad($this->_expiry_month, 2, '0', STR_PAD_LEFT);
158     }
159
160     function getExpiryYear() {
161       return $this->_expiry_year;
162     }
163
164     function getCVC() {
165       return $this->_cvc;
166     }
167
168     function getOwner() {
169       return $this->_owner;
170     }
171
172     function getTypePattern($id) {
173       return $this->_data[$id]['pattern'];
174     }
175
176     function setOwner($name) {
177       $this->_owner = trim($name);
178     }
179
180     function setCVC($cvc) {
181       $this->_cvc = trim($cvc);
182     }
183   }
184 ?>