-
Notifications
You must be signed in to change notification settings - Fork 120
/
callback.go
152 lines (131 loc) · 3.4 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
package gopay
import (
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"github.com/milkbobo/gopay/client"
"github.com/milkbobo/gopay/common"
"github.com/milkbobo/gopay/util"
"encoding/json"
)
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" {
//错误日志
panic("签名类型未知")
}
client.DefaultAliWebClient().CheckSign(signData, m["sign"])
var aliPay common.AliWebPayResult
err := util.MapStringToStruct(m, &aliPay)
if err != nil {
w.Write([]byte("error"))
panic(err)
}
w.Write([]byte("success"))
return &aliPay, nil
}
// 支付宝app支付回调
func AliAppCallback(w http.ResponseWriter, r *http.Request) (*common.AliWebPayResult, error) {
var result string
defer func() {
w.Write([]byte(result))
}()
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"] != "RSA2" {
result = "error"
panic("签名类型未知")
}
client.DefaultAliAppClient().CheckSign(signData, m["sign"])
mByte, err := json.Marshal(m)
if err != nil {
result = "error"
panic(err)
}
var aliPay common.AliWebPayResult
err = json.Unmarshal(mByte, &aliPay)
if err != nil {
result = "error"
panic(fmt.Sprintf("m is %v, err is %v", m, err))
}
result = "success"
return &aliPay, nil
}
// WeChatCallback 微信支付
func WeChatCallback(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, err := ioutil.ReadAll(r.Body)
if err != nil {
returnCode = "FAIL"
returnMsg = "Bodyerror"
return &reXML, errors.New(returnCode + ":" + returnMsg)
}
err = xml.Unmarshal(body, &reXML)
if err != nil {
returnCode = "FAIL"
returnMsg = "参数错误"
return &reXML, errors.New(returnCode + ":" + returnMsg)
}
if reXML.ReturnCode != "SUCCESS" {
returnCode = "FAIL"
return &reXML, errors.New(reXML.ReturnCode)
}
m := util.XmlToMap(body)
var signData []string
for k, v := range m {
if k == "sign" {
continue
}
signData = append(signData, fmt.Sprintf("%v=%v", k, v))
}
key := client.DefaultWechatAppClient().Key
mySign, err := client.WechatGenSign(key, m)
if err != nil {
return &reXML, err
}
if mySign != m["sign"] {
panic(errors.New("签名交易错误"))
}
returnCode = "SUCCESS"
return &reXML, nil
}
func WeChatWebCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPayResult, error) {
return WeChatCallback(w, r)
}
func WeChatAppCallback(w http.ResponseWriter, r *http.Request) (*common.WeChatPayResult, error) {
return WeChatCallback(w, r)
}