项目作者: laravel-validation-rules

项目描述 :
Credit Card Validation
高级语言: PHP
项目地址: git://github.com/laravel-validation-rules/credit-card.git
创建时间: 2017-09-18T06:42:17Z
项目社区:https://github.com/laravel-validation-rules/credit-card

开源协议:Apache License 2.0

下载


Laravel Validator Rules - Credit Card

This rule will validate that a given credit card number, expiration date or cvc is valid.




















Installation

  1. composer require laravel-validation-rules/credit-card

Usage

As FormRequest

  1. <?php
  2. namespace App\Http\Requests;
  3. use LVR\CreditCard\CardCvc;
  4. use LVR\CreditCard\CardNumber;
  5. use LVR\CreditCard\CardExpirationYear;
  6. use LVR\CreditCard\CardExpirationMonth;
  7. use Illuminate\Foundation\Http\FormRequest;
  8. class CreditCardRequest extends FormRequest
  9. {
  10. /**
  11. * Get the validation rules that apply to the request.
  12. *
  13. * @return array
  14. */
  15. public function rules()
  16. {
  17. return [
  18. 'card_number' => ['required', new CardNumber],
  19. 'expiration_year' => ['required', new CardExpirationYear($this->get('expiration_month'))],
  20. 'expiration_month' => ['required', new CardExpirationMonth($this->get('expiration_year'))],
  21. 'cvc' => ['required', new CardCvc($this->get('card_number'))]
  22. ];
  23. }
  24. }

Card number

From request

  1. $request->validate(
  2. ['card_number' => '37873449367100'],
  3. ['card_number' => new LVR\CreditCard\CardNumber]
  4. );

Directly

  1. (new LVR\CreditCard\Cards\Visa)
  2. ->setCardNumber('4012888888881881')
  3. ->isValidCardNumber()

Card expiration

From request

  1. // CardExpirationYear requires card expiration month
  2. $request->validate(
  3. ['expiration_year' => '2017'],
  4. ['expiration_year' => ['required', new LVR\CreditCard\CardExpirationYear($request->get('expiration_month'))]]
  5. );
  6. // CardExpirationMonth requires card expiration year
  7. $request->validate(
  8. ['expiration_month' => '11'],
  9. ['expiration_month' => ['required', new LVR\CreditCard\CardExpirationMonth($request->get('expiration_year'))]]
  10. );
  11. // CardExpirationDate requires date format
  12. $request->validate(
  13. ['expiration_date' => '02-18'],
  14. ['expiration_date' => ['required', new LVR\CreditCard\CardExpirationDate('my')]]
  15. );

Directly

  1. LVR\CreditCard\Cards\ExpirationDateValidator(
  2. $expiration_year,
  3. $expiration_month
  4. )->isValid();
  5. // Or static
  6. LVR\CreditCard\Cards\ExpirationDateValidator::validate(
  7. $expiration_year,
  8. $expiration_month
  9. );

Card CVC

From request

  1. // CardCvc requires card number to determine allowed cvc length
  2. $request->validate(
  3. ['cvc' => '123'],
  4. ['cvc' => new LVR\CreditCard\CardCvc($request->get('card_number'))]
  5. );

Directly

  1. LVR\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!