Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Php Iban Laravel Package

globalcitizen/php-iban

PHP library to parse, validate, generate, and format IBAN/IIBANs. Extracts country, checksum, BBAN, bank/branch/account codes, supports legacy national checksums, conversions (human/machine), obfuscation, test IBANs, and typo-based correction suggestions.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to First Use

  1. Installation

    composer require globalcitizen/php-iban
    

    Add to composer.json if not using autoloading:

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "GlobalCitizen\\IBAN\\": "vendor/globalcitizen/php-iban/src/"
        }
    }
    

    Run composer dump-autoload.

  2. Basic Validation

    use GlobalCitizen\IBAN\IBAN;
    
    $iban = new IBAN('DE89370400440532013000');
    if ($iban->isValid()) {
        echo "Valid IBAN for " . $iban->getBank()->getCountryCode();
    }
    
  3. Generate an IBAN

    $iban = IBAN::generate('DE', '37040044', '5320130000');
    echo $iban->getNumber();
    

First Use Case: Form Validation

use GlobalCitizen\IBAN\IBAN;
use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
    'account_number' => [
        'required',
        function ($attribute, $value, $fail) {
            $iban = new IBAN($value);
            if (!$iban->isValid()) {
                $fail('The '.$attribute.' must be a valid IBAN.');
            }
        }
    ]
]);

Implementation Patterns

Core Workflows

  1. Validation & Parsing

    $iban = new IBAN('LT9273000100123456789');
    if ($iban->isValid()) {
        $country = $iban->getBank()->getCountryCode();
        $bic = $iban->getBank()->getBIC();
        $account = $iban->getAccountNumber();
    }
    
  2. Error Correction

    $iban = new IBAN('LT92730001001234567890'); // Invalid (extra digit)
    $corrected = $iban->correct();
    if ($corrected) {
        echo "Corrected IBAN: " . $corrected->getNumber();
    }
    
  3. IBAN Generation

    // Generate for Germany (DE)
    $iban = IBAN::generate('DE', 'DEUTDEBB', '12345678');
    echo $iban->getNumber(); // DE89370400440123456789
    
  4. Country-Specific Rules

    $iban = new IBAN('FR1420041010050500013M02606');
    if ($iban->isValid()) {
        $countryRules = $iban->getBank()->getCountryRules();
        echo "Account length: " . $countryRules->getAccountNumberLength();
    }
    

Integration Tips

  • Laravel Request Validation Use custom validation rules in app/Providers/AppServiceProvider.php:

    Validator::extend('valid_iban', function ($attribute, $value, $parameters, $validator) {
        return (new IBAN($value))->isValid();
    });
    

    Then in your form request:

    $this->rules = [
        'iban' => 'required|valid_iban',
    ];
    
  • Database Storage Store IBANs as strings (e.g., VARCHAR(34)) in MySQL. The package handles all validation logic in PHP.

  • Internationalization Use IBAN::getCountryList() to fetch supported countries for dropdowns:

    $countries = IBAN::getCountryList();
    foreach ($countries as $country) {
        echo "<option value='{$country->getCode()}'>{$country->getName()}</option>";
    }
    
  • Testing Use the IBANTestCase trait (if available) or mock the IBAN class:

    $mockIban = $this->createMock(IBAN::class);
    $mockIban->method('isValid')->willReturn(true);
    

Gotchas and Tips

Pitfalls

  1. Case Sensitivity IBANs are case-insensitive, but the package may return uppercase letters. Normalize with:

    $iban->getNumber(); // Always uppercase
    
  2. Country-Specific Validation Not all IBANs follow the same rules. For example:

    • Germany (DE): Requires 10-digit account number + 8-digit branch code.
    • France (FR): Uses a 11-digit account number + 5-digit branch code. Validate with:
    if (!$iban->isValid()) {
        $errors = $iban->getErrors();
        // Handle country-specific errors (e.g., wrong length)
    }
    
  3. IIBAN Support The package supports IIBAN (International IBAN-like formats, e.g., for cryptocurrencies). Ensure you’re not mixing them with standard IBANs:

    if ($iban->isIIBAN()) {
        // Handle IIBAN-specific logic
    }
    
  4. BIC/BICSF Validation Some countries (e.g., Germany) require BIC/BICSF validation. The package may not auto-validate these—check manually:

    $bic = $iban->getBank()->getBIC();
    if (empty($bic)) {
        throw new \InvalidArgumentException("BIC is required for this country.");
    }
    
  5. Performance Avoid instantiating IBAN for every request in a loop. Cache validated IBANs or use a service layer:

    class IBANService {
        private $cache = [];
    
        public function validate($ibanString) {
            if (!isset($this->cache[$ibanString])) {
                $this->cache[$ibanString] = (new IBAN($ibanString))->isValid();
            }
            return $this->cache[$ibanString];
        }
    }
    

Debugging Tips

  1. Error Messages Use getErrors() to debug validation failures:

    $iban = new IBAN('INVALID123');
    print_r($iban->getErrors());
    // Output: Array ( [0] => Invalid IBAN checksum )
    
  2. Logging Log invalid IBANs for analysis:

    if (!$iban->isValid()) {
        \Log::warning("Invalid IBAN submitted: {$iban->getNumber()}", [
            'errors' => $iban->getErrors(),
            'country' => $iban->getBank()?->getCountryCode(),
        ]);
    }
    
  3. Testing Edge Cases Test with:

    • Minimum/maximum length (e.g., AL47212110090000000235698741 for Albania).
    • Non-Latin characters (e.g., LT9273000100123456789).
    • Corrupted IBANs (e.g., swapped digits).

Extension Points

  1. Custom Country Rules Extend GlobalCitizen\IBAN\Country\CountryRules for unsupported countries:

    class CustomCountryRules extends CountryRules {
        public function __construct() {
            $this->setAccountNumberLength(16);
            $this->setBranchCodeLength(8);
        }
    }
    

    Register in a service provider:

    IBAN::addCountry('XX', new CustomCountryRules());
    
  2. Override Validation Logic Extend the IBAN class to add custom checks:

    class CustomIBAN extends IBAN {
        public function isBlacklisted() {
            return in_array($this->getNumber(), ['BLACKLISTED1', 'BLACKLISTED2']);
        }
    }
    
  3. Hooks for Post-Validation Use events (if supported) or callbacks:

    $iban = new IBAN('DE89370400440532013000');
    if ($iban->isValid()) {
        event(new \App\Events\ValidIBANSubmitted($iban));
    }
    
  4. Fallback for Unsupported Countries Handle unsupported IBANs gracefully:

    try {
        $iban = new IBAN('ZZ1234567890');
    } catch (\InvalidArgumentException $e) {
        // Fallback logic for unsupported countries
    }
    

Configuration Quirks

  • BIC Database: The package relies on an internal BIC database. For large-scale
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor