def parse_payment_resp(self, resp, sign_key): if resp.code != 200: self.send_response(err_code=1001, err_msg='wechat %d' % resp.code) return None resp_data = dtools.xml2dict(resp.body.decode('utf8')) if resp_data['return_code'].lower() != 'success': self.send_response(err_code=1001, err_msg=resp_data.get('return_msg')) return None if not security.check_sign(resp_data, sign_key, 'md5'): self.send_response(err_code=1002) return None if resp_data['result_code'].lower() != 'success': self.send_response(err_code=errinfo.alias_map.get(resp_data.get('err_code'), 9001), err_msg=resp_data.get('err_code_des')) return None return resp_data
def parse_payment_resp(self, resp, sign_key): if resp.code != 200: self.send_response(err_code=1001, err_msg='wechat %d' % resp.code) return None resp_data = dtools.xml2dict(resp.body.decode('utf8')) if resp_data['return_code'].lower() != 'success': self.send_response(err_code=1001, err_msg=resp_data.get('return_msg')) return None if not security.check_sign(resp_data, sign_key, 'md5'): self.send_response(err_code=1002) return None if resp_data['result_code'].lower() != 'success': self.send_response(err_code=errinfo.alias_map.get( resp_data.get('err_code'), 9001), err_msg=resp_data.get('err_code_des')) return None return resp_data
def post(self): post_args = dtools.xml2dict(self.request.body.decode('utf8')) logging.info(post_args) appinfo = self.storage.get_app_info(openid=post_args['ToUserName']) appid = appinfo['appid'] crypter = None if appinfo['is_encrypted']: crypter = Prpcrypt(appinfo['encoding_key']) plain_xml = crypter.decrypt(post_args['Encrypt'], appid) if plain_xml: post_args = dtools.xml2dict(plain_xml) else: # TODO: error pass req_data = dtools.transfer( post_args, renames=[ ('FromUserName', 'openid'), ('MsgType', 'msg_type'), ('MsgId', 'msg_id'), ('CreateTime', 'msg_time'), ('Content', 'content'), ('Event', 'event_type'), ('PicUrl', 'pic_url'), ('Location_X', 'latitude'), ('Location_Y', 'longitude'), ('Label', 'label'), ('MediaId', 'media_id')], allow_empty=False ) logging.info('message from tencent: %s', req_data) req_data['appid'] = appid site_info = self.storage.get_site_info(appinfo['siteid']) security.add_sign(req_data, site_info['sitekey']) try: resp = yield httputils.post_dict( url=site_info['msg_notify_url'], data=req_data) except tornado.httpclient.HTTPError: self.send_response(err_code=9002) raise tornado.gen.Return() if resp.code != 200: self.send_response(err_code=9002) raise tornado.gen.Return() try: resp_data = json.loads(resp.body.decode('utf8')) if resp_data.get('err_code') == 0: wx_resp = build_response(from_id=post_args['ToUserName'], to_id=post_args['FromUserName'], data=resp_data.get('data')) if appinfo['is_encrypted']: wx_resp = encrypt_data(wx_resp, crypter, appid) self.send_response(wx_resp) else: self.send_response(err_code=9003) except ValueError: self.send_response(err_code=9101) # Add basic user info openid = req_data['openid'] user_info = self.storage.get_user_info(appid=appid, openid=openid) if not user_info: if appinfo['is_verified']: yield wxclient.update_user_info(appid, openid) else: self.storage.add_user_info({ 'appid': appid, 'openid': openid, 'fakeid': openid }) # Use mock_browser to get user info if not appinfo['is_protected'] and (not user_info or not user_info.get('nickname')) and not req_data.get( 'event_type'): user_resp = yield wxclient.mock_browser.find_user( appid=appid, timestamp=int(req_data['msg_time']), mtype=req_data['msg_type'], content=req_data.get('content', '') ) if user_resp['err_code'] == 0: more_info = { 'appid': appid, 'openid': openid, 'fakeid': user_resp['data']['fakeid'], 'nickname': user_resp['data']['nick_name'] } if not appinfo['is_verified']: contact_resp = yield wxclient.mock_browser.get_contact_info( appid=appid, fakeid=user_resp['data']['fakeid'], msg_id=user_resp['data']['id'] ) contact_info = contact_resp['data']['contact_info'] more_info.update({ 'sex': contact_info['gender'], 'city': contact_info['city'], 'province': contact_info['province'], 'country': contact_info['country'] }) logging.info('userinfo updated: %s', more_info) self.storage.add_user_info(more_info)
def prepare(self): self.post_args = dtools.xml2dict(self.request.body) if self.sign_check: key = self.storage.get_app_info(self.post_args.get('appid'), select_key='apikey') self.check_signature(self.post_args, sign_key=key, method='md5')