jlorente/php-credit-cards

一个用于在借记卡和信用卡上执行操作的PHP包,如格式化、验证品牌、号码和Luhn算法。它验证了Visa、Mastercard、American Express等流行品牌。

1.0.3 2024-07-16 12:01 UTC

This package is auto-updated.

Last update: 2024-09-16 12:21:20 UTC


README

一个用于在借记卡和信用卡上执行操作的PHP包,如格式化、验证品牌、号码和Luhn算法。它验证了Visa、Mastercard、American Express等流行品牌。

此包基于braintree/credit-card-type JavaScript包。所有卡片类型配置都是从其中提取的。

当前卡片类型验证器

  • Visa
  • Mastercard
  • American Express
  • Diners Club
  • Discover
  • JCB
  • UnionPay
  • Maestro
  • Elo
  • Mir
  • Hiper
  • Hipercard
  • Troy

安装

安装此扩展的首选方式是通过composer

安装Composer后,您可以使用以下命令安装扩展

$ php composer.phar require jlorente/php-credit-cards

或添加

...
    "require": {
        "jlorente/php-credit-cards": "*"
    }

到您的composer.json文件的require部分。

使用方法

您可以通过使用公共构造函数或静态make方法来创建验证器的实例。

$validator = new CreditCardValidator();

$validator = CreditCardValidator::make();

默认情况下,验证器将加载与包一起提供的所有卡片类型的配置,但您可以通过提供一个包含卡片类型代码的数组来限制允许的类型。

$validator = new CreditCardValidator([
    CreditCardValidator::TYPE_VISA,
    CreditCardValidator::TYPE_MASTERCARD,
]);

$validator = CreditCardValidator::make([
    CreditCardValidator::TYPE_VISA,
    CreditCardValidator::TYPE_MASTERCARD,
]);

不知道类型的信用卡号码验证

$validator->isValid('4242424242424242');

知道类型的信用卡号码验证

$validator->is(CreditCardValidator::TYPE_VISA, '4242424242424242');

$validator->isVisa('4242424242424242');

获取卡片号码的类型配置

$typeConfig = $validator->getType('4242424242424242');

使用类型配置,您可以了解元数据信息,执行一些验证或使用类方法格式化卡片号码。

CreditCardTypeConfig

贡献

请随意添加新的信用卡配置或修复现有的配置,并创建一个pull request以保持包的更新。

信用卡类型配置具有以下结构

[
    '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
    ],
];

许可证

版权所有 © 2020 José Lorente Martín jose.lorente.martin@gmail.com

在MIT许可证下发布。有关详细信息,请参阅LICENSE.txt。