RE: [PHP] Creditcard checksum and identification

2001-08-22 Thread Jason Murray

Here's what I pulled from px.sklar.com and modified to suit my own 
devious ends... :)

http://www.analysisandsolutions.com/code/ccvs-ph.htm
 // Checks the 1st 4 digits for validity, then checks
 // that the card number is the right length, and checks
 // the mod10 test, and finally checks that the card type
 // is the card type the user input.
 // Returns an array:
 //  - [success] 0 or 1 for a valid card
 //  - [reason] text explaining why the card's not valid
 //  - [callifvalid] redundant and not used.

 // Step 1 - Get rid of spaces and non-numeric characters.
 $Number = ereg_replace("[^0-9]", "", $Number);
 
 // Step 2 - Do the first four digits fit within proper ranges?
 //  If so, who's the card issuer and how long should the number
be?
 $NumberLeft = substr($Number, 0, 4);
 $NumberLength = strlen($Number);
 
 if ($NumberLeft >= 3000 and $NumberLeft <= 3059) 
 {
   $CardName = "Diners Club";
   $checkcard = "DINERS";
   $ShouldLength = 14;
 } 
 elseif ($NumberLeft >= 3600 and $NumberLeft <= 3699) 
 {
   $CardName = "Diners Club";
   $checkcard = "DINERS";
   $ShouldLength = 14;
 } 
 elseif ($NumberLeft >= 3800 and $NumberLeft <= 3889) 
 {
   $CardName = "Diners Club";
   $checkcard = "DINERS";
   $ShouldLength = 14;
 } 
 elseif ($NumberLeft >= 3400 and $NumberLeft <= 3499) 
 {
   $CardName = "American Express";
   $checkcard = "AMEX";
   $ShouldLength = 15;
 } 
 elseif ($NumberLeft >= 3700 and $NumberLeft <= 3799) 
 {
   $CardName = "American Express";
   $checkcard = "AMEX";
   $ShouldLength = 15;
 } 
 //elseif ($NumberLeft >= 3528 and $NumberLeft <= 3589) 
 //{
 //$CardName = "JCB";
 //$ShouldLength = 16;
 //} 
 //elseif ($NumberLeft >= 3890 and $NumberLeft <= 3899) 
 //{
 //$CardName = "Carte Blache";
 //$ShouldLength = 14;
 //} 
 elseif ($NumberLeft >= 4000 and $NumberLeft <= 4999) 
 {
   $CardName = "Visa";
   $checkcard = "VISA";
   if ($NumberLength > 14) 
   {
 $ShouldLength = 16;
   } 
   elseif ($NumberLength < 14) 
   {
 $ShouldLength = 13;
   } 
   else 
   {
 $debug .= "The Visa number entered, $Number, in is 14 \n"
  ."digits long.Visa cards usually have 16 digits, \n"
  ."though some have 13.Please check the number and
\n"
  ."try again.\n";
 $result = 0;
 $reason = "Visa cards have either 16 or 13 digits";
   }
 } 
 elseif ($NumberLeft >= 5100 and $NumberLeft <= 5599) 
 {
   $CardName = "MasterCard";
   $checkcard = "MASTERCARD";
   $ShouldLength = 16;
 } 
 elseif ($NumberLeft == 5610) 
 {
 $CardName = "Australian BankCard";
 $checkcard = "BANKCARD";
 $ShouldLength = 16;
 } 
 //elseif ($NumberLeft == 6011) 
 //{
 //$CardName = "Discover/Novus";
 //$ShouldLength = 16;
 //} 
 else 
 {
   $debug .= "The first four digits of the number entered are
\n"
."$NumberLeft. If that's correct, we don't accept that
\n"
."type of credit card.If it's wrong, please try
again.\n"
."\n";
   //return FALSE;
   $result = 0;
   $reason = "Your card number was not recognised as valid";
   $callifvalid = 1;
 }
 
 
 // Step 3 - Is the number the right length?
 if ($NumberLength <> $ShouldLength) 
 {
   $Missing = $NumberLength - $ShouldLength;
   if ($Missing < 0) 
   {
 $debug .= "The $CardName number entered, $Number, is
missing \n"
  .abs($Missing)." digit(s).Please check the number
and \n"
  ."try again.\n";
   } 
   else 
   {
 $debug .= "The $CardName number entered, $Number, has \n"
  ."$Missing too many digit(s).Please check the number
\n"
  ."and try again.\n";
   }
   $result = 0;
   $failed = 1;
   $reason = "Your card number has the wrong number of digits";
 }
 
 
 // Step 4 - Does the number pass the Mod 10 Algorithm Checksum?
 if (ccVerifyMod10($Number) == TRUE)
 {
   $debug .= "Passed mod10";
   $result = 1;
 } 
 else 
 {
   $debug .= "The $CardName number entered, $Number, is \n"
."invalid.Please check the number and try
again.\n";
   $failed = 1;
   $result = 0;
   $reason = "Your card number is invalid";
 }
 if ($failed)
 { $result = 0; }
 
 // Step 5 - Does the card type match the user's selected card type?
 if ($creditcardtype)
 {
   if ($creditcardtype != $checkcard)
   {
 $result = 0;
 $reason = "Your card number and card type don't match";
   }
 }
 
 $return[result] = $result;
  

Re: [PHP] Creditcard checksum and identification

2001-08-22 Thread Egan

On Thu, 23 Aug 2001 00:22:28 +0200, Peter Ostry <[EMAIL PROTECTED]> wrote:

>Has anyone a PHP Script for the checksum of Creditcards?
>And a Script which can identify card types based on their first numbers?
>(VISA, AMEX, MASTER, DINERS, JBC, DISCOVERY)
>
>There where a lot of JavaScript pages on the web some years ago, but I 
>have lost my local copies and the pages are vanished...
>
>
>TIA,
>Peter

Here is what I use for Visa, MasterCard, AMEX, and Discover. Feel free
to improve it by adding other card types:



function test_mod10 ($mycard) {

$mylen = strlen($mycard);
$sumdigits = 0;

// Add doubled digits from odd/even positions in even/odd length
for ($tx = ($mylen % 2); $tx < $mylen; $tx += 2) {
$digit = substr($mycard, $tx, 1) * 2;
if ($digit < 10) {
$sumdigits += $digit;
} else {
$sumdigits += $digit - 9;
}
}

// Add single digits from even/odd postions in even/odd length
for ($tx = 1 - ($mylen % 2); $tx < $mylen; $tx += 2) {
$digit = substr($mycard, $tx, 1);
$sumdigits += $digit;
}

// If divisible by 10, return true
return (($sumdigits % 10) == 0);
}


$cn = $HTTP_GET_VARS['cn'];

$tn = substr(ereg_replace("[^0-9]", "", $cn) , 0, 25);

$vl = strlen($tn);
$v1 = substr($tn, 0, 1);
$v2 = substr($tn, 0, 2);
$v4 = substr($tn, 0, 4);

$ep_cn = '';

if ($ct == 'V') {
if (($vl != 13 && $vl != 16) || $v1 != 4 || !(test_mod10($tn))) {
$ep_cn = "$cn is not a Visa card.";
}

} elseif ($ct == 'M') {
if ($vl != 16 || $v1 != 5 || !(test_mod10($tn))) {
$ep_cn = "$cn is not a MasterCard.";
}

} elseif ($ct == 'A') {
if ($vl != 15 || $v2 != 37 || !(test_mod10($tn))) {
$ep_cn = "$cn is not an AMEX card.";
}

} elseif ($ct == 'D') {
if ($vl != 16 || !($v4 == 6100) || !(test_mod10($tn))) {
$ep_cn = "$cn is not a Discover card.";
}
}



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




[PHP] Creditcard checksum and identification

2001-08-22 Thread Peter Ostry

Has anyone a PHP Script for the checksum of Creditcards?
And a Script which can identify card types based on their first numbers?
(VISA, AMEX, MASTER, DINERS, JBC, DISCOVERY)

There where a lot of JavaScript pages on the web some years ago, but I 
have lost my local copies and the pages are vanished...


TIA,
Peter

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]