views.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # coding=utf-8
  2. import json
  3. import xmltodict
  4. from django.db import transaction
  5. from django.http import HttpResponse
  6. from apps.account import tenant_log
  7. from rest_framework.views import APIView
  8. from utils.wx.WXBizMsgCrypt import WXBizMsgCrypt
  9. from utils.exceptions import CustomError
  10. from utils import response_ok, response_error
  11. from utils.wechatpay import WechatPayNotify
  12. from apps.foundation.models import Config,BizLog
  13. from apps.pay.models import Pay
  14. class WechatNotifyView(APIView):
  15. def dispatch(self, request, *args, **kwargs):
  16. param = request.body.decode('utf-8')
  17. tenant_key = Config.getConfigValue(Config.KEY_WECHAT_TENANT_KEY)
  18. notify = WechatPayNotify(param, tenant_key)
  19. try:
  20. data = notify.handle()
  21. if not data:
  22. raise CustomError(u'错误的请求!')
  23. result_code = data['result_code']
  24. if result_code != 'SUCCESS':
  25. raise CustomError(u'错误的请求!')
  26. no = data['out_trade_no']
  27. amount = int(data['total_fee']) * 100
  28. with transaction.atomic():
  29. pay = Pay.getByNo(no)
  30. pay.payConfirm(amount)
  31. tenant_log(pay.customer.user, BizLog.INSERT, u'支付成功,no=%s' % no, param)
  32. except:
  33. return HttpResponse(WechatPayNotify.response_fail())
  34. return HttpResponse(WechatPayNotify.response_ok())