Skip to content

Latest commit

 

History

History
107 lines (76 loc) · 2.1 KB

README.md

File metadata and controls

107 lines (76 loc) · 2.1 KB

PHP Credit Card Validator

License Latest Stable Version Total Downloads Daily Downloads

Validates popular debit and credit card numbers against regular expressions and the Luhn algorithm. It also validates the CVC and the expiration date.

Installation

composer require irabbi360/php-credit-card-validator

Require the package in composer.json

"require": {
    "irabbi360/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'      => 'Irabbi360\CreditCard',

),

Usage

Validate a card number knowing the type:

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

Output:

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

Validate a card number and return the type:

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

Output:

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

Validate the CVC

$validCvc = CreditCard::validCvc('234', '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