Skip to content
Shesh Ghimire edited this page Jan 18, 2026 · 6 revisions

📦 This Payment Card package is a sub-repo split of a PHP Validator which is currently under development.

Welcome

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.

Features

Quick Setup

Installation

composer require thewebsolver/payment-card

To use Luhn validation using the first-party package:

composer require thewebsolver/luhn-algorithm

Bootstrap

To 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.

Usage

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

Clone this wiki locally