-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathalipay_handle_wap.go
226 lines (189 loc) · 6.06 KB
/
alipay_handle_wap.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
package alipay
import (
"encoding/xml"
"errors"
"fmt"
"io"
"log"
"net/http"
"net/url"
"regexp"
)
func AlipayWapRequest(alipayConfig *AlipayConfig, r *AlipayWebRequest, w io.Writer) error {
var format = "xml"
var v = "2.0"
args := []interface{}{}
args = append(args, alipayConfig.Notify_url)
args = append(args, alipayConfig.Wap_callback_url)
args = append(args, alipayConfig.Seller_id)
args = append(args, r.OutTradeNo)
args = append(args, r.Subject)
args = append(args, fmt.Sprintf("%.2f", r.TotalFee))
args = append(args, alipayConfig.Wap_merchant_url)
var token_req_data = fmt.Sprintf("<direct_trade_create_req><notify_url>%v</notify_url><call_back_url>%v</call_back_url><seller_account_name>%v</seller_account_name><out_trade_no>%v</out_trade_no><subject>%v</subject><total_fee>%v</total_fee><merchant_url>%v</merchant_url></direct_trade_create_req>", args...)
log.Println("token_req_data = %v", token_req_data)
p := Kvpairs{
Kvpair{`service`, alipayConfig.Wap_Service},
Kvpair{`partner`, alipayConfig.Partner},
Kvpair{`sec_id`, alipayConfig.Sign_type},
Kvpair{`format`, format},
Kvpair{`v`, v},
Kvpair{`req_id`, r.OutTradeNo},
Kvpair{`req_data`, token_req_data},
Kvpair{`_input_charset`, alipayConfig.Input_charset},
}
paraFilter(&p)
argSort(&p)
buildRequestPara(&p, alipayConfig)
log.Println("pararms is = %v", p)
origin_token, err := getHttpResponsePOST(wap_alipayGatewayNew, "", alipayConfig.Input_charset, &p)
if nil != err {
log.Println("get token fail : %v", err)
return err
}
log.Println("origin_token = %v", origin_token)
origin_token, _ = url.QueryUnescape(origin_token)
log.Println("origin_token url.QueryUnescape = %v", origin_token)
token := ParseOriginTokenMsg(origin_token, alipayConfig)
if token == "" {
fmt.Fprintln(w, origin_token)
return errors.New("解析token失败")
}
var trade_req_data = fmt.Sprintf("<auth_and_execute_req><request_token>%v</request_token></auth_and_execute_req>", token)
p2 := Kvpairs{
Kvpair{`service`, alipayConfig.Service},
Kvpair{`partner`, alipayConfig.Partner},
Kvpair{`sec_id`, alipayConfig.Sign_type},
Kvpair{`format`, format},
Kvpair{`v`, v},
Kvpair{`req_id`, r.OutTradeNo},
Kvpair{`req_data`, trade_req_data},
Kvpair{`_input_charset`, alipayConfig.Input_charset},
}
paraFilter(&p2)
argSort(&p2)
buildRequestPara(&p2, alipayConfig)
// log.Println("pararm2 = %v", p2)
fmt.Fprintln(w, `<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head><body>`)
fmt.Fprintf(w, `<form name='alipaysubmit' action='%s_input_charset=utf-8' method='get'> `, wap_alipayGatewayNew)
for _, kv := range p2 {
fmt.Fprintf(w, `<input type='hidden' name='%s' value='%s' />`, kv.K, kv.V)
}
fmt.Fprintln(w, `<script>document.forms['alipaysubmit'].submit();</script>`)
fmt.Fprintln(w, `</body></html>`)
return nil
}
/**
* 验证消息是否是支付宝发出的合法消息
* @return 验证结果
*/
func VerifyWapNotify(r *http.Request, alipayConfig *AlipayConfig) (Notify, error) {
log.Println("VerifyWapNotify begin")
p := &Kvpairs{}
sign := ""
sign_type := ""
notify_data := ""
var alipayNotify Notify
for k := range r.Form {
v := r.Form.Get(k)
switch k {
case "sign":
sign = v
continue
case "sign_type":
sign_type = v
continue
case "notify_data":
notify_data = v
}
*p = append(*p, Kvpair{k, v})
}
//除去待签名参数数组中的空值和签名参数
paraFilter(p)
//对待签名参数数组排序
argSort(p)
if notify_data == "" {
log.Println("notify_data is null")
return alipayNotify, errors.New("notify_data is null")
}
err := xml.Unmarshal([]byte(notify_data), &alipayNotify)
if err != nil {
log.Println("xml Unmarshal err : %v", err)
return alipayNotify, err
}
// log.Println("p = %v", p)
p2 := sortNotifyPara(p)
// log.Println("p2 = %v", p2)
//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
prestr := createLinkStringNoUrl(&p2)
log.Println("VerifyWapNotify prestr is : %v ", prestr)
log.Println("VerifyWapNotify sign is : %v , sign_type is %v", sign, sign_type)
switch alipayConfig.Sign_type {
case "MD5":
if md5Sign(prestr, alipayConfig.Key) != sign {
return alipayNotify, fmt.Errorf("sign invalid")
}
break
default:
return alipayNotify, fmt.Errorf("no right sign_type")
}
log.Println("VerifyWapNotify Notify is %v", alipayNotify)
notify_id := alipayNotify.Notify_id
//获取支付宝远程服务器ATN结果(验证是否是支付宝发来的消息)(1分钟认证)
responseTxt, err := getResponse(notify_id, alipayConfig)
if err != nil {
return alipayNotify, err
}
log.Println("VerifyWapNotify responseTxt is: %v", responseTxt)
reg := regexp.MustCompile(`true`)
if 0 == len(reg.FindAllString(responseTxt, -1)) {
log.Println("VerifyWapNotify responseTxt verify fail ")
return alipayNotify, fmt.Errorf("VerifyWapNotify responseTxt is wrong")
}
log.Println("VerifyWapNotify responseTxt verify success ")
return alipayNotify, nil
}
/**
* 验证消息是否是支付宝发出的合法消息
* @return 验证结果
*/
func VerifyWapCallback(r *http.Request, alipayConfig *AlipayConfig) error {
log.Println("VerifyWapCallback begin")
p := &Kvpairs{}
sign := ""
sign_type := ""
for k := range r.Form {
v := r.Form.Get(k)
switch k {
case "sign":
sign = v
continue
case "sign_type":
sign_type = v
continue
}
*p = append(*p, Kvpair{k, v})
}
//除去待签名参数数组中的空值和签名参数
paraFilter(p)
//对待签名参数数组排序
argSort(p)
// log.Println("VerifyWapCallback p = %v", p)
//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串
prestr := createLinkStringNoUrl(p)
log.Println("VerifyWapCallback prestr is : %v ", prestr)
log.Println("VerifyWapCallback sign is : %v , sign_type is %v", sign, sign_type)
switch sign_type {
case "MD5":
if md5Sign(prestr, alipayConfig.Key) != sign {
return fmt.Errorf("sign invalid")
}
break
default:
return fmt.Errorf("no right sign_type")
}
log.Println("VerifyWapCallback success")
return nil
}