-
Notifications
You must be signed in to change notification settings - Fork 6
/
iban_bic.go
111 lines (83 loc) · 2.95 KB
/
iban_bic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package finance
import (
"encoding/xml"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
"github.com/pkg/errors"
)
// DefaultIBANBICServiceURL is the default IBANBIC service URL to use
const DefaultIBANBICServiceURL = "https://www.ibanbic.be/IBANBIC.asmx"
// IBANBICServiceURL is the SOAP URL to be used when checking a bank account number
var IBANBICServiceURL = DefaultIBANBICServiceURL
// DefaultIBANBICTimeout is the default timeout to use when checking the bank account number
const DefaultIBANBICTimeout = 5 * time.Second
// IBANBICTimeout is the timeout to use when checking the bank account number
var IBANBICTimeout = DefaultIBANBICTimeout
var (
// ErrIBANBICServiceUnreachable is the error returned when the IBANBIC service is unreachable
ErrIBANBICServiceUnreachable = errors.New("IBANBIC service is unreachable")
// ErrIBANBICInvalidInput is the error returned when the bank account number is invalid
ErrIBANBICInvalidInput = errors.New("Number is not a valid bank account number")
// ErrIBANBICServiceError is the error returned when we get a non-standard error from the IBANBIC service
ErrIBANBICServiceError = "IBANBIC service returns an error: "
)
// IBANBICInfo contains the info about a Belgian Bank Account number
type IBANBICInfo struct {
BBAN string // The Belgian Bank Account Number
BankName string // The name of the bank which issues the account
IBAN string // The IBAN number of the bank account
BIC string // The Bank Identification Code of the bank
}
// CheckIBAN checks the Bank Account Number and returns the IBAN and BIC information
func CheckIBAN(number string) (*IBANBICInfo, error) {
if len(number) == 0 {
return nil, ErrIBANBICInvalidInput
}
result := &IBANBICInfo{
BBAN: number,
}
bankName, err := performIBANBICRequest("BBANtoBANKNAME", number)
if err != nil {
return nil, err
}
result.BankName = bankName
ibanAndBic, err := performIBANBICRequest("BBANtoIBANandBIC", number)
if err != nil {
return nil, err
}
ibanBicParts := strings.SplitN(ibanAndBic, "#", 2)
if len(ibanBicParts) < 2 {
return nil, errors.New(ErrIBANBICServiceError + "Failed to get BIC and IBAN code")
}
result.IBAN = ibanBicParts[0]
result.BIC = ibanBicParts[1]
return result, nil
}
func performIBANBICRequest(action string, value string) (string, error) {
url := IBANBICServiceURL + "/" + url.PathEscape(action) + "?Value=" + url.QueryEscape(value)
client := http.Client{
Timeout: VATTimeout,
}
res, err := client.Get(url)
if err != nil {
return "", ErrIBANBICServiceUnreachable
}
defer res.Body.Close()
xmlRes, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", err
}
xmlString := string(xmlRes)
if strings.Contains(xmlString, "Exception") {
exceptionParts := strings.Split(xmlString, "\n")
return "", errors.New(ErrIBANBICServiceError + strings.TrimSpace(exceptionParts[0]))
}
var result string
if err := xml.Unmarshal(xmlRes, &result); err != nil {
return "", err
}
return result, nil
}