oxccvalidator.php

Go to the documentation of this file.
00001 <?php
00002 
00007 class oxCcValidator
00008 {
00009 
00015     protected $_aCardsInfo = array( // name              // digits     // starting digits
00016         "amx" => '/^3[47].{13}$/', // American Express     16            34, 37
00017         "dlt" => '/^4.{15}$/', // Delta                16            4
00018         "dnc" => '/^30[0-5].{11}$|^3[68].{12}$/', // Diners Club          14            300-305, 36, 38
00019         "dsc" => '/^6011.{12}$/', // Discover             16            6011
00020         "enr" => '/^2014.{11}$|^2149.{11}$/', // enRoute              15            2014, 2149
00021         "jcb" => '/^3.{15}$|^2131|1800.{11}$/', // JCB                  15/16         3/ 2131, 1800
00022         "mcd" => '/^5[1-5].{14}$/', // MasterCard           16            51-55
00023         "swi" => '/^[456].{15}$|^[456].{17,18}$/', // Switch               16, 18, 19    4-6
00024         "vis" => '/^4.{15}$|^4.{12}$/', // Visa                 13, 16        4
00025     );
00026 
00035     protected function _isValidType($sType, $sNumber)
00036     {
00037         $blValid = true;
00038 
00039         // testing if card type is known and matches pattern
00040         if (isset($this->_aCardsInfo[$sType])) {
00041             $blValid = preg_match($this->_aCardsInfo[$sType], $sNumber);
00042         }
00043 
00044         return $blValid;
00045     }
00046 
00054     protected function _isExpired($sDate)
00055     {
00056         $blExpired = false;
00057 
00058         if ($sDate) {
00059             $sYears = substr($sDate, 2, 2);
00060             $sMonth = substr($sDate, 0, 2);
00061             $sDay = date("t", mktime(11, 59, 59, $sMonth, 1, $sYears));
00062 
00063             $iExpDate = mktime(23, 59, 59, $sMonth, $sDay, $sYears);
00064             if (time() > $iExpDate) {
00065                 $blExpired = true;
00066             }
00067         }
00068 
00069         return $blExpired;
00070     }
00071 
00079     protected function _isValidNumer($sNumber)
00080     {
00081         $blValid = false;
00082         if (($iLength = strlen($sNumber))) {
00083             $iModSum = 0;
00084             $iMod = $iLength % 2;
00085 
00086             // Luhn algorithm
00087             for ($iPos = 0; $iPos < $iLength; $iPos++) {
00088 
00089                 // taking digit to check..
00090                 $iCurrDigit = ( int ) $sNumber{$iPos};
00091 
00092                 // multiplying if needed..
00093                 $iAddValue = (($iPos % 2 == $iMod) ? 2 : 1) * $iCurrDigit;
00094 
00095                 // adding prepared current digit
00096                 $iModSum += ($iAddValue > 9) ? $iAddValue - 9 : $iAddValue;
00097             }
00098 
00099             $blValid = ($iModSum % 10) == 0;
00100         }
00101 
00102         return $blValid;
00103     }
00104 
00114     public function isValidCard($sNumber, $sType = "", $sDate = "")
00115     {
00116         // cleanup
00117         $sNumber = preg_replace("/[^0-9]/", "", $sNumber);
00118 
00119         return (!$this->_isExpired($sDate) && $this->_isValidType($sType, $sNumber) && $this->_isValidNumer($sNumber));
00120     }
00121 }