PHP
  Home arrow PHP arrow Page 4 - Creating a Credit Card Validation Class Wi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Creating a Credit Card Validation Class With PHP
By: David Rusik
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 36
    2002-02-25

    Table of Contents:
  • Creating a Credit Card Validation Class With PHP
  • Credit card validation
  • Creating the CCreditCard class
  • Creating the CCreditCard class (contd.)
  • Using our CCreditCard class
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More PHP Articles
    More By David Rusik


     

    PHP ARTICLES

    - 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
    - PHP Frontend to ImageMagick
    - Using PEAR's mimeDecode Module
    - Incoming Mail and PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT