Sha256: 3659be90d19be14c20f0440467069475c3f7297a01ad40888cb661238fa97071

Contents?: true

Size: 1 KB

Versions: 3

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Buckaruby
  # Helper for calculating the IBAN for a given account number, bank code and country code.
  module Iban
    module_function

    def calculate_iban(account_number, bank_code, country_code = "NL")
      if account_number.nil? || account_number.to_s.empty?
        raise ArgumentError, "Invalid account number"
      end

      if bank_code.nil? || bank_code.to_s.empty?
        raise ArgumentError, "Invalid bank code"
      end

      if country_code.nil? || !country_code.match(/^[A-Z]{2}$/)
        raise ArgumentError, "Invalid or unknown country code"
      end

      account_identification = bank_code + account_number.rjust(10, '0')
      country_code_added = account_identification + country_code
      all_numbers = country_code_added.gsub(/[A-Z]/) { |p| (p.respond_to?(:ord) ? p.ord : p[0]) - 55 } + '00'
      verification_number = (98 - (all_numbers.to_i % 97)).to_s.rjust(2, '0')

      country_code + verification_number + account_identification
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
buckaruby-1.7.0 lib/buckaruby/iban.rb
buckaruby-1.6.0 lib/buckaruby/iban.rb
buckaruby-1.5.0 lib/buckaruby/iban.rb