joonis Logo

Accepting invalid German IBANs

Using the kontocheck package sometimes results in rejecting seemingly valid German IBANs. The reason is that some banks don't follow their own IBAN generation rules (eg. Deutsche Bank and Commerzbank).

Upon request we got the following information:

  • Deutsche Bank: The invalid IBANs can be used but transactions will be processed manually (Feb. 2018).
  • Commerzbank: The invalid IBANs must be used, otherwise transactions will be rejected (Feb. 2017).

To accept such IBANs you have to patch the kontocheck package with the following code:

import kontocheck

def normalize_iban(iban):
    """
    Try to correct a possibly incorrect IBAN.
    """
    iban = iban.upper()
    if not iban.startswith('DE'):
        return iban
    try:
        kontocheck._check_iban(iban, True)
    except kontocheck.KontoCheckError as err:
        #IBAN_CHKSUM_OK_UNTERKTO_MISSING = -129
        #IBAN_CHKSUM_OK_RULE_IGNORED = -130
        if err.code in (-129, -130):
            bankcode = iban[4:12]
            account = iban[12:]
            try:
                iban_new = kontocheck.create_iban(bankcode, account)
            except kontocheck.KontoCheckError:
                return iban
            bankcode_new = iban_new[4:12]
            account_new = iban_new[12:]
            # Only accept wrong zero padded account numbers
            # Known banks: Deutsche Bank, Commerzbank
            account = account.strip('0')
            account_new = account_new.strip('0')
            if bankcode == bankcode_new and account == account_new:
                return iban_new
    return iban

def _check_iban(iban, error=False):
    iban = normalize_iban(iban)
    return kontocheck._check_iban(iban, error)

kontocheck._check_iban = kontocheck.check_iban
kontocheck.check_iban = _check_iban

def _get_bic(iban):
    iban = normalize_iban(iban)
    return kontocheck._get_bic(iban)

kontocheck._get_bic = kontocheck.get_bic
kontocheck.get_bic = _get_bic

Can I
  help you?


Just drop me a line at
giraffe@joonis.de