[ Index ]
 

Code source de osCommerce 2.2ms2-060817

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

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

   1  <?php
   2  /*
   3    $Id: cc_validation.php,v 1.3 2003/02/12 20:43:41 hpdl Exp $
   4  
   5    osCommerce, Open Source E-Commerce Solutions
   6    http://www.oscommerce.com
   7  
   8    Copyright (c) 2003 osCommerce
   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      function validate($number, $expiry_m, $expiry_y) {
  17        $this->cc_number = ereg_replace('[^0-9]', '', $number);
  18  
  19        if (ereg('^4[0-9]{12}([0-9]{3})?$', $this->cc_number)) {
  20          $this->cc_type = 'Visa';
  21        } elseif (ereg('^5[1-5][0-9]{14}$', $this->cc_number)) {
  22          $this->cc_type = 'Master Card';
  23        } elseif (ereg('^3[47][0-9]{13}$', $this->cc_number)) {
  24          $this->cc_type = 'American Express';
  25        } elseif (ereg('^3(0[0-5]|[68][0-9])[0-9]{11}$', $this->cc_number)) {
  26          $this->cc_type = 'Diners Club';
  27        } elseif (ereg('^6011[0-9]{12}$', $this->cc_number)) {
  28          $this->cc_type = 'Discover';
  29        } elseif (ereg('^(3[0-9]{4}|2131|1800)[0-9]{11}$', $this->cc_number)) {
  30          $this->cc_type = 'JCB';
  31        } elseif (ereg('^5610[0-9]{12}$', $this->cc_number)) { 
  32          $this->cc_type = 'Australian BankCard';
  33        } else {
  34          return -1;
  35        }
  36  
  37        if (is_numeric($expiry_m) && ($expiry_m > 0) && ($expiry_m < 13)) {
  38          $this->cc_expiry_month = $expiry_m;
  39        } else {
  40          return -2;
  41        }
  42  
  43        $current_year = date('Y');
  44        $expiry_y = substr($current_year, 0, 2) . $expiry_y;
  45        if (is_numeric($expiry_y) && ($expiry_y >= $current_year) && ($expiry_y <= ($current_year + 10))) {
  46          $this->cc_expiry_year = $expiry_y;
  47        } else {
  48          return -3;
  49        }
  50  
  51        if ($expiry_y == $current_year) {
  52          if ($expiry_m < date('n')) {
  53            return -4;
  54          }
  55        }
  56  
  57        return $this->is_valid();
  58      }
  59  
  60      function is_valid() {
  61        $cardNumber = strrev($this->cc_number);
  62        $numSum = 0;
  63  
  64        for ($i=0; $i<strlen($cardNumber); $i++) {
  65          $currentNum = substr($cardNumber, $i, 1);
  66  
  67  // Double every second digit
  68          if ($i % 2 == 1) {
  69            $currentNum *= 2;
  70          }
  71  
  72  // Add digits of 2-digit numbers together
  73          if ($currentNum > 9) {
  74            $firstNum = $currentNum % 10;
  75            $secondNum = ($currentNum - $firstNum) / 10;
  76            $currentNum = $firstNum + $secondNum;
  77          }
  78  
  79          $numSum += $currentNum;
  80        }
  81  
  82  // If the total has no remainder it's OK
  83        return ($numSum % 10 == 0);
  84      }
  85    }
  86  ?>


Généré le : Mon Nov 26 19:48:25 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics