Skip to content

Commit

Permalink
Belgium BBAN check fix (#136)
Browse files Browse the repository at this point in the history
* Belgium BBAN check fix

The Belgian BBAN check has an issue when the checksum digits are exactly
"97". This is because the BBAN calculation is done using the "account
number" mod 97; normally, this would result in a checksum digit of "00",
but the standard in that case uses "97" instead.

Updated the checksum calculation to handle this case, plus updated the
related unit test.

* Docs update BE BBAN validation

Indicate in docs that Belgian BBAN is also checked.
  • Loading branch information
mhemeryck authored Feb 28, 2023
1 parent aedb821 commit 568a828
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions schwifty/checksum/belgium.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def compute(self, bban: str) -> str:
spec = registry.get("iban")
assert isinstance(spec, dict)
checksum = int(bban[: spec["BE"]["bban_length"] - self.checksum_length]) % 97
# Specific value in case checksum is expliclity 97
checksum = checksum if checksum != 0 else 97
return str(checksum).zfill(self.checksum_length)

def validate(self, bban: str) -> bool:
Expand Down
4 changes: 2 additions & 2 deletions schwifty/iban.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ def validate(self, validate_bban: bool = False) -> bool:
"""Validate the structural integrity of this IBAN.
This function will verify the country specific format as well as the Luhn checksum in the
3rd and 4th position of the IBAN. For some countries (currently Germany and Italy) it will
also verify the correctness of the country specific checksum within the BBAN if the
3rd and 4th position of the IBAN. For some countries (currently Belgium, Germany and Italy)
it will also verify the correctness of the country specific checksum within the BBAN if the
`validate_bban` parameter is set to `True`. For German banks it will pick the appropriate
algorithm based on the bank code and verify that the account code has the correct checksum.
Expand Down
4 changes: 4 additions & 0 deletions tests/test_checksum.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ def test_belgium_checksum():

def test_belgium_checksum_failure():
assert algorithms["BE:default"].validate("050000123456") is False


def test_belgium_checksum_checksum_edge_case():
assert algorithms["BE:default"].validate("050000017797") is True
1 change: 1 addition & 0 deletions tests/test_iban.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def test_iban_properties():
(("BE", "050", "123456"), "BE45050012345689"),
(("BE", "539", "0075470"), "BE68539007547034"),
(("FR", "2004101005", "0500013M026"), "FR1420041010050500013M02606"),
(("BE", "050", "177"), "BE54050000017797"),
],
)
def test_generate_iban(components, compact):
Expand Down

0 comments on commit 568a828

Please sign in to comment.