Senior & Expert PHP Developers Discussion Forum by Nyros Technologies

HIRE PHP Expert Developers Programmers Coders From India
PHP .Net Ruby on Rails Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

Credit Card Validation Script

 
Post new topic   Reply to topic    Senior & Expert PHP Developers Discussion Forum by Nyros Technologies Index -> Credit Cards
View previous topic :: View next topic  
Author Message
nyros
Site Admin


Joined: 09 Jul 2007
Posts: 49

PostPosted: Thu Sep 27, 2007 5:07 am    Post subject: Credit Card Validation Script Reply with quote

Below is the credit card validation script, which checks:

1. A number has been provided
2. The number is a right length for the card
3. The number has an appropriate prefix for the card
4. The number has a valid modulus 10 number check digit if required

If the validation fails an error is reported.


<?
function checkCreditCard ($cardnumber, $cardname, &$errornumber, &$errortext) {

// Define the cards we support. You may add additional card types.

// Name: As in the selection box of the form - must be same as user's
// Length: List of possible valid lengths of the card number for the card
// prefixes: List of possible prefixes for the card
// checkdigit Boolean to say whether there is a check digit

// Don't forget - all but the last array definition needs a comma separator!

$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',
'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
$cardType = -1;
for ($i=0; $i<sizeof($cards); $i++) {

// See if it is this card (ignoring the case of the string)
if (strtolower($cardname) == strtolower($cards[$i]['name'])) {
$cardType = $i;
break;
}
}

// If card type not found, report an error
if ($cardType == -1) {
$errornumber = 0;
$errortext = $ccErrors [$errornumber];
return false;
}

// Ensure that the user has provided a credit card number
if (strlen($cardnumber) == 0) {
$errornumber = 1;
$errortext = $ccErrors [$errornumber];
return false;
}

// Remove any spaces from the credit card number
$cardNo = str_replace (' ', '', $cardnumber);

// Check that the number is numeric and of the right sort of length.
if (!eregi('^[0-9]{13,19}$',$cardNo)) {
$errornumber = 2;
$errortext = $ccErrors [$errornumber];
return false;
}

// Now check the modulus 10 check digit - if required
if ($cards[$cardType]['checkdigit']) {
$checksum = 0; // running checksum total
$mychar = ""; // next char to process
$j = 1; // takes value of 1 or 2

// Process each digit one by one starting at the right
for ($i = strlen($cardNo) - 1; $i >= 0; $i--) {

// Extract the next digit and multiply by 1 or 2 on alternative digits.
$calc = $cardNo{$i} * $j;

// If the result is in two digits add 1 to the checksum total
if ($calc > 9) {
$checksum = $checksum + 1;
$calc = $calc - 10;
}

// Add the units element to the checksum total
$checksum = $checksum + $calc;

// Switch the value of j
if ($j ==1) {$j = 2;} else {$j = 1;};
}

// All done - if checksum is divisible by 10, it is a valid modulus 10.
// If not, report an error.
if ($checksum % 10 != 0) {
$errornumber = 3;
$errortext = $ccErrors [$errornumber];
return false;
}
}

// The following are the card-specific checks we undertake.

// Load an array with the valid prefixes for this card
$prefix = split(',',$cards[$cardType]['prefixes']);

// Now see if any of them match what we have in the card number
$PrefixValid = false;
for ($i=0; $i<sizeof($prefix); $i++) {
$exp = '^' . $prefix[$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<sizeof($lengths); $j++) {
if (strlen($cardNo) == $lengths[$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;
}
?>
Back to top
View user's profile Send private message Send e-mail
pastcow



Joined: 05 Oct 2009
Posts: 1

PostPosted: Mon Oct 05, 2009 10:14 am    Post subject: Reply with quote

Its the Luhn algorithm thats used to check the card numbers, the prefixes you provide are only a small sample of the many that are available - if you goto wikipedia you can see a complete list of the card prefixes. http://en.wikipedia.org/wiki/Bank_card_number
_________________
My
Back to top
View user's profile Send private message
michelle2010



Joined: 24 Dec 2009
Posts: 1

PostPosted: Thu Dec 24, 2009 12:34 pm    Post subject: Reply with quote

Thanks for the credit card validation script. But I saw a company (paymentmax.com) which checks:complete list of the card prefixes.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert PHP Developers Discussion Forum by Nyros Technologies Index -> Credit Cards
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts About foxycart
0 venkatadapa 5 Fri Jul 30, 2010 10:02 am
venkatadapa View latest post
No new posts How to change the mysql storage engine from MyISAM to innodb
1 aababu 10 Fri Jul 30, 2010 9:57 am
venkatadapa View latest post
No new posts How can we increase the execution time of a php script?
0 samir 6 Fri Jul 30, 2010 4:15 am
samir View latest post
No new posts Get no of days between two dates & current dates using M
0 samir 4 Fri Jul 30, 2010 4:09 am
samir View latest post
No new posts What are the different types of errors in php
0 samir 6 Fri Jul 30, 2010 4:00 am
samir View latest post
No new posts How to remove a value in an array using javascript
0 samir 10 Thu Jul 29, 2010 7:27 am
samir View latest post
No new posts Agile methodology plays vital role in software development
0 venkatadapa 7 Thu Jul 29, 2010 7:20 am
venkatadapa View latest post
No new posts How to get complete information about $_SERVER variable
0 Sanjana 15 Wed Jul 28, 2010 6:58 am
Sanjana View latest post
No new posts DNL Reader for php website
0 Mike 22 Mon Jul 26, 2010 12:33 pm
Mike View latest post
No new posts Use mysql_real_escape_string() function to avoid mysql injec
0 venkatadapa 26 Mon Jul 26, 2010 11:05 am
venkatadapa View latest post





Hire an expert PHP developer / coder / programmer or development team from India now!!

Other Forums : Ruby on Rails   ::   .Net   |   Free unlimited HTML CSS Joomla Wordpress Drupal templates download

Nyros Technologies   |   Developers Blog   |   Kakinada City Portal   |   About PHP Experts   |   More