Credit Card Validation

Use GitHub to post feature requests for phpwcms.
Locked
pbeckermann
Posts: 4
Joined: Wed 17. Dec 2003, 13:23
Location: Burlington
Contact:

Credit Card Validation

Post by pbeckermann »

Oliver this is great

Do you think it would be possible to include a CREDIT CARD validation function into the form??

here is the code i have - 3 files
----------------------------------------------------------------------------------
cardgen.php
----------------------------------------------------------------------------------
<?
if ($Calculate) {
srand((double)microtime()*1000000);
$Num = $StartWith;

while (strlen($Num) < $Digits - 1) $Num.= rand(0,9);

$rNum = strrev($Num);

$Total = 0;

for ($x=0; $x<strlen($rNum); $x++) {
$digit = substr($rNum, $x, 1);

if ($x/2 == floor($x/2)) $digit *= 2;

if (strlen($digit) == 2) $digit = substr($digit, 0, 1) + substr($digit, 1, 1);
$Total += $digit;
}

$CheckDigit = 0;
while (($Total + $CheckDigit) % 10 > 0) $CheckDigit++;

$Num.= $CheckDigit;

$Result = chunk_split($Num, 4, " ");
}
?>
<HTML>
<HEAD>
<TITLE>Credit Card Number Generator</TITLE>
</HEAD>
<BODY>

Enter the number of digits and, optionally, the specific starting digit(s) from the
table below, and this script will calculate a number that will pass the Luhn Mod 10
formula.

<FORM METHOD=POST ACTION="cardgen.php">
<TABLE WIDTH=300>
<TR>
<TD><B># of Digits</B></TD>
<TD><B>Starting With</B></TD>
</TR>
<TR>
<TD>
<INPUT TYPE=TEXT NAME="Digits" SIZE=2>
</TD>
<TD>
<INPUT TYPE=TEXT NAME="StartWith" SIZE=5>
</TD>
</TR>
<TR>
<TD COLSPAN=2>
<INPUT TYPE=SUBMIT NAME="Calculate" VALUE=" Calculate ">
</TD>
</TR>
<TR>
<TD>
<? if ($Result) echo "<B>Result:</B>"?>
</TD>
<TD>
<?=$Result?>&nbsp;
</TD>
</TR>
</TABLE>
</FORM>

<TABLE WIDTH=300 BORDER=1>
<TR>
<TD><B>Name</B></TD>
<TD><B># of Digits</B></TD>
<TD><B>Starts With</B></TD>
</TR>
<TR>
<TD WIDTH=148>MasterCard</TD>
<TD WIDTH=78>16</TD>
<TD WIDTH=86>51-55</TD>
</TR>
<TR>
<TD WIDTH=148>Visa</TD>
<TD WIDTH=78>13, 16</TD>
<TD WIDTH=86>4</TD>
</TR>
<TR>
<TD WIDTH=148>AmEx</TD>
<TD WIDTH=78>15</TD>
<TD WIDTH=86>34<BR>37</TD>
</TR>
<TR>
<TD WIDTH=148>Diners Club/<BR>Carte Blanche</TD>
<TD WIDTH=78>14</TD>
<TD WIDTH=86>300-305<BR>36<BR>38</TD>
</TR>
<TR>
<TD WIDTH=148>Discover</TD>
<TD WIDTH=78>16</TD>
<TD WIDTH=86>6011</TD>
</TR>
<TR>
<TD WIDTH=148>enRoute</TD>
<TD WIDTH=78>15</TD>
<TD WIDTH=86>2014<BR>2149</TD>
</TR>
<TR>
<TD WIDTH=148>JCB</TD>
<TD WIDTH=78>16</TD>
<TD WIDTH=86>3</TD>
</TR>
<TR>
<TD WIDTH=148>JCB</TD>
<TD WIDTH=78>15</TD>
<TD WIDTH=86>2131<BR>1800</TD>
</TR>
<TR>
<TD WIDTH=148>Delta</TD>
<TD WIDTH=78>16</TD>
<TD WIDTH=86>4</TD>
</TR>
<TR>
<TD WIDTH=148>Switch</TD>
<TD WIDTH=78>16, 18, 19</TD>
<TD WIDTH=86>4-6</TD>
</TR>
</TABLE>

</BODY>
</HTML>


---------------------------------------------------------------------
ccval.php
---------------------------------------------------------------------

