GithubHelp home page GithubHelp logo

keely4u / laravel-credit-cards Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jlorente/laravel-credit-cards

0.0 0.0 0.0 11 KB

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

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%

laravel-credit-cards's Introduction

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.

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 [email protected].

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

laravel-credit-cards's People

Contributors

jlorente avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.