Source for file CreditCard.class.php

Documentation is available at CreditCard.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Valid
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Credit Card Validation Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Valid
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Credit Card Validation Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Valid");
  34.  
  35.     
  36.     /**
  37.      * Constructor
  38.      * @param bool $throw throw Exceptions
  39.      * @param int $level Error Level thrown
  40.      */
  41.     public function __construct ($throw=false$level=null{
  42.         parent::__construct ($throw$level);
  43.     }
  44.     
  45.     
  46.     
  47.     /**
  48.      * Returns if the value is a Credit Card Number (Luhn Formula)
  49.      * @param string $val 
  50.      * @return bool 
  51.      * @throws Gumbo_Exception
  52.      */
  53.     public function isCreditCard ($val{
  54.         $this->turnOn ();
  55.         try {
  56.             // verify precondition
  57.             $this->precondition ($val"str""num");
  58.             $val ereg_replace ("[^0-9]"""$val);
  59.             
  60.             $val strrev ($val);
  61.             $total 0;
  62.             for ($x=0;$x strlen ($val);$x++{
  63.                 $digit substr ($val,$x,1);
  64.                 
  65.                 // odd number
  66.                 if ($x/!= floor ($x/2)) {
  67.                     $digit *= 2;
  68.             
  69.                     // if result two digits, add them
  70.                     if (strlen($digit== 2{
  71.                         $digit substr ($digit01substr ($digit11);
  72.                     }
  73.                 }
  74.             
  75.                 // add to the total
  76.                 $total += $digit;
  77.             }
  78.             $passed ($total 10 == 0);
  79.         catch (Exception $e{
  80.             $e null;
  81.         }
  82.         $this->turnOff ();
  83.         
  84.         if (!$passed && $this->isThrowing ()) {
  85.             throw new Gumbo_Exception ("Validation Failed: Credit Card"$this->getLevel ());
  86.         }
  87.         return $passed;
  88.     }
  89.     
  90.         /**
  91.          * Returns if the Credit Card is a VISA
  92.          * @param string $val 
  93.          * @return bool 
  94.           * @throws Gumbo_Exception
  95.          */
  96.         public function isCreditCardVisa ($val{
  97.             $passed false;
  98.             
  99.             $this->turnOn ();
  100.             try {
  101.                 // verify precondition
  102.                 $this->precondition ($val"str""num");
  103.                 $this->isMatch ($val"/^([4]{1})([0-9]{12,15})$/");
  104.                 $this->isCreditCard ($val);
  105.                 $passed true;
  106.             catch (Exception $e{
  107.                 $e null;
  108.             }
  109.             $this->turnOff ();
  110.             
  111.             if (!$passed && $this->isThrowing ()) {
  112.                 throw new Gumbo_Exception ("Validation Failed: VISA Credit Card"$this->getLevel ());
  113.             }
  114.             return $passed;
  115.         }
  116.         
  117.         /**
  118.          * Returns if the Credit Card is a MasterCARD
  119.          * @param string $val 
  120.          * @return bool 
  121.           * @throws Gumbo_Exception
  122.          */
  123.         public function isCreditCardMasterCard ($val{
  124.             $passed false;
  125.             
  126.             $this->turnOn ();
  127.             try {
  128.                 // verify precondition
  129.                 $this->precondition ($val"str""num");
  130.                 $this->isMatch ($val"/^([51|52|53|54|55]{2})([0-9]{14})$/");
  131.                 $this->isCreditCard ($val);
  132.                 $passed true;
  133.             catch (Exception $e{
  134.                 $e null;
  135.             }
  136.             $this->turnOff ();
  137.             
  138.             if (!$passed && $this->isThrowing ()) {
  139.                 throw new Gumbo_Exception ("Validation Failed: MASTERCARD Credit Card"$this->getLevel ());
  140.             }
  141.             return $passed;
  142.         }
  143.         
  144.         /**
  145.          * Returns if the Credit Card is a Discover
  146.          * @param string $val 
  147.          * @return bool 
  148.           * @throws Gumbo_Exception
  149.          */
  150.         public function isCreditCardDiscover ($val{
  151.             $passed false;
  152.             
  153.             $this->turnOn ();
  154.             try {
  155.                 // verify precondition
  156.                 $this->precondition ($val"str""num");
  157.                 $this->isMatch ($val"/^([6011]{4})([0-9]{12})$/");
  158.                 $this->isCreditCard ($val);
  159.                 $passed true;
  160.             catch (Exception $e{
  161.                 $e null;
  162.             }
  163.             $this->turnOff ();
  164.             
  165.             if (!$passed && $this->isThrowing ()) {
  166.                 throw new Gumbo_Exception ("Validation Failed: DISCOVER Credit Card"$this->getLevel ());
  167.             }
  168.             return $passed;
  169.         }
  170.         
  171.         /**
  172.          * Returns if the Credit Card is a American Express
  173.          * @param string $val 
  174.          * @return bool 
  175.           * @throws Gumbo_Exception
  176.          */
  177.         public function isCreditCardAmericanExpress ($val{
  178.             $passed false;
  179.             
  180.             $this->turnOn ();
  181.             try {
  182.                 // verify precondition
  183.                 $this->precondition ($val"str""num");
  184.                 $this->isMatch ($val"/^([34|37]{2})([0-9]{13})$/");
  185.                 $this->isCreditCard ($val);
  186.                 $passed true;
  187.             catch (Exception $e{
  188.                 $e null;
  189.             }
  190.             $this->turnOff ();
  191.             
  192.             if (!$passed && $this->isThrowing ()) {
  193.                 throw new Gumbo_Exception ("Validation Failed: AMERICAN EXPRESS Credit Card"$this->getLevel ());
  194.             }
  195.             return $passed;
  196.         }
  197.         
  198.         /**
  199.          * Returns if the Credit Card is a Diner's Club
  200.          * @param string $val 
  201.          * @return bool 
  202.           * @throws Gumbo_Exception
  203.          */
  204.         public function isCreditCardDinersClub ($val{
  205.             $passed false;
  206.             
  207.             $this->turnOn ();
  208.             try {
  209.                 // verify precondition
  210.                 $this->precondition ($val"str""num");
  211.                 $this->isMatch ($val"/^([30|36|38]{2})([0-9]{12})$/");
  212.                 $this->isCreditCard ($val);
  213.                 $passed true;
  214.             catch (Exception $e{
  215.                 $e null;
  216.             }
  217.             $this->turnOff ();
  218.             
  219.             if (!$passed && $this->isThrowing ()) {
  220.                 throw new Gumbo_Exception ("Validation Failed: DINERS CLUB Credit Card"$this->getLevel ());
  221.             }
  222.             return $passed;
  223.         }
  224.     
  225. }
  226.  
  227. ?>