| 1 |
package iban |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"math/big" |
| 7 |
"strings" |
| 8 |
) |
| 9 |
|
| 10 |
const countryDE = "DE" |
| 11 |
const germanAccountIDLength = 10 |
| 12 |
const maxAllowedIBANLength = 34 |
| 13 |
|
| 14 |
// IsValid validates the IBAN for its proof number |
| 15 |
func IsValid(input string) bool { |
| 16 |
iban, err := From(input) |
| 17 |
if err != nil { |
| 18 |
return false |
| 19 |
} |
| 20 |
return iban.Valid() |
| 21 |
} |
| 22 |
|
| 23 |
// NewGerman calculates the Iban for the provided bankID and accountID. |
| 24 |
// It will return an error if the accountID can not be parsed as int. |
| 25 |
// It returns only valid german IBANs, as it is hard coded to use german |
| 26 |
// settings. |
| 27 |
func NewGerman(bankID, accountID string) (IBAN, error) { |
| 28 |
if len(accountID) == (germanAccountIDLength - 1) { |
| 29 |
accountID = "0" + accountID |
| 30 |
} |
| 31 |
|
| 32 |
bban := fmt.Sprintf("%s%s", bankID, accountID) |
| 33 |
|
| 34 |
return New(countryDE, bban) |
| 35 |
} |
| 36 |
|
| 37 |
// New returns a new IBAN for the provided countryCode and BBAN |
| 38 |
func New(countryCode string, BBAN string) (IBAN, error) { |
| 39 |
if len(countryCode) != 2 { |
| 40 |
return "", fmt.Errorf("malformed countryCode: must have two characters") |
| 41 |
} |
| 42 |
countryCode = strings.ToUpper(countryCode) |
| 43 |
|
| 44 |
ibanLen, ok := ibanMaxLengthForCountry[countryCode] |
| 45 |
if !ok { |
| 46 |
ibanLen = maxAllowedIBANLength |
| 47 |
} |
| 48 |
if len(BBAN) == 0 { |
| 49 |
return "", fmt.Errorf("missing BBAN") |
| 50 |
} |
| 51 |
if len(BBAN) > (ibanLen - 4) { |
| 52 |
return "", fmt.Errorf("malformed BBAN: must have at max %d characters", ibanLen) |
| 53 |
} |
| 54 |
|
| 55 |
BBAN = strings.ToUpper(BBAN) |
| 56 |
|
| 57 |
step1 := fmt.Sprintf("%s%s00", transformLettersToDigits(BBAN), transformLettersToDigits(countryCode)) |
| 58 |
step2, ok := new(big.Int).SetString(step1, 10) |
| 59 |
if !ok { |
| 60 |
return "", fmt.Errorf("Malformed iban string: %s", step1) |
| 61 |
} |
| 62 |
step3 := step2.Mod(step2, big.NewInt(97)) |
| 63 |
proofNumber := new(big.Int).Sub(big.NewInt(98), step3) |
| 64 |
|
| 65 |
iban := fmt.Sprintf("%s%02d%s", countryCode, proofNumber.Int64(), BBAN) |
| 66 |
return IBAN(iban), nil |
| 67 |
} |
| 68 |
|
| 69 |
// From creates an IBAN from the provided input. If the input is not a valid IBAN, it |
| 70 |
// returns an error. |
| 71 |
func From(input string) (IBAN, error) { |
| 72 |
countryCode := input[:2] |
| 73 |
proofNumber := input[2:4] |
| 74 |
bban := input[4:] |
| 75 |
iban, err := New(countryCode, bban) |
| 76 |
if err != nil { |
| 77 |
return "", err |
| 78 |
} |
| 79 |
if proofNumber != iban.ProofNumber() { |
| 80 |
return "", fmt.Errorf("proof number invalid") |
| 81 |
} |
| 82 |
return iban, nil |
| 83 |
} |
| 84 |
|
| 85 |
// IBAN represents an International Bank Account Number. |
| 86 |
// It is defined by ISO 13616:2007 |
| 87 |
type IBAN string |
| 88 |
|
| 89 |
// BBAN returns the BBAN, that is, the bank account identifier |
| 90 |
func (i IBAN) BBAN() string { |
| 91 |
return string(i[4:]) |
| 92 |
} |
| 93 |
|
| 94 |
// BankID returns the parts of the IBAN which refer to the bank institute ID |
| 95 |
func (i IBAN) BankID() string { |
| 96 |
return string(i[4:12]) |
| 97 |
} |
| 98 |
|
| 99 |
// AccountID returns the parts of the IBAN which refer to the AccountID |
| 100 |
func (i IBAN) AccountID() string { |
| 101 |
accountID := string(i[12:]) |
| 102 |
return strings.TrimLeft(accountID, "0") |
| 103 |
} |
| 104 |
|
| 105 |
// CountryCode returns the country code used by the IBAN. |
| 106 |
// The country code is defined by ISO 3166-1 alpha-2 |
| 107 |
func (i IBAN) CountryCode() string { |
| 108 |
return string(i[:2]) |
| 109 |
} |
| 110 |
|
| 111 |
// ProofNumber returns the used number to make a sanity check whether the IBAN is valid or not. |
| 112 |
func (i IBAN) ProofNumber() string { |
| 113 |
return string(i[2:4]) |
| 114 |
} |
| 115 |
|
| 116 |
// Valid returns true if the IBAN is valid, false otherwise |
| 117 |
func (i IBAN) Valid() bool { |
| 118 |
if len(i) > maxAllowedIBANLength { |
| 119 |
return false |
| 120 |
} |
| 121 |
countryCode := i.CountryCode() |
| 122 |
countryCode = strings.ToUpper(countryCode) |
| 123 |
bban := i.BBAN() |
| 124 |
bban = strings.ToUpper(bban) |
| 125 |
|
| 126 |
digitCountryCode := transformLettersToDigits(countryCode) |
| 127 |
digitBBAN := transformLettersToDigits(bban) |
| 128 |
checkSumString := fmt.Sprintf("%s%s%s", digitBBAN, digitCountryCode, i.ProofNumber()) |
| 129 |
checkSum, ok := new(big.Int).SetString(checkSumString, 10) |
| 130 |
if !ok { |
| 131 |
return false |
| 132 |
} |
| 133 |
mod := checkSum.Mod(checkSum, big.NewInt(97)) |
| 134 |
return mod.Cmp(big.NewInt(1)) == 0 |
| 135 |
} |
| 136 |
|
| 137 |
// String returns the string representation of i |
| 138 |
func (i IBAN) String() string { |
| 139 |
return string(i) |
| 140 |
} |
| 141 |
|
| 142 |
// Print returns the IBAN in paper format, i.e. with spaces after every fourth |
| 143 |
// character |
| 144 |
func Print(iban IBAN) string { |
| 145 |
ibanStr := iban.String() |
| 146 |
for len(ibanStr)%4 != 0 { |
| 147 |
ibanStr += " " |
| 148 |
} |
| 149 |
var out bytes.Buffer |
| 150 |
for i := 4; i <= len(ibanStr); i += 4 { |
| 151 |
out.WriteString(ibanStr[i-4 : i]) |
| 152 |
out.WriteString(" ") |
| 153 |
} |
| 154 |
return strings.TrimSpace(out.String()) |
| 155 |
} |
| 156 |
|
| 157 |
func transformLettersToDigits(letters string) string { |
| 158 |
var replaced []string |
| 159 |
for k, v := range alphaToDigit { |
| 160 |
replaced = append(replaced, k) |
| 161 |
replaced = append(replaced, v) |
| 162 |
} |
| 163 |
return strings.NewReplacer(replaced...).Replace(letters) |
| 164 |
} |
| 165 |
|
| 166 |
var alphaToDigit = map[string]string{ |
| 167 |
"A": "10", |
| 168 |
"B": "11", |
| 169 |
"C": "12", |
| 170 |
"D": "13", |
| 171 |
"E": "14", |
| 172 |
"F": "15", |
| 173 |
"G": "16", |
| 174 |
"H": "17", |
| 175 |
"I": "18", |
| 176 |
"J": "19", |
| 177 |
"K": "20", |
| 178 |
"L": "21", |
| 179 |
"M": "22", |
| 180 |
"N": "23", |
| 181 |
"O": "24", |
| 182 |
"P": "25", |
| 183 |
"Q": "26", |
| 184 |
"R": "27", |
| 185 |
"S": "28", |
| 186 |
"T": "29", |
| 187 |
"U": "30", |
| 188 |
"V": "31", |
| 189 |
"W": "32", |
| 190 |
"X": "33", |
| 191 |
"Y": "34", |
| 192 |
"Z": "35", |
| 193 |
} |
| 194 |
|
| 195 |
var ibanMaxLengthForCountry = map[string]int{ |
| 196 |
"AL": 28, |
| 197 |
"AD": 24, |
| 198 |
"AZ": 28, |
| 199 |
"BH": 22, |
| 200 |
"BE": 16, |
| 201 |
"BA": 20, |
| 202 |
"BR": 29, |
| 203 |
"VG": 24, |
| 204 |
"BG": 22, |
| 205 |
"CR": 22, |
| 206 |
"DK": 18, |
| 207 |
"DE": 22, |
| 208 |
"DO": 28, |
| 209 |
"SV": 28, |
| 210 |
"EE": 20, |
| 211 |
"FO": 18, |
| 212 |
"FI": 18, |
| 213 |
"FR": 27, |
| 214 |
"GE": 22, |
| 215 |
"GI": 23, |
| 216 |
"GR": 27, |
| 217 |
"GL": 18, |
| 218 |
"GB": 22, |
| 219 |
"GT": 28, |
| 220 |
"IQ": 23, |
| 221 |
"IE": 22, |
| 222 |
"IS": 26, |
| 223 |
"IL": 23, |
| 224 |
"IT": 27, |
| 225 |
"JO": 30, |
| 226 |
"KZ": 20, |
| 227 |
"QA": 29, |
| 228 |
"XK": 20, |
| 229 |
"HR": 21, |
| 230 |
"KW": 30, |
| 231 |
"LV": 21, |
| 232 |
"LB": 28, |
| 233 |
"LI": 21, |
| 234 |
"LT": 20, |
| 235 |
"LU": 20, |
| 236 |
"MT": 31, |
| 237 |
"MR": 27, |
| 238 |
"MU": 30, |
| 239 |
"MK": 19, |
| 240 |
"MD": 24, |
| 241 |
"MC": 27, |
| 242 |
"ME": 22, |
| 243 |
"NL": 18, |
| 244 |
"NO": 15, |
| 245 |
"AT": 20, |
| 246 |
"PK": 24, |
| 247 |
"PS": 29, |
| 248 |
"PL": 28, |
| 249 |
"PT": 25, |
| 250 |
"RO": 24, |
| 251 |
"LC": 32, |
| 252 |
"SM": 27, |
| 253 |
"ST": 25, |
| 254 |
"SA": 24, |
| 255 |
"SE": 24, |
| 256 |
"CH": 21, |
| 257 |
"RS": 22, |
| 258 |
"SC": 31, |
| 259 |
"SK": 24, |
| 260 |
"SI": 19, |
| 261 |
"ES": 24, |
| 262 |
"TL": 23, |
| 263 |
"TR": 26, |
| 264 |
"CZ": 24, |
| 265 |
"TN": 24, |
| 266 |
"UA": 29, |
| 267 |
"HU": 28, |
| 268 |
"AE": 23, |
| 269 |
"BY": 28, |
| 270 |
"CY": 28, |
| 271 |
} |
| 272 |
var sepaSupportForCountry = map[string]bool{ |
| 273 |
"AL": false, |
| 274 |
"AD": false, |
| 275 |
"AZ": false, |
| 276 |
"BH": false, |
| 277 |
"BE": true, |
| 278 |
"BA": false, |
| 279 |
"BR": false, |
| 280 |
"VG": false, |
| 281 |
"BG": true, |
| 282 |
"CR": false, |
| 283 |
"DK": true, |
| 284 |
"DE": true, |
| 285 |
"DO": false, |
| 286 |
"SV": false, |
| 287 |
"EE": true, |
| 288 |
"FO": true, |
| 289 |
"FI": true, |
| 290 |
"FR": true, |
| 291 |
"GE": false, |
| 292 |
"GI": true, |
| 293 |
"GR": true, |
| 294 |
"GL": true, |
| 295 |
"GB": true, |
| 296 |
"GT": false, |
| 297 |
"IQ": false, |
| 298 |
"IE": true, |
| 299 |
"IS": true, |
| 300 |
"IL": false, |
| 301 |
"IT": true, |
| 302 |
"JO": false, |
| 303 |
"KZ": false, |
| 304 |
"QA": false, |
| 305 |
"XK": false, |
| 306 |
"HR": true, |
| 307 |
"KW": false, |
| 308 |
"LV": true, |
| 309 |
"LB": false, |
| 310 |
"LI": true, |
| 311 |
"LT": true, |
| 312 |
"LU": true, |
| 313 |
"MT": true, |
| 314 |
"MR": false, |
| 315 |
"MU": false, |
| 316 |
"MK": false, |
| 317 |
"MD": false, |
| 318 |
"MC": true, |
| 319 |
"ME": false, |
| 320 |
"NL": true, |
| 321 |
"NO": true, |
| 322 |
"AT": true, |
| 323 |
"PK": false, |
| 324 |
"PS": false, |
| 325 |
"PL": true, |
| 326 |
"PT": true, |
| 327 |
"RO": true, |
| 328 |
"LC": false, |
| 329 |
"SM": true, |
| 330 |
"ST": false, |
| 331 |
"SA": false, |
| 332 |
"SE": true, |
| 333 |
"CH": true, |
| 334 |
"RS": false, |
| 335 |
"SC": false, |
| 336 |
"SK": true, |
| 337 |
"SI": true, |
| 338 |
"ES": true, |
| 339 |
"TL": false, |
| 340 |
"TR": false, |
| 341 |
"CZ": true, |
| 342 |
"TN": false, |
| 343 |
"UA": false, |
| 344 |
"HU": true, |
| 345 |
"AE": false, |
| 346 |
"BY": false, |
| 347 |
"CY": true, |
| 348 |
} |