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