annaghd / php-credit-card-validator by annaghd
forked from inacho/php-credit-card-validator

Validates popular debit and credit cards numbers against regular expressions and Luhn algorithm. Also validates the CVC and the expiration date
7,742
0
1
Package Data
Maintainer Username: annaghd
Maintainer Contact: annaghandevosyan@gmail.com (Anna)
Package Create Date: 2017-08-15
Package Last Update: 2017-11-01
Language: PHP
License: MIT
Last Refreshed: 2024-05-18 15:03:34
Package Statistics
Total Downloads: 7,742
Monthly Downloads: 461
Daily Downloads: 16
Total Stars: 0
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

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": {
    "annaghd/php-credit-card-validator-plus": "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', '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

$validMonth = CreditCard::validMonth('07'); // past date
var_dump($validMonth);

$validYear = CreditCard::validYear('2007'); // past date
var_dump($validYear);

Output:

bool(false)

bool(false)

Tests

Execute the following command to run the unit tests:

vendor/bin/phpunit