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")