-
Notifications
You must be signed in to change notification settings - Fork 0
Home
📦 This Payment Card package is a sub-repo split of a PHP Validator which is currently under development.
This package can be used to validate various Payment Cards such as Debit/Credit Card. For more flexibility, the Luhn validation is not enforced by default but can easily be incorporated with first-party package.
If Luhn Algorithm package is not installed, the Luhn validation is skipped. Meaning the Card Number will never be checked against Luhn Algorithm and will always pass the Luhn Validation.
- Payment Card Interface: Contract to create implementation of Payment Card
- Payment Card Factory: Card Factory to create Payment Cards on demand from an array, a PHP file, or a JSON file (recommended)
- Payment Card Type: Base class used by Card Factory that can be extended for specific card type. Eg: test Napas Card
- Traits: Promote composition over inheritance for maximum flexibility and fluent Developer Experience
composer require thewebsolver/payment-cardTo use Luhn validation using the first-party package:
composer require thewebsolver/luhn-algorithmTo validate Payment Card, either using defaults or from your Payment Cards defined in a JSON file, we first need to define a class that uses Payment Card Resolver.
In the first-party PHP Validator package (currently in development and unfortunately not available), this is provided as a validation rule and you do not need to manually create your own class.
For more details, check with cached JSON file as well as other test resources on how Payment Card Type can be defined using Payment Card Schema.
Using Test Payment Card Validator in your bootstrap file.
namespace App\Validation;
use TheWebSolver\Codegarage\PaymentCard\PaymentCardFactory;
use TheWebSolver\Codegarage\Test\Fixture\PaymentCardValidator;
$payload = 'path/to/Resource/paymentCards.json';
$americanExpressCardNumber = 378282246310005;
$discoverCardNumber = 6493505952542224798;
$allCardsValidator = new PaymentCardValidator(new PaymentCardFactory($payload));
$allCardsValidator->validate($americanExpressCardNumber); // true
$allCardsValidator->validate($discoverCardNumber); // true
// If application only allows subset of payment cards, then allowed payload indices can be passed as a second parameter.
$allowedPayloadIndices = [ 'americanExpress', 'mastercard', 'visa' ];
$allowedCardsValidator = new PaymentCardValidator(new PaymentCardFactory($payload, $allowedPayloadIndices));
$allowedCardsValidator->validate($americanExpressCardNumber); // true
$allowedCardsValidator->validate($discoverCardNumber); // false