forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wechat_servier_api.go
217 lines (191 loc) · 6.15 KB
/
wechat_servier_api.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
//==================================
// * Name:Jerry
// * DateTime:2019/5/6 13:16
// * Desc:
//==================================
package gopay
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/hmac"
"crypto/md5"
"crypto/sha256"
"crypto/tls"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/parnurzeal/gorequest"
"reflect"
"strings"
)
func HttpAgent() (agent *gorequest.SuperAgent) {
agent = gorequest.New()
agent.TLSClientConfig(&tls.Config{InsecureSkipVerify: true})
return
}
//JSAPI支付,支付参数后,再次计算出小程序用的paySign
func GetMiniPaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey string) (paySign string) {
buffer := new(bytes.Buffer)
buffer.WriteString("appId=")
buffer.WriteString(appId)
buffer.WriteString("&nonceStr=")
buffer.WriteString(nonceStr)
buffer.WriteString("&package=")
buffer.WriteString(prepayId)
buffer.WriteString("&signType=")
buffer.WriteString(signType)
buffer.WriteString("&timeStamp=")
buffer.WriteString(timeStamp)
buffer.WriteString("&key=")
buffer.WriteString(secretKey)
signStr := buffer.String()
var hashSign []byte
if signType == SignType_MD5 {
hash := md5.New()
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
} else {
hash := hmac.New(sha256.New, []byte(secretKey))
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
}
paySign = strings.ToUpper(hex.EncodeToString(hashSign))
return
}
//JSAPI支付,支付参数后,再次计算出微信内H5支付需要用的paySign
func GetH5PaySign(appId, nonceStr, prepayId, signType, timeStamp, secretKey string) (paySign string) {
buffer := new(bytes.Buffer)
buffer.WriteString("appId=")
buffer.WriteString(appId)
buffer.WriteString("&nonceStr=")
buffer.WriteString(nonceStr)
buffer.WriteString("&package=")
buffer.WriteString(prepayId)
buffer.WriteString("&signType=")
buffer.WriteString(signType)
buffer.WriteString("&timeStamp=")
buffer.WriteString(timeStamp)
buffer.WriteString("&key=")
buffer.WriteString(secretKey)
signStr := buffer.String()
var hashSign []byte
if signType == SignType_MD5 {
hash := md5.New()
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
} else {
hash := hmac.New(sha256.New, []byte(secretKey))
hash.Write([]byte(signStr))
hashSign = hash.Sum(nil)
}
paySign = strings.ToUpper(hex.EncodeToString(hashSign))
return
}
//解密开放数据
// encryptedData:包括敏感数据在内的完整用户信息的加密数据
// iv:加密算法的初始向量
// sessionKey:会话密钥
// beanPtr:需要解析到的结构体指针
func DecryptOpenDataToStruct(encryptedData, iv, sessionKey string, beanPtr interface{}) (err error) {
//验证参数类型
beanValue := reflect.ValueOf(beanPtr)
if beanValue.Kind() != reflect.Ptr {
return errors.New("传入beanPtr类型必须是以指针形式")
}
//验证interface{}类型
if beanValue.Elem().Kind() != reflect.Struct {
return errors.New("传入interface{}必须是结构体")
}
aesKey, _ := base64.StdEncoding.DecodeString(sessionKey)
ivKey, _ := base64.StdEncoding.DecodeString(iv)
cipherText, _ := base64.StdEncoding.DecodeString(encryptedData)
if len(cipherText)%len(aesKey) != 0 {
return errors.New("encryptedData is error")
}
//fmt.Println("cipherText:", cipherText)
block, err := aes.NewCipher(aesKey)
if err != nil {
return err
}
//解密
blockMode := cipher.NewCBCDecrypter(block, ivKey)
plainText := make([]byte, len(cipherText))
blockMode.CryptBlocks(plainText, cipherText)
//fmt.Println("plainText1:", plainText)
plainText = PKCS7UnPadding(plainText)
//fmt.Println("plainText:", plainText)
//解析
err = json.Unmarshal(plainText, beanPtr)
if err != nil {
return err
}
return nil
}
//获取微信用户的OpenId、SessionKey、UnionId
// appId:APPID
// appSecret:AppSecret
// wxCode:小程序调用wx.login 获取的code
func Code2Session(appId, appSecret, wxCode string) (sessionRsp *Code2SessionRsp, err error) {
sessionRsp = new(Code2SessionRsp)
url := "https://api.weixin.qq.com/sns/jscode2session?appid=" + appId + "&secret=" + appSecret + "&js_code=" + wxCode + "&grant_type=authorization_code"
agent := HttpAgent()
_, _, errs := agent.Get(url).EndStruct(sessionRsp)
if len(errs) > 0 {
return nil, errs[0]
} else {
return sessionRsp, nil
}
}
//获取小程序全局唯一后台接口调用凭据(AccessToken:157字符)
// appId:APPID
// appSecret:AppSecret
func GetAccessToken(appId, appSecret string) (accessToken *AccessToken, err error) {
accessToken = new(AccessToken)
url := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret
agent := HttpAgent()
_, _, errs := agent.Get(url).EndStruct(accessToken)
if len(errs) > 0 {
return nil, errs[0]
} else {
return accessToken, nil
}
}
//用户支付完成后,获取该用户的 UnionId,无需用户授权。
// accessToken:接口调用凭据
// openId:用户的OpenID
// transactionId:微信支付订单号
func GetPaidUnionId(accessToken, openId, transactionId string) (unionId *PaidUnionId, err error) {
unionId = new(PaidUnionId)
url := "https://api.weixin.qq.com/wxa/getpaidunionid?access_token=" + accessToken + "&openid=" + openId + "&transaction_id=" + transactionId
agent := HttpAgent()
_, _, errs := agent.Get(url).EndStruct(unionId)
if len(errs) > 0 {
return nil, errs[0]
} else {
return unionId, nil
}
}
//获取用户基本信息(UnionID机制)
// accessToken:接口调用凭据
// openId:用户的OpenID
// lang:默认为 zh_CN ,可选填 zh_CN 简体,zh_TW 繁体,en 英语
func GetWeChatUserInfo(accessToken, openId string, lang ...string) (userInfo *WeChatUserInfo, err error) {
userInfo = new(WeChatUserInfo)
var url string
if len(lang) > 0 {
url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=" + lang[0]
} else {
url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId + "&lang=zh_CN"
}
fmt.Println(url)
agent := HttpAgent()
_, _, errs := agent.Get(url).EndStruct(userInfo)
if len(errs) > 0 {
return nil, errs[0]
} else {
return userInfo, nil
}
}