forked from tihtw/taiwan-invoice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
169 lines (132 loc) · 3.54 KB
/
client.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package invoice
import (
"crypto/hmac"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
type Client struct {
AppId string
ApiKey string
ConnectionHost string
}
func NewClient(appId string) *Client {
return &Client{
AppId: appId,
}
}
func (c *Client) CheckMobilePhoneExist(code string) (bool, error) {
params := url.Values{}
params.Add("version", "1.0")
params.Add("action", "bcv")
params.Add("barCode", code)
params.Add("TxID", "1.0")
params.Add("appId", c.AppId)
requestBody := params.Encode()
host := HOST_VC_EINVOICE
data, err := c.doRquest(host, requestBody)
if err != nil {
return false, err
}
dataMap := map[string]interface{}{}
json.Unmarshal(data, &dataMap)
// fmt.Println(string(data), dataMap)
isExist, ok := dataMap["isExist"].(string)
if !ok {
return false, fmt.Errorf("isExist field missing")
}
return isExist == "Y", nil
}
func (c *Client) CheckLoveCodeExist(code string) (bool, error) {
params := url.Values{}
params.Add("version", "1.0")
params.Add("action", "preserveCodeCheck")
params.Add("pCode", code)
params.Add("TxID", "1.0")
params.Add("appId", c.AppId)
requestBody := params.Encode()
host := HOST_VC_EINVOICE
data, err := c.doRquest(host, requestBody)
if err != nil {
return false, err
}
dataMap := map[string]interface{}{}
json.Unmarshal(data, &dataMap)
// fmt.Println(string(data), dataMap)
isExist, ok := dataMap["isExist"].(string)
if !ok {
return false, fmt.Errorf("isExist field missing")
}
return isExist == "Y", nil
}
func (c *Client) CheckBusinessAdministrationNumberExist(ban string) (bool, error) {
timestamp := fmt.Sprintf("%d", time.Now().Unix()+1000)
params := url.Values{}
params.Add("version", "1.0")
params.Add("serial", "0000000001")
params.Add("action", "qryBanUnitTp")
params.Add("ban", ban)
params.Add("timeStamp", timestamp)
params.Add("appId", c.AppId)
requestBody := params.Encode()
// fmt.Println("appId", c.AppId)
// fmt.Println("param", requestBody)
// TODO: make sure the order is alphabet order
params.Add("signature", c.hmac(requestBody))
requestBody = params.Encode()
host := HOST_VC_EINVOICE
data, err := c.doRquest(host, requestBody)
if err != nil {
return false, err
}
dataMap := map[string]interface{}{}
json.Unmarshal(data, &dataMap)
// fmt.Println(string(data), dataMap)
isExist, ok := dataMap["banUnitTpStatus"].(string)
if !ok {
return false, fmt.Errorf("banUnitTpStatus field missing")
}
return isExist == "Y", nil
}
func (c *Client) doRquest(host string, requestBody string) ([]byte, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{
// InsecureSkipVerify: true
ServerName: host,
},
}
client := &http.Client{Transport: tr}
body := strings.NewReader(requestBody)
hostname := host
if c.ConnectionHost != "" {
hostname = c.ConnectionHost
}
req, err := http.NewRequest("POST", "https://"+hostname+"/BIZAPIVAN/biz", body)
if err != nil {
// handle err
return nil, fmt.Errorf("new request: %q", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("client connection: %q", err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
return data, err
}
func (c *Client) hmac(message string) string {
key := c.ApiKey
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(message))
expectedMAC := mac.Sum(nil)
// fmt.Println(expectedMAC)
return base64.StdEncoding.EncodeToString(expectedMAC)
}