Kaynağa Gözat

微信支付 回调通知

chenshibin 4 yıl önce
ebeveyn
işleme
3bcac289d2
2 değiştirilmiş dosya ile 70 ekleme ve 10 silme
  1. 10 10
      Controller/CloudStorage.py
  2. 60 0
      Object/WechatPayObject.py

+ 10 - 10
Controller/CloudStorage.py

@@ -85,7 +85,7 @@ class CloudStorageView(View):
         elif operation == 'doalicallback':
             return self.do_pay_by_ali_callback(request)
         elif operation == 'dowechatnotify':
-            return self.do_pay_by_wechat_callback(request, request_dict, response)
+            return self.do_pay_by_wechat_callback(request, response)
         elif operation == 'getsignsts':
             ip = CommonService.get_ip_address(request)
             return self.do_get_sign_sts(request_dict, ip, response)
@@ -954,22 +954,22 @@ class CloudStorageView(View):
         red_url = "{SERVER_DOMAIN}cloudstorage/payOK".format(SERVER_DOMAIN=SERVER_DOMAIN)
         return HttpResponseRedirect(red_url)
 
-    def do_pay_by_wechat_callback(self, request, request_dict, response):
-        result_code = request_dict.get('result_code', None)
-        result_codetwo = request.POST.dict()
+    def do_pay_by_wechat_callback(self, request, response):
+
         logger = logging.getLogger('log')
 
-        data = json.loads(request.body)
-        logger.info(data)
+        logger.info(request.body)
 
-        result_code = data.get('return_code')
+        request_dict = WechatPayObject.weixinpay_call_back(request.body)
+        trade_status = request_dict['result_code']  # 业务结果  SUCCESS/FAIL
         try:
-            if result_code == 'SUCCESS':
+            if trade_status == "SUCCESS":
                 logger.info('微信回调返回值 进来了。')
-                check_sign = WechatPayObject.get_notifypay(request_dict)
+                check_sign = WechatPayObject.get_notifypay(trade_status)
                 if not check_sign:
                     return HttpResponse(WechatPayObject.xml_to_dict({'return_code': 'FAIL', 'return_msg': '签名失败'}))
-                orderID = request_dict.get('out_trade_no', None)
+                logger.info('签名成功')
+                orderID = request_dict['out_trade_no']  # 商户订单号
                 print("进来了,微信支付成功回调")
                 self.callback_dostatus(orderID)
 

+ 60 - 0
Object/WechatPayObject.py

@@ -150,3 +150,63 @@ class WechatPayObject:
             xml += '<' + k + '>' + v + '</' + k + '>'
         xml += "</xml>"
         return xml
+
+
+
+    def weixinpay_call_back(self, body):
+        """
+
+        微信支付回调
+
+        :param request: 回调参数
+
+        :return:
+
+        """
+        args = str(body, 'utf-8')
+
+        if args is None:
+             return None
+
+        print(args)
+
+        # 验证平台签名
+
+        resp_dict = self.handle_wx_response_xml(args)
+        if resp_dict is None:
+            print('签名验证失败!!!')
+            return None
+
+        return resp_dict
+
+
+
+    def handle_wx_response_xml(self, params):
+
+        """
+
+        处理微信支付返回的xml格式数据
+
+        """
+
+        try:
+
+            resp_dict = xmltodict.parse(params)['xml']
+
+            if not resp_dict or len(resp_dict) < 1:
+                print('resp_dict is zero+++++++++')
+
+                return None
+
+            return_code = resp_dict.get('return_code')
+
+            if return_code == 'SUCCESS':  # 仅仅判断通信标识成功,非交易标识成功,交易需判断result_code
+                return resp_dict
+
+
+        except Exception as e:
+            print(e)
+
+            return None
+
+        return None