Swedish bank tools

Build Status

Ruby gem to validate, normalize/prettify and to some extent interpret

  • Swedish bank account numbers
  • Swedish plusgiro numbers
  • Swedish bankgiro numbers

It can also generate and unpack payment OCR numbers (reference codes) with a check digit, optional length digit and optional padding digits.

This gem does what it can to weed out invalid numbers but errs on the side of allowing too much, in the absence of good specifications, so be advised that a "valid" number might still be incorrect.

Inspired by iulianu/iban-tools. Please consider contributing gems for your country.

Usage

# Bankgiro

 = BankTools::SE::Bankgiro.new(" 5402968 1 ")
.valid?  # => true
.errors  # => []
.normalize  # => "5402-9681"

 = BankTools::SE::Bankgiro.new(" 1X ")
.valid?  # => false
.errors  # => [ :too_short, :invalid_characters, :bad_checksum ]
.normalize  # => " 1 "

# 90-konto
 = BankTools::SE::Bankgiro.new("902-0033")
.fundraising?  # => true

# OCR

BankTools::SE::OCR.from_number("123")  # => "1230"
BankTools::SE::OCR.from_number("123", length_digit: true)  # => "12351"
BankTools::SE::OCR.from_number("123", length_digit: true, pad: "0")  # => "123067"

BankTools::SE::OCR.to_number("1230")  # => "123"
BankTools::SE::OCR.to_number("123067", length_digit: true, pad: "0")  # => "123"
BankTools::SE::OCR.to_number("1230", length_digit: true, pad: "0")  # Raises exception because there's no length digit or padding.

# This feature is intended to try to find all OCR numbers in a noisy bank statement string.
# By design it may find too many numbers (e.g. valid substrings of other numbers), so you should check results against actual outstanding invoices.
# By default, it excludes OCRs shorter than 4 digits and longer than 19 digits, but this limit can be specified per below.
BankTools::SE::OCR.find_all_in_string("OCR1230 and ref4564 and 7890")  # => [ "1230", "4564", … ]
BankTools::SE::OCR.find_all_in_string("1230 and 123067", length_digit: true, pad: "0")  # => [ "123067" ]
BankTools::SE::OCR.find_all_in_string("00 and 18 and 1230", min_length: 2, max_length: 3)  # => [ "00", "018", "18" ]

# Plusgiro

 = BankTools::SE::Plusgiro.new("2865434")
.valid?  # => true
.errors  # => []
.normalize  # => "28 65 53-4"

 = BankTools::SE::Plusgiro.new(" 1X ")
.valid?  # => false
.errors  # => [ :too_short, :invalid_characters, :bad_checksum ]
.normalize  # => " 1 "

# 90-konto
 = BankTools::SE::Plusgiro.new("90 20 03-3")
.fundraising?  # => true


# Bank account

 = BankTools::SE::Account.new("11000000000")
.valid?  # => true
.errors  # => []
.normalize  # => "1100-0000000"
.bank  # => "Nordea"
.clearing_number  # => "1100"
.serial_number  # => "0000000"

 = BankTools::SE::Account.new(" 0000-0000000X ")
.valid?  # => false
.errors  # => [ :invalid_characters, :unknown_clearing_number ]
.bank  # => nil
.normalize  # => " 0000-0000000X "


# Error codes

BankTools::SE::Errors::TOO_SHORT                # => :too_short
BankTools::SE::Errors::TOO_LONG                 # => :too_long
BankTools::SE::Errors::INVALID_CHARACTERS       # => :invalid_characters
BankTools::SE::Errors::BAD_CHECKSUM             # => :bad_checksum
BankTools::SE::Errors::UNKNOWN_CLEARING_NUMBER  # => :unknown_clearing_number

Installation

Add this line to your application's Gemfile:

gem 'banktools-se'

And then execute:

$ bundle

Or install it yourself as:

$ gem install banktools-se

TODO

Possible improvements to make:

Also see

Credits and license

By Henrik Nyh for Barsoom under the MIT license:

Copyright (c) 2011 Barsoom AB

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.