Skip to content

drcreazy/php-credit-card-validator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP Credit Card Validator

Build Status Coverage Status Latest Stable Version Total Downloads

Validates popular debit and credit cards numbers against regular expressions and Luhn algorithm. Also validates the CVC and the expiration date.

Installation

Require the package in composer.json

"require": {
    "inacho/php-credit-card-validator": "1.*"
},

If you are using Laravel, add an alias in config/app.php

'aliases' => array(

    'App'             => 'Illuminate\Support\Facades\App',
    ...
    'View'            => 'Illuminate\Support\Facades\View',

    'CreditCard'      => 'Inacho\CreditCard',

),

Usage

Validate a card number knowing the type:

$card = CreditCard::validCreditCard('5500005555555559', CreditCard::TYPE_MASTERCARD);
print_r($card);

Output:

Array
(
    [valid] => true
    [number] => 5500005555555559
    [type] => mastercard
)

Validate a card number using a range of allowed types:

$card = CreditCard::validCreditCard('5500005555555559', array(CreditCard::TYPE_VISA, CreditCard::TYPE_MASTERCARD));
print_r($card);

Output:

Array
(
    [valid] => true
    [number] => 5500005555555559
    [type] => mastercard
)
$card = CreditCard::validCreditCard('371449635398431', array(CreditCard::TYPE_VISA, CreditCard::TYPE_MASTERCARD));
print_r($card);

Output:

Array
(
    [valid] => false
    [number] => 
    [type] => 
)

Validate a card number and return the type:

$card = CreditCard::validCreditCard('371449635398431');
print_r($card);

Output:

Array
(
    [valid] => true
    [number] => 371449635398431
    [type] => amex
)

Validate the CVC

$validCvc = CreditCard::validCvc('234', CreditCard::TYPE_VISA);
var_dump($validCvc);

Output:

bool(true)

Validate the expiration date

$validDate = CreditCard::validDate('2013', '07'); // past date
var_dump($validDate);

Output:

bool(false)

Tests

Execute the following command to run the unit tests:

vendor/bin/phpunit

About

Validates popular debit and credit cards numbers against regular expressions and Luhn algorithm. Also validates the CVC and the expiration date.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%