forked from apilayer/goiban-service
-
Notifications
You must be signed in to change notification settings - Fork 7
/
iban_generation.go
85 lines (68 loc) · 1.78 KB
/
iban_generation.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
package main
import (
"encoding/json"
"net/http"
"github.com/fourcube/goiban"
"github.com/julienschmidt/httprouter"
)
type CalculateSuccess struct {
Valid bool `json:"valid"`
IBAN string `json:"iban"`
}
type CalculateError struct {
Valid bool `json:"valid"`
Message string `json:"message"`
}
func calculateIBAN(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Add("Content-Type", "application/json; charset=utf-8")
// Allow CORS
w.Header().Set("Access-Control-Allow-Origin", "*")
result := goiban.CalculateIBAN(
ps.ByName("countryCode"),
ps.ByName("bankCode"),
ps.ByName("accountNumber"))
var data []byte
var err error
if result.Valid {
data, err = json.Marshal(CalculateSuccess{true, result.Data})
} else {
data, err = json.Marshal(CalculateError{false, result.Message})
}
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
r.Body.Close()
return
}
w.WriteHeader(http.StatusOK)
w.Write(data)
}
func calculateAndValidateIBAN(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Add("Content-Type", "application/json; charset=utf-8")
// Allow CORS
w.Header().Set("Access-Control-Allow-Origin", "*")
iban := goiban.CalculateIBAN(
ps.ByName("countryCode"),
ps.ByName("bankCode"),
ps.ByName("accountNumber"))
if !iban.Valid {
data, err := json.Marshal(CalculateError{false, iban.Message})
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
r.Body.Close()
return
}
w.WriteHeader(http.StatusOK)
w.Write(data)
r.Body.Close()
return
}
param := httprouter.Param{
Key: "iban",
Value: iban.Data,
}
r.ParseForm()
r.Form.Add("validateBankCode", "true")
r.Form.Add("getBIC", "true")
// Delegate to validation
validationHandler(w, r, []httprouter.Param{param})
}