-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunionpay.go
280 lines (254 loc) · 6.52 KB
/
unionpay.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package unionpay
import (
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"sort"
"strings"
"time"
)
//UnionPay unionpay
type UnionPay struct {
Mode string
MerID string
URL string
FrontURL string
BackURL string
PfxPath string
PfxPwd string
UnionCert UnionCert
apiurl string
apiaddr string
}
// UnionCert cert info
type UnionCert struct {
Private *rsa.PrivateKey
Cert *x509.Certificate
CertID string
}
// NewUnionPay new unionpay client.
func NewUnionPay(unionPay UnionPay) *UnionPay {
if err := unionPay.checkConfig(); err != nil {
log.Fatalf("New UnionPay client error: %v", err)
}
return &unionPay
}
// Post requset
func (c *UnionPay) Post(m map[string]string) (map[string]string, error) {
contentType := "application/x-www-form-urlencoded;charset=utf-8"
client := http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
c, err := net.DialTimeout(netw, addr, time.Second*5)
if err != nil {
log.Println("dail timeout", err)
return nil, err
}
return c, nil
},
MaxIdleConnsPerHost: 10,
ResponseHeaderTimeout: time.Second * 3,
},
}
resp, err := client.Post(c.apiurl, contentType, strings.NewReader(MapURLEncode(m)))
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("http request response StatusCode:%v", resp.StatusCode)
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
result := c.ParseRespone(respBytes)
if err = c.ResultCheck(result); err != nil {
return nil, err
}
if ok, err := SignVerify(result); !ok && err != nil {
return nil, err
}
return result, nil
}
// check UnionPay config
func (c *UnionPay) checkConfig() (err error) {
if c.MerID == "" {
return fmt.Errorf("MerID is empty")
} else if c.PfxPath == "" {
return fmt.Errorf("PfxPath is empty")
} else if c.PfxPwd == "" {
return fmt.Errorf("PfxPwd is empty")
} else if c.Mode == "" {
return fmt.Errorf("Mode is empty")
} else if c.Mode != "dev" && c.Mode != "prod" {
return fmt.Errorf("Mode is error: %v", c.Mode)
}
c.UnionCert.Private, c.UnionCert.Cert, err = ParserPfxToCert(c.PfxPath, c.PfxPwd)
if err != nil {
return fmt.Errorf("pfxCert error: %v", err)
}
c.UnionCert.CertID = fmt.Sprintf("%v", c.UnionCert.Cert.SerialNumber)
if c.Mode == "dev" {
log.Println("sign cert sn: ", c.UnionCert.CertID)
}
return
}
// public params
func (c *UnionPay) publicParams(m map[string]string) map[string]string {
m["version"] = "5.1.0"
m["encoding"] = "utf-8"
m["signMethod"] = "01"
m["certId"] = c.UnionCert.CertID
m["merId"] = c.MerID
if c.FrontURL != "" && m["frontUrl"] != "0" {
m["frontUrl"] = c.FrontURL
}
if m["backUrl"] != "0" {
m["backUrl"] = c.BackURL
}
switch c.Mode {
case "prod":
c.URL = "https://gateway.95516.com"
case "dev":
c.URL = "https://gateway.test.95516.com"
}
c.apiurl = c.URL + c.apiaddr
m["txnTime"] = time.Now().Format("20060102150405")
sign, err := Sign(m, c.UnionCert.Private)
if err != nil {
log.Fatalf("UnionPay sign with privateKey error: %v", err)
}
m["signature"] = sign
if c.Mode == "dev" {
log.Println("UnionPay sign: ", sign)
}
return m
}
// ParseRespone parse respone.
func (c *UnionPay) ParseRespone(bytes []byte) map[string]string {
m := make(map[string]string)
values := strings.Split(string(bytes), "&")
for _, v := range values {
kv := strings.SplitN(v, "=", 2)
m[kv[0]] = kv[1]
}
return m
}
// ResultCheck check result is success.
func (c *UnionPay) ResultCheck(m map[string]string) error {
if m["respCode"] != "00" {
return errors.New(m["respMsg"])
}
return nil
}
// ParseNotify parse and url decode notification.
func (c *UnionPay) ParseNotify(data string) (map[string]string, error) {
m := make(map[string]string)
data, err := url.QueryUnescape(data)
if err != nil {
return nil, err
}
values := strings.Split(data, "&")
for _, v := range values {
kv := strings.SplitN(v, "=", 2)
m[kv[0]] = kv[1]
}
return m, nil
}
// SortUnionMap unionpay requset map sort.
func SortUnionMap(m map[string]string) string {
var keys, str []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if k != "signature" {
str = append(str, k+"="+m[k])
}
}
return strings.Join(str, "&")
}
// MapURLEncode Map to string and url encode.
func MapURLEncode(m map[string]string) string {
qs := url.Values{}
for k, v := range m {
qs.Add(k, v)
}
return qs.Encode()
}
// AppConsume 消费获取Tn
func (c *UnionPay) AppConsume(txnamt int, orderNo, attach string) (map[string]string, error) {
c.apiaddr = "/gateway/api/appTransReq.do"
m := make(map[string]string)
m["txnType"] = "01"
m["txnSubType"] = "01"
m["bizType"] = "000201"
m["channelType"] = "08"
m["accessType"] = "0"
m["currencyCode"] = "156"
m["orderId"] = orderNo
m["txnAmt"] = fmt.Sprintf("%d", txnamt)
if attach != "" {
m["reqReserved"] = base64.StdEncoding.EncodeToString([]byte(attach))
}
return c.Post(c.publicParams(m))
}
// ConsumeUndo 消费撤销交易
func (c *UnionPay) ConsumeUndo(txnamt int, orderNo, queryID, attach string) (map[string]string, error) {
c.apiaddr = "/gateway/api/backTransReq.do"
m := make(map[string]string)
m["txnType"] = "31"
m["txnSubType"] = "00"
m["bizType"] = "000201"
m["channelType"] = "07"
m["accessType"] = "0"
m["orderId"] = orderNo
m["origQryId"] = queryID
m["txnAmt"] = fmt.Sprintf("%d", txnamt)
if attach != "" {
m["reqReserved"] = base64.StdEncoding.EncodeToString([]byte(attach))
}
m["frontUrl"] = "0"
return c.Post(c.publicParams(m))
}
// Refund 退货交易
func (c *UnionPay) Refund(txnamt int, orderNo, queryID, attach string) (map[string]string, error) {
c.apiaddr = "/gateway/api/backTransReq.do"
m := make(map[string]string)
m["txnType"] = "04"
m["txnSubType"] = "00"
m["bizType"] = "000201"
m["channelType"] = "07"
m["accessType"] = "0"
m["orderId"] = orderNo
m["origQryId"] = queryID
m["txnAmt"] = fmt.Sprintf("%d", txnamt)
if attach != "" {
m["reqReserved"] = base64.StdEncoding.EncodeToString([]byte(attach))
}
m["frontUrl"] = "0"
return c.Post(c.publicParams(m))
}
// Query 交易状态查询
func (c *UnionPay) Query(orderNo string) (map[string]string, error) {
c.apiaddr = "/gateway/api/queryTrans.do"
m := make(map[string]string)
m["txnType"] = "00"
m["txnSubType"] = "00"
m["bizType"] = "000000"
m["channelType"] = "07"
m["accessType"] = "0"
m["orderId"] = orderNo
m["frontUrl"] = "0"
m["backUrl"] = "0"
return c.Post(c.publicParams(m))
}