jlorente/php-credit-cards
Composer 安装命令:
composer require jlorente/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.
关键字:
README 文档
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.
use Jlorente\CreditCards\CreditCardValidator; $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
| 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:
[
'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.
jlorente/php-credit-cards 适用场景与选型建议
jlorente/php-credit-cards 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 453.08k 次下载、GitHub Stars 达 44, 最近一次更新时间为 2020 年 09 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「card」 「validate」 「debit」 「cards」 「creditcard」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jlorente/php-credit-cards 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jlorente/php-credit-cards 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jlorente/php-credit-cards 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Runn Me! Validation and Sanitization Library
Supercharged text field validation.
Settings Card for Laravel Nova.
A tool for scraping URL resources (oEmbed, OpenGraph, Twitter cards, JSON-LD)
Integration bundle for Aura.Input with Aura.Filter
Bureaux A Partager Edit - Parse, validate, manipulate, and display dates in PHP w/ i18n support. Inspired by moment.js
统计信息
- 总下载量: 453.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 44
- 点击次数: 50
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-09-25