Skip to content

Validator For Real World

Shesh Ghimire edited this page Jan 11, 2026 · 8 revisions

This example assumes Luhn Validator package is installed.

💡 Even if Luhn package is not installed, the validation returns the same result. Its just Luhn Validation is skipped.

PHPUnit Test file covers more examples on how to use the validator.

namespace App\Validation;

use TheWebSolver\Codegarage\PaymentCard\PaymentCardFactory;
use TheWebSolver\Codegarage\Test\Fixture\PaymentCardValidator;

$discover = [
    'alias'      => 'discover',
    'name'       => 'Discover',
    'breakpoint' => [4, 8, 12],
    'code'       => ['name' => "CID", 'size' => 3],
    'length'     => [16, 19],
    'idRange'    => [6011, [644, 649], 65],
];
// Co-branded with Discover for international network and has matching ID Range "65".
$troy = [
    'alias'      => 'troy',
    'name'       => 'Troy',
    'breakpoint' => [4, 8, 12],
    'code'       => ['name' => "CID", 'size' => 3],
    'length'     => [16],
    'idRange'    => [65, 9792],
];


$domesticCardsFactory      = new PaymentCardFactory(['troy' => $troy /* ...other domestic cards */]);
$internationalCardsFactory = new PaymentCardFactory(['discover' => $discover /* ...other globally accepted cards */]);

// Passing in domestic factory as first argument will use the TROY payload data,
// and never even uses international factory when validating TROY card number.
$validator = new PaymentCardValidator( $domesticCardsFactory, $internationalCardsFactory );
$validator->validate(6500830000000002); // true. Validates using domestic cards factory only.
$validator->getCoveredCardStatus();     // ['troy' => Status::Success]

$validator = new PaymentCardValidator( $domesticCardsFactory, $internationalCardsFactory );
$validator->validate(6011000990139424); // true. Validation fails on domestic but passes with international cards factory.
$validator->getCoveredCardStatus();     // ['troy' => Status::Failure, 'discover' => Status::Success]

Clone this wiki locally