Download the PHP package jlorente/php-credit-cards without Composer
On this page you can find all versions of the php package jlorente/php-credit-cards. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jlorente/php-credit-cards
More information about jlorente/php-credit-cards
Files in jlorente/php-credit-cards
Package php-credit-cards
Short Description 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.
License MIT
Informations about the package php-credit-cards
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.
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:
or add
to the 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.
or
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.
or
Validating a credit card number without knowing the type
Validating a credit card number knowing the type
or
Get the type configuration of a card number
With the type configuration you can know metadata info, perform some validation or format the card number using the class methods.
CreditCardTypeConfig
Method | Description | Return example |
---|---|---|
getType(): string | Get the type of the card type configuration | "visa", "mastercard" |
getNiceType(): string | Get the nice type of the card type configuration | "Visa", "Mastercard" |
getPatterns(): array | Get the patterns that the card type configuration uses to validate a card number | [50, [55, 59]] |
getGaps(): array | Get the index of the position in card number string where to put blank spaces on card formatting | [4, 8, 12] |
getLengths(): array | Get the allowed lengths that the card type configuration uses to validate the card numbers | [16, 19] |
getCode(): string | Get the security code configuration of the card type | ["name" => "CVC", "size" => 4] |
getLuhnCheck(): bool | Get the luhn check value of the configuration | true |
setLuhnCheck(bool $value): $this | Set the luhn check value. If true, the validator will validate the card number with the Luhn's algorithm | |
matches(string $cardNumber): bool | Check if the given card number matches the card type configuration | false |
matchesPatterns(string $cardNumber): bool | Check if the card number matches one of the patterns array configuration | true |
matchesLengths(string $cardNumber): bool | Check if the card number matches one of the lengths array configuration | true |
satisfiesLuhn(string $cardNumber): bool | Check if the card number satisfies the luhn's algorithm | true |
matchesSecurityCode(string $cardNumber): bool | Check if the card number satisfies the luhn's algorithm | false |
format(string $cardNumber): string | Format the card number according to the gap configuration | "4242 4242 4242 4242" |
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:
License
Copyright © 2020 José Lorente Martín [email protected].
Licensed under the MIT License. See LICENSE.txt for details.