Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

82 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Banking

Go Reference

Go library for IBAN validation, IBAN generation, BBAN extraction, SEPA membership checks, and BIC parsing.

Install

go get github.com/jacoelho/banking

IBAN

IBAN validation uses country rules generated from the Swift ISO 13616 IBAN Registry and ISO/IEC 7064 MOD 97-10 check digits.

Validation is structural. It does not verify that an account exists or can receive transactions.

Test IBANs online

Usage

package main

import (
	"fmt"

	"github.com/jacoelho/banking/iban"
)

func main() {
	if err := iban.Validate("GB29NWBK60161331926819"); err != nil {
		panic(err)
	}

	corrected, err := iban.ReplaceChecksum("GB99NWBK60161331926819")
	if err != nil {
		panic(err)
	}
	fmt.Println(corrected)

	generated, err := iban.Generate("GB")
	if err != nil {
		panic(err)
	}
	fmt.Println(len(generated), generated[:2])

	generatedWithBBAN, err := iban.GenerateWithBBAN("GB", iban.BBANParts{
		BankCode:      "NWBK",
		BranchCode:    "601613",
		AccountNumber: "31926819",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(generatedWithBBAN)

	fmt.Println(iban.PaperFormat("GB29NWBK60161331926819"))

	bban, err := iban.GetBBAN("GB29NWBK60161331926819")
	if err != nil {
		panic(err)
	}
	fmt.Println(bban.BBAN, bban.BankCode, bban.BranchCode, bban.AccountNumber)

	isSEPA, err := iban.IsSEPA("GB29NWBK60161331926819")
	if err != nil {
		panic(err)
	}
	fmt.Println(isSEPA)

	isSEPACountry, err := iban.IsSEPACountryCode("GB")
	if err != nil {
		panic(err)
	}
	fmt.Println(isSEPACountry)
}

Output:

GB29NWBK60161331926819
22 GB
GB29NWBK60161331926819
GB29 NWBK 6016 1331 9268 19
NWBK60161331926819 NWBK 601613 31926819
true
true

Validation Errors

Validation returns structured errors that work with errors.Is and errors.As.

package main

import (
	"errors"
	"fmt"

	"github.com/jacoelho/banking/iban"
)

func main() {
	err := iban.Validate("GB99INVALID")

	if errors.Is(err, iban.ErrInvalidIBAN) {
		fmt.Println("invalid IBAN")
	}

	var validationErr *iban.ValidationError
	if errors.As(err, &validationErr) && validationErr.Reason == iban.ReasonInvalidLength {
		fmt.Printf("invalid length: expected %d, got %d\n",
			validationErr.ExpectedLength,
			validationErr.ActualLength)
	}
}

Output:

invalid IBAN
invalid length: expected 22, got 11

Validation reasons:

  • iban.ReasonInvalidLength
  • iban.ReasonInvalidChecksum
  • iban.ReasonInvalidCharacters
  • iban.ReasonUnsupportedCountry

Country-code APIs such as Generate and IsSEPACountryCode return *iban.CountryCodeError.

BIC

package main

import (
	"fmt"

	"github.com/jacoelho/banking/bic"
)

func main() {
	code, err := bic.Parse("ABCDBEB1XXX")
	if err != nil {
		panic(err)
	}

	fmt.Println(code.IsValid())
	fmt.Println(code.BIC8())
}

Output:

true
ABCDBEB1

Generate Registry Data

To regenerate IBAN validation code from an external Swift TXT registry file:

make update-registry SOURCE_REGISTRY=/path/to/iban-registry.txt

The full registry source is not committed to this repository. Generated Go files under iban/ are the committed registry artifact.

License

MIT License

See LICENSE to see the full text.

Releases

Packages

Used by

Contributors

Languages