def query_and_set_order(trade): if trade.transaction_id: fromWxData = WxNativePayApi.order_query({'transaction_id': trade.transaction_id}) else: fromWxData = WxNativePayApi.order_query({'out_trade_no': trade.trade_no}) if fromWxData['return_code'] == 'SUCCESS': if fromWxData["result_code"] == "SUCCESS": if checkSign(fromWxData) == 0: trade.order_status = WxOrderQueryPayStatusView.__wx_pay_trade_status.get(fromWxData['trade_state'], 1) trade.trade_status_desc = fromWxData.get('trade_state_desc', '') if not trade.transaction_id and 'transaction_id' in fromWxData: trade.transaction_id = fromWxData['transaction_id'] else: logging.error('from weixin order query: sign error: ' + trade.trade_no) logging.error(fromWxData) return None else: trade.order_status = 1 if fromWxData.get('time_end'): trade.end_time = datetime.strptime(fromWxData['time_end'], '%Y%m%d%H%M%S') trade.wx_result_code = fromWxData['result_code'] trade.wx_err_code = fromWxData.get('err_code', '') trade.wx_err_code_des = fromWxData.get('err_code_des', '') with transaction.atomic(): # 支付成功 if trade.order_status == 3 and trade.is_order_over is False: trade.wx_response = fromWxData trade.is_order_over = True # 通知订单支付成功 product_order = ProductOrder.objects.select_for_update().get(order_no=trade.product_id) # product_order.status = 2 # product_order.pay_type = 2 # product_order.save() notify_order_pay_success(product_order) trade.save(force_update=True) return trade else: return None
def get_notify_data(self, request): """ 接收从微信支付后台发送过来的数据并验证签名 :return: 微信支付后台返回的数据 """ # 接收从微信后台POST过来的数据 logging.debug("in notify start ######################## ") try: fromWxData = make_dict_from_xml(request.body) logging.debug("in notify : notify request.body ok ######################## ") if fromWxData["return_code"] == "SUCCESS": if checkSign(fromWxData) != 0: raise WxException(WxException.ERROR_SIGN, u"签名错误") return fromWxData except WxException as e: logging.error("from weixin notification: sign error: " + request.body) data = ( "<xml><return_code><![CDATA[FAIL]]></return_code>" "<return_msg><![CDATA[{}]]></return_msg></xml>".format(".".join(e.msgs)) ) return HttpResponse(data, content_type="text/xml")
def post(self, request, format=None): """ 接收微信支付后台发送的支付结果并对订单有效性进行验证,将验证结果反馈给微信支付后台 根据支付结果修改交易单,通知订单状态发生改变 :param format: :return: """ try: logging.info(request.data) notify_data = self.get_notify_data(request.data) if isinstance(notify_data, HttpResponse): return notify_data transaction_id = notify_data.get('transaction_id') if not transaction_id: return HttpResponse('<xml><return_code><![CDATA[FAIL]]></return_code>' '<return_msg><![CDATA[支付结果中微信订单号不存在]]></return_msg></xml>') elif self.__query_order(transaction_id) == False: logging.error(u'订单查询失败' + str(notify_data)) return HttpResponse('<xml><return_code><![CDATA[FAIL]]></return_code>' '<return_msg><![CDATA[订单查询失败]]></return_msg></xml>') if notify_data["return_code"] != "SUCCESS": data = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[通信失败]]></return_msg></xml>' return HttpResponse(data) trade_no = notify_data.get('out_trade_no') if not trade_no: logging.error(u'订单通知失败,缺少必要参数out_trade_no : ' + str(notify_data)) return HttpResponse('<xml><return_code><![CDATA[FAIL]]></return_code>' '<return_msg><![CDATA[缺少必要的参数:out_trade_no]]></return_msg></xml>') need_notify_order_success = False with transaction.atomic(): trade = WxPaymentTradeOrder.objects.select_for_update().get(trade_no=trade_no) if trade.order_status not in (0, 2): # 已经处理过,直接返回成功接收 return Response(data=self.__success_data, status=status.HTTP_200_OK) product_order = ProductOrder.objects.select_for_update().get(order_no=trade.product_id) trade.transaction_id = notify_data.get('transaction_id') if not trade.prepay_id and notify_data.get('prepay_id'): trade.prepay_id = notify_data.get('prepay_id') if notify_data['result_code'] == 'SUCCESS': trade.order_status = 3 product_order.status = 2 product_order.pay_type = 2 else: trade.order_status = 1 product_order.status = 3 if notify_data.get('time_end'): trade.end_time = datetime.strptime(notify_data['time_end'], '%Y%m%d%H%M%S') trade.wx_result_code = notify_data['result_code'] trade.wx_err_code = notify_data.get('err_code', '') trade.wx_err_code_des = notify_data.get('err_code_des', '') trade.wx_response = notify_data if trade.order_status == 3 and trade.is_order_over is False: trade.is_order_over = True need_notify_order_success = True trade.save(force_update=True) product_order.save() if need_notify_order_success: # 通知订单支付成功 notify_order_pay_success(product_order) content = '<xml><return_code><![CDATA[SUCCESS]]></return_code></xml>' return HttpResponse(content) except WxPaymentTradeOrder.DoesNotExist as e: logging.exception(e) return HttpResponse('<xml><return_code><![CDATA[FAIL]]></return_code>' '<return_msg><![CDATA[该订单号不存在]]></return_msg></xml>') except ProductOrder.DoesNotExist as e: logging.exception(e) return HttpResponse('<xml><return_code><![CDATA[FAIL]]></return_code>' '<return_msg><![CDATA[该订单不存在]]></return_msg></xml>') except Exception as e: logging.exception(e) return HttpResponse(body='<xml><return_code><![CDATA[FAIL]]></return_code><return_msg></return_msg></xml>', status=status.HTTP_500_INTERNAL_SERVER_ERROR)