?
Solved

cc validation class

Posted on 2005-02-25
2
Medium Priority
?
243 Views
Last Modified: 2013-12-12
Hi,

Ive tried many different credit card validation classes and cant seem to get one that works for me for some reason. Its probabbly me doing something completely stupid in all of them, im not sure to be honest!

Here is the code for one that im assured will work:

function validate_cc($argNum)
{
    $cc = str_replace(" ", "", $argNum);
    $prefix = substr($cc, 0, 1);
   
    /* PREFIX TEST */
    switch ($prefix) {
        case "1":
            if (substr($cc, 0, 4) == "1800") {
                $valid = "jcb1";
            } else {
                $valid = false;
            }
            break;
        case "2":
            if (substr($cc, 0, 4) == "2014" || substr($cc, 0, 4) == "2149")    {
                $valid = "enRoute";
            } elseif (substr($cc, 0, 4) == "2131") {
                $valid = "jcb2";
            } else {
                $valid = false;
            }
            break;
        case "3":
            if (substr($cc, 0, 3) > 299 && substr($cc, 0, 3) < 306) {
                $valid = "diners";
            } elseif (substr($cc, 0, 2) == 36 || substr($cc, 0, 2) == 38) {
                $valid = "diners";
            } elseif (substr($cc, 0, 2) == 34 || substr($cc, 0, 2) == 37) {
                $valid = "amex";
            } else {
                $valid = "jcb3";
            }
            break;
        case "4":
            $valid = "visa";
            break;
        case "5":
            if (substr($cc, 0, 2) > 50 && substr($cc, 0, 2) < 56) {
                $valid = "mc";
            }
            break;
        case "6":
            if (substr($cc, 0, 4) == "6011") {
                $valid = "discover";
            } else {
                $valid = false;
            }
            break;
    }

    /* LENGTH TEST */
    if ($valid) {
        switch ($valid) {
            case "mc":
            case "disocver":
            case "jcb3":
                if (strlen($cc) <> 16) {
                    $valid = false;
                }
                break;
            case "amex":
            case "enRoute":
            case "jcb1":
            case "jcb2":
                if (strlen($cc) <> 15) {
                    $valid = false;
                }
                break;
            case "diners":
                if (strlen($cc) <> 14) {
                    $valid = false;
                }
           
                break;
            case "visa":
                if (strlen($cc) != 13 || strlen($cc) != 16)    {
                    $valid = false;
                }
                break;
        }
    }

    /* MOD10 TEST */
    if ($valid)    {
        switch ($valid)    {
            case "mc":
            case "visa":
            case "amex":
            case "diners":
            case "discover":
            case "jcb1":
            case "jcb2":
            case "jcb3":
                for ($i = strlen($cc) - 2; $i >= 0; $i = $i - 2) {
                    $odds[] = (int) $cc{$i};
                    $cc{$i} = "%";
                }
                $cc = str_replace("%", "", $cc);

                foreach ($odds as $key => $value) {
                    $oddnumber = $value * 2;
                    $i = 0;
                    $oddnumber = (string) $oddnumber;
                    do {
                        $total = $total + (int) $oddnumber{$i};
                        $i++;
                    } while ($oddnumber{$i});
                    $numbers[] = (int) $total;
                    unset($total);
                }
                $i = 0;

                while ($i <= (strlen($cc) - 1)) {
                    $numbers[] = (int) $cc{$i};
                    $i++;
                }

                foreach ($numbers as $key => $value) {
                    $total = $total + (int) $value;
                }
               
                if ($total % 10 !== 0)    {
                    $valid = false;
                }
            break;
        }
    }
   
    if ($valid == "jcb1" || $valid == "jcb2" || $valid == "jcb3")
    {
        $valid = "jcb";
    }
    return $valid;
}

Ok, to save me from making a mess of another one, seen as this one "definately works" I wil give the code of my form that im using to allow the customer to enter their cc details below and hopefully someone will be kind enough as to show me how to apply the class to it as i have very little dealiing with this type of thing in the past :/

