-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
251 lines (227 loc) · 5.14 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package wxpay
import (
"bytes"
"crypto/tls"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
const bodyType = "application/xml; charset=utf-8"
// Clienter ...
type Clienter interface {
Config() Config
UnifiedOrder(req *UnifiedOrderRequest) (resp *UnifiedOrderResponse, err error)
}
// Client 客户端
type Client struct {
conf *Config
httpClient *http.Client
}
// NewClient ...
func NewClient(conf *Config) (client *Client, err error) {
var transport http.RoundTripper
if conf.CaCertFile != "" && conf.CaKeyFile != "" {
var cert tls.Certificate
cert, err = tls.LoadX509KeyPair(conf.CaCertFile, conf.CaKeyFile)
if err != nil {
return
}
t := &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
}
if conf.HttpProxy != "" {
var proxyURL *url.URL
proxyURL, err = url.Parse(conf.HttpProxy)
if err != nil {
return
}
t.Proxy = http.ProxyURL(proxyURL)
}
transport = t
} else {
defaultTransport := http.DefaultTransport
if conf.HttpProxy != "" {
var proxyURL *url.URL
proxyURL, err = url.Parse(conf.HttpProxy)
if err != nil {
return
}
defaultTransport.(*http.Transport).Proxy = http.ProxyURL(proxyURL)
}
transport = defaultTransport
}
client = &Client{
conf: conf,
httpClient: &http.Client{
Transport: transport,
},
}
return
}
// Config ...
func (c *Client) Config() Config {
return *c.conf
}
// ResponseStatus 响应状态
type ResponseStatus struct {
ReturnCode string `xml:"return_code"`
ReturnMsg string `xml:"return_msg"`
ResultCode string `xml:"result_code"`
ErrCode string `xml:"err_code"`
ErrCodeDes string `xml:"err_code_des"`
}
// ResponseReturn 成功
type ResponseReturn struct {
XMLName xml.Name `xml:"xml"`
Code CDATA `xml:"return_code"`
Msg CDATA `xml:"return_msg"`
}
// execute 执行
func (c *Client) execute(url string, param interface{}) (body []byte, err error) {
value := new(bytes.Buffer)
xen := xml.NewEncoder(value)
err = xen.Encode(param)
if err != nil {
return
}
resp, err := c.httpClient.Post(c.conf.BaseURL+url, bodyType, value)
if err != nil {
return
}
defer resp.Body.Close()
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return
}
// UnifiedOrder 统一下单
func (c *Client) UnifiedOrder(req *UnifiedOrderRequest) (resp *UnifiedOrderResponse, err error) {
req.AppID = c.conf.AppID
req.MchID = c.conf.MchID
if req.NotifyURL == "" {
req.NotifyURL = c.conf.NotifyURL
}
if c.conf.SignType == HMACSHA256 {
err = req.SignHMACSHA256([]byte(c.conf.SignKey), c.conf.APIKey)
if err != nil {
return
}
} else {
err = req.SignMD5(c.conf.APIKey)
if err != nil {
return
}
}
var body []byte
body, err = c.execute("/pay/unifiedorder", req)
if err != nil {
return
}
resp = new(UnifiedOrderResponse)
err = xml.Unmarshal(body, resp)
if err != nil {
resp = nil
return
}
if resp.ReturnCode == "FAIL" {
err = fmt.Errorf("通信错误:%s", resp.ReturnMsg)
return
}
if resp.ResultCode == "FAIL" {
err = fmt.Errorf("业务错误:%s", resp.ErrCodeDes)
return
}
return
}
// SendRedPack 发现金红包
func (c *Client) SendRedPack(req *SendRedPackRequest) (resp *SendRedPackResponse, err error) {
req.WxAppID = c.conf.AppID
req.MchID = c.conf.MchID
err = req.SignMD5(c.conf.APIKey)
if err != nil {
return
}
var body []byte
body, err = c.execute("/mmpaymkttransfers/sendredpack", req)
if err != nil {
return
}
resp = new(SendRedPackResponse)
err = xml.Unmarshal(body, resp)
if err != nil {
resp = nil
return
}
if resp.ReturnCode == "FAIL" {
err = fmt.Errorf("通信错误:%s", resp.ReturnMsg)
return
}
if resp.ResultCode == "FAIL" {
err = fmt.Errorf("业务错误:%s", resp.ErrCodeDes)
return
}
return
}
// SendGroupRedPack 发裂变红包
func (c *Client) SendGroupRedPack(req *SendGroupRedPackRequest) (resp *SendGroupRedPackResponse, err error) {
req.WxAppID = c.conf.AppID
req.MchID = c.conf.MchID
err = req.SignMD5(c.conf.APIKey)
if err != nil {
return
}
var body []byte
body, err = c.execute("/mmpaymkttransfers/sendgroupredpack", req)
if err != nil {
return
}
resp = new(SendGroupRedPackResponse)
err = xml.Unmarshal(body, resp)
if err != nil {
resp = nil
return
}
if resp.ReturnCode == "FAIL" {
err = fmt.Errorf("通信错误:%s", resp.ReturnMsg)
return
}
if resp.ResultCode == "FAIL" {
err = fmt.Errorf("业务错误:%s", resp.ErrCodeDes)
return
}
return
}
// PromotionTransfers 企业向微信用户个人付款
func (c *Client) PromotionTransfers(req *PromotionTransfersRequest) (resp *PromotionTransfersResponse, err error) {
req.MchAppID = c.conf.AppID
req.MchID = c.conf.MchID
err = req.SignMD5(c.conf.APIKey)
if err != nil {
return
}
var body []byte
body, err = c.execute("/mmpaymkttransfers/promotion/transfers", req)
if err != nil {
return
}
resp = new(PromotionTransfersResponse)
err = xml.Unmarshal(body, resp)
if err != nil {
resp = nil
return
}
if resp.ReturnCode == "FAIL" {
err = fmt.Errorf("通信错误:%s", resp.ReturnMsg)
return
}
if resp.ResultCode == "FAIL" {
err = fmt.Errorf("业务错误:%s", resp.ErrCodeDes)
return
}
return
}