Ibandit 
Ibandit is a Ruby library for manipulating and validating
IBANs.
The primary objective is to provide an interface that enables the storage and retrieval of national banking details as a single value. This may be an IBAN, if a country fully and unambiguously supports it, or a combination of IBAN and/or pseudo-IBAN.
Therefore, there are three distinct modes:
- For countries that support any form of IBAN: construct and validate IBAN from national banking details
- For countries that have unambiguous IBANs: deconstruct an IBAN into national banking details
- For countries where either of the above is not possible: a pseudo-IBAN as a substitute for the above.
For storage, you should always try to use the pseudo_iban
, falling back to iban
if it is not available.
For example:
- Sweden does support IBANs (1.) but the format is ambiguous due to variable length account numbers so they cannot be deconstructed (2.). For persistence, we therefore recommend using pseudo-IBANs (3.) because the national banking details can be recovered from them.
- Australia does not support IBANs (1. & 2.), therefore pseudo-IBANs (3.) can be created from national banking details for storage. To get back the national banking details, you can pass the pseudo-IBAN to Ibandit and it will parse out the national banking details again for use.
Supported Countries
Australia | | | :white_check_mark: |
Austria | :white_check_mark: | :white_check_mark: | |
Belgium | :white_check_mark: | :white_check_mark: | |
Bulgaria | :white_check_mark: | :white_check_mark: | |
Croatia | :white_check_mark: | :white_check_mark: | |
Canada | | | :white_check_mark: |
Cyprus | :white_check_mark: | :white_check_mark: | |
Czech Republic | :white_check_mark: | :white_check_mark: | |
Denmark | :white_check_mark: | :white_check_mark: | |
Estonia | :white_check_mark: | :white_check_mark: | |
Finland | :white_check_mark: | :white_check_mark: | |
France | :white_check_mark: | :white_check_mark: | |
Germany | :white_check_mark: | :white_check_mark: | |
Greece | :white_check_mark: | :white_check_mark: | |
Hungary | :white_check_mark: | :white_check_mark: | |
Ireland | :white_check_mark: | :white_check_mark: | |
Iceland | :white_check_mark: | :white_check_mark: | |
Italy | :white_check_mark: | :white_check_mark: | |
Latvia | :white_check_mark: | :white_check_mark: | |
Lithuania | :white_check_mark: | :white_check_mark: | |
Luxembourg | :white_check_mark: | :white_check_mark: | |
Monaco | :white_check_mark: | :white_check_mark: | |
Malta | :white_check_mark: | :white_check_mark: | |
Netherlands | :white_check_mark: | :white_check_mark: | |
Norway | :white_check_mark: | :white_check_mark: | |
New Zealand | | | :white_check_mark: |
Poland | :white_check_mark: | :white_check_mark: | |
Portugal | :white_check_mark: | :white_check_mark: | |
Romania | :white_check_mark: | :white_check_mark: | |
San Marino | :white_check_mark: | :white_check_mark: | |
Slovakia | :white_check_mark: | :white_check_mark: | |
Slovenia | :white_check_mark: | :white_check_mark: | |
Spain | :white_check_mark: | :white_check_mark: | |
Sweden | :white_check_mark: | | :white_check_mark: |
United Kingdom | :white_check_mark: | :white_check_mark: | |
USA | | | :white_check_mark: |
Usage
Installation
You don't need this source code unless you want to modify the gem. If you just
want to use it, you should run:
gem install ibandit
Creating IBANs
All functionality is based around IBAN
objects. To create one, simply pass a
string to Ibandit::IBAN.new
:
iban = Ibandit::IBAN.new("xq75 B a dCode 666")
iban.to_s
iban.to_s(:formatted)
Alternatively, you can create an IBAN from national banking details.
Validating an IBAN
IBANs are validated based on their structure and check-digits:
iban = Ibandit::IBAN.new("XQ75 BADCODE 666")
iban.valid?
After validations, you can fetch details of any errors:
iban.errors
The following error keys may be set:
country_code
bank_code
branch_code
account_number
check_digits
characters
length
format
Ibandit will also apply local modulus checks if you set a modulus checker:
module ModulusChecker
def self.valid_bank_code?(iban)
end
def self.valid_branch_code?(iban)
end
def self.valid_account_number?(iban)
end
end
Ibandit.modulus_checker = ModulusChecker
All three of the valid_bank_code?
, valid_branch_code?
and valid_account_number?
methods will receive an IBAN
object.
valid_bank_code?
and valid_branch_code?
should return true unless it is known that the bank/branch code in this IBAN
are invalid in the country specified. valid_account_number?
should return true unless it is known that the account number
in this IBAN cannot be valid due to local modulus checking rules.
Deconstructing an IBAN into national banking details
SWIFT define the following components for IBANs, and publish details of how each
country combines them:
country_code
: The ISO 3166-1 country code prefix
check_digits
: Two digits calculated using part of the ISO/IEC 7064:2003 standard
swift_bank_code
: The SWIFT identifier for the bank to which the IBAN refers
swift_branch_code
: The SWIFT identifer for the branch to which the IBAN refers (not used in all countries)
swift_account_number
: The account number for the account
swift_national_id
: The national ID for the bank / branch as documented by SWIFT
The SWIFT IBAN components are all available as methods on an IBAN
object:
iban = Ibandit::IBAN.new("GB82 WEST 1234 5698 7654 32")
iban.country_code
iban.check_digits
iban.swift_bank_code
iban.swift_branch_code
iban.swift_account_number
iban.swift_national_id
In addition, it is often useful to extract any local check digits from the IBAN.
These are available through a local_check_digits
method:
iban = Ibandit::IBAN.new("ES12 1234 5678 9112 3456 7890")
iban.local_check_digits
In some countries, the SWIFT-defined details differ from the local details that
customers are familiar with. For this reason, there are also bank_code
,
branch_code
and account_number
methods on an IBAN
object. At present,
these only differ from the swift_
equivalents for Swedish bank accounts.
iban = Ibandit::IBAN.new(
country_code: 'SE',
account_number: '7507-1211203'
)
iban.swift_account_number
iban.account_number
iban.swift_branch_code
iban.branch_code
Initializing Ibandit
The UK and Ireland both use part of the BIC as the bank_code
in their IBANs.
If you wish to construct UK or Irish IBANs you will either need to pass the
bank_code
explicitly, or configure Ibandit with a BIC finder:
Ibandit.bic_finder = -> (country_code, national_id) do
BankDirectoryPlus.find_by(country_code: country_code,
national_id: national_id).
try(:bic)
end
Creating an IBAN from national banking details
In many countries customers are familiar with national details rather than
their IBAN. For example, in the UK customers use their Account Number and Sort
Code.
To build an IBAN from local details:
iban = Ibandit::IBAN.new(
country_code: 'AT',
account_number: '234573201',
bank_code: '19043'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'BE',
account_number: '510-0075470-61'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'BG',
bank_code: 'BNBG',
branch_code: '9661'
account_number: '1020345678'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'HR',
account_number: '1001005-1863000160',
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'CY',
account_number: '1200527600',
bank_code: '002',
branch_code: '00128'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'CZ',
bank_code: '0800',
account_number_prefix: '19',
account_number: '2000145399'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'DK',
account_number: '345-3179681',
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'EE',
account_number: '111020145685'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'FI',
bank_code: '123456'
account_number: '785'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'FR',
bank_code: '20041',
branch_code: '01005',
account_number: '0500013M02606',
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'DE',
bank_code: '37040044',
account_number: '0532013000'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'GR',
bank_code: '011',
branch_code: '0125',
account_number: '0000000012300695'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'HU',
account_number: '11773016-11111018'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'IE',
bank_code: 'AIBK',
branch_code: '931152',
account_number: '12345678'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'IS',
bank_code: '1175'
account_number: '26-19530-670269-6399'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'IT',
bank_code: '05428',
branch_code: '11101',
account_number: '000000123456'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'LV',
account_number: '1234567890123',
bank_code: 'BANK'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'LT',
account_number: '11101001000',
bank_code: '10000'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'LU',
account_number: '1234567890123',
bank_code: 'BANK'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'MC',
bank_code: '20041',
branch_code: '01005',
account_number: '0500013M026'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'MT',
bank_code: 'MMEB',
branch_code: '44093',
account_number: '9027293051'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'NL',
account_number: '0417164300',
bank_code: 'ABNA'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'NO',
account_number: '8601.1117947',
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'PL',
account_number: '60102010260000042270201111',
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'PT',
bank_code: '0002',
branch_code: '0023',
account_number: '0023843000578'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'RO',
bank_code: 'AAAA',
account_number: '1B31007593840000'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'SM',
bank_code: '05428',
branch_code: '11101',
account_number: '000000123456'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'SK',
bank_code: '1200',
account_number_prefix: '19',
account_number: '8742637541'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'SI',
bank_code: '19100',
account_number: '1234'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'ES',
account_number: '23100001180000012345'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'SE',
account_number: '7507-1211203'
)
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'GB',
bank_code: 'BARC',
branch_code: '200000',
account_number: '55779911'
)
iban.iban
Pseudo-IBANs
Pseudo-IBANs can be recognized by the fact that they have ZZ
as the third and fourth characters (these would be check digits for a regular
IBAN). Note: pseudo-IBANs can be used in conjunction with IBANs depending on the country. See Supported Countries.
iban = Ibandit::IBAN.new(
country_code: 'SE',
branch_code: '7507',
account_number: '1211203'
)
iban.pseudo_iban
iban.iban
iban = Ibandit::IBAN.new('SEZZX7507XXX1211203')
iban.country_code
iban.branch_code
iban.account_number
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'AU',
branch_code: '123-456',
account_number: '123456789'
)
iban.pseudo_iban
iban.iban
iban = Ibandit::IBAN.new('AUZZ123456123456789')
iban.country_code
iban.branch_code
iban.account_number
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'CA',
bank_code: '0036',
branch_code: '00063',
account_number: '0123456'
)
iban.pseudo_iban
iban.iban
iban = Ibandit::IBAN.new('CAZZ003600063000000123456')
iban.country_code
iban.bank_code
iban.branch_code
iban.account_number
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'NZ',
bank_code: '01',
branch_code: '0004',
account_number: '3333333-44'
)
iban.pseudo_iban
iban.iban
iban = Ibandit::IBAN.new(
country_code: 'NZ',
account_number: '01-0004-3333333-44'
)
iban.pseudo_iban
iban.bank_code
iban.branch_code
iban.account_number
iban = Ibandit::IBAN.new('NZZZ0100043333333044')
iban.country_code
iban.bank_code
iban.branch_code
iban.account_number
iban = Ibandit::IBAN.new(
country_code: 'US',
bank_code: '026073150',
account_number: '2715500356'
)
iban.pseudo_iban
iban.iban
iban = Ibandit::IBAN.new('USZZ026073150_______2715500356')
iban.country_code
iban.bank_code
iban.account_number
iban.iban
Other libraries
Another gem, iban-tools, also
exists and is an excellent choice if you only require basic IBAN validation.
We built Ibandit because iban-tools doesn't provide a comprehensive, consistent
interface for the construction and deconstruction of IBANs into national
details.
GoCardless ♥ open source. If you do too, come join us.