Validatore di carte di credito con php
Posted on 24. Apr, 2009 by Administrator in php, tutorials
< ?
if( checkCreditCard ("40236007878787", "Visa", &$errornumber, &$errortext))
echo "OK";
else echo"MO";
function checkCreditCard ($cardnumber, $cardname, &$errornumber, &$errortext) {
$cards = array ( array ('name' => 'American Express',
'length' => '15',
'prefixes' => '34,37',
'checkdigit' => true
),
array ('name' => 'Carte Blanche',
'length' => '14',
'prefixes' => '300,301,302,303,304,305,36,38',
'checkdigit' => true
),
array ('name' => 'Diners Club',
'length' => '14',
'prefixes' => '300,301,302,303,304,305,36,38',
'checkdigit' => true
),
array ('name' => 'Discover',
'length' => '16',
'prefixes' => '6011',
'checkdigit' => true
),
array ('name' => 'Enroute',
'length' => '15',
'prefixes' => '2014,2149',
'checkdigit' => true
),
array ('name' => 'JCB',
'length' => '15,16',
'prefixes' => '3,1800,2131',
'checkdigit' => true
),
array ('name' => 'Maestro',
'length' => '16,18',
'prefixes' => '5020,6',
'checkdigit' => true
),
array ('name' => 'MasterCard',
'length' => '16',
'prefixes' => '51,52,53,54,55',
'checkdigit' => true
),
array ('name' => 'Solo',
'length' => '16,18,19',
'prefixes' => '6334,6767',
'checkdigit' => true
),
array ('name' => 'Switch',
'length' => '16,18,19',
'prefixes' => '4903,4905,4911,4936,564182,633110,6333,6759',
'checkdigit' => true
),
array ('name' => 'Visa',
'length' => '13,16',
'prefixes' => '4',
'checkdigit' => true
),
array ('name' => 'Visa Electron',
'length' => '16',
'prefixes' => '417500,4917,4913',
'checkdigit' => true
)
);
$ccErrorNo = 0;
$ccErrors [0] = "Unknown card type";
$ccErrors [1] = "No card number provided";
$ccErrors [2] = "Credit card number has invalid format";
$ccErrors [3] = "Credit card number is invalid";
$ccErrors [4] = "Credit card number is wrong length";
// Establish card type // See if it is this card (ignoring the case of the string) // If card type not found, report an error // Ensure that the user has provided a credit card number // Remove any spaces from the credit card number // Check that the number is numeric and of the right sort of length. // Now check the modulus 10 check digit - if required // Process each digit one by one starting at the right // Extract the next digit and multiply by 1 or 2 on alternative digits. // If the result is in two digits add 1 to the checksum total // Add the units element to the checksum total // Switch the value of j // All done - if checksum is divisible by 10, it is a valid modulus 10. // The following are the card-specific checks we undertake. // Load an array with the valid prefixes for this card // Now see if any of them match what we have in the card number
$cardType = -1;
for ($i=0; $i
if (strtolower($cardname) == strtolower($cards[$i]['name'])) {
$cardType = $i;
break;
}
}
if ($cardType == -1) {
$errornumber = 0;
$errortext = $ccErrors [$errornumber];
return false;
}
if (strlen($cardnumber) == 0) {
$errornumber = 1;
$errortext = $ccErrors [$errornumber];
return false;
}
$cardNo = str_replace (' ', '', $cardnumber);
if (!eregi('^[0-9]{13,19}$',$cardNo)) {
$errornumber = 2;
$errortext = $ccErrors [$errornumber];
return false;
}
if ($cards[$cardType]['checkdigit']) {
$checksum = 0; // running checksum total
$mychar = ""; // next char to process
$j = 1; // takes value of 1 or 2
for ($i = strlen($cardNo) - 1; $i >= 0; $i--) {
$calc = $cardNo{$i} * $j;
if ($calc > 9) {
$checksum = $checksum + 1;
$calc = $calc - 10;
}
$checksum = $checksum + $calc;
if ($j ==1) {$j = 2;} else {$j = 1;};
}
// If not, report an error.
if ($checksum % 10 != 0) {
$errornumber = 3;
$errortext = $ccErrors [$errornumber];
return false;
}
}
$prefix = split(',',$cards[$cardType]['prefixes']);
$PrefixValid = false;
for ($i=0; $i
if (ereg($exp,$cardNo)) {
$PrefixValid = true;
break;
}
}
// If it isn't a valid prefix there's no point at looking at the length
if (!$PrefixValid) {
$errornumber = 3;
$errortext = $ccErrors [$errornumber];
return false;
}
// See if the length is valid for this card
$LengthValid = false;
$lengths = split(',',$cards[$cardType]['length']);
for ($j=0; $j
$LengthValid = true;
break;
}
}
// See if all is OK by seeing if the length was valid.
if (!$LengthValid) {
$errornumber = 4;
$errortext = $ccErrors [$errornumber];
return false;
};
// The credit card is in the required format.
return true;
}
?>




Leave a reply