-
Notifications
You must be signed in to change notification settings - Fork 7
/
callback.go
163 lines (148 loc) · 3.62 KB
/
callback.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
package pays_with_go
import (
"errors"
"fmt"
"./client"
"./common"
"./util"
"net/http"
"sort"
"strings"
"io/ioutil"
"encoding/xml"
)
// WeChatCallback 微信app支付
func WeChatAppCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPayResult, error) {
var returnCode = "FAIL"
var returnMsg = ""
defer func() {
formatStr := `<xml><return_code><![CDATA[%s]]></return_code>
<return_msg>![CDATA[%s]]</return_msg></xml>`
returnBody := fmt.Sprintf(formatStr, returnCode, returnMsg)
w.Write([]byte(returnBody))
}()
var reXML common.WeChatPayResult
//body := cb.Ctx.Input.RequestBody
body, err := ioutil.ReadAll(r.Body)
if err != nil {
//log.Error(string(body))
returnCode = "FAIL"
returnMsg = "Bodyerror"
return nil, err
}
err = xml.Unmarshal(body, &reXML)
if err != nil {
//log.Error(err, string(body))
returnMsg = "参数错误"
returnCode = "FAIL"
return nil, err
}
if reXML.ReturnCode != "SUCCESS" {
//log.Error(reXML)
returnCode = "FAIL"
return &reXML, errors.New(reXML.ReturnCode)
}
m, err := util.XmlToMap(body)
if err != nil {
//log.Error(err, body)
returnMsg = "参数错误"
returnCode = "FAIL"
return nil, err
}
//log.Info(m)
var signData []string
for k, v := range m {
if k == "sign" {
continue
}
signData = append(signData, fmt.Sprintf("%v=%v", k, v))
}
sort.Strings(signData)
signData2 := strings.Join(signData, "&")
err = client.DefaultWechatAppClient().CheckSign(signData2, m["sign"])
if err != nil {
returnCode = "FAIL"
return nil, err
}
returnCode = "SUCCESS"
return &reXML, nil
}
//支付宝web端回调
func AliWebCallback(w http.ResponseWriter, r *http.Request) (*common.AliWebPayResult, error) {
var m = make(map[string]string)
var signSlice []string
r.ParseForm()
for k, v := range r.Form {
// k不会有多个值的情况
m[k] = v[0]
if k == "sign" || k == "sign_type" {
continue
}
signSlice = append(signSlice, fmt.Sprintf("%s=%s", k, v[0]))
}
sort.Strings(signSlice)
signData := strings.Join(signSlice, "&")
if m["sign_type"] != "RSA" {
//错误日志
return nil, errors.New("签名类型未知")
}
err := client.DefaultAliWebClient().CheckSign(signData, m["sign"])
if err != nil {
//log.Error("签名验证失败:", err, signData, m)
return nil, err
}
var aliPay common.AliWebPayResult
err = util.MapStringToStruct(m, &aliPay)
if err != nil {
//log.Error(err)
w.Write([]byte("error"))
return nil, err
}
// err = biz.AliWebCallBack(&aliPay)
// if err != nil {
// //log.Error(err)
// w.Write([]byte("error"))
// return nil, err
// }
w.Write([]byte("success"))
return &aliPay, nil
}
// 支付宝app支付回调
func AliAppCallback(w http.ResponseWriter, r *http.Request) (*common.AliWebPayResult, error) {
var m = make(map[string]string)
var signSlice []string
r.ParseForm()
for k, v := range r.Form {
m[k] = v[0]
if k == "sign" || k == "sign_type" {
continue
}
signSlice = append(signSlice, fmt.Sprintf("%s=%s", k, v[0]))
}
sort.Strings(signSlice)
signData := strings.Join(signSlice, "&")
if m["sign_type"] != "RSA" {
//log.Error(m)
return nil, errors.New("签名类型未知")
}
err := client.DefaultAliAppClient().CheckSign(signData, m["sign"])
if err != nil {
//log.Error(err, m, signData)
w.Write([]byte("error"))
return nil, err
}
var aliPay common.AliWebPayResult
err = util.MapStringToStruct(m, &aliPay)
if err != nil {
//log.Error(err)
w.Write([]byte("error"))
return nil, err
}
// err = biz.AliAppCallBack(&aliPay)
// if err != nil {
// //log.Error(err)
// w.Write([]byte("error"))
// }
w.Write([]byte("success"))
return &aliPay, nil
}