Skip to content

Commit

Permalink
fix iban generator for czech accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
c4t-dr34m committed Dec 23, 2024
1 parent 4c2e7bf commit 94ace0d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class IBAN {
throw ValidationException(message = "Account prefix & number: Invalid value")
}

val prefixFormatted: String = Formatter.format("%06d", prefix ?: "000000")
val prefixFormatted: String = Formatter.format("%06d", prefix ?: 0)
val accountFormatted: String = Formatter.format("%010d", account)
val bankFormatted: String = Formatter.format("%04d", bank)

Expand Down Expand Up @@ -58,7 +58,8 @@ class IBAN {
}
checksum = 98 - checksum

return "CZ" + Formatter.format("%02d", checksum) + bankFormatted + prefixFormatted + accountFormatted
val accountForIban = Formatter.format("%02d", checksum) + bankFormatted + prefixFormatted + accountFormatted
return "CZ$accountForIban"
}

// Validate account prefix and account number
Expand All @@ -67,7 +68,7 @@ class IBAN {
var weight = 1
var sum = 0

for (i in 0.. number.length step -1) {
for (i in number.length - 1 downTo 0 step 1) {
sum += (number[i] - '0') * weight
weight *= 2
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.stepuplabs.spaydkmp.common

import io.stepuplabs.spaydkmp.exception.ValidationException
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

internal class IBANTest {
private val iban = IBAN()

@Test
fun czechAccount() {
assertEquals(
iban.createForCzechAccount(null, account = 76327632, bank = 300),
"CZ7603000000000076327632",
)
}
}

0 comments on commit 94ace0d

Please sign in to comment.