-
Notifications
You must be signed in to change notification settings - Fork 36
/
order.go
205 lines (177 loc) · 5.22 KB
/
order.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
package wxpay
import (
"bytes"
"compress/gzip"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
const (
kUnifiedOrder = "/pay/unifiedorder"
kOrderQuery = "/pay/orderquery"
kCloseOrder = "/pay/closeorder"
kDownloadBill = "/pay/downloadbill"
)
// UnifiedOrder https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1
func (this *Client) UnifiedOrder(param UnifiedOrderParam) (result *UnifiedOrderRsp, err error) {
if err = this.doRequest("POST", this.BuildAPI(kUnifiedOrder), param, &result); err != nil {
return nil, err
}
return result, err
}
// AppPay APP 支付 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2#
func (this *Client) AppPay(param UnifiedOrderParam) (result *PayInfo, err error) {
param.TradeType = TradeTypeApp
rsp, err := this.UnifiedOrder(param)
if err != nil {
return nil, err
}
if rsp != nil {
result = &PayInfo{}
result.AppId = this.appId
result.PartnerId = this.mchId
result.PrepayId = rsp.PrepayId
result.Package = "Sign=WXPay"
result.NonceStr = GetNonceStr()
result.TimeStamp = fmt.Sprintf("%d", time.Now().Unix())
result.SignType = SignTypeMD5
var p = url.Values{}
p.Set("appid", this.appId)
p.Set("noncestr", result.NonceStr)
p.Set("partnerid", result.PartnerId)
p.Set("prepayid", result.PrepayId)
p.Set("package", result.Package)
p.Set("timestamp", result.TimeStamp)
result.Sign = SignMD5(p, this.apiKey)
result.RawRsp = rsp
}
return result, err
}
// JSAPIPay 微信内H5调起支付-公众号支付 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_7&index=6
func (this *Client) JSAPIPay(param UnifiedOrderParam) (result *PayInfo, err error) {
param.TradeType = TradeTypeJSAPI
rsp, err := this.UnifiedOrder(param)
if err != nil {
return nil, err
}
if rsp != nil {
result = &PayInfo{}
result.AppId = this.appId
result.PartnerId = this.mchId
result.PrepayId = rsp.PrepayId
result.Package = fmt.Sprintf("prepay_id=%s", rsp.PrepayId)
result.NonceStr = GetNonceStr()
result.TimeStamp = fmt.Sprintf("%d", time.Now().Unix())
result.SignType = SignTypeMD5
var p = url.Values{}
p.Add("appId", this.appId)
p.Add("nonceStr", result.NonceStr)
p.Add("package", result.Package)
p.Add("signType", result.SignType)
p.Add("timeStamp", result.TimeStamp)
result.Sign = SignMD5(p, this.apiKey)
result.RawRsp = rsp
}
return result, err
}
// MiniAppPay 小程序支付 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=5
func (this *Client) MiniAppPay(param UnifiedOrderParam) (result *PayInfo, err error) {
return this.JSAPIPay(param)
}
// WebPay H5 支付 https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_20&index=1
func (this *Client) WebPay(param UnifiedOrderParam) (result *WebPayInfo, err error) {
param.TradeType = TradeTypeMWeb
rsp, err := this.UnifiedOrder(param)
if err != nil {
return nil, err
}
if rsp != nil {
result = &WebPayInfo{}
result.MWebURL = rsp.MWebURL
result.RawRsp = rsp
}
return result, err
}
// NativePay NATIVE 扫码支付 https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_1
func (this *Client) NativePay(param UnifiedOrderParam) (result *NativePayInfo, err error) {
param.TradeType = TradeTypeNative
rsp, err := this.UnifiedOrder(param)
if err != nil {
return nil, err
}
if rsp != nil {
result = &NativePayInfo{}
result.CodeURL = rsp.CodeURL
result.RawRsp = rsp
}
return result, err
}
// OrderQuery https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
func (this *Client) OrderQuery(param OrderQueryParam) (result *OrderQueryRsp, err error) {
if err = this.doRequest("POST", this.BuildAPI(kOrderQuery), param, &result); err != nil {
return nil, err
}
return result, err
}
// CloseOrder https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_3
func (this *Client) CloseOrder(param CloseOrderParam) (result *CloseOrderRsp, err error) {
if err = this.doRequest("POST", this.BuildAPI(kCloseOrder), param, &result); err != nil {
return nil, err
}
return result, err
}
var (
kXML = []byte("<xml>")
)
// DownloadBill https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6
func (this *Client) DownloadBill(param DownloadBillParam) (result *DownloadBillRsp, err error) {
key, err := this.getKey()
if err != nil {
return nil, err
}
p, err := this.URLValues(param, key)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", this.BuildAPI(kDownloadBill), strings.NewReader(URLValueToXML(p)))
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/xml")
req.Header.Set("Content-Type", "application/xml;charset=utf-8")
resp, err := this.Client.Do(req)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if bytes.Index(data, kXML) == 0 {
err = xml.Unmarshal(data, &result)
} else {
if this.isProduction {
var r = bytes.NewReader(data)
gr, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
defer gr.Close()
if data, err = ioutil.ReadAll(gr); err != nil {
return nil, err
}
}
result = &DownloadBillRsp{}
result.ReturnCode = ReturnCodeSuccess
result.ReturnMsg = "ok"
result.Data = data
}
return result, err
}