cebugle / laravel-credit-card-rule
Validate credit card number, expiration date, cvc
v2.0.0
2024-03-18 06:34 UTC
Requires
- php: ^8.1
- illuminate/support: ^11.0
- illuminate/translation: ^11.0
- illuminate/validation: ^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~3.14
- orchestra/testbench: ^9.0
- phpunit/phpunit: ^11.0
This package is auto-updated.
Last update: 2024-11-18 08:13:23 UTC
README
This rule will validate that a given credit card number, expiration date or cvc is valid.
Installation
composer require cebugle/laravel-credit-card-rule
Usage
As FormRequest
<?php namespace App\Http\Requests; use Cebugle\CreditCard\CardCvc; use Cebugle\CreditCard\CardNumber; use Cebugle\CreditCard\CardExpirationYear; use Cebugle\CreditCard\CardExpirationMonth; use Illuminate\Foundation\Http\FormRequest; class CreditCardRequest extends FormRequest { /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'card_number' => ['required', new CardNumber], 'expiration_year' => ['required', new CardExpirationYear($this->get('expiration_month'))], 'expiration_month' => ['required', new CardExpirationMonth($this->get('expiration_year'))], 'cvc' => ['required', new CardCvc($this->get('card_number'))] ]; } }
Card number
From request
$request->validate( ['card_number' => '37873449367100'], ['card_number' => new Cebugle\CreditCard\CardNumber] );
Directly
(new Cebugle\CreditCard\Cards\Visa) ->setCardNumber('4012888888881881') ->isValidCardNumber()
Card expiration
From request
// CardExpirationYear requires card expiration month $request->validate( ['expiration_year' => '2017'], ['expiration_year' => ['required', new Cebugle\CreditCard\CardExpirationYear($request->get('expiration_month'))]] ); // CardExpirationMonth requires card expiration year $request->validate( ['expiration_month' => '11'], ['expiration_month' => ['required', new Cebugle\CreditCard\CardExpirationMonth($request->get('expiration_year'))]] ); // CardExpirationDate requires date format $request->validate( ['expiration_date' => '02-18'], ['expiration_date' => ['required', new Cebugle\CreditCard\CardExpirationDate('my')]] );
Directly
Cebugle\CreditCard\Cards\ExpirationDateValidator( $expiration_year, $expiration_month )->isValid(); // Or static Cebugle\CreditCard\Cards\ExpirationDateValidator::validate( $expiration_year, $expiration_month );
Card CVC
From request
// CardCvc requires card number to determine allowed cvc length $request->validate( ['cvc' => '123'], ['cvc' => new Cebugle\CreditCard\CardCvc($request->get('card_number'))] );
Directly
Cebugle\CreditCard\Cards\Card::isValidCvcLength($cvc);
License
This project is licensed under an Apache 2.0 license which you can find in this LICENSE.
Feedback
If you have any feedback, comments or suggestions, please feel free to open an issue within this repository!