Validate_Finance_CreditCard
[ class tree: Validate_Finance_CreditCard ] [ index: Validate_Finance_CreditCard ] [ all elements ]

Source for file CreditCard.php

Documentation is available at CreditCard.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. /**
  4.  * Validation methods for credit card related data
  5.  *
  6.  * PHP Versions 4 and 5
  7.  *
  8.  * This source file is subject to the New BSD license, That is bundled
  9.  * with this package in the file LICENSE, and is available through
  10.  * the world-wide-web at
  11.  * http://www.opensource.org/licenses/bsd-license.php
  12.  * If you did not receive a copy of the new BSDlicense and are unable
  13.  * to obtain it through the world-wide-web, please send a note to
  14.  * pajoye@php.net so we can mail you a copy immediately.
  15.  *
  16.  * @category  Validate
  17.  * @package   Validate_Finance_CreditCard
  18.  * @author    Stefan Neufeind <pear.neufeind@speedpartner.de>
  19.  * @author    Philippe Jausions <Philippe.Jausions@11abacus.com>
  20.  * @copyright 1997-2005  Stefan Neufeind
  21.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  22.  * @version   CVS: $Id: CreditCard.php,v 1.13 2007/09/16 16:42:34 kguest Exp $
  23.  * @link      http://pear.php.net/package/Validate_Finance_CreditCard
  24.  */
  25.  
  26. /**
  27.  * Credit card related information validation class
  28.  *
  29.  * This class provides methods to validate:
  30.  *  - Credit card number
  31.  *  - Card security code
  32.  *  - Card type (i.e. Visa, Mastercard...)
  33.  *
  34.  * The methods only check the format of the data. For instance
  35.  * the package does NOT check if a card is a legitimate card registered
  36.  * with a card issuer, or if the card is reported stolen, etc...
  37.  *
  38.  * @category  Validate
  39.  * @package   Validate_Finance_CreditCard
  40.  * @author    Philippe Jausions <Philippe.Jausions@11abacus.com>
  41.  * @author    Ondrej Jombik <nepto@pobox.sk>
  42.  * @copyright 1997-2005  Stefan Neufeind
  43.  * @license   http://www.opensource.org/licenses/bsd-license.php  New BSD License
  44.  * @version   Release: @package_version@
  45.  * @link      http://pear.php.net/package/Validate_Finance_CreditCard
  46.  */
  47. {
  48.     /**
  49.      * Validates a number according to Luhn check algorithm
  50.      *
  51.      * This function checks given number according Luhn check
  52.      * algorithm. It is published on several places. See links for details.
  53.      *
  54.      * @param string $number number to check
  55.      *
  56.      * @return bool    TRUE if number is valid, FALSE otherwise
  57.      * @access public
  58.      * @static
  59.      * @deprecated
  60.      * @link http://www.webopedia.com/TERM/L/Luhn_formula.html
  61.      * @link http://www.merriampark.com/anatomycc.htm
  62.      * @link http://hysteria.sk/prielom/prielom-12.html#3 (Slovak language)
  63.      * @link http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html (Perl lib)
  64.      */
  65.     function Luhn($number)
  66.     {
  67.         return Validate_Finance_CreditCard::_luhn($number);
  68.     }
  69.  
  70.     /**
  71.      * Validates a number according to Luhn check algorithm
  72.      *
  73.      * This function checks given number according Luhn check
  74.      * algorithm. It is published on several places. See links for details.
  75.      *
  76.      * @param string $number number to check
  77.      *
  78.      * @return bool    TRUE if number is valid, FALSE otherwise
  79.      * @access private
  80.      * @static
  81.      * @link http://www.webopedia.com/TERM/L/Luhn_formula.html
  82.      * @link http://www.merriampark.com/anatomycc.htm
  83.      * @link http://hysteria.sk/prielom/prielom-12.html#3 (Slovak language)
  84.      * @link http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html (Perl lib)
  85.      */
  86.     function _luhn($number)
  87.     {
  88.         $len_number strlen($number);
  89.         $sum        = 0;
  90.         for ($k $len_number % 2; $k $len_number$k += 2{
  91.             if ((intval($number{$k}* 2> 9{
  92.                 $sum += (intval($number{$k}* 2- 9;
  93.             else {
  94.                 $sum += intval($number{$k}* 2;
  95.             }
  96.         }
  97.         for ($k ($len_number % 2^ 1; $k $len_number$k += 2{
  98.             $sum += intval($number{$k});
  99.         }
  100.         return ($sum % 10? false : true;
  101.     }
  102.  
  103.     /**
  104.      * Validates a credit card number
  105.      *
  106.      * If a type is passed, the card will be checked against it.
  107.      * This method only checks the number locally. No banks or payment
  108.      * gateways are involved.
  109.      * This method doesn't guarantee that the card is legitimate. It merely
  110.      * checks the card number passes a mathematical algorithm.
  111.      *
  112.      * @param string $creditCard number (spaces and dashes tolerated)
  113.      * @param string $cardType   type/brand of card (case insensitive)
  114.      *                "MasterCard", "Visa", "AMEX", "AmericanExpress",
  115.      *                "American Express", "Diners", "DinersClub", "Diners Club",
  116.      *                "CarteBlanche", "Carte Blanche", "Discover", "JCB",
  117.      *                "EnRoute", "Eurocard", "Eurocard/MasterCard".
  118.      *
  119.      * @return bool   TRUE if number is valid, FALSE otherwise
  120.      * @access public
  121.      * @static
  122.      */
  123.     function number($creditCard$cardType = null)
  124.     {
  125.         $cc str_replace(array('-'' ')''$creditCard);
  126.         if (($len strlen($cc)) < 13
  127.             || strspn($cc'0123456789'!= $len{
  128.  
  129.             return false;
  130.         }
  131.  
  132.         // Only apply the Luhn algorithm for cards other than enRoute
  133.         // So check if we have a enRoute card now
  134.         if (strlen($cc!= 15
  135.             || (substr($cc04!= '2014'
  136.                 && substr($cc04!= '2149')) {
  137.  
  138.             if (!Validate_Finance_CreditCard::_luhn($cc)) {
  139.                 return false;
  140.             }
  141.         }
  142.  
  143.         if (is_string($cardType)) {
  144.             return Validate_Finance_CreditCard::type($cc$cardType);
  145.         }
  146.  
  147.         return true;
  148.     }
  149.  
  150.  
  151.     /**
  152.      * Validates the credit card number against a type
  153.      *
  154.      * This method only checks for the type marker. It doesn't
  155.      * validate the card number. Some card "brands" share the same
  156.      * numbering system, so checking the card type against any of the
  157.      * sister brand will return the same result.
  158.      *
  159.      * For instance, if a $card is a MasterCard, type($card, 'EuroCard')
  160.      * will also return true.
  161.      *
  162.      * @param string $creditCard number (spaces and dashes tolerated)
  163.      * @param string $cardType   type/brand of card (case insensitive)
  164.      *                "MasterCard", "Visa", "AMEX", "AmericanExpress",
  165.      *                "American Express", "Diners", "DinersClub", "Diners Club",
  166.      *                "CarteBlanche", "Carte Blanche", "Discover", "JCB",
  167.      *                "EnRoute", "Eurocard", "Eurocard/MasterCard".
  168.      *
  169.      * @return bool   TRUE is type matches, FALSE otherwise
  170.      * @access public
  171.      * @static
  172.      * @link http://www.beachnet.com/~hstiles/cardtype.html
  173.      */
  174.     function type($creditCard$cardType)
  175.     {
  176.         switch (strtoupper($cardType)) {
  177.         case 'MASTERCARD':
  178.         case 'EUROCARD':
  179.         case 'EUROCARD/MASTERCARD':
  180.             $regex '5[1-5][0-9]{14}';
  181.             break;
  182.         case 'VISA':
  183.             $regex '4([0-9]{12}|[0-9]{15})';
  184.             break;
  185.         case 'AMEX':
  186.         case 'AMERICANEXPRESS':
  187.         case 'AMERICAN EXPRESS':
  188.             $regex '3[47][0-9]{13}';
  189.             break;
  190.         case 'DINERS':
  191.         case 'DINERSCLUB':
  192.         case 'DINERS CLUB':
  193.         case 'CARTEBLANCHE':
  194.         case 'CARTE BLANCHE':
  195.             $regex '3(0[0-5][0-9]{11}|[68][0-9]{12})';
  196.             break;
  197.         case 'DISCOVER':
  198.             $regex '6011[0-9]{12}';
  199.             break;
  200.         case 'JCB':
  201.             $regex '(3[0-9]{15}|(2131|1800)[0-9]{11})';
  202.             break;
  203.         case 'ENROUTE':
  204.             $regex '2(014|149)[0-9]{11}';
  205.             break;
  206.         default:
  207.             return false;
  208.         }
  209.         $regex '/^' $regex '$/';
  210.  
  211.         $cc str_replace(array('-'' ')''$creditCard);
  212.         return (bool)preg_match($regex$cc);
  213.     }
  214.  
  215.  
  216.     /**
  217.      * Validates a card verification value format
  218.      *
  219.      * This method only checks for the format. It doesn't
  220.      * validate that the value is the one on the card.
  221.      *
  222.      * CVV is also known as
  223.      *  - CVV2 Card Validation Value 2 (Visa)
  224.      *  - CVC  Card Validation Code (MasterCard)
  225.      *  - CID  Card Identification (American Express and Discover)
  226.      *  - CIN  Card Identification Number
  227.      *  - CSC  Card Security Code
  228.      *
  229.      * Important information regarding CVV:
  230.      *    If you happen to have to store credit card information, you must
  231.      *    NOT retain the CVV after transaction is complete. Usually this
  232.      *    means you cannot store it in a database, not even in an encrypted
  233.      *    form. See http://www.pcisecuritystandards.org/
  234.      *
  235.      * This method returns FALSE for card types that don't support CVV.
  236.      *
  237.      * @param string $cvv      value to verify
  238.      * @param string $cardType type/brand of card (case insensitive)
  239.      *                "MasterCard", "Visa", "AMEX", "AmericanExpress",
  240.      *                "American Express", "Discover", "Eurocard/MasterCard",
  241.      *                "Eurocard"
  242.      *
  243.      * @return bool   TRUE if format is correct, FALSE otherwise
  244.      * @access public
  245.      * @static
  246.      * @link http://www.pcisecuritystandards.org/
  247.      */
  248.     function cvv($cvv$cardType)
  249.     {
  250.         switch (strtoupper($cardType)) {
  251.         case 'MASTERCARD':
  252.         case 'EUROCARD':
  253.         case 'EUROCARD/MASTERCARD':
  254.         case 'VISA':
  255.         case 'DISCOVER':
  256.             $digits = 3;
  257.             break;
  258.         case 'AMEX':
  259.         case 'AMERICANEXPRESS':
  260.         case 'AMERICAN EXPRESS':
  261.             $digits = 4;
  262.             break;
  263.         default:
  264.             return false;
  265.         }
  266.  
  267.         if (strlen($cvv== $digits
  268.             && strspn($cvv'0123456789'== $digits{
  269.             return true;
  270.         }
  271.  
  272.         return false;
  273.     }
  274. }
  275.  
  276. ?>

Documentation generated on Mon, 01 Sep 2008 14:00:04 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.