Home arrow PHP arrow Page 4 - Creating a Credit Card Validation Class With PHP
PHP

Creating a Credit Card Validation Class With PHP


One of the most important functions of any eCommerce web site is credit card number storage and validation. In this article David develops a PHP class that stores the details of a credit card and validates its number using the Mod 10 algorithm.

Author Info:
By: David Rusik
Rating: 5 stars5 stars5 stars5 stars5 stars / 48
February 25, 2002
TABLE OF CONTENTS:
  1. · Creating a Credit Card Validation Class With PHP
  2. · Credit card validation
  3. · Creating the CCreditCard class
  4. · Creating the CCreditCard class (contd.)
  5. · Using our CCreditCard class
  6. · Conclusion

print this article
SEARCH DEVARTICLES

Creating a Credit Card Validation Class With PHP - Creating the CCreditCard class (contd.)
(Page 4 of 6 )

In our CCreditCard class, the only way to set the values of the credit cards details is through the constructor. To actually retrieve the values of our class specific variables ($__ccName, $__ccType, etc), we create several functions, like this:

function Name()

{

return $this->__ccName;

}

function Type()

{

switch($this->__ccType)

{

case CARD_TYPE_MC:

{

return 'mastercard [1]';

break;

}

case CARD_TYPE_VS:

{

return 'Visa [2]';

break;

}

case CARD_TYPE_AX:

{

return 'Amex [3]';

break;

}

case CARD_TYPE_DC:

{

return 'Diners Club [1]';

break;

}

case CARD_TYPE_DS:

{

return 'Discover [5]';

break;

}

case CARD_TYPE_JC:

{

return 'JCB [6]';

break;

}

default:

{

return 'Unknown [-1]';

}

}

function Number()

{

return $this->__ccNum;

}

function ExpiryMonth()

{

return $this->__ccExpM;

}

function ExpiryYear()

{

return $this->__ccExpY;

}


These functions allow us to retrieve the values of the variables contained within our class. For example, If I created an instance of our CCreditCard class called $cc1, then I could retrieve its expiration month using $cc1->ExpiryMonth().

A common function when working with credit cards is displaying the details that you've captured from that user back to them as a confirmation. For example, if the user entered a credit card number of 4111111111111111, then you might want to only show part of the number to them, such as 4111111111111xxxx. Our CCreditCard class contains a function called SafeNumber, which accepts two arguments. The first is the character to mask the digits with, and the second is the number of digits to mask (from the right):

function SafeNumber($char = 'x', $numToHide = 4)

{

// Return only part of the number

if($numToHide < 4)

{

$numToHide = 4;

}

if($numToHide > 10)

{

$numToHide = 10;

}

$cardNumber = $this->__ccNum;

$cardNumber = substr($cardNumber, 0, strlen($cardNumber) - $numToHide);

for($i = 0; $i < $numToHide; $i++)

{

$cardNumber .= $char;

}

return $cardNumber;

}


If we had an instance of our CCreditCard class called $cc1 and the credit card number stored in this class was 4242424242424242, then we could mask the last 6 digits like this: echo $cc1->SafeNumber('x', 6).

The last function contained in our CCreditCard class is called IsValid, and implements the Mod 10 algorithm against the credit card number of our class, returning true/false.

It starts of by setting two variables ($validFormat and $passCheck) to false:

function IsValid()

{

// Not valid by default

$validFormat = false;

$passCheck = false;


Next we make sure that the credit card number is formatted correctly. We use PHP's ereg function to do this. The regular expression that must be matched is different for each card:

// Is the number in the correct format?

switch($this->__ccType)

{

case CARD_TYPE_MC:

{

$validFormat = ereg("^5[1-5][0-9]{14}$", $this->__ccNum);

break;

}

case CARD_TYPE_VS:

{

$validFormat = ereg("^4[0-9]{12}$|^4[0-9]{15}$", $this->__ccNum);

break;

}

case CARD_TYPE_AX:

{

$validFormat = ereg("^3[4|7][0-9]{13}$", $this->__ccNum);

break;

}

case CARD_TYPE_DC:

{

$validFormat = ereg("^30[0-5][0-9]{11}$|^3[6|8][0-9]{12}$", $this->__ccNum);

break;

}

case CARD_TYPE_DS:

{

$validFormat = ereg("^6011[0-9]{12}$", $this->__ccNum);

break;

}

case CARD_TYPE_JC:

{

$validFormat = ereg("^3[0-9]{15}$|^[2131|1800][0-9]{11}$", $this->__ccNum);

break;

}

default:

{

// Should never be executed

$validFormat = false;

}

}


At this point, $validFormat will be true (ereg returns true/false) if the credit card number is in the correct format, and false if it's not.

We now implement a PHP version of the Mod 10 algorithm, using exactly the same steps that we described earlier:

// Is the number valid against luhn?

$cardNumber = strrev($this->__ccNum);

$numSum = 0;

for($i = 0; $i < strlen($cardNumber); $i++)

{

$currentNum = substr($cardNumber, $i, 1);

if(floor($currentNum / 2) != $currentNum / 2)

{

$currentNum *= 2;

}

if(strlen($currentNum) == 2)

{

$firstNum = substr($currentNum, 0, 1);

$secondNum = substr($currentNum, 1, 1);

$currentNum = $firstNum + $secondNum;

}

$numSum += $currentNum;

}


The $numSum variable will contain the sum of all of the variables from step two of the Mod 10 algorithm, which we described earlier. PHP's symbol for the modulus operator is '%', so we assign true/false to the $passCheck variable, depending on whether or not $numSum has a modulus of zero:

// If the total has no remained its OK

$passCheck = ($numSum % 10 == 0 ? true : false);


If both $validFormat and $passCheck are true, then we return true, indicating that the card number is valid. If not, we return false, indicating that either the card number was in an incorrect format, or if failed the Mod 10 check:

if($validFormat && $passCheck)

{

return true;

}

else

{

return false;

}

}

}

?>


And that's all there is to our CCreditCard class! Let's now look at a simple validation example using HTML forms, PHP, and an instance of our CCreditCard class.
blog comments powered by Disqus
PHP ARTICLES

- Removing Singletons in PHP
- Singletons in PHP
- Implement Facebook Javascript SDK with PHP
- Making Usage Statistics in PHP
- Installing PHP under Windows: Further Config...
- File Version Management in PHP
- Statistical View of Data in a Clustered Bar ...
- Creating a Multi-File Upload Script in PHP
- Executing Microsoft SQL Server Stored Proced...
- Code 10x More Efficiently Using Data Access ...
- A Few Tips for Speeding Up PHP Code
- The Modular Web Page
- Quick E-Commerce with PHP and PayPal
- Regression Testing With JMeter
- Building an Iterator with PHP

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials