arturmkrtchyan / iban4j

A Java library for generation and validation of the International Bank Account Numbers (IBAN ISO_13616) and Business Identifier Codes (BIC ISO_9362).
http://iban4j.org
Apache License 2.0
271 stars 124 forks source link

Gives incorrect result? Please advise #83

Closed breakline87 closed 3 years ago

breakline87 commented 3 years ago

I'm trying with Hungarian bank accounts. This is a Hungarian bank account format:

11600006-00000000-34953226

This is how it looks:

116 is the bank code 0000 is the branch code 6 is the national digit 0000000034953226 is the account number (16 long)

According to the converter here: https://www.iban.com/calculate-iban

This is the IBAN number for that account: HU74116000060000000034953226

So putting this into code:

Iban iban = Iban.valueOf("HU74116000060000000034953226"); Iban.Builder builder = new Iban.Builder(); System.out.println(iban.getNationalCheckDigit()); //gives 6, correct builder.nationalCheckDigit(iban.getNationalCheckDigit()); System.out.println(iban.getCountryCode()); //gives HU, correct builder.countryCode(iban.getCountryCode()); System.out.println(iban.getAccountNumber()); //gives 6000000003495322, incorrect builder.accountNumber(iban.getAccountNumber()); System.out.println(iban.getBankCode()); builder.bankCode(iban.getBankCode()); //gives 116, correct System.out.println(iban.getBranchCode()); builder.branchCode(iban.getBranchCode()); //gives 0000, correct Iban iban2 = builder.build(true); System.out.println(iban2.toString()); //gives HU74116000060000000034953226, correct

So this gives me the same IBAN:

Iban iban = new Iban.Builder() .bankCode("116") .branchCode("0000") .nationalCheckDigit("6") .accountNumber("6000000003495322") .countryCode(CountryCode.HU) .build(true); System.out.println("IBAN: "+iban.toString());

However, with the correct account number:

Iban iban = new Iban.Builder() .bankCode("116") .branchCode("0000") .nationalCheckDigit("6") .accountNumber("0000000034953226") .countryCode(CountryCode.HU) .build(true); System.out.println("IBAN: "+iban.toString());

I get a totally different IBAN number: HU36116000000000000349532266

This is presented as "valid", but this is an incorrect number.

Looks like the numbers inside the account number are shifted in some way?

FrankHossfeld commented 3 years ago

Yeap, I think, you are right.

Looking into the structure for HU, we'll find the following definition:

structures.put(CountryCode.HU,
                new BbanStructure(
                        BbanStructureEntry.bankCode(3, 'n'),
                        BbanStructureEntry.branchCode(4, 'n'),
                        BbanStructureEntry.accountNumber(16, 'n'),
                        BbanStructureEntry.nationalCheckDigit(1, 'n')));

I think, the national check digit should be located before the account number.

FrankHossfeld commented 3 years ago

I have updated my clone and published a new SNAPSHOT.

@breakline87 Would be nice, if you could test the fix. To use the lib, please add the following dependency:

<dependency>
  <groupId>com.github.nalukit</groupId>
  <artifactId>iban4g</artifactId>
  <version>HEAD-SNAPSHOT</version>
</dependency>

Thanks

breakline87 commented 3 years ago

Hey, so I also did a hotfix to the jar itself, so basically had to move the national check digit forward, that worked.

FrankHossfeld commented 3 years ago

Correct, that was the problem.

I did the fix and will release a new version of iban4g (2.0.1) at the weekend.

breakline87 commented 3 years ago

Thank you, very nice, I'm gonna update to yours. I'll close this.

FrankHossfeld commented 3 years ago

Done ... should land in Maven Central in a few hours ...