<?php
/************************************************************************
*
* CCVal - Credit Card Validation function.
*
* Copyright (c) 1999, 2003 Holotech Enterprises. All rights reserved.
* You may freely modify and use this function for your own purposes. You
* may freely distribute it, without modification and with this notice
* and entire header intact.
*
* This function accepts a credit card number and, optionally, a code for
* a credit card name. If a Name code is specified, the number is checked
* against card-specific criteria, then validated with the Luhn Mod 10
* formula. Otherwise it is only checked against the formula. Valid name
* codes are:
*
* mcd - Master Card
* vis - Visa
* amx - American Express
* dsc - Discover
* dnc - Diners Club
* jcb - JCB
* swi - Switch
* dlt - Delta
* enr - EnRoute
*
* You can also optionally specify an expiration date in the formay mmyy.
* If the validation fails on the date, the function returns 0. If it
* fails on the number validation, it returns false.
*
* A description of the criteria used in this function can be found at
* http://www.paylib.net/ccval.html. If you have any questions or
* comments, please direct them to ccval@holotech.net
*
* Alan Little
* Holotech Enterprises
* http://www.holotech.net/
* August 2003
*
************************************************************************/

function CCVal($Num, $Name = "n/a", $Exp = "") {

// Check the expiration date first
if (strlen($Exp)) {
$Month = substr($Exp, 0, 2);
$Year = substr($Exp, -2);

$WorkDate = "$Month/01/$Year";
$WorkDate = strtotime($WorkDate);
$LastDay = date("t", $WorkDate);

$Expires = strtotime("$Month/$LastDay/$Year 11:59:59");
if ($Expires < time()) return 0;
}

// Innocent until proven guilty
$GoodCard = true;

// Get rid of any non-digits
$Num = ereg_replace("[^0-9]", "", $Num);

// Perform card-specific checks, if applicable
switch ($Name) {

case "mcd" :
$GoodCard = ereg("^5[1-5].{14}$", $Num);
break;

case "vis" :
$GoodCard = ereg("^4.{15}$|^4.{12}$", $Num);
break;

case "amx" :
$GoodCard = ereg("^3[47].{13}$", $Num);
break;

case "dsc" :
$GoodCard = ereg("^6011.{12}$", $Num);
break;

case "dnc" :
$GoodCard = ereg("^30[0-5].{11}$|^3[68].{12}$", $Num);
break;

case "jcb" :
$GoodCard = ereg("^3.{15}$|^2131|1800.{11}$", $Num);
break;

case "dlt" :
$GoodCard = ereg("^4.{15}$", $Num);
break;

case "swi" :
$GoodCard = ereg("^[456].{15}$|^[456].{17,18}$", $Num);
break;

case "enr" :
$GoodCard = ereg("^2014.{11}$|^2149.{11}$", $Num);
break;
}

// The Luhn formula works right to left, so reverse the number.
$Num = strrev($Num);

$Total = 0;

for ($x=0; $x<strlen($Num); $x++) {
$digit = substr($Num,$x,1);

// If it's an odd digit, double it
if ($x/2 != floor($x/2)) {
$digit *= 2;

// If the result is two digits, add them
if (strlen($digit) == 2)
$digit = substr($digit,0,1) + substr($digit,1,1);
}

// Add the current digit, doubled and added if applicable, to the Total
$Total += $digit;
}

// If it passed (or bypassed) the card-specific check and the Total is
// evenly divisible by 10, it's cool!
if ($GoodCard && $Total % 10 == 0) return true; else return false;
}
?>

----------------------------------------------------------------------------------
example.php
----------------------------------------------------------------------------------

<?
include("ccval.php");

$Number = "5105 1051 0510 5100"; // MasterCard test number
$Type = "mcd";
$Exp = "0810";

$Result = ccval($Number, $Type, $Exp);
if ($Result) {
echo "Good Card.";
}
else {
if (is_int($Result)) echo "Bad Date.";
else echo "Bad Number";
}
?>
User avatar
Oliver Georgi
Site Admin
Posts: 9919
Joined: Fri 3. Oct 2003, 22:22
Contact:

Post by Oliver Georgi »

Thanks for this info.

But please. Never use longer codes than some lines. It's absolutely confusing to have so many lines of code.

I will allow attachements for such code stuff - that's easier. Put everything into one compressed archive and upload or put a link inside your post.
[UPDATE] No attachements - I have to install a mod into phpBB and have no time to do it.

Thank you
Oliver
Oliver Georgi | phpwcms Developer | GitHub | LinkedIn | Систрон
Locked