swift package - code.pfad.fr/swift - Go Packages (original) (raw)

package swift provides methods to check the validity of an IBAN (and retrieve the BIC for some countries). Play with the demo in the browser, powered by JS+Wasm.

// SPDX-FileCopyrightText: 2023 Olivier Charvin git@olivier.pfad.fr // // SPDX-License-Identifier: CC0-1.0

package main

import ( "fmt"

"code.pfad.fr/swift"
"code.pfad.fr/swift/bic"

)

var germanTestIBAN = "DE75512108001245126199"

func main() { iban, err := swift.NewIBAN(germanTestIBAN, swift.CountryBelongsToSEPA()) if err != nil { panic(err) } fmt.Println("IBAN:", iban.MaskedAndSpaced()) fmt.Println("BIC: ", bic.FromIBAN(iban).String()) }

Output:

IBAN: DE75 512* **** **** ***1 99 BIC: SOGEDEFFXXX

This section is empty.

ErrCountryNotInRegistry is returned when a country is not listed in the iban registry.

View Source

var ErrCountryOutsideSEPA = errors.New("country is outside the Single Euro Payments Area (SEPA)")

ErrCountryOutsideSEPA is returned when a country does not belong to the Single Euro Payments Area.

This section is empty.

BIC represents the routing information as specified by ISO 9362 (also known as Business Identifier Codes (BIC), SWIFT ID or SWIFT code, and SWIFT-BIC).

NewBIC uppercases a BIC and perform some basic checks (format and length). If the country code is unknown the BIC will be accepted, call CountryCode.BelongsToIBANRegistry or CountryCode.BelongsToSEPA to perform additional validation.

type CheckDigitsError struct { Expected int Actual int NationalCheck bool }

CheckDigitsError indicates that the checksum did not match the expected check digits.

CountryCode is the ISO 3166-1 alpha-2 code of the country (uppercase)

FormatError indicates that an unexpected character type was encountered.

type IBAN struct { CountryCode CountryCode CheckDigits int BBAN string }

IBAN represents an International Bank Account Number

NewIBAN sanitizes, parses and checks an IBAN (length, format, check digit sum). See CountryBelongsToIBANRegistry and CountryBelongsToSEPA to perform country code validation. See NationalFormatCheck to additionnally verify the national format.

func (IBAN) MaskedAndSpaced

func (iban IBAN) MaskedAndSpaced() string

MaskedAndSpaced returns the spaced IBAN with only the first and last 3 characters of the BBAN visible (other chars are replaced with "*").

IBANs are not secret, but it shouldn't hurt to hide parts of them. Since BBANs are longer than 11 characters, at least 5 chars will be hidden (still partially recoverable using the leading check digits). Example:

DE75 512* **** **** ***1 99

Spaced returns the IBAN with spaces between every 4 characters.

String returns the IBAN, without spaces

type IBANValidator func(IBAN) error

IBANValidator makes additional checks on a given IBAN.

func NationalFormatCheck() IBANValidator

NationalFormatCheck checks the national rules (format and check digits), if there is a registered BBANChecker for this country code.

type TooShortError struct { ActualLen int ExpectedLen int }

TooShortError indicates that the provided string is shorter than expected.