__ccName = $name; } // Make sure card type is valid switch(strtolower($type)) { case 'mc': case 'mastercard': case 'm': case '1': $this->__ccType = CARD_TYPE_MC; break; case 'vs': case 'visa': case 'v': case '2': $this->__ccType = CARD_TYPE_VS; break; case 'ax': case 'american express': case 'a': case '3': $this->__ccType = CARD_TYPE_AX; break; case 'dc': case 'diners club': case '4': $this->__ccType = CARD_TYPE_DC; break; case 'ds': case 'discover': case '5': $this->__ccType = CARD_TYPE_DS; break; case 'jc': case 'jcb': case '6': $this->__ccType = CARD_TYPE_JC; break; case 'ls': case 'laser': case '7': $this->__ccType = CARD_TYPE_LR; break; default: die('Invalid type ' . $type . ' passed to constructor'); } // Don't check the number yet just kill all non numerics if(!empty($num)) { $cardNumber = ereg_replace("[^0-9]", "", $num); // Make sure the card number isnt empty if(!empty($cardNumber)) { $this->__ccNum = $cardNumber; } } if(!is_numeric($expm) || $expm < 1 || $expm > 12) { $this->_ccMissingGear = 1; } else { $this->__ccExpM = $expm; } // Get the current year $currentYear = date('Y'); settype($currentYear, 'integer'); if(!is_numeric($expy) || $expy < $currentYear || $expy > $currentYear + 10) { $this->_ccMissingGear = 1; } else { $this->__ccExpY = $expy; } } function Name() { return $this->__ccName; } function Type() { switch($this->__ccType) { case CARD_TYPE_MC: return 'mastercard [1]'; break; case CARD_TYPE_VS: return 'Visa [2]'; break; case CARD_TYPE_AX: return 'Amex [3]'; break; case CARD_TYPE_DC: return 'Diners Club [4]'; break; case CARD_TYPE_DS: return 'Discover [5]'; break; case CARD_TYPE_JC: return 'JCB [6]'; break; case CARD_TYPE_LR: return 'Laser [7]'; break; default: return 'Unknown [-1]'; } } function Number() { return $this->__ccNum; } function ExpiryMonth() { return $this->__ccExpM; } function ExpiryYear() { return $this->__ccExpY; } function SafeNumber($char = 'x', $numToHide = 4) { // Return only part of the number if($numToHide < 4) { $numToHide = 4; } if($numToHide > 10) { $numToHide = 10; } $cardNumber = $this->__ccNum; $cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide); for($i = 0; $i < $numToHide; $i++) { $cardNumber .= $char; } return $cardNumber; } function IsValid() { // Not valid by default $validFormat = false; $passCheck = false; $expiryCheck = true; $currentMonth = date('m'); $currentY = date('Y'); $expMonth = $this->__ccExpM; $expYear = $this->__ccExpY; settype($currentMonth, 'integer'); settype($currentY, 'integer'); settype($expYear, 'integer'); settype($expMonth, 'integer'); if(($expYear == $currentY) && ($expMonth < $currentMonth)) { $expiryCheck = false; } // Is the number in the correct format? switch($this->__ccType) { case CARD_TYPE_MC: $validFormat = ereg("^5[1-5][0-9]{14}$", $this->__ccNum); break; case CARD_TYPE_VS: $validFormat = ereg("^4[0-9]{12}([0-9]{3})?$", $this->__ccNum); break; case CARD_TYPE_AX: $validFormat = ereg("^3[47][0-9]{13}$", $this->__ccNum); break; case CARD_TYPE_DC: $validFormat = ereg("^3(0[0-5]|[68][0-9])[0-9]{11}$", $this->__ccNum); break; case CARD_TYPE_DS: $validFormat = ereg("^6011[0-9]{12}$", $this->__ccNum); break; case CARD_TYPE_JC: $validFormat = ereg("^(3[0-9]{4}|2131|1800)[0-9]{11}$", $this->__ccNum); break; case CARD_TYPE_LR: $validFormat = ereg("^(6[0-9]{15}|6[0-9]{17}|6[0-9]{18})$", $this->__ccNum); break; default: // Should never be executed $validFormat = false; } // Is the number valid? $cardNumber = strrev($this->__ccNum); $numSum = 0; for($i = 0; $i < strlen($cardNumber); $i++) { $currentNum = substr($cardNumber, $i, 1); // Double every second digit if($i % 2 == 1) { $currentNum *= 2; } // Add digits of 2-digit numbers together if($currentNum > 9) { $firstNum = $currentNum % 10; $secondNum = ($currentNum - $firstNum) / 10; $currentNum = $firstNum + $secondNum; } $numSum += $currentNum; } // If the total has no remainder it's OK $passCheck = ($numSum % 10 == 0); settype($mG, 'integer'); if($validFormat && $passCheck && $expiryCheck) return true; else return false; } } ?>