PHP code example of carddetective / card-provider-detector
1. Go to this page and download the library: Download carddetective/card-provider-detector 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/ */
carddetective / card-provider-detector example snippets
use CardDetective\CardProviderDetector\CardDetection\CardDetective;
$detector = new CardDetective();
// Detect card provider
$provider = $detector->detectCardProvider('4111111111111111');
echo $provider; // "Visa"
// Validate card number
$isValid = $detector->isCardNumberValid('4111111111111111');
echo $isValid ? 'Valid' : 'Invalid'; // "Valid"
// Mask card number
$masked = $detector->maskCardNumber('4111111111111111');
echo $masked; // "4111********1111"
// Format card number
$formatted = $detector->formatCardNumber('4111111111111111');
echo $formatted; // "4111 1111 1111 1111"
use CardDetective\CardProviderDetector\CardDetection\CardDetective;
$detector = new CardDetective();
$cardInfo = $detector->getCardInfo('4111111111111111');
// Access card information
echo $cardInfo->provider; // "Visa"
echo $cardInfo->type; // "Credit/Debit"
echo $cardInfo->length; // 16
echo $cardInfo->validLengths; // [13, 16, 19]
echo $cardInfo->hasSecurityCode; // true
echo $cardInfo->securityCodeLength; // 3
echo $cardInfo->isValid; // true
echo $cardInfo->maskedNumber; // "4111********1111"
echo $cardInfo->formattedNumber; // "4111 1111 1111 1111"
echo $cardInfo->features; // ["chip", "contactless", "online_payments"]
// Convert to array or JSON
$array = $cardInfo->toArray();
$json = $cardInfo->toJson();
// Validate CVV/CVC code
$isValidCvv = $detector->isValidSecurityCode('4111111111111111', '123');
echo $isValidCvv ? 'Valid CVV' : 'Invalid CVV';
// Validate expiry date
$isValidExpiry = $detector->isValidExpiryDate('12', '2025');
echo $isValidExpiry ? 'Valid expiry' : 'Invalid expiry';
// Check if provider is supported
$isSupported = $detector->isProviderSupported('Visa');
echo $isSupported ? 'Supported' : 'Not supported';
// Get all supported providers
$providers = $detector->getSupportedProviders();
print_r($providers); // Array of all supported provider names
// Custom mask character
$masked = $detector->maskCardNumber('4111111111111111', 'X');
echo $masked; // "4111XXXXXXXX1111"
// Custom separator for formatting
$formatted = $detector->formatCardNumber('4111111111111111', '-');
echo $formatted; // "4111-1111-1111-1111"
'providers' => [
// ...
CardDetective\CardProviderDetector\Providers\CardDetectiveServiceProvider::class,
],
'aliases' => [
// ...
'CardDetective' => CardDetective\CardProviderDetector\Facades\CardDetective::class,
],
use CardDetective;
// Detect card provider
$provider = CardDetective::detectCardProvider('4111111111111111');
// Get comprehensive card info
$cardInfo = CardDetective::getCardInfo('4111111111111111');
// Validate card
$isValid = CardDetective::isCardNumberValid('4111111111111111');
// Mask card number
$masked = CardDetective::maskCardNumber('4111111111111111');
// Format card number
$formatted = CardDetective::formatCardNumber('4111111111111111');
// Security validation
$isValidCvv = CardDetective::isValidSecurityCode('4111111111111111', '123');
$isValidExpiry = CardDetective::isValidExpiryDate('12', '2025');
use CardDetective\CardProviderDetector\CardDetection\CardDetective;
class PaymentController extends Controller
{
public function processPayment(CardDetective $detector)
{
$cardInfo = $detector->getCardInfo(request('card_number'));
if (!$cardInfo->isValid) {
return response()->json(['error' => 'Invalid card number'], 400);
}
// Process payment...
}
}
// config/carddetective.php
return [
// Default mask character
'mask_character' => '*',
// Default card number separator
'card_number_separator' => ' ',
// Enable strict validation
'strict_validation' => true,
// Enabled card providers
'enabled_providers' => [
'visa', 'mastercard', 'amex', 'diners', 'jcb', 'discover',
'rupay', 'unionpay', 'maestro', 'elo', 'mir', 'hipercard',
],
// Custom providers
'custom_providers' => [
'my_custom_provider' => [
'regex' => '/^1234[0-9]{0,}$/',
'name' => 'My Custom Provider',
'type' => 'Credit',
'valid_lengths' => [16],
'has_security_code' => true,
'security_code_length' => 3,
'features' => ['chip', 'contactless'],
],
],
// Security settings
'security' => [
'validate_security_code' => true,
'validate_expiry_date' => true,
'expiry_buffer_months' => 0,
],
// Logging settings
'logging' => [
'enabled' => false,
'level' => 'info',
'channel' => 'default',
],
];
use CardDetective\CardProviderDetector\Exceptions\InvalidCardNumberException;
use CardDetective\CardProviderDetector\Exceptions\UnsupportedCardTypeException;
try {
$provider = $detector->detectCardProvider('');
} catch (InvalidCardNumberException $e) {
// Handle invalid card number
echo $e->getMessage(); // "Card number cannot be empty"
}
// In your configuration
'custom_providers' => [
'my_bank' => [
'regex' => '/^1234[0-9]{0,}$/',
'name' => 'My Bank Card',
'type' => 'Debit',
'valid_lengths' => [16],
'has_security_code' => true,
'security_code_length' => 3,
'features' => ['chip', 'online_payments'],
],
],
$cards = ['4111111111111111', '5555555555554444', '378282246310005'];
$results = [];
foreach ($cards as $card) {
$results[] = $detector->getCardInfo($card);
}
public function validatePaymentCard($cardNumber, $cvv, $expiryMonth, $expiryYear)
{
$detector = new CardDetective();
// Validate card number
if (!$detector->isCardNumberValid($cardNumber)) {
throw new InvalidCardException('Invalid card number');
}
// Validate CVV
if (!$detector->isValidSecurityCode($cardNumber, $cvv)) {
throw new InvalidCardException('Invalid CVV');
}
// Validate expiry date
if (!$detector->isValidExpiryDate($expiryMonth, $expiryYear)) {
throw new InvalidCardException('Card expired');
}
// Get card information
$cardInfo = $detector->getCardInfo($cardNumber);
return [
'provider' => $cardInfo->provider,
'type' => $cardInfo->type,
'masked_number' => $cardInfo->maskedNumber,
'features' => $cardInfo->features,
];
}
bash
php artisan vendor:publish --provider="CardDetective\CardProviderDetector\Providers\CardDetectiveServiceProvider" --tag="carddetective-config"