p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Credit card validation code not working.


Message #1 by Sri_core@m... on Thu, 2 May 2002 01:01:50
I am writing this validation code I got it from one of my friend who got 
it from the web I think.
This is the class file .
<?php 

define("CARD_TYPE_MC", 0); 
define("CARD_TYPE_VS", 1); 
define("CARD_TYPE_AX", 2); 
define("CARD_TYPE_DC", 3); 
define("CARD_TYPE_DS", 4); 
define("CARD_TYPE_JC", 5); 
class CCreditCard 
{ 
	// Class Members 
		var $__ccName = ''; 
		var $__ccType = ''; 
		var $__ccNum = ''; 
		var $__ccExpM = 0; 
		var $__ccExpY = 0; 

	// Constructor 
		function CCreditCard($name, $type, $num, $expm, $expy) 
	{ 
	// Set member variables 
		if(!empty($name)) 
	{ 
	$this->__ccName = $name; 
	} 
	else 
{ 
	die('Must pass name to constructor'); 
	} 
// 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; 
} 
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; 
} 
else 
{ 
die('Must pass number to constructor'); 
} 
} 
else 
{ 
die('Must pass number to constructor'); 
} 
if(!is_numeric($expm) || $expm < 0 || $expm > 12) 
{ 
die('Invalid expiry month of ' . $expm . ' passed to constructor'); 
} 
else 
{ 
$this->__ccExpM = $expm; 
} 

// Get the current year 
$currentYear = date('Y'); 
settype($currentYear, 'integer'); 

if(!is_numeric($expy) || $expy < $currentYear || $expy > $currentYear + 
10) 
{ 
die('Invalid expiry year of ' . $expy . ' passed to constructor'); 
} 
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 [1]'; 
break; 
} 
case CARD_TYPE_DS: 
{ 
return 'Discover [5]'; 
break; 
} 
case CARD_TYPE_JC: 
{ 
return 'JCB [6]'; 
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; 
// 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}$|^4[0-9]{15}$", $this->__ccNum); 
break; 
} 
case CARD_TYPE_AX: 
{ 
$validFormat = ereg("^3[4|7][0-9]{13}$", $this->__ccNum); 
break; 
} 
case CARD_TYPE_DC: 
{ 
$validFormat = ereg("^30[0-5][0-9]{11}$|^3[6|8][0-9]{12}$", $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]{15}$|^[2131|1800][0-9]{11}$", $this-
>__ccNum); 
break; 
} 
default: 
{ 
// Should never be executed 
$validFormat = false; 
} 
} 
// Is the number valid against luhn? 
$cardNumber = strrev($this->__ccNum); 
$numSum = 0; 

for($i = 0; $i < strlen($cardNumber); $i++) 
{ 
$currentNum = substr($cardNumber, $i, 1); 

if(floor($currentNum / 2) != $currentNum / 2) 
{ 
$currentNum *= 2; 
} 

if(strlen($currentNum) == 2) 
{ 
$firstNum = substr($currentNum, 0, 1); 
$secondNum = substr($currentNum, 1, 1); 
$currentNum = $firstNum + $secondNum; 
} 

$numSum += $currentNum; 
} 
// If the total has no remained its OK 
$passCheck = ($numSum % 10 == 0 ? true : false); 

if($validFormat && $passCheck) 
{ 
return true; 
} 
else 
{ 
return false; 
} 
} 
}
?> 
This is the testcc.php file
<?php include('class.creditcard.php'); ?> 

<?php 

global $submit; 

if(!isset($submit)) 
{ 
?> 

<h2>Validate Credit Card</h2> 

<form name="frmCC" action="testcc.php" method="post"> 

Cardholders name: <input type="text" name="ccName"><br> 
Card number: <input type="text" name="ccNum"><br> 
Card type: <select name="ccType"> 
<option value="1">Mastercard</option> 
<option value="2">Visa</option> 
<option value="3">Amex</option> 
<option value="4">Diners</option> 
<option value="5">Discover</option> 
<option value="6">JCB</option> 
</select><br> 

Expiry Date: <select name="ccExpM"> 

<?php 

for($i = 1; $i < 13; $i++) 
{ echo '<option>' . $i . '</option>'; } 

?> 

</select> 

<select name="ccExpY"> 

<?php 

for($i = 2002; $i < 2013; $i++) 
{ echo '<option>' . $i . '</option>'; } 

?> 

</select><br><br> 

<input type="submit" name="submit" value="Validate"> 
</form> 

<? 

} 
else 
{ 
// Check if the card is valid 
global $ccName; 
global $ccNum; 
global $ccType; 
global $ccExpM; 
global $ccExpY; 

$cc = new CCreditCard($ccName, $ccType, $ccNum, $ccExpM, $ccExpY); 

?> 

<h2>Validation Results</h2> 
<b>Name: </b><?php echo $cc->Name(); ?><br> 
<b>Number: </b><?php echo $cc->SafeNumber('x', 6); ?><br> 
<b>Type: </b><?php echo $cc->Type(); ?><br> 
<b>Expires: </b><?php echo $cc->ExpiryMonth() . '/' . $cc->ExpiryYear(); ?
><br><br> 

<font color="blue" size="2"><b> 

<?php 

if($cc->IsValid()) 
echo "VALID CARD"; 
else 
echo "INVALID CARD"; 

echo "</b></font>"; 
} 
?> 
 And this does not work...
Any help will be appreciated.
Thanks
Sri


  Return to Index