1. Go to this page and download the library: Download ccgenerator/test-cards library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use CCGenerator\TestCards\Brand;
use CCGenerator\TestCards\TestCards;
// One card, random network
$card = TestCards::generate();
// A specific network — string or enum, whichever suits the caller
TestCards::generate('amex');
TestCards::generate(Brand::Amex);
// A length that network actually issues
TestCards::generate('maestro', length: 19);
// A batch for a data provider
$cards = TestCards::many(100, 'visa');
// One of every network: 14, 15, 16 and 19 digits, 3 and 4-digit CVVs
$corpus = TestCards::oneOfEach();
// A number that looks right and fails the checksum
$typo = TestCards::invalid('visa');
// Structural validation
$result = TestCards::validate('4111 1111 1111 1111');
$result->valid; // true
$result->brand; // Brand::Visa
$result->errors; // []
// config/test-cards.php
return [
'seed' => env('TEST_CARDS_SEED'), // null = fresh numbers every run
'expiry_years_ahead' => 5,
'accepted_networks' => [], // [] = every network in the table
];
use TestCards; // the facade alias
use CCGenerator\TestCards\CardService; // or inject the service
class CheckoutSeeder
{
public function __construct(private CardService $cards) {}
public function run(): void
{
$card = $this->cards->generate('visa');
// ...
}
}
TestCards::generate('amex');
TestCards::many(50);
TestCards::validate($request->input('card_number'));
use CCGenerator\TestCards\CardService;
use CCGenerator\TestCards\CardValidator;
final class CheckoutFixtures
{
public function __construct(
private readonly CardService $cards,
private readonly CardValidator $validator,
) {
}
public function aVisaCard(): string
{
return $this->cards->generate('visa')->pan;
}
}
use CCGenerator\TestCards\Symfony\Validator\TestCardNumber;
use Symfony\Component\Validator\Constraints as Assert;
final class CheckoutInput
{
#[Assert\NotBlank]
#[TestCardNumber(networks: ['visa', 'mastercard'])]
public string $cardNumber = '';
// A stored number where the checksum is no longer meaningful
#[TestCardNumber(
#[TestCardNumber(
networks: ['visa'],
message: 'card.number.invalid', // a translation key works too
)]