Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Splitting Validate_Finance_CreditCard into its own repo.

  • Loading branch information...
commit 6b5709ab7f2f9790944a9c6945d25375bf576e5b 1 parent b553a92
@gauthierm gauthierm authored
View
260 Validate/Finance/CreditCard.php
@@ -1,260 +0,0 @@
-<?php
-/* vim: set expandtab tabstop=4 shiftwidth=4: */
-/**
- * Validation methods for credit card related data
- *
- * PHP Versions 4 and 5
- *
- * This source file is subject to the New BSD license, That is bundled
- * with this package in the file LICENSE, and is available through
- * the world-wide-web at
- * http://www.opensource.org/licenses/bsd-license.php
- * If you did not receive a copy of the new BSDlicense and are unable
- * to obtain it through the world-wide-web, please send a note to
- * pajoye@php.net so we can mail you a copy immediately.
- *
- * @category Validate
- * @package Validate_Finance_CreditCard
- * @author Stefan Neufeind <pear.neufeind@speedpartner.de>
- * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
- * @copyright 1997-2005 Stefan Neufeind
- * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
- * @version CVS: $Id$
- * @link http://pear.php.net/package/Validate_Finance_CreditCard
- */
-
-/**
- * Credit card related information validation class
- *
- * This class provides methods to validate:
- * - Credit card number
- * - Card security code
- * - Card type (i.e. Visa, Mastercard...)
- *
- * The methods only check the format of the data. For instance
- * the package does NOT check if a card is a legitimate card registered
- * with a card issuer, or if the card is reported stolen, etc...
- *
- * @category Validate
- * @package Validate_Finance_CreditCard
- * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
- * @author Ondrej Jombik <nepto@pobox.sk>
- * @copyright 1997-2005 Stefan Neufeind
- * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
- * @version Release: @package_version@
- * @link http://pear.php.net/package/Validate_Finance_CreditCard
- */
-class Validate_Finance_CreditCard
-{
- /**
- * Validates a number according to Luhn check algorithm
- *
- * This function checks given number according Luhn check
- * algorithm. It is published on several places. See links for details.
- *
- * @param string $number number to check
- *
- * @link http://www.webopedia.com/TERM/L/Luhn_formula.html
- * @link http://www.merriampark.com/anatomycc.htm
- * @link http://hysteria.sk/prielom/prielom-12.html#3 (Slovak language)
- * @link http://www.speech.cs.cmu.edu/~sburke/pub/luhn_lib.html (Perl lib)
- *
- * @return bool TRUE if number is valid, FALSE otherwise
- * @access public
- * @static
- */
- function Luhn($number)
- {
- $len_number = strlen($number);
- $sum = 0;
- for ($k = $len_number % 2; $k < $len_number; $k += 2) {
- if ((intval($number{$k}) * 2) > 9) {
- $sum += (intval($number{$k}) * 2) - 9;
- } else {
- $sum += intval($number{$k}) * 2;
- }
- }
- for ($k = ($len_number % 2) ^ 1; $k < $len_number; $k += 2) {
- $sum += intval($number{$k});
- }
- return ($sum % 10) ? false : true;
- }
-
-
- /**
- * Validates a credit card number
- *
- * If a type is passed, the card will be checked against it.
- * This method only checks the number locally. No banks or payment
- * gateways are involved.
- * This method doesn't guarantee that the card is legitimate. It merely
- * checks the card number passes a mathematical algorithm.
- *
- * @param string $creditCard number (spaces and dashes tolerated)
- * @param string $cardType type/brand of card (case insensitive)
- * "MasterCard", "Visa", "AMEX", "AmericanExpress",
- * "American Express", "Diners", "DinersClub", "Diners Club",
- * "CarteBlanche", "Carte Blanche", "Discover", "JCB",
- * "EnRoute", "Eurocard", "Eurocard/MasterCard".
- *
- * @return bool TRUE if number is valid, FALSE otherwise
- * @access public
- * @static
- * @see Luhn()
- */
- function number($creditCard, $cardType = null)
- {
- $cc = str_replace(array('-', ' '), '', $creditCard);
- if ((($len = strlen($cc)) < 13)
- || (strspn($cc, '0123456789') != $len)
- ) {
-
- return false;
- }
-
- // Only apply the Luhn algorithm for cards other than enRoute
- // So check if we have a enRoute card now
- if ((strlen($cc) != 15)
- || ((substr($cc, 0, 4) != '2014')
- && (substr($cc, 0, 4) != '2149'))
- ) {
-
- if (!Validate_Finance_CreditCard::Luhn($cc)) {
- return false;
- }
- }
-
- if (is_string($cardType)) {
- return Validate_Finance_CreditCard::type($cc, $cardType);
- }
-
- return true;
- }
-
-
- /**
- * Validates the credit card number against a type
- *
- * This method only checks for the type marker. It doesn't
- * validate the card number. Some card "brands" share the same
- * numbering system, so checking the card type against any of the
- * sister brand will return the same result.
- *
- * For instance, if a $card is a MasterCard, type($card, 'EuroCard')
- * will also return true.
- *
- * @param string $creditCard number (spaces and dashes tolerated)
- * @param string $cardType type/brand of card (case insensitive)
- * "MasterCard", "Visa", "AMEX", "AmericanExpress",
- * "American Express", "Diners", "DinersClub", "Diners Club",
- * "CarteBlanche", "Carte Blanche", "Discover", "JCB",
- * "EnRoute", "Eurocard", "Eurocard/MasterCard".
- *
- * @return bool TRUE is type matches, FALSE otherwise
- * @access public
- * @static
- * @link http://www.beachnet.com/~hstiles/cardtype.html
- */
- function type($creditCard, $cardType)
- {
- switch (strtoupper($cardType)) {
- case 'MASTERCARD':
- case 'EUROCARD':
- case 'EUROCARD/MASTERCARD':
- $regex = '5[1-5][0-9]{14}';
- break;
- case 'VISA':
- $regex = '4([0-9]{12}|[0-9]{15})';
- break;
- case 'AMEX':
- case 'AMERICANEXPRESS':
- case 'AMERICAN EXPRESS':
- $regex = '3[47][0-9]{13}';
- break;
- case 'DINERS':
- case 'DINERSCLUB':
- case 'DINERS CLUB':
- case 'CARTEBLANCHE':
- case 'CARTE BLANCHE':
- $regex = '3(0[0-5][0-9]{11}|[68][0-9]{12})';
- break;
- case 'DISCOVER':
- $regex = '6011[0-9]{12}';
- break;
- case 'JCB':
- $regex = '(3[0-9]{15}|(2131|1800)[0-9]{11})';
- break;
- case 'ENROUTE':
- $regex = '2(014|149)[0-9]{11}';
- break;
- default:
- return false;
- }
- $regex = '/^' . $regex . '$/';
-
- $cc = str_replace(array('-', ' '), '', $creditCard);
- return (bool)preg_match($regex, $cc);
- }
-
-
- /**
- * Validates a card verification value format
- *
- * This method only checks for the format. It doesn't
- * validate that the value is the one on the card.
- *
- * CVV is also known as
- * - CVV2 Card Validation Value 2 (Visa)
- * - CVC Card Validation Code (MasterCard)
- * - CID Card Identification (American Express and Discover)
- * - CIN Card Identification Number
- * - CSC Card Security Code
- *
- * Important information regarding CVV:
- * If you happen to have to store credit card information, you must
- * NOT retain the CVV after transaction is complete. Usually this
- * means you cannot store it in a database, not even in an encrypted
- * form.
- *
- * This method returns FALSE for card types that don't support CVV.
- *
- * @param string $cvv value to verify
- * @param string $cardType type/brand of card (case insensitive)
- * "MasterCard", "Visa", "AMEX", "AmericanExpress",
- * "American Express", "Discover", "Eurocard/MasterCard",
- * "Eurocard"
- *
- * @return bool TRUE if format is correct, FALSE otherwise
- * @access public
- * @static
- */
- function cvv($cvv, $cardType)
- {
- switch (strtoupper($cardType)) {
- case 'MASTERCARD':
- case 'EUROCARD':
- case 'EUROCARD/MASTERCARD':
- case 'VISA':
- case 'DISCOVER':
- $digits = 3;
- break;
- case 'AMEX':
- case 'AMERICANEXPRESS':
- case 'AMERICAN EXPRESS':
- $digits = 4;
- break;
- default:
- return false;
- }
-
- if ((strlen($cvv) == $digits)
- && (strspn($cvv, '0123456789') == $digits)
- ) {
- return true;
- }
-
- return false;
- }
-}
-
-?>
View
4 docs/sample_multiple.php
@@ -26,8 +26,6 @@ function number($number, $decimal = null, $dec_prec = null, $min = null, $max =
'nocompte' => '12345678901',
'key' => '46'
),
- 'cc' => '6762195515061813',
- 'cc2' => '6762195515061814',
'mail' => 'foo@example.com',
'hissiret' => '441 751 245 00016',
'mystring' => 'ABCDEabcde',
@@ -39,8 +37,6 @@ function number($number, $decimal = null, $dec_prec = null, $min = null, $max =
'name' => array('type'=>'email','check_domain'=>false),
'rib' => array('type'=>'FR_rib'),
'rib2' => array('type'=>'FR_rib'),
- 'cc' => array('type'=>'Finance_CreditCard_number'),
- 'cc2' => array('type'=>'Finance_CreditCard_number'),
'mail' => array('type'=>'email'),
'hissiret' => array('type'=>'FR_siret'),
'mystring' => array('type'=>'string',array('format'=>VALIDATE_ALPHA, 'min_length'=>3)),
View
94 package_CreditCard.xml
@@ -1,94 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<package packagerversion="1.9.0" version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
- http://pear.php.net/dtd/tasks-1.0.xsd
- http://pear.php.net/dtd/package-2.0
- http://pear.php.net/dtd/package-2.0.xsd">
- <name>Validate_Finance_CreditCard</name>
- <channel>pear.php.net</channel>
- <summary>Validation class for Credit Cards</summary>
- <description>Package to validate Credit Card numbers and types.
- </description>
- <lead>
- <name>Philippe Jausions</name>
- <user>jausions</user>
- <email>Philippe.Jausions@11abacus.com</email>
- <active>yes</active>
- </lead>
- <date>2010-03-11</date>
- <time>17:28:33</time>
- <version>
- <release>0.5.2</release>
- <api>0.5.2</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <license>New BSD</license>
- <notes>
- - Switch to the new BSD License
- (see http://www.opensource.org/licenses/bsd-license.php)
- </notes>
- <contents>
- <dir name="/">
- <dir name="tests">
- <file name="credit_card.phpt" role="test" />
- </dir> <!-- /tests -->
- <dir name="Validate">
- <dir name="Finance">
- <file name="CreditCard.php" role="php" />
- </dir> <!-- /Validate/Finance -->
- </dir> <!-- /Validate -->
- <file name="LICENSE" role="doc" />
- </dir> <!-- / -->
- </contents>
- <dependencies>
- <required>
- <php>
- <min>4.2.0</min>
- </php>
- <pearinstaller>
- <min>1.4.0b1</min>
- </pearinstaller>
- <extension>
- <name>pcre</name>
- </extension>
- </required>
- </dependencies>
- <phprelease />
- <changelog>
- <release>
- <version>
- <release>0.5.1</release>
- <api>0.5.1</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <date>2005-07-20</date>
- <license>New BSD</license>
- <notes>
- - Added support for Eurocard/Mastercard. Can be referenced by &quot;EuroCard&quot; or &quot;EuroCard/MasterCard&quot; (case-insensitive)
- </notes>
- </release>
- <release>
- <version>
- <release>0.5.0</release>
- <api>0.5.0</api>
- </version>
- <stability>
- <release>alpha</release>
- <api>alpha</api>
- </stability>
- <date>2005-05-20</date>
- <license>New BSD</license>
- <notes>
- - Split from Validate into independent package
- - BC break: creditCard() method now named number() and creditCardType() method is now named type()
- - Request #2194 Improved CreditCard validation with CreditCard Type check, patch by Philippe
- - Added a cvv() method to check card verification value (aka CVV2, CVC, CID, CSC, CIN)
- </notes>
- </release>
- </changelog>
-</package>
View
214 tests/credit_card.phpt
@@ -1,214 +0,0 @@
---TEST--
-credit_card.phpt: Unit tests for 'Validate/Finance/CreditCard.php'
---FILE--
-<?php
-// $Id$
-// Validate test script
-$noYes = array('NO', 'YES');
-require_once 'Validate/Finance/CreditCard.php';
-
-$cards = array(
- 0, // NOK
- -1, // NOK
- '6762195515061813', // OK
- '6762 1955 1506 1813', // OK
- '6762195515061814', // NOK
- '0x1A6A195515061813', // NOK
- '4405771709596026', // OK
- '4405771709596,26', // NOK
- '4405771709596.26', // NOK
- 'Big brother watch', // NOK
- '4222222222222', // OK
- '4222222221.00'); // NOK
-
-// Test card type detection
-$cardTypes = array(
- '4111 1111 1111 1111' => 'VISA',
- '4111-1111-1111-1111' => 'VISA',
- '4111111111111111' => 'VISA',
- '5500 0000 0000 0004' => 'MasterCard',
- '3400 0000 0000 009' => 'AMEX',
- '3088 0000 0000 0009' => 'JCB',
- '2014 0000 0000 009' => 'ENROUTE',
- '6011 0000 0000 0004' => 'DISCOVER',
- '3000 0000 0000 04' => 'DINERSCLUB');
-
-// Test CVV
-$cvvs = array(
- array('1', 'visa'), // NOK
- array('23', 'visa'), // NOK
- array('111', 'visa'), // OK
- array('123', 'visa'), // OK
- array('13', 'visa'), // NOK
- array('1234', 'visa'), // NOK
- array('897', 'mastercard'), // OK
- array('123', 'jcb'), // NOK
- array('8765', 'jcb'), // NOK
- array('8765', ''), // NOK
- array('1234', 'amex'), // OK
- array('465', 'amex'), // NOK
- array('10O7', 'amex'), // NOK
- array('abc', 'visa'), // NOK
- array('123.0', 'visa'), // NOK
- array('1.2', 'visa'), // NOK
- array('123', 'discover'), // OK
- array('4429', 'discover')); // NOK
-
-/* // Test valid LUHN, but invalid cards
-$shortCards = array(
- 'VISA' => '41111',
- 'MasterCard' => '5413',
- 'AMEX' => '3400009',
- 'JCB' => '21311',
- 'ENROUTE' => '2014009',
- 'DISCOVER' => '60110004',
- 'DINERSCLUB' => '300004'); */
-
-echo "Test Validate_Finance_CreditCard class\n";
-
-echo "*** Test credit card number only ***\n";
-foreach ($cards as $card) {
- echo "{$card}: ".
- $noYes[Validate_Finance_CreditCard::number($card)]."\n";
-}
-
-echo "*** Test credit card without type (should be OK) ***\n";
-foreach ($cardTypes as $card => $type) {
- echo $card . ' : '.
- $noYes[Validate_Finance_CreditCard::number($card)]."\n";
-}
-
-echo "*** Test credit card number with type ***\n";
-$cardBrands = array_reverse(array_unique($cardTypes));
-foreach ($cardBrands as $brand) {
- foreach ($cardTypes as $card => $type) {
- echo 'On "' . $card . '" is ' .
- (($type == $brand) ? '' : 'not') . ' a "' . $brand . '" : '.
- $noYes[Validate_Finance_CreditCard::type($card, $brand)]."\n";
- }
-}
-
-echo "*** Test card verification value ***\n";
-foreach ($cvvs as $cvv) {
- echo "{$cvv[0]} by '{$cvv[1]}': ".
- $noYes[Validate_Finance_CreditCard::cvv($cvv[0], $cvv[1])]."\n";
-}
-
-/* function testValidLuhn()
-{
- $pass = false;
- foreach ($shortCards as $type => $number) {
- $r = Validate_Finance_CreditCard::isValid($number, $type);
- $pass |= $r;
- }
- $assertFalse($pass);
-} */
-?>
---EXPECT--
-Test Validate_Finance_CreditCard class
-*** Test credit card number only ***
-0: NO
--1: NO
-6762195515061813: YES
-6762 1955 1506 1813: YES
-6762195515061814: NO
-0x1A6A195515061813: NO
-4405771709596026: YES
-4405771709596,26: NO
-4405771709596.26: NO
-Big brother watch: NO
-4222222222222: YES
-4222222221.00: NO
-*** Test credit card without type (should be OK) ***
-4111 1111 1111 1111 : YES
-4111-1111-1111-1111 : YES
-4111111111111111 : YES
-5500 0000 0000 0004 : YES
-3400 0000 0000 009 : YES
-3088 0000 0000 0009 : YES
-2014 0000 0000 009 : YES
-6011 0000 0000 0004 : YES
-3000 0000 0000 04 : YES
-*** Test credit card number with type ***
-On "4111 1111 1111 1111" is not a "DINERSCLUB" : NO
-On "4111-1111-1111-1111" is not a "DINERSCLUB" : NO
-On "4111111111111111" is not a "DINERSCLUB" : NO
-On "5500 0000 0000 0004" is not a "DINERSCLUB" : NO
-On "3400 0000 0000 009" is not a "DINERSCLUB" : NO
-On "3088 0000 0000 0009" is not a "DINERSCLUB" : NO
-On "2014 0000 0000 009" is not a "DINERSCLUB" : NO
-On "6011 0000 0000 0004" is not a "DINERSCLUB" : NO
-On "3000 0000 0000 04" is a "DINERSCLUB" : YES
-On "4111 1111 1111 1111" is not a "DISCOVER" : NO
-On "4111-1111-1111-1111" is not a "DISCOVER" : NO
-On "4111111111111111" is not a "DISCOVER" : NO
-On "5500 0000 0000 0004" is not a "DISCOVER" : NO
-On "3400 0000 0000 009" is not a "DISCOVER" : NO
-On "3088 0000 0000 0009" is not a "DISCOVER" : NO
-On "2014 0000 0000 009" is not a "DISCOVER" : NO
-On "6011 0000 0000 0004" is a "DISCOVER" : YES
-On "3000 0000 0000 04" is not a "DISCOVER" : NO
-On "4111 1111 1111 1111" is not a "ENROUTE" : NO
-On "4111-1111-1111-1111" is not a "ENROUTE" : NO
-On "4111111111111111" is not a "ENROUTE" : NO
-On "5500 0000 0000 0004" is not a "ENROUTE" : NO
-On "3400 0000 0000 009" is not a "ENROUTE" : NO
-On "3088 0000 0000 0009" is not a "ENROUTE" : NO
-On "2014 0000 0000 009" is a "ENROUTE" : YES
-On "6011 0000 0000 0004" is not a "ENROUTE" : NO
-On "3000 0000 0000 04" is not a "ENROUTE" : NO
-On "4111 1111 1111 1111" is not a "JCB" : NO
-On "4111-1111-1111-1111" is not a "JCB" : NO
-On "4111111111111111" is not a "JCB" : NO
-On "5500 0000 0000 0004" is not a "JCB" : NO
-On "3400 0000 0000 009" is not a "JCB" : NO
-On "3088 0000 0000 0009" is a "JCB" : YES
-On "2014 0000 0000 009" is not a "JCB" : NO
-On "6011 0000 0000 0004" is not a "JCB" : NO
-On "3000 0000 0000 04" is not a "JCB" : NO
-On "4111 1111 1111 1111" is not a "AMEX" : NO
-On "4111-1111-1111-1111" is not a "AMEX" : NO
-On "4111111111111111" is not a "AMEX" : NO
-On "5500 0000 0000 0004" is not a "AMEX" : NO
-On "3400 0000 0000 009" is a "AMEX" : YES
-On "3088 0000 0000 0009" is not a "AMEX" : NO
-On "2014 0000 0000 009" is not a "AMEX" : NO
-On "6011 0000 0000 0004" is not a "AMEX" : NO
-On "3000 0000 0000 04" is not a "AMEX" : NO
-On "4111 1111 1111 1111" is not a "MasterCard" : NO
-On "4111-1111-1111-1111" is not a "MasterCard" : NO
-On "4111111111111111" is not a "MasterCard" : NO
-On "5500 0000 0000 0004" is a "MasterCard" : YES
-On "3400 0000 0000 009" is not a "MasterCard" : NO
-On "3088 0000 0000 0009" is not a "MasterCard" : NO
-On "2014 0000 0000 009" is not a "MasterCard" : NO
-On "6011 0000 0000 0004" is not a "MasterCard" : NO
-On "3000 0000 0000 04" is not a "MasterCard" : NO
-On "4111 1111 1111 1111" is a "VISA" : YES
-On "4111-1111-1111-1111" is a "VISA" : YES
-On "4111111111111111" is a "VISA" : YES
-On "5500 0000 0000 0004" is not a "VISA" : NO
-On "3400 0000 0000 009" is not a "VISA" : NO
-On "3088 0000 0000 0009" is not a "VISA" : NO
-On "2014 0000 0000 009" is not a "VISA" : NO
-On "6011 0000 0000 0004" is not a "VISA" : NO
-On "3000 0000 0000 04" is not a "VISA" : NO
-*** Test card verification value ***
-1 by 'visa': NO
-23 by 'visa': NO
-111 by 'visa': YES
-123 by 'visa': YES
-13 by 'visa': NO
-1234 by 'visa': NO
-897 by 'mastercard': YES
-123 by 'jcb': NO
-8765 by 'jcb': NO
-8765 by '': NO
-1234 by 'amex': YES
-465 by 'amex': NO
-10O7 by 'amex': NO
-abc by 'visa': NO
-123.0 by 'visa': NO
-1.2 by 'visa': NO
-123 by 'discover': YES
-4429 by 'discover': NO
Please sign in to comment.
Something went wrong with that request. Please try again.