-
Notifications
You must be signed in to change notification settings - Fork 2
/
currency.go
110 lines (91 loc) · 2.17 KB
/
currency.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
package main
import (
"encoding/xml"
"log"
"net/http"
"sort"
"strconv"
)
var rates = map[string]float64{}
type xmlCurRate struct {
XMLName xml.Name `xml:"Cube"`
Cur string `xml:"currency,attr"`
Rate string `xml:"rate,attr"`
}
type xmlCube1 struct {
XMLName xml.Name `xml:"Cube"`
Rates []xmlCurRate `xml:"Cube"`
}
type xmlCube struct {
XMLName xml.Name `xml:"Cube"`
Cube xmlCube1 `xml:"Cube"`
}
type xmlEnvelope struct {
XMLName xml.Name `xml:"Envelope"`
Cube xmlCube `xml:"Cube"`
}
const (
urlSrc = "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"
defaultCurrency = "USD"
cookieMaxAge = 60 * 60 * 48
cookiePrefix = "shop_"
cookieCurrency = cookiePrefix + "currency"
)
var whitelistedCurrencies = map[string]bool{
"USD": true,
"EUR": true,
"CAD": true,
"JPY": true,
"GBP": true,
"TRY": true}
func init() {
res, err := http.Get(urlSrc)
if res.Body != nil {
defer res.Body.Close()
}
if err != nil || res.StatusCode != http.StatusOK {
log.Fatalf("unable to request rates: code: %d, err: %v", res.StatusCode, err)
}
var x xmlEnvelope
if err = xml.NewDecoder(res.Body).Decode(&x); err != nil {
log.Fatalf("unable to parse currency responce: %v", err)
}
var r float64
for _, cr := range x.Cube.Cube.Rates {
r, err = strconv.ParseFloat(cr.Rate, 64)
if err != nil || !whitelistedCurrencies[cr.Cur] {
continue
}
rates[cr.Cur] = r
}
rates["EUR"] = 1.0
log.Printf("currencies rates successfully retraived: %d\n", len(rates))
}
func Rates() map[string]float64 {
return rates
}
func Currencies() []string {
cs := []string{}
for c := range rates {
if whitelistedCurrencies[c] {
cs = append(cs, c)
}
}
sort.Strings(cs)
return cs
}
func Convert(price Money, currency string) Money {
if currency == "USD" {
return price
}
if rates[price.CurrencyCode] == 0.0 || rates[currency] == 0.0 {
return Money{CurrencyCode: currency}
}
eurUnits := float64(price.Units) / rates[price.CurrencyCode]
eurNanos := float64(price.Nanos) / rates[price.CurrencyCode]
return Money{
CurrencyCode: currency,
Units: int64(eurUnits * rates[currency]),
Nanos: int32(eurNanos * rates[currency]),
}
}