-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathnotify_view_demo.py
36 lines (28 loc) · 1.37 KB
/
notify_view_demo.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
#coding=UTF-8
'''
统一下单回调处理
'''
import xmltodict
from django.http import HttpResponse
def payback(request):
msg = request.body.decode('utf-8')
xmlmsg = xmltodict.parse(msg)
return_code = xmlmsg['xml']['return_code']
if return_code == 'FAIL':
# 官方发出错误
return HttpResponse("""<xml><return_code><![CDATA[FAIL]]></return_code>
<return_msg><![CDATA[Signature_Error]]></return_msg></xml>""",
content_type='text/xml', status=200)
elif return_code == 'SUCCESS':
# 拿到这次支付的订单号
out_trade_no = xmlmsg['xml']['out_trade_no']
# order = Order.objects.get(out_trade_no=out_trade_no)
if xmlmsg['xml']['nonce_str'] != order.nonce_str:
# 随机字符串不一致
return HttpResponse("""<xml><return_code><![CDATA[FAIL]]></return_code>
<return_msg><![CDATA[Signature_Error]]></return_msg></xml>""",
content_type='text/xml', status=200)
# 根据需要处理业务逻辑
return HttpResponse("""<xml><return_code><![CDATA[SUCCESS]]></return_code>
<return_msg><![CDATA[OK]]></return_msg></xml>""",
content_type='text/xml', status=200)