def get_response_text(api_url, post_data=None, is_xml=False, cert=None): """ request api server to get response text, response text is json str or xml format :param api_url: :param post_data: dict data to send to api server or None :param is_xml: format post data to xml if True otherwise json str :param cert: cert file path if needed """ assert type(post_data) in [type(None), dict], u"post_data must be dict or None" assert type(cert) in [tuple, str, type(None)], u"cert must be tuple, str, or None" if post_data is None: ret = requests.get(api_url) else: if is_xml: post_data = xml_helper.data_to_xml(post_data) else: post_data = json.dumps(post_data) context = { "url": api_url, "data": post_data, } if cert: context["cert"] = cert ret = requests.post(**context) return ret.content
def parse_msg_xml(msg, data): if 'ToUserName' not in data: data['ToUserName'] = msg['FromUserName'] if 'FromUserName' not in data: data['FromUserName'] = msg['ToUserName'] if 'CreateTime' not in data: data['CreateTime'] = int(time.time()) return xml_helper.data_to_xml(data)
def pay_notify(request, pay_info_model, **kwargs): """ 微信支付后通知接口,如果需要在收到结果后处理业务逻辑,使用以下提供的两个信号 """ msg = util_xml.parse_xml_request(request) if msg.get("return_code") == "SUCCESS": sigs.sig_pay_notify.send(sender=request, msg=msg) trade_no, transaction_id = msg.get("out_trade_no"), msg.get("transaction_id") if trade_no and transaction_id: # 检查是否重复推送 try: pay_info_model.objects.get(transaction_id=transaction_id) except pay_info_model.DoesNotExist: # 记录微信支付结果 weixin_pay_info = pay_info_model.objects.create( nonce_str=msg.get("nonce_str"), sign=msg.get("sign"), result_code=msg.get("result_code"), err_code=msg.get("err_code"), err_code_des=msg.get("err_code_des"), openid=msg.get("openid"), is_subscribe=msg.get("is_subscribe"), trade_type=msg.get("trade_type"), bank_type=msg.get("bank_type"), total_fee=int(msg.get("total_fee") or 0), cash_fee=int(msg.get("cash_fee") or 0), coupon_count=int(msg.get("coupon_count") or 0), coupon_fee=int(msg.get("coupon_fee") or 0), transaction_id=(msg.get("transaction_id")), attach=(msg.get("attach")), time_end=(msg.get("time_end")), **kwargs ) weixin_pay_info.save() sigs.sig_pay_info_created.send(sender=request, msg=msg, pay_info=weixin_pay_info) logger.debug("%s accepted and saved msg:%s" % (transaction_id, msg)) else: logger.error("%s exists in db msg:%s" % (transaction_id, msg)) return HttpResponse(util_xml.data_to_xml({ "return_code": "SUCCESS", "return_msg": "OK", }))