Home arrow PHP arrow Page 3 - 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
(Page 3 of 6 )

Let's now create a PHP class that we can use to store and validate the details of a credit card. Our class will be able to hold the cardholders name, the card type (mastercard, visa, etc), the card number, and the expiry month and date.

Create a new PHP file called class.creditcard.php. As we walk through the following two pages, copy-paste each piece of code shown to the file and save it.

We start of by defining several card type constants. These values will be used to represent the type of card that our class will be validating:

<?php

define("CARD_TYPE_MC", 0);

define("CARD_TYPE_VS", 1);

define("CARD_TYPE_AX", 2);

define("CARD_TYPE_DC", 3);

define("CARD_TYPE_DS", 4);

define("CARD_TYPE_JC", 5);


Next, we have our class declaration. Our class is called CCreditCard. Note that there is an extra 'C' at the front of the class name intentionally: it's a common programming practice to prefix the name of a class with 'C' to in fact indicate that it is a class.

We also define five member variables, which will be used internally to hold the credit cards name, type, number, expiry month and year respectively:

class CCreditCard

{

// Class Members

var $__ccName = '';

var $__ccType = '';

var $__ccNum = '';

var $__ccExpM = 0;

var $__ccExpY = 0;


Next we have our class's custom constructor. A constructor is a function that has the same names as the class in which it exists. It accepts no arguments and returns no values. It is special in a sense that it is automatically executed whenever we create a new instance of that class.

Whenever we want to create a new instance of our CCreditCard class, we must explicitly pass in five arguments to its constructor: the cardholders name, card type, number, and expiry date. Because we have created our own custom constructor (PHP implements a default constructor that accepts no arguments if we donít explicitly create one), we must pass in values for each of these five arguments every time we instantiate the class. If we omit them then PHP will raise an error.

// Constructor

function CCreditCard($name, $type, $num, $expm, $expy)

{


If the value of the $name variable passed into the constructor is empty, then we use the die() function to terminate the instantiation of our class and output an error message telling the user that they must pass a name to the constructor:

// Set member variables

if(!empty($name))

{

$this->__ccName = $name;

}

else

{

die('Must pass name to constructor');

}


To make our CCreditCard class flexible, it accepts several different ways to specify the type of card that is being stored. For example, if we want to add the details of a mastercard to a new instance of our CCreditCard class, then we could pass in the following values for the $type variable of the constructor: "mc", "mastercard", "m", or "1".

We make sure that a valid card type has been passed in, and set the value of our classes $__ccType variable to one of the constant card type values that we defined earlier:

// Make sure card type is valid

switch(strtolower($type))

{

case 'mc':

case 'mastercard':

case 'm':

case '1':

{

$this->__ccType = CARD_TYPE_MC;

break;

}

case 'vs':

case 'visa':

case 'v':

case '2':

{

$this->__ccType = CARD_TYPE_VS;

break;

}

case 'ax':

case 'american express':

case 'a':

case '3':

{

$this->__ccType = CARD_TYPE_AX;

break;

}

case 'dc':

case 'diners club':

case '4':

{

$this->__ccType = CARD_TYPE_DC;

break;

}

case 'ds':

case 'discover':

case '5':

{

$this->__ccType = CARD_TYPE_DS;

break;

}

case 'jc':

case 'jcb':

case '6':

{

$this->__ccType = CARD_TYPE_JC;

break;

}

default:

{

die('Invalid type ' . $type . ' passed to constructor');

}

}


If an invalid card type is passed in, then the default branch of our switch statement will be called, resulting in our script terminating with the die() function.

We can take advantage of PHP's built-in support for regular expressions by using the ereg_replace function to strip out all non-numeric characters from the credit card number:

// Don't check the number yet,

// just kill all non numerics

if(!empty($num))

{

$cardNumber = ereg_replace("[^0-9]", "", $num);

// Make sure the card number isnt empty

if(!empty($cardNumber))

{

$this->__ccNum = $cardNumber;

}

else

{

die('Must pass number to constructor');

}

}

else

{

die('Must pass number to constructor');

}


We finish off our CCreditCard constructor by making sure that both the expiry month and year are valid, numerical values:

if(!is_numeric($expm) || $expm < 0 || $expm > 12)

{

die('Invalid expiry month of ' . $expm . ' passed to constructor');

}

else

{

$this->__ccExpM = $expm;

}

// Get the current year

$currentYear = date('Y');

settype($currentYear, 'integer');

if(!is_numeric($expy) || $expy < $currentYear || $expy > $currentYear + 10)

{

die('Invalid expiry year of ' . $expy . ' passed to constructor');

}

else

{

$this->__ccExpY = $expy;

}

}

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