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

cant convert bban to iban because bban for Norway is appended with "null" which increases its length and validation is failing #71

Closed sanathkd closed 11 months ago

sanathkd commented 4 years ago

BBAN for Norway is appended with "null" which increases its length and hence validation is failing. Can you help me if i'm missing something? Or can this issue be fixed?

I feel its messed up in "iban.java" under method "formatIban()" when converting "toString" in the end.

        private String formatIban() {
            final StringBuilder sb = new StringBuilder();
            sb.append(countryCode.getAlpha2());
            sb.append(DEFAULT_CHECK_DIGIT);
            sb.append(formatBban());
            return sb.toString();
        }

Test Data BBAN: 44350343730 Code used:

String set = "44350343730";
String bankCode = "4435";
String account = "0343730";

Iban.Builder builder = new Iban.Builder();
builder.countryCode(CountryCode.NO);
builder.bankCode(bankCode);
builder.accountNumber(account);

Iban value = builder.build();

Error thrown:

Exception in thread "main" org.iban4j.IbanFormatException: [44350343730null] length is 15, expected BBAN length is: 11
    at org.iban4j.IbanUtil.validateBbanLength(IbanUtil.java:362)
    at org.iban4j.IbanUtil.validate(IbanUtil.java:79)
    at org.iban4j.Iban$Builder.build(Iban.java:365)
    at org.iban4j.Iban$Builder.build(Iban.java:337)
hajk1 commented 3 years ago

Norway BBAN structure requires 'National Check digit' and 6 character 'Account number'. I think you are providing both in the account number field which is not correct. You might change your code to the below: `

        String bankCode = "4435";
        String account = "034373";
        String nationalCheckDigit = "0";

        Iban.Builder builder = new Iban.Builder();
        builder.countryCode(CountryCode.NO);
        builder.bankCode(bankCode);
        builder.accountNumber(account);
        builder.nationalCheckDigit(nationalCheckDigit);
        Iban value = builder.build();

`

By the way, it is much better to validate this in the library. I will commit a change in order to validate such a case.