OXID eShop CE  4.10.5
 All Classes Namespaces Files Functions Variables Pages
oxccvalidator.php
Go to the documentation of this file.
1 <?php
2 
8 {
9 
15  protected $_aCardsInfo = array( // name // digits // starting digits
16  "amx" => '/^3[47].{13}$/', // American Express 16 34, 37
17  "dlt" => '/^4.{15}$/', // Delta 16 4
18  "dnc" => '/^30[0-5].{11}$|^3[68].{12}$/', // Diners Club 14 300-305, 36, 38
19  "dsc" => '/^6011.{12}$/', // Discover 16 6011
20  "enr" => '/^2014.{11}$|^2149.{11}$/', // enRoute 15 2014, 2149
21  "jcb" => '/^3.{15}$|^2131|1800.{11}$/', // JCB 15/16 3/ 2131, 1800
22  "mcd" => '/^5[1-5].{14}$/', // MasterCard 16 51-55
23  "swi" => '/^[456].{15}$|^[456].{17,18}$/', // Switch 16, 18, 19 4-6
24  "vis" => '/^4.{15}$|^4.{12}$/', // Visa 13, 16 4
25  );
26 
35  protected function _isValidType($sType, $sNumber)
36  {
37  $blValid = true;
38 
39  // testing if card type is known and matches pattern
40  if (isset($this->_aCardsInfo[$sType])) {
41  $blValid = preg_match($this->_aCardsInfo[$sType], $sNumber);
42  }
43 
44  return $blValid;
45  }
46 
54  protected function _isExpired($sDate)
55  {
56  $blExpired = false;
57 
58  if ($sDate) {
59  $sYears = substr($sDate, 2, 2);
60  $sMonth = substr($sDate, 0, 2);
61  $sDay = date("t", mktime(11, 59, 59, $sMonth, 1, $sYears));
62 
63  $iExpDate = mktime(23, 59, 59, $sMonth, $sDay, $sYears);
64  if (time() > $iExpDate) {
65  $blExpired = true;
66  }
67  }
68 
69  return $blExpired;
70  }
71 
79  protected function _isValidNumer($sNumber)
80  {
81  $blValid = false;
82  if (($iLength = strlen($sNumber))) {
83  $iModSum = 0;
84  $iMod = $iLength % 2;
85 
86  // Luhn algorithm
87  for ($iPos = 0; $iPos < $iLength; $iPos++) {
88 
89  // taking digit to check..
90  $iCurrDigit = ( int ) $sNumber{$iPos};
91 
92  // multiplying if needed..
93  $iAddValue = (($iPos % 2 == $iMod) ? 2 : 1) * $iCurrDigit;
94 
95  // adding prepared current digit
96  $iModSum += ($iAddValue > 9) ? $iAddValue - 9 : $iAddValue;
97  }
98 
99  $blValid = ($iModSum % 10) == 0;
100  }
101 
102  return $blValid;
103  }
104 
114  public function isValidCard($sNumber, $sType = "", $sDate = "")
115  {
116  // cleanup
117  $sNumber = preg_replace("/[^0-9]/", "", $sNumber);
118 
119  return (!$this->_isExpired($sDate) && $this->_isValidType($sType, $sNumber) && $this->_isValidNumer($sNumber));
120  }
121 }