def up_prod(): timestamp = int(time.time()) account = 'xzy039' password = '******' carrier = 'cmcc' sign = to_md5(account + to_md5(password) + str(timestamp) + carrier) body = { "account": account, "timestamp": timestamp, "sign": sign, "carrier": carrier } body = json.dumps(body) url = "http://api.zhjxzy.com/flowProduct" http_client = HTTPClient() resp = http_client.fetch(url, method='POST', body=body) if resp.code == 200: print(resp.body.decode())
def query_sms_template_list(self, userid, md5_password): try: sign = to_md5(userid + md5_password) url = URL_PREFIX + 'FCMsgTemplateServlet' url = self.QUERY_SMS_TEMPLATE_LIST_FORMAT.format(url, userid, sign) http_client = HTTPClient() request = HTTPRequest(url=url, method='GET') print('QUERY SMS TEMPLATE LIST REQU: {0}'.format(url)) response = http_client.fetch(request) response_info = response.body.decode() response_info = json.loads(response_info) print('QUERY SMS TEMPLATE LIST RESP: {0}'.format(response_info)) print('>>SMS TEMPLATE LIST BEGIN<<') for i in response_info: print(i['id'], i['templateContent']) print('>>SMS TEMPLATE LIST END<<') except Exception as e: print('EXCEPTION', e)
result = 60000 elif up_result == '00': result = 0 else: result = 60000 + int(up_result) handler.up_result = result handler.up_order_id = handler.order_id handler.up_resp_time = time.localtime() return result # 查询产品和短消息模板 if __name__ == '__main__': test_flag = True if test_flag: userid = 'adminQuxun' passord = 'test5871' else: userid = 'AdminNjqx' passord = 'test7725' md5_password = to_md5(passord) print(md5_password) a = DahanFcInterface() a.query_sms_template_list(userid, md5_password) a.query_product_list(userid)
def up_faliuliang2(handler, partner): handler.up_req_time = time.localtime() username = partner["username"] mobiles = handler.mobile key = partner["key"] tradeNo = handler.order_id timestamp = int(time.time() * 1000) url = partner["back_url"] + "?" spec = None k = 'private:faliuliang2:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) spec = handler.slave.get(k) if spec is None: handler.up_result = 5003 return handler.up_result signature = to_md5(str(timestamp) + tradeNo + mobiles + spec + url + key) body = { "areaType": "c", "effectiveType": "tm", "mobiles": mobiles, "signature": signature, "spec": spec, "timestamp": int(timestamp), "tradeNo": tradeNo, "username": username, "url": url } body = json.dumps(body) url = partner["url_busi"] headers = {'Content-Type': 'application/json;charset=UTF-8'} result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, headers=headers, request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body["code"] result = RESULT_MAP.get(up_result, 9) handler.up_result = up_result except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_trafficweb(handler, partner): handler.up_req_time = time.localtime() user = partner["user"] password = partner["password"] private_key = partner.get('private_key', 'trafficweb') request_no = handler.order_id mobile = handler.mobile scope = handler.scope or '0' k1 = 'private:{private_key}:{carrier}:{area}:{price}:{scope}'.format( private_key=private_key, carrier=handler.carrier, price=handler.price, area=handler.area, scope=scope) k2 = 'private:{private_key}:{carrier}:CN:{price}:{scope}'.format( private_key=private_key, carrier=handler.carrier, price=handler.price, scope=scope) prodtype1, prodtype2 = handler.slave.mget(k1, k2) prodtype = prodtype1 or prodtype2 if prodtype is None: handler.up_result = 5003 return handler.up_result key = 'pool:trafficweb:{0}'.format(prodtype) v = handler.master.get(key) if v: i = handler.master.incrby(key, -1) if i < 0: request_log.info('TRAFFICWEB QUOTA %d', i, extra={'orderid': handler.order_id}) handler.up_result = 5003 return handler.up_result password = to_md5(password) data = mobile + prodtype + request_no + user + password sign = to_md5(data) body = { "request_no": request_no, "user": user, "prodtype": prodtype, "mobile": mobile, "sign": sign, } body = urlencode(body) url = partner["url_busi"] url = url + "?" + body result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("TRAFFICWEB REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='GET', request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode() request_log.info("TRAFFICWEB RESP {%s}", response_body.strip(), extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body.get("result_code") result = RESULT_MAP.get(up_result, 9) handler.up_result = up_result if handler.up_result == '99999': if v: i = handler.master.incr(key) request_log.info('TRAFFICWEB QUOTA %d', i, extra={'orderid': handler.order_id}) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_shangtong(handler, partner): handler.up_req_time = time.localtime() account = partner["account"] action = 'Charge' apikey = partner["apikey"] phone = handler.mobile range = 0 timeStamp = str(int(time.time())) size = None k = 'private:shangtong:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) size = handler.slave.get(k) if size is None: handler.up_result = 5003 return handler.up_result data = '{apikey}account={account}&action={action}&phone={phone}&range={range}&size={size}&timeStamp={timeStamp}{apikey}'.format( apikey=apikey, account=account, action=action, phone=phone, size=int(size), range=range, timeStamp=timeStamp) sign = to_md5(data) body = { "account": account, "action": action, "phone": phone, "size": size, "range": range, "timeStamp": timeStamp, 'sign': sign, } body = urlencode(body) url = partner['url.order'] result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body["respCode"] sp_order_id = response_body.get("orderID") handler.up_order_id = sp_order_id result = RESULT_MAP.get(up_result, 9) handler.up_result = up_result if handler.up_result == '0000': handler.master.set( "map:shangtong:{sp_order_id}".format( sp_order_id=sp_order_id), handler.order_id) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def post(self, path_args): user_id = self.current_user['partner_id'] prod = self.json_args['prod'] number = self.json_args['number'] downstream = self.application.config['downstream'][user_id] if 'master' in downstream: master_id = downstream['master'] else: master_id = user_id key = 'product:{master_id}:sinopec:{prod}'.format(master_id=master_id, prod=prod) product = self.master.hget(key, 'value') if product[0] is None: self.write_error(405) return if not re.match(r'\d{19}', number): self.write_error(405) return try: order_id = self.order_id() ORDER_FORMAT = 'product=sinopec&userid={userid}&price={price}&num=1&account_number={account_number}&spordertime={spordertime}&sporderid={sporderid}' back_url = self.application.config['connection'][ 'fuel_card_callback'] order = ORDER_FORMAT.format( userid=user_id, price=product, account_number=number, spordertime=datetime.now().strftime("%Y%m%d%H%M%S"), sporderid=order_id) sign = order + "&key=" + downstream['key'] requ_body = order + "&sign=" + to_md5( sign) + "&back_url=" + back_url url = 'http://{shard}/order.do'.format(shard=downstream['shard']) http_client = AsyncHTTPClient() request = HTTPRequest(url=url, method='POST', body=requ_body) response = yield http_client.fetch(request) if response.code == 200: resp = response.body.decode('utf8') print(resp) root = ET.fromstring(resp) result = root.find('resultno').text request_no = root.find('sporderid').text if result == '0': order_id = root.find('orderid').text msg = { 'status': 'ok', 'msg': '充值请求发送成功', 'sp_order_id': request_no, 'order_id': order_id } self.master.hmset('latest:%s' % user_id, { 'prod': prod, 'number': number }) else: msg = { 'status': 'fail', 'msg': '充值请求发送失败,错误原因:{info}({result})'.format( info=escape_fee_result(result), result=result), 'sporderid': request_no } except Exception as e: request_log.error("SINOPEC CHARGE EXCEPTION {0}".format(e)) request_log.exception("SINOPEC CHARGE EXCEPTION") msg = {'status': 'fail'} self.write(json.dumps(msg))
def up_wingames(handler, partner): handler.up_req_time = time.localtime() t = str(datetime.datetime.now().microsecond) timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime()) + t appid = partner["appid"] appsecert = partner["appsecert"] user = handler.mobile extorder = handler.order_id seqno = handler.order_id ordertype = "1" #固定 packageid = None k = 'private:wingames:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) packageid = handler.slave.get(k) if packageid is None: handler.up_result = 5003 return handler.up_result secertkey = to_md5(timestamp + str(seqno) + appid + appsecert) sign = to_md5(appsecert + user + packageid + ordertype + extorder) body = { "HEADER": { "VERSION": "V1.1", "TIMESTAMP": timestamp, "SEQNO": seqno, "APPID": appid, "SECERTKEY": secertkey }, "MSGBODY": { "CONTENT": { "SIGN": sign, "ORDERTYPE": ordertype, "USER": user, "PACKAGEID": packageid, "EXTORDER": extorder, "NOTE": '', } } } body = json.dumps(body) url = partner["url_busi"] result = 9999 http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) resp_code = response_body["MSGBODY"]["RESP"]["RCODE"] result = RESULT_MAP.get(resp_code, 9) handler.up_result = str(result) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_llfengbao(handler, partner): handler.up_req_time = time.localtime() timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime()) trade_user = partner["trade_user"] user_key = partner["user_key"] trade_mobile = handler.mobile trade_order = handler.order_id trade_product = None k = 'private:llreader:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) trade_product = handler.slave.get(k) if trade_product is None: handler.up_result = 5003 return handler.up_result data = trade_user + '-' + trade_order + '-' + trade_product + '-' + trade_mobile + '-' + user_key trade_passcode = to_md5(data) body = { 'trade_user': trade_user, 'trade_order': trade_order, 'trade_product': trade_product, 'trade_mobile': trade_mobile, 'trade_passcode': trade_passcode, } body = urlencode(body) url = partner["url_busi"] result = 9999 try: http_client = AsyncHTTPClient() request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, request_timeout=120) except HTTPError as http_error: request_log.exception('CALL UPSTREAM FAIL', extra={'orderid': handler.order_id}) result = 60000 + http_error.code return result except Exception as e: request_log.exception('CALL UPSTREAM FAIL', extra={'orderid': handler.order_id}) response = None handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) handler.up_order_id = response_body["trade_order"] resp_code = response_body["retinfo"] result = RESULT_MAP.get(resp_code, 0) handler.up_result = str(result) except Exception as e: result = 9999 handler.up_result = result request_log.exception('PARSE UPSTREAM', extra={'orderid': handler.order_id}) return result
def up_zhjxzy(handler, partner): handler.up_req_time = time.localtime() timestamp = int(time.time()) account = partner["account"] password = partner["password"] mobile = handler.mobile productId = None k = 'private:zhjxzy:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) productId = handler.slave.get(k) if productId is None: handler.up_result = 5003 return handler.up_result sign = to_md5(account + to_md5(password) + str(timestamp) + mobile + productId) body = { "account": account, "timestamp": timestamp, "mobile": mobile, "productId": productId, "sign": sign } body = json.dumps(body) url = partner["url_busi"] h = {'Content-Type': 'application/json;charset=UTF-8'} result = 9999 http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', headers=h, body=body, request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) status = response_body.get("status") result = RESULT_MAP.get(status, 9) if result == 0: handler.up_order_id = response_body.get("data") if handler.up_order_id: # expire in 10 days handler.master.setex('map:xzy:%s' % handler.up_order_id, 864000, handler.order_id) handler.up_result = str(result) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_telecom_gz(handler, partner): handler.up_req_time = time.localtime() tsp = str(int(time.time() * 1000)) k = 'private:telecom_gz:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) flowType = handler.slave.get(k) if flowType is None: handler.up_result = 5003 return handler.up_result account = partner["account"] password = partner["password"] ran_num = random.randint(10001, 100001) hashCode = str(tsp) + str(ran_num) password = to_md5(to_md5(password) + hashCode) userPhone = partner["userPhone"] productCode = partner["productCode"] smsId = partner["smsId"] activityName = partner["activityName"] body = r'''<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:zz="http://zz.protocol.intf.tisson.cn"> <soapenv:Header/> <soapenv:Body> <zz:odrerFlowRequest> <accountAuth> <account>{account}</account> <password>{password}</password> <hashCode>{hashCode}</hashCode> </accountAuth> <userPhone>{userPhone}</userPhone> <productCode>{productCode}</productCode> <flowType>{flowType}</flowType> <smsId>{smsId}</smsId> <activityName>{activityName}</activityName> <param1></param1> <param2></param2> </zz:odrerFlowRequest> </soapenv:Body> </soapenv:Envelope>'''.format( account=account, password=password, hashCode=hashCode, userPhone=userPhone, productCode=productCode, flowType=flowType, smsId=smsId, activityName=activityName, ) url = partner['url.order'] h = {'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': '""'} result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("PEOPLE REQ %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, headers=h, request_timeout=120) except HTTPError as http_error: request_log.error('PEOPLE CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('PEOPLE CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode() request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: sessionId = None root = ET.fromstring(response_body) if 'sessionId' in response_body: sessionId = root.find('.//sessionId').text if 'resultCode' in response_body: up_result = root.find('.//resultCode').text print(type(up_result)) result = RESULT_MAP.get(up_result, 9) handler.up_result = str(up_result) handler.up_order_id = sessionId if handler.up_result == '0': handler.master.set( "map:telecom_gz:{sessionId}".format(sessionId=sessionId), handler.order_id) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_ibumobile(handler, partner): handler.up_req_time = time.localtime() orderNo = handler.order_id userName = partner["userName"] userPwd = to_md5(partner["userPwd"]) bcallbackUrl = partner["bcallbackUrl"] mobile = handler.mobile proKey = None k = 'private:ibumobile:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) proKey = handler.slave.get(k) if proKey is None: handler.up_result = 5003 return handler.up_result data = "userName="******"&userPwd=" + userPwd + "&mobile=" + mobile + "&proKey=" + proKey + "&orderNo=" + orderNo + "&bcallbackUrl=" + bcallbackUrl sign = to_md5(data) body = { "orderNo": orderNo, "mobile": mobile, "userName": userName, "userPwd": userPwd, "proKey": proKey, "bcallbackUrl": bcallbackUrl, "sign": sign } body = urlencode(body) url = partner["url_busi"] url = url + '?' + body result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='GET', request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body["code"] result = RESULT_MAP.get(up_result, 9) handler.up_result = up_result except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_ibumobile2(handler, partner): handler.up_req_time = time.localtime() orderNo = handler.order_id mobile = handler.mobile userName = partner['userName'] userPwd = to_md5(partner['userPwd']) bcallbackUrl = partner['bcallbackUrl'] f = partner['f'] flowType = partner['flowType'] interfaceSign = partner['interfaceSign'] k = 'private:zhongshenyuan:{carrier}:{price}'.format( carrier=handler.carrier, price=handler.price) proKey = handler.slave.get(k) if proKey is None: handler.up_result = 5003 return handler.up_result data = 'userName='******'&userPwd=' + userPwd + interfaceSign + '&mobile=' + mobile + '&proKey=' + proKey + '&orderNo=' + orderNo + '&bcallbackUrl=' + bcallbackUrl # print('DATA:', data) sign = to_md5(data) # print("SIGN:", sign) body = { "orderNo": orderNo, "mobile": mobile, "userName": userName, "userPwd": userPwd, "proKey": proKey, "bcallbackUrl": bcallbackUrl, "sign": sign, "f": f, "flowType": flowType, } body = urlencode(body) url = partner['url.order'] url = url + '?' + body result = 9999 http_client = AsyncHTTPClient() try: request_log.info("IBUMOBILE REQ %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body='', request_timeout=120) except HTTPError as http_error: request_log.error('IBUMOBILE CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('IBUMOBILE CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body.get('code') result = RESULT_MAP.get(up_result, 9) handler.up_result = up_result except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_raiyi(handler, partner): handler.up_req_time = time.localtime() timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime()) authAppkey = partner["authAppkey"] appsecret = partner["appsecret"] notifyUrl = partner["notifyUrl"] mobile = handler.mobile mobile = to_para(mobile).decode() partnerOrderNo = handler.order_id productId = None k = 'private:raiyi:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) productId = handler.slave.get(k) if productId is None: handler.up_result = 5003 return handler.up_result data = authAppkey + 'authTimespan' + '=' + timestamp + 'mobile' + '=' + mobile + 'notifyUrl' + '=' + notifyUrl + 'partnerOrderNo' + '=' + partnerOrderNo + 'productId' + '=' + productId + appsecret authSign = to_md5(data) mobile1 = quote(mobile) body = CODE.format(authTimespan=timestamp, authSign=authSign, authAppkey=authAppkey, notifyUrl=notifyUrl, productId=productId, partnerOrderNo=partnerOrderNo, mobile=mobile1) url = partner["url_busi"] url = url + '?' + body result = 9999 try: http_client = AsyncHTTPClient() request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='GET', request_timeout=120) except HTTPError as http_error: request_log.exception('CALL UPSTREAM FAIL', extra={'orderid': handler.order_id}) result = 60000 + http_error.code return result except Exception as e: request_log.exception('CALL UPSTREAM FAIL', extra={'orderid': handler.order_id}) response = None handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) if 'data' in response_body: if response_body['data']: handler.up_order_id = response_body["data"]["orderNo"] resp_code = response_body["code"] result = RESULT_MAP.get(resp_code, 9) handler.up_result = str(result) except Exception as e: result = 9999 handler.up_result = result request_log.exception('PARSE UPSTREAM', extra={'orderid': handler.order_id}) return result
def up_xiamen_zyt(handler, partner): handler.up_req_time = time.localtime() timeStamp = time.strftime("%Y%m%d%H%M%S", time.localtime()) requesttime = timeStamp msisdn = handler.mobile orderno = handler.order_id channelNo = partner['channelNo'] key = partner["key"] productid = None k = 'private:telecom800:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) productid = handler.slave.get(k) if productid is None: handler.up_result = 5003 return handler.up_result data = 'channelNo={0}&msisdn={1}&orderno={2}&productid={4}&requesttime={3}&key={5}'.format( channelNo, msisdn, orderno, requesttime, productid, key) data_md5 = to_md5(data) bytesString = data_md5.encode(encoding="utf-8") sign = base64.b64encode(bytesString).decode('gb2312') body = { "channelNo": channelNo, "msisdn": msisdn, "productid": productid, "requesttime": requesttime, "orderno": orderno, "sign": sign } body = urlencode(body) url = partner["url_busi"] result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body["resultcode"] handler.up_result = str(up_result) result = RESULT_MAP.get(up_result, 9) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_trafficweb(handler, partner): handler.up_req_time = time.localtime() user = partner["user"] password = partner["password"] request_no = handler.order_id mobile = handler.mobile prodtype = None k = 'private:trafficweb:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) prodtype = handler.slave.get(k) if prodtype is None: handler.up_result = 5003 return handler.up_result password = to_md5(password) data = mobile + prodtype + request_no + user + password sign = to_md5(data) body = { "request_no": request_no, "user": user, "prodtype": prodtype, "mobile": mobile, "sign": sign, } body = urlencode(body) url = partner["url_busi"] url = url + "?" + body result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("TRAFFICWEB REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='GET', request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode() request_log.info("TRAFFICWEB RESP {%s}", response_body.strip(), extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body.get("result_code") result = RESULT_MAP.get(up_result, 9) handler.up_result = up_result except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_yflow(handler, partner): handler.up_req_time = time.localtime() confId = partner["confId"] callback = partner["callback"] apikey = partner["apikey"] mobile = handler.mobile seqNo = handler.order_id t = int(time.time()) data = apikey + str(confId) + mobile + str(t) sign = to_md5(data) flow = None k = 'private:yflow:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) flow = handler.slave.get(k) flow = int(flow) if flow is None: handler.up_result = 5003 return handler.up_result body = { "confId": confId, "mobile": mobile, "flow": flow, "callback": callback, "t": t, "sign": sign, "seqNo": seqNo, } body = urlencode(body) url = partner["url_busi"] url = url + '?' + body result = 9999 http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='GET', request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) retCode = response_body["retCode"] result = RESULT_MAP.get(retCode, 9) handler.up_result = str(result) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_cmcc_snbj(handler, partner): handler.up_req_time = time.localtime() master = handler.master serialNumber = handler.order_id[:-4].replace("Q", "W") master.set('map:cmccbj:%s' % serialNumber, handler.order_id) timestamp = time.strftime("%Y%m%d%H%M%S", time.localtime()) account = partner['account'] bisCode = partner['bisCode'] serviceCode = partner['serviceCode'] key = partner['key'] sign = to_md5(serialNumber + account + bisCode + timestamp + key) k = 'private:cmcc_snbj:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) discntCode = handler.slave.get(k) if discntCode is None: handler.up_result = 5003 return handler.up_result body = CODE.format( serialNumber=serialNumber, account=account, bisCode=bisCode, timeStamp=timestamp, sign=sign, mobile=handler.mobile, serviceCode=serviceCode, discntCode=discntCode, month='1', capFlow='', effectiveWay='', ) url = partner["url_busi"] result = 9999 http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) resp_code = response_body["resultCode"] result = RESULT_MAP.get(resp_code, 0) handler.up_result = str(result) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) if result == 1: yield handler.callback('1') return result
def up_telecom_gz(handler, partner): handler.up_req_time = time.localtime() tsp = str(int(time.time() * 1000)) scope = handler.scope or 0 k = 'private:telecom_gz:{scope}:{price}'.format(price=handler.price, scope=scope) productCode = handler.slave.get(k) if productCode is None: handler.up_result = 5003 return handler.up_result account = partner["account"] password = partner["password"] ran_num = random.randint(10000, 99999) hashCode = str(tsp) + str(ran_num) password = to_md5(to_md5(password) + hashCode) userPhone = handler.mobile activityName = partner["activityName"] body = BODY.format(account=account, password=password, hashCode=hashCode, userPhone=userPhone, productCode=productCode, activityName=activityName) url = partner['url.order'] h = {'Content-Type': 'text/xml;charset=UTF-8', 'SOAPAction': '""'} result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("TELECOM_GZ REQ %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, headers=h, request_timeout=120) except HTTPError as http_error: request_log.error('TELECOM_GZ CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('TELECOM_GZ CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode() request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: sessionId = None root = ET.fromstring(response_body) if 'sessionId' in response_body: sessionId = root.find('.//sessionId').text if 'resultCode' in response_body: up_result = root.find('.//resultCode').text print(type(up_result)) result = RESULT_MAP.get(up_result, 9) handler.up_result = str(up_result) handler.up_order_id = sessionId if handler.up_result == '0': handler.master.set( "map:telecom_gz:{sessionId}".format(sessionId=sessionId), handler.order_id) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def send_data_order(self, url, userid, md5_password, order_id, mobile, cmcc_product_id=None, cucc_product_id=None, ct_product_id=None): result = None try: tsp_now = str(int(time.time() * 1000)) sign = to_md5(userid + md5_password + tsp_now + mobile) request_dict = { 'timestamp': tsp_now, 'account': userid, 'mobiles': mobile, 'sign': sign, 'packageSize': '', 'mobileProductId': cmcc_product_id, 'unicomProductId': cucc_product_id, 'telecomProductId': ct_product_id, # 'msgTemplateId': sms_template_id, 'clientOrderId': order_id, } http_request = HTTPRequest(url=url, method='POST', body=json.dumps(request_dict)) # print('SEND DATA ORDER REQU: {0}'.format(http_request.body)) request_log.info('CALL_REQ %s', http_request.body, extra={'orderid': order_id}) http_client = AsyncHTTPClient() response = yield http_client.fetch(http_request) response_info = response.body.decode() request_log.info('CALL_RESP %s', response_info, extra={'orderid': order_id}) # print('SEND DATA ORDER RESP: {0}'.format(response_info)) response_info = json.loads(response_info) result = response_info['resultCode'] except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': order_id}) result = 60000 + http_error.code except Exception as e: request_log.error('{0} UPORDER EXCEPTION'.format(order_id)) request_log.exception('{0} UPORDER EXCEPTION'.format(order_id)) result = None return result
def up_jiebei(handler, partner): handler.up_req_time = time.localtime() userid = partner["userid"] orderid = handler.order_id account = handler.mobile amount = handler.price shoptype = partner["shoptype"] shopid = partner["shopid"] area = partner["area"] ordertime = time.strftime("%Y-%m-%d %H:%M:%S", handler.up_req_time) backurl = partner["backurl"] key = partner["key"] data = 'userid=' + str(userid) + '&orderid=' + orderid + '&account=' + account + '&amount=' + str( amount) + '&shoptype=' + str( shoptype) + '&shopid=' + shopid + '&area=' + area + '&ordertime=' + ordertime + '&backurl=' + backurl + '&key=' + key vstr = to_md5(data) # package body = { 'userid': userid, 'orderid': orderid, 'account': account, 'amount': amount, 'shoptype': shoptype, 'shopid': shopid, 'area': area, 'ordertime': ordertime, 'backurl': backurl, 'vstr': vstr } body = urlencode(body) url = partner["url.order"] request_log.info('JIEBEI CALL_REQ %s', body, extra={'orderid': handler.order_id}) # call & wait result = 9999 http_client = AsyncHTTPClient() try: response = yield http_client.fetch(url, method='POST', body=body, connect_timeout=30, request_timeout=60) except HTTPError as http_error: request_log.error('JIEBEI CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('JIEBEI CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() # <-------------- if response and response.code == 200: body = response.body.decode('gb2312') request_log.info('JIEBEI CALL_RESP %s', body, extra={'orderid': handler.order_id}) try: root = ET.fromstring(body) handler.up_result = root.find("status").text result = RESULT_MAP.get(handler.up_result, 0) except Exception as e: result = 9999 request_log.error('JIEBEI PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def send_fuel_card_order(self,task_id): #判断当前状态 task_info = self.get_task_info_by_task_id(task_id) if task_info['task_status'] != FuelCardTask.Status.RUNNING: return price = self.get_price(task_id) if not price: #没有订单了 self.stop_task(task_id, stop_info={'msg':'本轮订单已处理完毕'}) TaskStatusHistory.push(self.master, task_id,TaskStatusHistory.STATUS_TYPE.OTHER, '已没有可以处理的订单,任务暂停!!!' ) return account = self.master.hget('map:task_info:{0}'.format(task_id), 'account') downstream = self.application.config['downstream'][self.user_id] order_id = None try: order_id = self.order_id() back_url = self.application.config['connection']['fuel_card_callback'] order = self.ORDER_FORMAT.format(userid=self.user_id, price=price, account_number= account, spordertime=datetime.now().strftime("%Y%m%d%H%M%S"), sporderid=order_id ) sign = order + "&key=" + downstream['key'] requ_body = order + "&sign="+to_md5(sign) + "&back_url=" + back_url url = 'http://{shard}/order.do'.format(shard=downstream['shard']) log.info("{0} {1} RECHARGE REQU: {2} {3} ".format(task_id, order_id, url, order) ) http_client = AsyncHTTPClient() request = HTTPRequest(url=url, method='POST', body=requ_body) response = yield http_client.fetch(request) resp_data = response.body.decode() log.info("{0} {1} RECHARGE RESP: {2} ".format(task_id, order_id,resp_data) ) root = ET.fromstring(resp_data) result = root.find('resultno').text related_id = root.find('orderid').text if result == '0': self.master.hmset('map:task_order:{0}'.format(related_id), {'task_id': task_id, 'price': price, 'user_id': self.user_id, 'order_id': order_id}) self.master.expire('map:task_order:{0}'.format(related_id), 60*60*24*30) #一个月之后自动删除 self.master.set('task_up_order:{0}'.format(order_id), related_id) self.master.expire('task_up_order:{0}'.format(order_id), 60*60*24*30) #一个月之后自动删除 TaskStatusHistory.push(self.master, task_id,TaskStatusHistory.STATUS_TYPE.OTHER, '{0} {1}元订单创建成功'.format(related_id,price) ) log.info("{0} {1} RECHARGE REQU SUCCESS".format(task_id, order_id) ) IOLoop.instance().call_later(5, self.send_fuel_card_order, task_id) #每隔5秒发送一个订单 else: self.restore_price(task_id, price) self.stop_task(task_id, order_id=related_id or order_id, stop_info={'msg':'内部异常,订单直接失败'}) TaskStatusHistory.push(self.master, task_id,TaskStatusHistory.STATUS_TYPE.OTHER, '{0} {1}元订单创建失败,任务停止!!!'.format(related_id,price) ) log.info("{0} {1} RECHARGE REQU FAIL".format(task_id, order_id) ) except: log.exception("{0} {1} RECHARGE EXCEPTION".format(task_id, order_id) ) if order_id: self.save_order(task_id, order_id)
def up_cmcc_sx(handler, partner): handler.up_req_time = time.localtime() appKey = partner["appKey"] timeStamp = (datetime.now() + timedelta(seconds=0)).strftime("%Y%m%d%H%M%S") login_no = partner["login_no"] acc_nbr = partner["acc_nbr"] group_id = partner["group_id"] member_role_id = partner["member_role_id"] gprs_accept = group_id + timeStamp + "000001" prod_prcid = None k = 'private:cmcc_sx:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) prod_prcid = handler.slave.get(k) if prod_prcid is None: handler.up_result = 5003 return handler.up_result data = ('acc_nbr={acc_nbr}&' 'appKey={appKey}&' 'gprs_accept={gprs_accept}&' 'group_id={group_id}&' 'login_no={login_no}&' 'member_role_id={member_role_id}&' 'prod_prcid={prod_prcid}&' 'timeStamp={timeStamp}').format(appKey=appKey, timeStamp=timeStamp, login_no=login_no, acc_nbr=acc_nbr, prod_prcid=prod_prcid, group_id=group_id, member_role_id=member_role_id, gprs_accept=gprs_accept) sign = to_md5(quote(data)) rsa_sign = to_para(sign) body = '&'.join([ "acc_nbr=%s" % quote(acc_nbr), "appKey=%s" % quote(appKey), "gprs_accept=%s" % quote(gprs_accept), "group_id=%s" % quote(group_id), "login_no=%s" % quote(login_no), "member_role_id=%s" % quote(member_role_id), "prod_prcid=%s" % quote(prod_prcid), "timeStamp=%s" % quote(timeStamp), "sign=%s" % quote(rsa_sign), ]) url = partner['url_auth'] url = url + '?' + body h = {'Content-Type': 'application/json; charset=utf-8'} result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='GET', headers=h, request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body.get("resCode") sp_order_id = response_body.get("orderId") handler.up_order_id = sp_order_id result = RESULT_MAP.get(up_result, 9) if result == 1: handler.back_result = result yield handler.callback('1') elif result == 9: pass handler.up_result = up_result if handler.up_result == '0000000': handler.master.set( "map:cmcc_sx:{sp_order_id}".format( sp_order_id=sp_order_id), handler.order_id) except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result
def up_zhixin(handler, partner): handler.up_req_time = time.localtime() time_now = time.localtime() secret_key = partner["secret_key"] mrch_no = partner["mrch_no"] site_num = "" request_time = time.strftime("%Y%m%d%H%M%S", time_now) client_order_no = handler.order_id product_type = 4 phone_no = handler.mobile cp = "" city_code = "" recharge_type = 0 recharge_desc = "" notify_url = partner["notify_url"] recharge_amount = None k = 'private:zhixin:{carrier}:{price}'.format(carrier=handler.carrier, price=handler.price) recharge_amount = handler.slave.get(k) if recharge_amount is None: handler.up_result = 5003 return handler.up_result sign = to_md5("city_code" + city_code + "client_order_no" + client_order_no + "cp" + cp + "mrch_no" + mrch_no + "notify_url" + notify_url + "phone_no" + phone_no + "product_type" + str(product_type) + "recharge_amount" + str(recharge_amount) + "recharge_desc" + recharge_desc + "recharge_type" + str(recharge_type) + "request_time" + request_time + "site_num" + site_num + secret_key) body = { "mrch_no": mrch_no, "site_num": site_num, "request_time": request_time, "client_order_no": client_order_no, "product_type": product_type, "phone_no": phone_no, "cp": cp, "city_code": city_code, "recharge_amount": recharge_amount, "recharge_type": recharge_type, "recharge_desc": recharge_desc, "notify_url": notify_url, "sign": sign, } body = json.dumps(body) url = partner["url_busi"] h = {'Content-Type': 'application/json; charset=utf-8'} result = 9999 up_result = None http_client = AsyncHTTPClient() try: request_log.info("REQU %s", body, extra={'orderid': handler.order_id}) response = yield http_client.fetch(url, method='POST', body=body, headers=h, request_timeout=120) except HTTPError as http_error: request_log.error('CALL UPSTREAM FAIL %s', http_error, extra={'orderid': handler.order_id}) result = 60000 + http_error.code response = None except Exception as e: request_log.error('CALL UPSTREAM FAIL %s', e, extra={'orderid': handler.order_id}) response = None finally: http_client.close() handler.up_resp_time = time.localtime() if response and response.code == 200: response_body = response.body.decode('utf8') request_log.info("RESP %s", response_body, extra={'orderid': handler.order_id}) try: response_body = json.loads(response_body) up_result = response_body["code"] result = RESULT_MAP.get(up_result, 9) handler.up_result = up_result except Exception as e: result = 9999 handler.up_result = result request_log.error('PARSE UPSTREAM %s', e, extra={'orderid': handler.order_id}) return result