forked from panjjo/ppp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wxpay_single.go
286 lines (270 loc) · 7.28 KB
/
wxpay_single.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
281
282
283
284
285
286
package ppp
import (
"bytes"
"encoding/xml"
"fmt"
"time"
"gopkg.in/mgo.v2/bson"
)
var (
wxPaySGUrl string //微信支付请求地址
wxPaySGAppId string //微信公众号ID
wxPaySGMchId string //微信支付商户号
wxPaySGNotifyUrl string //异步通知地址
)
const (
FC_WXPAYSG_TRADEINFO string = "WXPaySG.TradeInfo" //订单详情
FC_WXPAYSG_TRADEPARAMS string = "WXPaySG.PayParams" //网站支付参数组装
)
type WXPaySGInit struct {
AppId string
Url string
MchId string
ApiKey string
ConfigPath string
NotifyUrl string
}
func (w *WXPaySGInit) Init() {
wxPaySGUrl = w.Url
wxPaySGAppId = w.AppId
wxPaySGMchId = w.MchId
wxPaySGSecretKey = w.ApiKey
wxPaySGNotifyUrl = w.NotifyUrl
loadWXPaySGCertKey(w.ConfigPath)
}
//微信支付接口主体
type WXPaySG struct {
}
// 获取支付单详情
// DOC:https://pay.weixin.qq.com/wiki/doc/api/micropay_sl.php?chapter=9_2
// 传入参数TradeRequest
// 返回参数TradeResult
func (W *WXPaySG) TradeInfo(request *TradeRequest, resp *TradeResult) error {
Log.DEBUG.Printf("WXSGPay api:TradeInfo,request:%+v", request)
defer Log.DEBUG.Printf("WXSGPay api:TradeInfo,response:%+v", resp)
if request.r.time == 0 {
request.r.time = getNowSec()
}
q := bson.M{"source": PAYTYPE_WXPAY}
if request.TradeId != "" {
q["tradeid"] = request.TradeId
}
if request.OutTradeId != "" {
q["outtradeid"] = request.OutTradeId
}
trade := getTrade(q)
if request.DisSync {
if trade.Id == "" {
resp.Code = TradeErrNotFound
}
resp.Data = trade
return nil
}
params := wxTradeInfoRequest{
AppId: wxPayAppId,
MchId: wxPayMchId,
NonceStr: randomString(32),
OutTradeId: request.OutTradeId,
TradeId: request.TradeId,
}
// params.SubMchId = request.r.mchid
// params.SubAppId = auth.AppId
params.Sign = WXPaySGSigner(structToMap(params, "xml"))
postBody, err := xml.Marshal(params)
if err != nil {
resp.Code = SysErrParams
resp.SourceData = err.Error()
return nil
}
var result interface{}
var next int
for getNowSec()-request.r.time < 30 {
result, next, err = W.request(wxPayUrl+"/pay/orderquery", postBody)
resp.SourceData = string(jsonEncode(result))
if err != nil {
if v, ok := wxErrMap[err.Error()]; ok {
resp.Code = v
} else {
resp.Code = TradeErr
}
switch next {
case 2, -1:
//网络异常 1s后重试
time.Sleep(1 * time.Second)
default:
//其他错误,直接返回
return nil
}
} else {
//成功返回
tmpresult := wxTradeResult{}
xml.Unmarshal(result.([]byte), &tmpresult)
resp.Data = Trade{
OutTradeId: tmpresult.OutTradeId,
TradeId: tmpresult.TradeId,
Status: wxTradeStatusMap[tmpresult.Status],
Amount: tmpresult.Amount,
PayTime: str2Sec("20060102150405", tmpresult.TimeEnd),
Id: trade.Id,
}
if trade.Id != "" {
updateTrade(bson.M{"id": trade.Id}, bson.M{"$set": bson.M{"status": resp.Data.Status, "uptime": getNowSec(), "paytime": resp.Data.PayTime}})
}
return nil
}
}
return nil
}
//网页支付
//子商户模式
func (W *WXPaySG) PayParams(request *WapPayRequest, resp *Response) error {
Log.DEBUG.Printf("WXSGPay api:WapPayParams,request:%+v", request)
defer Log.DEBUG.Printf("WXSGPay api:WapPayParams,response:%+v", resp)
var tradeType string
switch request.TradeType {
case APPPAYPARAMS:
tradeType = "APP"
}
if request.r.time == 0 {
request.r.time = getNowSec()
}
params := wxWapPayRequest{
AppId: wxPayAppId,
MchId: wxPayMchId,
// SubMchId: auth.MchId,
// SubAppId: auth.AppId,
NonceStr: randomString(32),
Body: request.ItemDes,
OutTradeId: request.OutTradeId,
Amount: fmt.Sprintf("%d", request.Amount),
IPAddr: request.IPAddr,
NotifyUrl: wxPayNotifyUrl,
TradeType: tradeType,
OpenId: request.OpenId,
SubOpenId: request.SubOpenId,
SceneInfo: string(jsonEncode(map[string]interface{}{
"h5_info": map[string]interface{}{"type": "Wap", "wap_url": request.Scene.Url, "wap_name": request.Scene.Name},
})),
}
if params.TradeType == JSPAYPARAMS {
if params.OpenId == "" && params.SubOpenId == "" {
resp.Code = SysErrParams
resp.SourceData = fmt.Sprintf("trade type:%s,openid or sub_openid must have one", params.TradeType)
}
}
params.Sign = WXPaySigner(structToMap(params, "xml"))
Log.ERROR.Printf("wxpay params:%+v", params)
postBody, err := xml.Marshal(params)
if err != nil {
resp.Code = SysErrParams
resp.SourceData = err.Error()
return nil
}
var result interface{}
var next int
for getNowSec()-request.r.time < 30 {
result, next, err = W.request(wxPayUrl+"/pay/unifiedorder", postBody)
resp.SourceData = string(jsonEncode(result))
if err != nil {
if v, ok := wxErrMap[err.Error()]; ok {
resp.Code = v
} else {
resp.Code = TradeErr
}
switch next {
case 2, -1:
//网络异常 1s后重试
time.Sleep(1 * time.Second)
default:
//其他错误,直接返回
return nil
}
} else {
//成功返回
tmpresult := wxWapPayResult{}
xml.Unmarshal(result.([]byte), &tmpresult)
if params.TradeType == JSPAYPARAMS {
jsParams := map[string]string{
"appId": wxPayAppId,
"timeStamp": fmt.Sprintf("%d", getNowSec()),
"nonceStr": randomString(32),
"package": "prepay_id=" + tmpresult.PrePayId,
"signType": "MD5",
}
jsParams["paySign"] = WXPaySigner(jsParams)
resp.SourceData = string(jsonEncode(jsParams))
} else {
resp.SourceData = string(jsonEncode(map[string]string{
"perpay_id": tmpresult.PrePayId,
"mweb_url": tmpresult.MWEBURL,
}))
}
//save tradeinfo
saveTrade(Trade{
OutTradeId: request.OutTradeId,
Status: 0,
Type: 1,
Amount: request.Amount,
Source: PAYTYPE_WXPAY,
UpTime: getNowSec(),
Ex: request.Ex,
Id: randomTimeString(), // PPPID
})
return nil
}
}
return nil
}
func (w *WXPaySG) requestTls(url string, data []byte) (interface{}, int, error) {
body, err := postRequestTls(url, "text/xml", bytes.NewBuffer(data), wxPayCertTlsConfig)
if err != nil {
//网络发起请求失败
//需重试
return nil, -1, err
}
result := wxResult{}
Log.DEBUG.Printf("WXSGPay request url:%s,body:%s", url, string(body))
if err := xml.Unmarshal(body, &result); err != nil {
return nil, 0, err
}
if result.ReturnCode != "SUCCESS" {
return nil, -1, newError(result.ReturnCode + result.ReturnMsg)
}
next, err := w.errorCheck(result)
return body, next, err
}
func (w *WXPaySG) request(url string, data []byte) (interface{}, int, error) {
body, err := postRequest(url, "text/xml", bytes.NewBuffer(data))
if err != nil {
//网络发起请求失败
//需重试
return nil, -1, err
}
result := wxResult{}
Log.DEBUG.Printf("WXSGPay request url:%s,body:%s", url, string(body))
if err := xml.Unmarshal(body, &result); err != nil {
return nil, 0, err
}
if result.ReturnCode != "SUCCESS" {
return nil, 0, newError("NOAUTH")
}
next, err := w.errorCheck(result)
return body, next, err
}
func (w *WXPaySG) errorCheck(result wxResult) (int, error) {
if result.ResultCode == "SUCCESS" {
//成功
return 0, nil
}
var code int
switch result.ErrCode {
case "SYSTEMERROR", "BANKERROR":
//需确认
code = 2
case "USERPAYING":
//需循环确认
code = 3
default:
}
return code, newError(result.ErrCode)
}