English Deutsch

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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