lloople / php-credit-cards
A PHP package to perform operations on debit and credit cards like format, validate brand, number and Luhn algorithm. It validates popular brands like Visa, Mastercard, American Express, etc.
Requires
- php: >=5.4
Requires (Dev)
- php: >=7.2
- mockery/mockery: ^1.3
- phpunit/phpunit: ^8.0.0
This package is auto-updated.
Last update: 2024-11-08 07:17:19 UTC
README
A PHP package to perform operations on debit and credit cards like format, validate brand, number and Luhn algorithm. It validates popular brands like Visa, Mastercard, American Express, etc.
This package is based on the braintree/credit-card-type javascript package. All the card types configuration have been extracted from it.
Current Cart Type Validators
- Visa
- Mastercard
- American Express
- Diners Club
- Discover
- JCB
- UnionPay
- Maestro
- Elo
- Mir
- Hiper
- Hipercard
- Troy
- Cabal
Installation
The preferred way to install this extension is through composer.
With Composer installed, you can then install the extension using the following commands:
$ php composer.phar require jlorente/php-credit-cards
or add
... "require": { "jlorente/php-credit-cards": "*" }
to the require
section of your composer.json
file.
Usage
You can create an instance of the validator either by using the common constructor or the static make method.
$validator = new CreditCardValidator();
or
$validator = CreditCardValidator::make();
By default, the validator will load the configuration of all the card types that come along with the package, but you can limit the allowed types by providing an array with the card type codes.
$validator = new CreditCardValidator([ CreditCardValidator::TYPE_VISA, CreditCardValidator::TYPE_MASTERCARD, ]);
or
$validator = CreditCardValidator::make([ CreditCardValidator::TYPE_VISA, CreditCardValidator::TYPE_MASTERCARD, ]);
Validating a credit card number without knowing the type
$validator->isValid('4242424242424242');
Validating a credit card number knowing the type
$validator->is(CreditCardValidator::TYPE_VISA, '4242424242424242');
or
$validator->isVisa('4242424242424242');
Get the type configuration of a card number
$typeConfig = $validator->getType('4242424242424242');
With the type configuration you can know metadata info, perform some validation or format the card number using the class methods.
CreditCardTypeConfig
Contribute
Feel free to add new credit card configurations or fix the current ones and create a pull request to keep the package up to date.
A credit type configuration has the following structure:
[ 'example-card' => [ 'niceType' => 'Test Card', // Display name 'type' => 'example-card', // Type/Code name 'patterns' => [ // Valid patterns for the card 272012, // Simple validator: true if the card begins with the pattern 272012 [5, 89], // Range validator: true if the card initial two digits value is between 5 and 89 both included ], 'gaps' => [4, 10], // Values where to put white spaces on pretty card formatting. In this example: XXXX XXXXXX XXXXXX 'lengths' => [ // Valid lengths for the card 15, // Simple validator: True if length is exactly 15 [17, 19], // Range validator: True if length is between 17 and 19 both included ], 'code' => [ // Security code configuration 'name' => 'CVV', // Name of the security code 'size' => 3, // Valid length of the security code ], 'luhnCheck' => true // To validate the Luhn's algorithm when calling matches ], ];
License
Copyright © 2020 José Lorente Martín jose.lorente.martin@gmail.com.
Licensed under the MIT License. See LICENSE.txt for details.