Click to See Complete Forum and Search --> : credit card validation class


clonmelog
02-27-2005, 03:17 PM
Hi,

Ive tried many different credit card validation classes and cant seem to get one that works for me for some reason. Its probabbly me doing something completely stupid in all of them, im not sure to be honest!

Here is the code for one that im assured will work:

function validate_cc($argNum)
{
$cc = str_replace(" ", "", $argNum);
$prefix = substr($cc, 0, 1);

/* PREFIX TEST */
switch ($prefix) {
case "1":
if (substr($cc, 0, 4) == "1800") {
$valid = "jcb1";
} else {
$valid = false;
}
break;
case "2":
if (substr($cc, 0, 4) == "2014" || substr($cc, 0, 4) == "2149") {
$valid = "enRoute";
} elseif (substr($cc, 0, 4) == "2131") {
$valid = "jcb2";
} else {
$valid = false;
}
break;
case "3":
if (substr($cc, 0, 3) > 299 && substr($cc, 0, 3) < 306) {
$valid = "diners";
} elseif (substr($cc, 0, 2) == 36 || substr($cc, 0, 2) == 38) {
$valid = "diners";
} elseif (substr($cc, 0, 2) == 34 || substr($cc, 0, 2) == 37) {
$valid = "amex";
} else {
$valid = "jcb3";
}
break;
case "4":
$valid = "visa";
break;
case "5":
if (substr($cc, 0, 2) > 50 && substr($cc, 0, 2) < 56) {
$valid = "mc";
}
break;
case "6":
if (substr($cc, 0, 4) == "6011") {
$valid = "discover";
} else {
$valid = false;
}
break;
}

/* LENGTH TEST */
if ($valid) {
switch ($valid) {
case "mc":
case "disocver":
case "jcb3":
if (strlen($cc) <> 16) {
$valid = false;
}
break;
case "amex":
case "enRoute":
case "jcb1":
case "jcb2":
if (strlen($cc) <> 15) {
$valid = false;
}
break;
case "diners":
if (strlen($cc) <> 14) {
$valid = false;
}

break;
case "visa":
if (strlen($cc) != 13 || strlen($cc) != 16) {
$valid = false;
}
break;
}
}

/* MOD10 TEST */
if ($valid) {
switch ($valid) {
case "mc":
case "visa":
case "amex":
case "diners":
case "discover":
case "jcb1":
case "jcb2":
case "jcb3":
for ($i = strlen($cc) - 2; $i >= 0; $i = $i - 2) {
$odds[] = (int) $cc{$i};
$cc{$i} = "%";
}
$cc = str_replace("%", "", $cc);

foreach ($odds as $key => $value) {
$oddnumber = $value * 2;
$i = 0;
$oddnumber = (string) $oddnumber;
do {
$total = $total + (int) $oddnumber{$i};
$i++;
} while ($oddnumber{$i});
$numbers[] = (int) $total;
unset($total);
}
$i = 0;

while ($i <= (strlen($cc) - 1)) {
$numbers[] = (int) $cc{$i};
$i++;
}

foreach ($numbers as $key => $value) {
$total = $total + (int) $value;
}

if ($total % 10 !== 0) {
$valid = false;
}
break;
}
}

if ($valid == "jcb1" || $valid == "jcb2" || $valid == "jcb3")
{
$valid = "jcb";
}
return $valid;
}

Ok, to save me from making a mess of another one, seen as this one "definately works" I wil give the code of my form that im using to allow the customer to enter their cc details below and hopefully someone will be kind enough as to show me how to apply the class to it as i have very little dealiing with this type of thing in the past :/

<form name="form1" method="post" action="register.php">

</p>
<p align="center"><b><font color="#FF0000">Payment Details</font></b></p>
<table width="75%" height="146" border="0">
<tr>
<td width="35%"><font color="#FFFFFF" face="Times New Roman, Times, serif">Card
Type</font></td>
<td width="65%"> <select name="C_CcType" id="C_CcType">
<option selected>Visa</option>
<option>MasterCard</option>
<option>American Express</option>
</select> </td>
</tr>
<tr>
<td width="35%"><font color="#FFFFFF" face="Times New Roman, Times, serif">Card
Number</font></td>
<td width="65%"> <input type="text" name="C_CcNum" class="textBoxNormal" onfocus="this.className='textBoxActive'" onblur="this.className='textBoxNormal'">
</td>
</tr>
<tr>
<td width="35%"><font color="#FFFFFF" face="Times New Roman, Times, serif">Expiry
Date</font></td>
<td width="65%"> <input type="text" name="C_ExpD" class="textBoxNormal" onfocus="this.className='textBoxActive'" onblur="this.className='textBoxNormal'"> </td>
</tr>
<tr>
<td width="35%"><font color="#FFFFFF" face="Times New Roman, Times, serif">Sec
Code </font></td>
<td width="65%"> <input type="text" name="C_Sec" class="textBoxNormal" onfocus="this.className='textBoxActive'" onblur="this.className='textBoxNormal'"> </td>
</tr>
<tr>
<td colspan="2">&nbsp; </td>
</tr>
</table>
<p align="center" class="tahbol style1">
<input name="total" type="hidden" id="total" value="<?php echo $total; ?>">
<input name="p_method" type="hidden" id="p_method" value="<?php echo $p_method; ?>">
</p>
<p>&nbsp;</p>
<p>
<input type="image" src="images/ok.gif" align="absmiddle" border="0" name="image">
</form>

just to clarify that i wish for the code to check if the card is valid and then proceed to the page "register.php".

Kyleva2204
02-27-2005, 07:06 PM
hey man, it doesnt look like your using 100% verifcation, all your doing is seeing if the numbers add up, so why not just use a simple javascript? haha.. or get an account with someme that has an API for checking creditcards 100%.. haha

DaiWelsh
02-28-2005, 04:15 AM
How are you calling the function? What error message do you get? What is not working as you expected? Please help us to help you... ;)

From what you have shown I would expect you to be doing something like

if(validate_cc($_POST['C_CcNum'])) {
// cc good
} else {
// cc bad
}

but the way you have shown it, the form is already submitting to register.php so I am not sure what you mean by redirecting to register.php only after testing it...?