oxccvalidator.php

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