https://github.com/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.
https://github.com/inacho/php-credit-card-validator
Last synced: about 1 year ago
JSON representation
Validates popular debit and credit cards numbers against regular expressions and Luhn algorithm. Also validates the CVC and the expiration date.
- Host: GitHub
- URL: https://github.com/inacho/php-credit-card-validator
- Owner: inacho
- License: mit
- Created: 2014-09-30T07:31:26.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-08-22T08:27:12.000Z (almost 3 years ago)
- Last Synced: 2025-04-12T06:18:46.465Z (about 1 year ago)
- Language: PHP
- Size: 227 KB
- Stars: 202
- Watchers: 20
- Forks: 104
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP Credit Card Validator
[](https://travis-ci.org/inacho/php-credit-card-validator) [](https://coveralls.io/github/inacho/php-credit-card-validator?branch=master) [](https://packagist.org/packages/inacho/php-credit-card-validator) [](https://packagist.org/packages/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.
## Installation
Require the package in `composer.json`
```json
"require": {
"inacho/php-credit-card-validator": "1.*"
},
```
If you are using Laravel, add an alias in `config/app.php`
```php
'aliases' => array(
'App' => 'Illuminate\Support\Facades\App',
...
'View' => 'Illuminate\Support\Facades\View',
'CreditCard' => 'Inacho\CreditCard',
),
```
## Usage
### Validate a card number knowing the type:
```php
$card = CreditCard::validCreditCard('5500005555555559', 'mastercard');
print_r($card);
```
Output:
```
Array
(
[valid] => 1
[number] => 5500005555555559
[type] => mastercard
)
```
### Validate a card number and return the type:
```php
$card = CreditCard::validCreditCard('371449635398431');
print_r($card);
```
Output:
```
Array
(
[valid] => 1
[number] => 371449635398431
[type] => amex
)
```
### Validate the CVC
```php
$validCvc = CreditCard::validCvc('234', 'visa');
var_dump($validCvc);
```
Output:
```
bool(true)
```
### Validate the expiration date
```php
$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