forked from wqcd/wxH5PayDemo
/
app.py
191 lines (155 loc) · 5.91 KB
/
app.py
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
#!/usr/bin/python
# -*- encoding:utf-8 -*-
import hashlib
import uuid
import re
from tornado.httpserver import HTTPServer
import tornado.web
from tornado.ioloop import IOLoop
import os
import requests
from urllib import request as reqt
from tornado.options import define, parse_command_line, options
from tornado.web import Application
session = requests.session()
session.verify = False
def get_token(md5str):
m1 = hashlib.md5()
m1.update(md5str.encode("utf-8"))
token = m1.hexdigest()
return token
class indexHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class Notify_URLHandler(tornado.web.RequestHandler):
def get(self):
print(self.request.body)
xml1 = str(self.request.body, encoding='utf-8')
print("cml", xml1)
pattertn = re.compile(r'out_trade_no><!\[CDATA\[(.*?)]]></out_trade_no')
out_trade_no = pattertn.findall(xml1)[0]
print(out_trade_no)
appid = ''
mch_id = ''
body = ""
key = ''
nonce_str = str(uuid.uuid4()).replace('-', "")[:-3]
signA = "appid=%s&mch_id=%s&nonce_str=%s&out_trade_no=%s" % (appid, mch_id, nonce_str, out_trade_no)
print(signA)
strSignTmp = signA + "&key=" + key
sign = get_token(strSignTmp).upper()
xml = '''<xml>
<appid>%s</appid>
<mch_id>%s</mch_id>
<nonce_str>%s</nonce_str>
<out_trade_no>%s</out_trade_no>
<sign>%s</sign>
</xml>''' % (appid,mch_id,nonce_str, out_trade_no, sign)
url = 'https://api.mch.weixin.qq.com/pay/orderquery'
headers = {
'Accept-Language': 'zh-CN,en-US;q=0.8',
# 'Content-Type': 'application/json'
# "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept": "application/xml;charset=UTF-8",
}
# 我用requests 出现乱码,用urllib
req = reqt.Request(url, headers=headers, data=xml.encode()) # POST方法
#
page = reqt.urlopen(req).read()
print(page)
page = page.decode('utf-8')
#
print(page)
pattertn2 = re.compile(r'trade_state_desc><!\[CDATA\[(.*?)]]></trade_state_desc')
trade_state_desc = pattertn2.findall(page)[0]
print(trade_state_desc)
if trade_state_desc == '支付成功':
print("匹配成功")
#######################
# 这里处理自己需要的处理的
#######################
self.write("")
def post(self):
print(self.request.body)
self.write("")
# 微信回调函数
class Notify_wxHandler(tornado.web.RequestHandler):
def get(self):
total_fee = "20"
# 金额
appid = 'appid'
mch_id = 'mch_id'
body = "微信Demo"
key = ''
host = "域名 "
# 微信填写的备案域名
nonce_str = str(uuid.uuid4()).replace('-', "")[:-3]
# 生成随机字符串
notify_url = 'http://{}/Notify_URL'.format(host)
# 回调函数
wap_name = '腾讯充值'
wap_name.encode('utf-8')
scene_info = {
"h5_info": {"type": "Wap", "wap_url": "http://{}/Notify_wx".format(host), "wap_name": wap_name}}
out_trade_no = get_token(nonce_str)
# md5 加密生成订单编号
ip = self.request.remote_ip
# x_forwarded_for = self.request.META.get('HTTP_X_FORWARDED_FOR')
# print(self.request.META)
# if x_forwarded_for:
# spbill_create_ip = x_forwarded_for.split(',')[0] # 所以这里是真实的ip
# else:
# spbill_create_ip = self.request.META.get('REMOTE_ADDR') # 这里获得代理ip
# 此处 为django获取ip的方法
total_amount = str(int(float(total_fee) * 100))
trade_type = 'MWEB'
signA = "appid=%s&body=%s&mch_id=%s&nonce_str=%s¬ify_url=%s&out_trade_no=%s&scene_info=%s&spbill_create_ip=%s&total_fee=%s&trade_type=%s" % (
appid, body, mch_id, nonce_str, notify_url, out_trade_no, scene_info, ip, total_amount,
trade_type)
print(signA)
strSignTmp = signA + "&key=" + key
sign = get_token(strSignTmp).upper()
# 进行MD5加密
print(sign)
post_data = "<xml>"
for i in (signA + "&sign=" + sign).split("&"):
xml1 = i.split("=")[0]
xml2 = i.split("=")[1]
post_data = post_data + '<' + xml1 + '>' + xml2 + '</' + xml1 + '>'
post_data = post_data + '</xml>'
# 组合xml请求
print(post_data)
# post_data.encode('utf-8')
headers = {'Content-Type': 'binary'}
# 解决post_data 中文编码问题
url = "https://api.mch.weixin.qq.com/pay/unifiedorder"
res = requests.post(url, data=post_data.encode(), headers=headers, verify=False)
# 提交订单信息
# res.text.encode('utf-8')
print(res.text.encode('latin_1').decode('utf8'))
pattern = re.compile("<mweb_url><!\[CDATA\[(.*?)]]></mweb_url")
redicrt_url = pattern.findall(res.text)[0]
# 匹配微信回调函数,调用微信app进行支付
self.redirect(redicrt_url)
# 重定向至微信
if __name__ == '__main__':
define("port", default=8001, help="默认端口8000")
parse_command_line()
app = Application(
[
(r'/index', indexHandler),
(r'/Notify_wx', Notify_wxHandler),
(r'/Notify_URL', Notify_URLHandler),
],
# 项目配置信息
# 网页模板
template_path=os.path.join(os.path.dirname(__file__), "templates"),
# 静态文件
static_path=os.path.join(os.path.dirname(__file__), "static"),
# debug=False
)
# 部署
server = HTTPServer(app)
server.listen(options.port)
# 轮询监听
IOLoop.current().start()