[ Index ]

PHP Cross Reference of osCMax 2.0.4

title

Body

[close]

/includes/classes/ -> cc_validation.php (source)

   1  <?php
   2  /*

   3  $Id: cc_validation.php 14 2006-07-28 17:42:07Z user $

   4  

   5    osCMax Power E-Commerce

   6    http://oscdox.com

   7  

   8    Copyright 2006 osCMax

   9  

  10    Released under the GNU General Public License

  11  */
  12  
  13    class cc_validation {
  14      var $cc_type, $cc_number, $cc_expiry_month, $cc_expiry_year;
  15  
  16  // LINE MODIFIED: Added $cvv, $cr_card_type

  17      function validate($number, $expiry_m, $expiry_y, $cvv, $cr_card_type) {
  18        $this->cc_number = ereg_replace('[^0-9]', '', $number);
  19  
  20        if (ereg('^4[0-9]{12}([0-9]{3})?$', $this->cc_number)) {
  21          $this->cc_type = 'Visa';
  22        } elseif (ereg('^5[1-5][0-9]{14}$', $this->cc_number)) {
  23          $this->cc_type = 'Mastercard';
  24        } elseif (ereg('^3[47][0-9]{13}$', $this->cc_number)) {
  25          $this->cc_type = 'Amex';
  26        } elseif (ereg('^3(0[0-5]|[68][0-9])[0-9]{11}$', $this->cc_number)) {
  27          $this->cc_type = 'Diners Club';
  28        } elseif (ereg('^6011[0-9]{12}$', $this->cc_number)) {
  29          $this->cc_type = 'Discover';
  30        } elseif (ereg('^(3[0-9]{4}|2131|1800)[0-9]{11}$', $this->cc_number)) {
  31          $this->cc_type = 'JCB';
  32        } elseif (ereg('^5610[0-9]{12}$', $this->cc_number)) { 
  33          $this->cc_type = 'Australian BankCard';
  34        } else {
  35          return -1;
  36        }
  37  
  38        if (is_numeric($expiry_m) && ($expiry_m > 0) && ($expiry_m < 13)) {
  39          $this->cc_expiry_month = $expiry_m;
  40        } else {
  41          return -2;
  42        }
  43  
  44        $current_year = date('Y');
  45        $expiry_y = substr($current_year, 0, 2) . $expiry_y;
  46        if (is_numeric($expiry_y) && ($expiry_y >= $current_year) && ($expiry_y <= ($current_year + 10))) {
  47          $this->cc_expiry_year = $expiry_y;
  48        } else {
  49          return -3;
  50        }
  51  
  52        if ($expiry_y == $current_year) {
  53          if ($expiry_m < date('n')) {
  54            return -4;
  55          }
  56        }
  57  
  58  // BOF: MOD - CVV

  59      $l = strlen($cvv);
  60      if (strlen($cr_card_type) > 0 && ($this->cc_type != $cr_card_type)) {
  61          return -5;
  62      }
  63  
  64      switch($cr_card_type) {
  65          case 'Amex':
  66              $len = 4;
  67              break;
  68          case 'Discover':
  69              $len = 3;
  70              break;
  71          case 'Mastercard':
  72              $len = 3;
  73              break;
  74          case 'Visa':
  75              $len = 3;
  76              break;
  77      }
  78  
  79      if ($len != $l) {
  80          return -6;
  81      }
  82  // EOF: MOD - CVV

  83        return $this->is_valid();
  84      }
  85  
  86      function is_valid() {
  87        $cardNumber = strrev($this->cc_number);
  88        $numSum = 0;
  89  
  90        for ($i=0; $i<strlen($cardNumber); $i++) {
  91          $currentNum = substr($cardNumber, $i, 1);
  92  
  93  // Double every second digit

  94          if ($i % 2 == 1) {
  95            $currentNum *= 2;
  96          }
  97  
  98  // Add digits of 2-digit numbers together

  99          if ($currentNum > 9) {
 100            $firstNum = $currentNum % 10;
 101            $secondNum = ($currentNum - $firstNum) / 10;
 102            $currentNum = $firstNum + $secondNum;
 103          }
 104  
 105          $numSum += $currentNum;
 106        }
 107  
 108  // If the total has no remainder it's OK

 109        return ($numSum % 10 == 0);
 110      }
 111    }
 112  ?>


Generated: Fri Jan 1 13:43:16 2010 Cross-referenced by PHPXref 0.7