jlorente/laravel-credit-cards

Laravel >=5.6 integration for the PHP Credit Cards package that allows to perform operations on debit and credit cards like format, validate brand, number and Luhn algorithm.

1.0.3 2023-09-07 11:38 UTC

This package is auto-updated.

Last update: 2024-04-07 12:49:20 UTC


README

Laravel >=5.6 integration for the PHP Credit Cards package that allows 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.

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/laravel-credit-cards

or add

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

to the require section of your composer.json file.

Configuration

  1. Register the ServiceProvider in your config/app.php service provider list.

config/app.php

return [
    //other stuff
    'providers' => [
        //other stuff
        \Jlorente\Laravel\CreditCards\CreditCardsServiceProvider::class,
    ];
];
  1. Add the following facade to the $aliases section.

config/app.php

return [
    //other stuff
    'aliases' => [
        //other stuff
        'CreditCardValidator' => \Jlorente\Laravel\CreditCards\Facades\CreditCardValidator::class,
    ];
];
  1. Publish the package configuration file.
$ php artisan vendor:publish --provider='Jlorente\Laravel\CreditCards\CreditCardsServiceProvider'
  1. If you want to limit the available credit card types in your system, you can use the credit_cards.php configuration file to specify them.

config/credit_cards.php

<?php

use Jlorente\CreditCards\CreditCardValidator;

return [
    'allowed_cards' => [
        CreditCardValidator::TYPE_VISA,
        CreditCardValidator::TYPE_MASTERCARD,
        CreditCardValidator::TYPE_AMERICAN_EXPRESS,
    ],
];

or in the env configuration by adding a string with array elements separeted by coma.

.env

//other configurations
CREDIT_CARDS_ALLOWED_CARDS="visa,mastercard,unionpay"

Usage

CreditCardValidator Facade

You can access a singleton of the CreditCardValidator class through the facade.

CreditCardValidator::isVisa($cardNumber);
CreditCardValidator::isMastercard($cardNumber);
$creditCardConfiguration = CreditCardValidator::getType($cardNumber);

To see a complete list of available methods of the package see the PHP Credit Cards documentation.

CreditCardRule

A rule to be use in the laravel validation flow is included in the package.

You can add the rule by using its string form:

class Request {

    public function rules() {
        return [
            'card' => 'credit_card'
        ];
    }
}

or the class form:

class Request {

    public function rules() {
        return [
            'card' => CreditCardRule::make()
        ];
    }
}

Validating security code format

If you provide an integer value as first argument of the rule, the format of the security code (CVC, CVV, etc.) will be validated.

class Request {

    public function rules() {
        return [
            'card' => 'credit_card:651'
        ];
    }
}

or

class Request {

    public function rules() {
        return [
            'card' => CreditCardRule::make(null, '651')
        ];
    }
}

Validating the credit card types

You can also provide a list separed by comas of the accepted credit card types. Remember that credit card types should be a subset of the configured system available types.

public function rules() {
    return [
        'card' => 'credit_card:' implode(',', [CreditCardValidator::TYPE_VISA, CreditCardValidator::TYPE_MASTERCARD]),
    ];
}

or

class Request {

    public function rules() {
        return [
            'card' => CreditCardRule::make([
                CreditCardValidator::TYPE_VISA, 
                CreditCardValidator::TYPE_MASTERCARD,
                CreditCardValidator::TYPE_AMERICAN_EXPRESS,
            ]),
        ];
    }
}

You can also combine credit cards types with security code validation:

class Request {

    public function rules() {
        return [
            'card' => 'credit_card:651,' . implode(',', [CreditCardValidator::TYPE_VISA, CreditCardValidator::TYPE_MASTERCARD]),
        ];
    }
}

or

class Request {

    public function rules() {
        return [
            'card' => CreditCardRule::make(
                [
                    CreditCardValidator::TYPE_VISA, 
                    CreditCardValidator::TYPE_MASTERCARD,
                    CreditCardValidator::TYPE_AMERICAN_EXPRESS,
                ], 
                '651',
            ),
        ];
    }
}

License

Copyright © 2020 José Lorente Martín jose.lorente.martin@gmail.com.

Licensed under the BSD 3-Clause License. See LICENSE.txt for details.