微信官方文档:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
首先 这个 notify_url 有俩个要求
1.公网能直接访问的
2.不能携带参数 (比如你的订单id)
返回的内容微信请求的内容,为xml格式
<xml>
<appid><![CDATA[wx2421b1c4370ec43b]]></appid>
<attach><![CDATA[支付测试]]></attach>
<bank_type><![CDATA[CFT]]></bank_type>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[Y]]></is_subscribe>
<mch_id><![CDATA[10000100]]></mch_id>
<nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>
<openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>
<out_trade_no><![CDATA[1409811653]]></out_trade_no>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>
<sub_mch_id><![CDATA[10000100]]></sub_mch_id>
<time_end><![CDATA[20140903131540]]></time_end>
<total_fee>1</total_fee>
<coupon_fee><![CDATA[10]]></coupon_fee>
<coupon_count><![CDATA[1]]></coupon_count>
<coupon_type><![CDATA[CASH]]></coupon_type>
<coupon_id><![CDATA[10000]]></coupon_id>
<coupon_fee><![CDATA[100]]></coupon_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>
<transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>
</xml>
如果支付成功了 会返回return_code 为 SUCCESS
失败时 return_code为 FAILD
详细代码 views.py
import xml.etree.ElementTree as et
def payback(request):
_xml = request.body
#拿到微信发送的xml请求 即微信支付后的回调内容
xml = str(_xml, encoding="utf-8")
print("xml", xml)
return_dict = {}
tree = et.fromstring(xml)
#xml 解析
return_code = tree.find("return_code").text
try:
if return_code == 'FAIL':
# 官方发出错误
return_dict['message'] = '支付失败'
#return Response(return_dict, status=status.HTTP_400_BAD_REQUEST)
elif return_code == 'SUCCESS':
#拿到自己这次支付的 out_trade_no
_out_trade_no = tree.find("out_trade_no").text
#这里省略了 拿到订单号后的操作 看自己的业务需求
except Exception as e:
pass
finally:
return HttpResponse(return_dict, status=status.HTTP_200_OK)
其实不需要return Response任何东西 微信不会对这个做处理
url.py
from .views.py import payback
url(r'^afterpay', payback),
微信可能会调这个url 多次 ,记得判断订单是否已经做过回调处理