IBAN + BIC
When IBAN validation is not enough.
Features
- IBAN validation (
ActiveModel::EachValidatorand control digits calculator function) and fixing. - National account digits control validation (currently only ES and PT are included, others countries can be added).
- Associated tags to countries (currently only SEPA and FIXED_CHECK tags are available).
- BICs mapping from IBAN bank code part: COUNTRY + BANK => BIC code.
- Currently, static data only includes some ES banks, PRs are welcomed.
- Optional database model to allow apps to dynamically add new BICs mapping.
- Random IBANs generator
Usage
- Validator: add IBAN validation to your models.
validates :iban, iban: true
You can also validate that IBAN is from a SEPA country.
validates :iban, iban: { tags: [:sepa] }
- IBAN control digits calculation
2.4.1 :001 > IbanBic.calculate_check("ES0000030000300000000000")
=> 87
- IBAN parsing
2.4.1 :001 > IbanBic.parse("ES8700030000300000000000")
=> {"country"=>"ES", "iban_check"=>"87", "bank"=>"0003", "branch"=>"0000", "check"=>"30", "account"=>"0000000000"}
- IBAN fixing (IBAN control digits and country control digits, if that code is available)
2.4.1 :001 > IbanBic.fix("ES0000030000200000000000")
=> "ES8700030000300000000000"
- BIC calculation (bank code must be in the static file or in the database)
2.4.1 :001 > IbanBic.calculate_bic("ES8700030000300000000000")
=> "BDEPESM1XXX"
- Random IBAN generation
2.4.1 :001 > require "iban_bic/random"
=> true
2.4.1 :002 > IbanBic.random_iban
=> "MU52BOIR2768144336487102000AWQ"
2.4.1 :003 > IbanBic.random_iban country: "ES"
=> "ES6111051493192369291292"
2.4.1 :004 > IbanBic.random_iban tags: [:sepa]
=> "FI5584518206233159"
2.4.1 :005 > IbanBic.random_iban not_tags: [:sepa]
=> "IL317532867920826062774"
Note: It can't generate a valid IBAN code for some countries where iban check digits are fixed if validation code for that country is not available.
Installation
- Add this line to your application's Gemfile
gem "iban_bic"
- Update bundle
$ bundle
- Run installer
3.a. Using BICs dynamic data from database
Add a bics table to your database and an initializer file for configuration:
$ bundle exec rails generate iban_bic:install
$ bundle exec rake db:migrate
Load static data to the created bics table:
$ bundle exec rake iban_bic:load_data
3.b. Using static data from YAML files
Create an initializer file for configuration:
$ bundle exec rails generate iban_bic:install --with-static-data
- Customize initializer if needed, adding validations for new countries, or overriding YAML files.
Changelog
1.0.0
Added IBAN fixing and random IBAN generator.
Changed country checks, they must change check parts to generate the correct IBAN. Comparison against the original IBAN is made by the caller.
0.1.0
- First version.
Contributing
Contribution directions go here.
License
The gem is available as open source under the terms of the MIT License.