def request(self, *args, **kwargs): kwargs.setdefault('headers', kwargs.get('headers', {})) kwargs['headers']['User-Agent'] = self.USER_AGENT kwargs['headers']['Accept'] = self.accept kwargs['headers']['Accept-Charset'] = self.encoding if 'body' in kwargs: kwargs['headers']['Content-Type'] = self.content_type if '/json' in self.content_type: kwargs['body'] = json.dumps(kwargs['body']) else: kwargs['body'] = urlencode(kwargs['body']) LOG.debug('sending request: %s || %s', args, kwargs) resp, body = super(HTTPClient, self).request(*args, **kwargs) content_type = resp.get('content-type') # may not be the same as ['headers']['Accept'] if body and content_type: body = self._extra_body(content_type, body) if resp.status in (400, 401, 403, 404, 408, 409, 413, 500, 501): raise exceptions.from_response(resp, body) return resp, body
def logout(request): ''' ##用户登出 --- **参数说明** ''' user = request.user user.session_key = None user.save() django_logout(request) LOG.debug(user.session_key) return Response({'message': '您已安全退出'})
def _extra_body(self, content_type, body): if '/xml' in content_type: try: return str_to_xml_tree(body) except ValueError: LOG.debug('can not extra body as XML ElementTree: %s', body) if '/json' in content_type: try: return json.loads(body) except ValueError: LOG.debug('can not dump body as JSON: %s', body) return body # nothing changed
def login(request): ''' ##用户登录(普通用户) --- **参数说明** * username=admin * password=1234qwer **返回值** ''' pwd = request.data.get('password') LOG.debug("pwd %s", pwd) username = request.data.get('username') LOG.debug("username %s", username) user = authenticate(username=username, password=pwd) if user is None: user = authenticate(username=username, password=pwd) if user is None: raise ParseError(u'账号或者密码错误') data = Response(_login_user(request, user)) return data
def send_sms(mobile, content): if not mobile: LOG.debug('ERROR for mobile %s', mobile) return req = urllib2.Request(settings.SMS_ZZ_URL) data = { 'UserName': settings.SMS_ZZ_ACCOUNT, 'UserPass': settings.SMS_ZZ_KEY, 'Mobile': mobile, 'Content': content.encode('utf-8'), 'Subid': '01' } req.add_data(urllib.urlencode(data)) res = urllib2.urlopen(req) result = res.read() res.close() if not result.startswith('00') and not result.startswith('03'): LOG.debug('ERROR for sms %s', result) return True
def month_name(dt): """ This may be replaced by default [date] filter: blog.created|date:'N' """ LOG.debug("##### month is: %s", dt.month) return MON_NAME.get(dt.month)
def i_get_new_item(step, commands, command, serv_as): """Get item from memcache server for the key (dict,str)""" result = getattr(world.memcache_client, world.memcache_maps[commands][command])('1000') LOG.debug('Result of command %s from memcache client is %s' % (command, result)) if result != "test_value": raise AssertionError('Suggested command is not get element from memcache: world.memcache_client.%s' % str(world.memcache_maps[commands][command])+"('1000')")
def i_add_new_item(step, commands, command, serv_as): """Add new item to memcache server (dict,str)""" result = getattr(world.memcache_client, world.memcache_maps[commands][command])('1000','test_value') LOG.debug('Result of command %s from memcache client is %s' % (command, result)) if not result: raise AssertionError('Suggested command is not added an element to memcache: world.memcache_client.%s' % str(world.memcache_maps[commands][command])+"('1000','test_value')")
def notify_url(request): #首先得到财付通的发来的GET请求 resHandler = ResponseHandler(request) resHandler.setKey(key) if resHandler.isTenpaySign(): #得到notify_id notify_id = resHandler.getParameter("notify_id") LOG.debug("got notify_id: %s" % notify_id) queryReq = RequestHandler() queryReq.init() queryReq.setKey(key) queryReq.setGateURL("https://gw.tenpay.com/gateway/verifynotifyid.xml") queryReq.setParameter("partner",partner) queryReq.setParameter("notify_id",notify_id) httpClient = TenpayHttpClient() httpClient.setTimeOut(5) httpClient.setReqContent(queryReq.getRequestURL()) LOG.debug("queryReq.getRequestURL() : %s" % queryReq.getRequestURL()) #根据得到的notify_id再次到财付通查询验证订单消息 if httpClient.call(): #负责解析httpClient从财付通得到的xml格式中的内容 queryRes = ClientResponseHandler() #加载由httpClient返回的内容 queryRes.setContent(httpClient.getResContent()) LOG.debug("queryRes.getALLParameters(): %s" % queryRes.getALLParameters()) LOG.debug("httpClient.getResContent() : %s" % httpClient.getResContent()) queryRes.setKey(key) if queryRes.isTenpaySign() and queryRes.getParameter("retcode") == "0" and queryRes.getParameter("trade_state") == "0" and queryRes.getParameter("trade_mode") == "1": out_trade_no = queryRes.getParameter("out_trade_no") transaction_id = queryRes.getParameter("transaction_id") trade_state = queryRes.getParameter("trade_state") trade_mode = queryRes.getParameter("trade_mode") total_fee = queryRes.getParameter("total_fee") discount = queryRes.getParameter("discount") #开始处理业务 #注意验证订单不要重复 #注意判断返回金额 p = PayOrder.objects.get(trade_no=out_trade_no) if p.total_fee == int(total_fee) and p.payment_info != '2': p.transaction_id = transaction_id p.trade_state = trade_state p.trade_mode = trade_mode p.ret_code = queryRes.getParameter("retcode") p.ret_msg = queryRes.getParameter("retmsg") p.payment_info = 2 p.save() try: tenant_id = p.tenant_id tenant = Tenant.objects.get(tenant_id=tenant_id) increase = (int(total_fee) * int(recharge_exchange_rate)) tenant.balance += increase tenant.save() if p.send_sms: send_balance_increase_sms(p, tenant, increase) except Exception,e: LOG.error(e) raise e else: pass #处理完毕 LOG.debug("Trade completed processed: out_trade_no: %s\n total_fee:%s\n transaction_id:%s\n" % (out_trade_no,total_fee,transaction_id)) return "success" else: LOG.debug("Check sign failed or trade err: trade_state=%s\n retcode=%s\n retmsg=%s\n" % (queryRes.getParameter("trade_state"), queryRes.getParameter("ret_code"), queryRes.getParameter("retmsg"))) return "fail"
def index(request): LOG.debug('request.user is [%s]', request.user) return shortcuts.render_to_response('index.html', {'request':request})
def put(self, request): super(SampleRestView, self).put(request) LOG.debug('PUT - request.PUT : %s', request.PUT) return self.json(request.PUT.dict())
def delete(self, request): super(SampleRestView, self).delete(request) LOG.debug('DELETE - request.DELETE : %s', request.DELETE) return self.json(request.DELETE.dict())
def post(self, request): LOG.debug('POST - request.POST : %s', request.POST) return self.json(request.POST.dict())
def get(self, request): LOG.debug('GET - request.GET : %s', request.GET) return self.json(request.GET.dict())
send_balance_increase_sms(p, tenant, increase) except Exception, e: LOG.error(e) raise e else: pass #处理完毕 LOG.debug( "Trade completed processed: out_trade_no: %s\n total_fee:%s\n transaction_id:%s\n" % (out_trade_no, total_fee, transaction_id)) return "success" else: LOG.debug( "Check sign failed or trade err: trade_state=%s\n retcode=%s\n retmsg=%s\n" % (queryRes.getParameter("trade_state"), queryRes.getParameter("ret_code"), queryRes.getParameter("retmsg"))) return "fail" else: LOG.debug( "Communication with tenpay failed: responsecode=%s\n error_info=%s\n" % (httpClient.getResponseCode(), httpClient.getErrInfo())) return "fail" return "fail"
def notify_url(request): #首先得到财付通的发来的GET请求 resHandler = ResponseHandler(request) resHandler.setKey(key) if resHandler.isTenpaySign(): #得到notify_id notify_id = resHandler.getParameter("notify_id") LOG.debug("got notify_id: %s" % notify_id) queryReq = RequestHandler() queryReq.init() queryReq.setKey(key) queryReq.setGateURL("https://gw.tenpay.com/gateway/verifynotifyid.xml") queryReq.setParameter("partner", partner) queryReq.setParameter("notify_id", notify_id) httpClient = TenpayHttpClient() httpClient.setTimeOut(5) httpClient.setReqContent(queryReq.getRequestURL()) LOG.debug("queryReq.getRequestURL() : %s" % queryReq.getRequestURL()) #根据得到的notify_id再次到财付通查询验证订单消息 if httpClient.call(): #负责解析httpClient从财付通得到的xml格式中的内容 queryRes = ClientResponseHandler() #加载由httpClient返回的内容 queryRes.setContent(httpClient.getResContent()) LOG.debug("queryRes.getALLParameters(): %s" % queryRes.getALLParameters()) LOG.debug("httpClient.getResContent() : %s" % httpClient.getResContent()) queryRes.setKey(key) if queryRes.isTenpaySign() and queryRes.getParameter( "retcode") == "0" and queryRes.getParameter( "trade_state") == "0" and queryRes.getParameter( "trade_mode") == "1": out_trade_no = queryRes.getParameter("out_trade_no") transaction_id = queryRes.getParameter("transaction_id") trade_state = queryRes.getParameter("trade_state") trade_mode = queryRes.getParameter("trade_mode") total_fee = queryRes.getParameter("total_fee") discount = queryRes.getParameter("discount") #开始处理业务 #注意验证订单不要重复 #注意判断返回金额 p = PayOrder.objects.get(trade_no=out_trade_no) if p.total_fee == int(total_fee) and p.payment_info != '2': p.transaction_id = transaction_id p.trade_state = trade_state p.trade_mode = trade_mode p.ret_code = queryRes.getParameter("retcode") p.ret_msg = queryRes.getParameter("retmsg") p.payment_info = 2 p.save() try: tenant_id = p.tenant_id tenant = Tenant.objects.get(tenant_id=tenant_id) increase = (int(total_fee) * int(recharge_exchange_rate)) tenant.balance += increase tenant.save() if p.send_sms: send_balance_increase_sms(p, tenant, increase) except Exception, e: LOG.error(e) raise e else: pass #处理完毕 LOG.debug( "Trade completed processed: out_trade_no: %s\n total_fee:%s\n transaction_id:%s\n" % (out_trade_no, total_fee, transaction_id)) return "success" else: LOG.debug( "Check sign failed or trade err: trade_state=%s\n retcode=%s\n retmsg=%s\n" % (queryRes.getParameter("trade_state"), queryRes.getParameter("ret_code"), queryRes.getParameter("retmsg"))) return "fail"
increase = (int(total_fee) * int(recharge_exchange_rate)) tenant.balance += increase tenant.save() if p.send_sms: send_balance_increase_sms(p, tenant, increase) except Exception,e: LOG.error(e) raise e else: pass #处理完毕 LOG.debug("Trade completed processed: out_trade_no: %s\n total_fee:%s\n transaction_id:%s\n" % (out_trade_no,total_fee,transaction_id)) return "success" else: LOG.debug("Check sign failed or trade err: trade_state=%s\n retcode=%s\n retmsg=%s\n" % (queryRes.getParameter("trade_state"), queryRes.getParameter("ret_code"), queryRes.getParameter("retmsg"))) return "fail" else: LOG.debug("Communication with tenpay failed: responsecode=%s\n error_info=%s\n" % (httpClient.getResponseCode(),httpClient.getErrInfo())) return "fail" return "fail"