Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kayalshri
Created April 25, 2014 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kayalshri/11288734 to your computer and use it in GitHub Desktop.
Save kayalshri/11288734 to your computer and use it in GitHub Desktop.
Credit Card Validation
<?php
function checkSum($ccnum) {
$checksum = 0;
for ($i=(2-(strlen($ccnum) % 2)); $i<=strlen($ccnum); $i+=2)
{
$checksum += (int)($ccnum{$i-1});
}
for ($i=(strlen($ccnum)% 2) + 1; $i<strlen($ccnum); $i+=2)
{
$digit = (int)($ccnum{$i-1}) * 2;
if ($digit < 10) {
$checksum += $digit;
}
else {
$checksum += ($digit-9);
}
}
if (($checksum % 10) == 0)
return true;
else
return 0;
}
function checkExpDate($month,$year) {
$expTs = mktime(0, 0, 0, $month + 1, 1, $year);
$curTs = time();
$maxTs = $curTs + (10 * 365 * 24 * 60 * 60);
if ($expTs > $curTs && $expTs < $maxTs) {
return true;
} else {
return 0;
}
}
function validateCVV($type,$cvv) {
$count = ($type === 'amex') ? 4 : 3;
if(preg_match('/^[0-9]{'.$count.'}$/', $cvv)) {
return true;
}else {
return 0;
}
}
function isValidCardNumber($cardNumber,$month,$year,$cvv) {
$creditcard = array( "Visa"=>"/^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/",
"Mastercard"=>"/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/",
"Discover"=>"/^6011-?\d{4}-?\d{4}-?\d{4}$/",
"Amex"=>"/^3[4,7]\d{13}$/",
//"diners"=>"/^3[0,6,8]\d{12}$/",
//"bankcard"=>"/^5610-?\d{4}-?\d{4}-?\d{4}$/",
//"jcb"=>"/^[3088|3096|3112|3158|3337|3528]\d{12}$/",
//"enroute"=>"/^[2014|2149]\d{11}$/",
"Switch"=>"/^[4903|4911|4936|5641|6333|6759|6334|6767]\d{12}$/");
$match=false;
foreach($creditcard as $type=>$pattern) {
if(preg_match($pattern,$cardNumber) === 1) {
$match=true;
$cardType = $type;
break;
}
}
if(!$match) {
//return "Invalid card number. Please check credit card number";
return "Fail";
}
else {
if((checkSum($cardNumber)==true) and (checkExpDate($month,$year)==true) and (validateCVV($cardType,$cvv)==true)){
return "Success";
}else{
return "Fail";
}
}
}
function isValidCardType($cardNumber) {
$creditcard = array( "Visa"=>"/^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/",
"Mastercard"=>"/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/",
"Discover"=>"/^6011-?\d{4}-?\d{4}-?\d{4}$/",
"Amex"=>"/^3[4,7]\d{13}$/",
//"diners"=>"/^3[0,6,8]\d{12}$/",
//"bankcard"=>"/^5610-?\d{4}-?\d{4}-?\d{4}$/",
//"jcb"=>"/^[3088|3096|3112|3158|3337|3528]\d{12}$/",
//"enroute"=>"/^[2014|2149]\d{11}$/",
"Switch"=>"/^[4903|4911|4936|5641|6333|6759|6334|6767]\d{12}$/");
$match=false;
foreach($creditcard as $type=>$pattern) {
if(preg_match($pattern,$cardNumber) === 1) {
$match=true;
$cardType = $type;
break;
}
}
if(!$match) {
//return "Invalid card number. Please check credit card number";
return "Plain";
}
else {
return $cardType;
}
}
$validation_type = (@$_GET['vtype']) ? : die("Invalid Type");
$credit_card_number = (@$_GET['cval']) ? : die("Plain");
$month = (@$_GET['month']) ? : "0";
$months = explode("/", $month);
$cvv = (@$_GET['cvv']) ? : "0";
if ($validation_type == 'full'){
echo isValidCardNumber($credit_card_number,$months[0],$months[1],$cvv);
}else{
echo isValidCardType($credit_card_number);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment