Quick Search:

View

Revision:

Diff

Diff from 1497 to:

Annotations

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