<form name="form1" method="post" action="register.php">
               
                      </p>
                      <p align="center"><b><font color="#FF0000">Payment Details</font></b></p>
                      <table width="75%" height="146" border="0">
                        <tr>
                          <td width="35%"><font color="#FFFFFF" face="Times New Roman, Times, serif">Card
                            Type</font></td>
                          <td width="65%"> <select name="C_CcType" id="C_CcType">
                              <option selected>Visa</option>
                              <option>MasterCard</option>
                              <option>American Express</option>
                            </select> </td>
                        </tr>
                        <tr>
                          <td width="35%"><font color="#FFFFFF" face="Times New Roman, Times, serif">Card
                            Number</font></td>
                          <td width="65%"> <input type="text" name="C_CcNum" class="textBoxNormal" onfocus="this.className='textBoxActive'" onblur="this.className='textBoxNormal'">
                          </td>
                        </tr>
                        <tr>
                          <td width="35%"><font color="#FFFFFF" face="Times New Roman, Times, serif">Expiry
                            Date</font></td>
                          <td width="65%"> <input type="text" name="C_ExpD" class="textBoxNormal" onfocus="this.className='textBoxActive'" onblur="this.className='textBoxNormal'"> </td>
                        </tr>
                        <tr>
                          <td width="35%"><font color="#FFFFFF" face="Times New Roman, Times, serif">Sec
                            Code </font></td>
                          <td width="65%"> <input type="text" name="C_Sec" class="textBoxNormal" onfocus="this.className='textBoxActive'" onblur="this.className='textBoxNormal'"> </td>
                        </tr>
                        <tr>
                          <td colspan="2">&nbsp; </td>
                        </tr>
                      </table>
                      <p align="center" class="tahbol style1">
                        <input name="total" type="hidden" id="total" value="<?php echo $total; ?>">
                        <input name="p_method" type="hidden" id="p_method" value="<?php echo $p_method; ?>">
                      </p>
                      <p>&nbsp;</p>
                      <p>
                        <input type="image" src="images/ok.gif" align="absmiddle" border="0" name="image">
                    </form>

just to clarify that i wish for the code to check if the card is valid and then proceed to the page "register.php".

0
Comment
Question by:clonmelog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 2

Author Comment

by:clonmelog
ID: 13415502
if someone is willing to take this on as an outsourced project then please email me cgallagher@gmail.com
0
 
LVL 5

Accepted Solution

by:
prsupriya earned 2000 total points
ID: 13442422
Hi!

It's good to know that the validation class I sent was useful.

Thanks,
S:

class validate_cc {
  var $cc_type, $cc_number, $cc_expiry_date;

function validate($number,$ctype, $expiry_date) {
      $this->cc_number = ereg_replace('[^0-9]', '', $number);
            if($ctype=='1'){
                  if (ereg('^4[0-9]{12}([0-9]{3})?$', $this->cc_number)) {
                          $this->cc_type = 'Visa';
                       }
                  else{
                   return -1;
                        }
            }
            if($ctype=='2'){
                  if (ereg('^5[1-5][0-9]{14}$', $this->cc_number)) {
                          $this->cc_type = 'Master Card';
                  }
                  else{
                         return -1;
                   }
            }
            if($ctype=='3'){
                  if (ereg('^3[47][0-9]{13}$', $this->cc_number)) {
                          $this->cc_type = 'American Express';
                    }
                 else{
                         return -1;
                   }
            }
            if($ctype=='4'){
                    if (ereg('^4[0-9]{12}([0-9]{3})?$', $this->cc_number)) {
                        $this->cc_type = 'Visa';
                    } elseif (ereg('^5[1-5][0-9]{14}$', $this->cc_number)) {
                        $this->cc_type = 'Master Card';
                    } elseif (ereg('^3[47][0-9]{13}$', $this->cc_number)) {
                        $this->cc_type = 'American Express';
                    } elseif (ereg('^3(0[0-5]|[68][0-9])[0-9]{11}$', $this->cc_number)) {
                        $this->cc_type = 'Diners Club';
                    } elseif (ereg('^6011[0-9]{12}$', $this->cc_number)) {
                        $this->cc_type = 'Discover';
                    } elseif (ereg('^(3[0-9]{4}|2131|1800)[0-9]{11}$', $this->cc_number)) {
                        $this->cc_type = 'JCB';
                    } elseif (ereg('^5610[0-9]{12}$', $this->cc_number)) {
                        $this->cc_type = 'Australian BankCard';
                    } else {
                        return -1;
                    }
           }
            $expiry_datearr=explode("/",$expiry_date);
            $expiry_m=$expiry_datearr[0];
            $expiry_y=$expiry_datearr[1];
            
      if (is_numeric($expiry_m) && ($expiry_m > 0) && ($expiry_m < 13)) {
        $this->cc_expiry_month = $expiry_m;
      } else {
        return -2;
      }

      $current_year = date('Y');
      $expiry_y = substr($current_year, 0, 2) . $expiry_y;
      if (is_numeric($expiry_y) && ($expiry_y >= $current_year) && ($expiry_y <= ($current_year + 10))) {
        $this->cc_expiry_year = $expiry_y;
      } else {
        return -3;
      }

      if ($expiry_y == $current_year) {
        if ($expiry_m < date('n')) {
          return -4;
        }
      }

      return $this->is_valid();
    }
function is_valid() {
      $cardNumber = strrev($this->cc_number);
      $numSum = 0;

      for ($i=0; $i<strlen($cardNumber); $i++) {
        $currentNum = substr($cardNumber, $i, 1);

// Double every second digit
        if ($i % 2 == 1) {
          $currentNum *= 2;
        }

// Add digits of 2-digit numbers together
        if ($currentNum > 9) {
          $firstNum = $currentNum % 10;
          $secondNum = ($currentNum - $firstNum) / 10;
          $currentNum = $firstNum + $secondNum;
        }

        $numSum += $currentNum;
      }

// If the total has no remainder it's OK
      return ($numSum % 10 == 0);
    }
  }
?>



0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question