def post(self, vendor_id, voucher_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got voucher_id %r in uri", voucher_id) ops = self.get_ops_info() _amount = self.get_argument("amount", "") logging.info("got _amount %r", _amount) _price = self.get_argument("price", "") logging.info("got _price %r", _price) _expired_time = self.get_argument("expired_time", "") logging.info("got _expired_time %r", _expired_time) # 转换为分(整数) _amount = float(_amount) * 100 _price = float(_price) * 100 # 转换为秒 _expired_time = date_timestamp(_expired_time) _timestamp = time.time() json = { "_id": voucher_id, "last_update_time": _timestamp, "amount": _amount, "price": _price, "expired_time": _expired_time } voucher_pay_dao.voucher_pay_dao().update(json) self.redirect('/vendors/' + vendor_id + '/vouchers?status=0')
def post(self, vendor_id): logging.info("got vendor_id %r in uri", vendor_id) ops = self.get_ops_info() _amount = self.get_argument("amount", "") logging.info("got _amount %r", _amount) _price = self.get_argument("price", "") logging.info("got price %r", _amount) _expired_time = self.get_argument("expired_time", "") logging.info("got _expired_time %r", _expired_time) # 转换为分(整数) _amount = float(_amount) * 100 _price = float(_price) * 100 # 转换为秒 _expired_time = date_timestamp(_expired_time) voucher_id = str(uuid.uuid1()).replace('-', '') wx_app_info = vendor_wx_dao.vendor_wx_dao().query(vendor_id) wx_notify_domain = wx_app_info['wx_notify_domain'] voucher_url = wx_notify_domain + "/bf/wx/vendors/" + vendor_id + "/vouchers/" + voucher_id data = {"url": voucher_url} _json = json_encode(data) logging.info("got ——json %r", _json) http_client = HTTPClient() response = http_client.fetch(QRCODE_CREATE_URL, method="POST", body=_json) logging.info("got response %r", response.body) qrcode_url = response.body logging.info("got qrcode_url %r", qrcode_url) _timestamp = time.time() json = { "_id": voucher_id, "vendor_id": vendor_id, "qrcode_url": qrcode_url, "create_time": _timestamp, "last_update_time": _timestamp, "price": _price, "amount": _amount, "expired_time": _expired_time, "status": 0 } voucher_pay_dao.voucher_pay_dao().create(json) self.redirect('/vendors/' + vendor_id + '/vouchers?status=0')
def get(self, vendor_id): logging.info("got vendor_id %r in uri", vendor_id) ops = self.get_ops_info() _status = self.get_argument("status", "") logging.info("got _status %r", _status) _status = int(_status) _before = time.time() #TODO 这里若有偿代金券过多会 显示过多 if (_status == 0): pay_vouchers = voucher_pay_dao.voucher_pay_dao( ).query_pagination_by_status(vendor_id, _status, _before, PAGE_SIZE_LIMIT) free_vouchers = voucher_dao.voucher_dao( ).query_pagination_by_status(vendor_id, _status, _before, PAGE_SIZE_LIMIT) _vouchers = pay_vouchers + free_vouchers else: _vouchers = voucher_dao.voucher_dao().query_pagination_by_status( vendor_id, _status, _before, PAGE_SIZE_LIMIT) for _data in _vouchers: # 转换成元 _data['amount'] = float(_data['amount']) / 100 if _data['price'] != 0: _data['price'] = float(_data['price']) / 100 _data['expired_time'] = timestamp_date(_data['expired_time']) _data['create_time'] = timestamp_datetime(_data['create_time']) if _data['status'] != 0: _customer = vendor_member_dao.vendor_member_dao( ).query_not_safe(vendor_id, _data['account_id']) try: _customer['account_nickname'] except: _customer['account_nickname'] = '' try: _customer['account_avatar'] except: _customer['account_avatar'] = '' _data['account_nickname'] = _customer['account_nickname'] _data['account_avatar'] = _customer['account_avatar'] counter = self.get_counter(vendor_id) self.render('vendor/vouchers.html', vendor_id=vendor_id, ops=ops, counter=counter, status=_status, vouchers=_vouchers)
def get(self, vendor_id, voucher_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got voucher_id %r in uri", voucher_id) _account_id = self.get_secure_cookie("account_id") _order_id = self.get_argument("order_id", "") _voucher = voucher_pay_dao.voucher_pay_dao().query_not_safe(voucher_id) _timestamp = time.time() # 更新用户代金券 _customer_profile = vendor_member_dao.vendor_member_dao( ).query_not_safe(vendor_id, _account_id) try: _customer_profile['vouchers'] except: _customer_profile['vouchers'] = 0 _vouchers_num = _customer_profile['vouchers'] + _voucher['amount'] _timestamp = time.time() _json = { 'vendor_id': vendor_id, 'account_id': _account_id, 'last_update_time': _timestamp, 'vouchers': _vouchers_num } vendor_member_dao.vendor_member_dao().update(_json) # 每分配一个有偿代金券则生成一个普通代金券记录,方便个人中心查询 _amount = _voucher['amount'] _price = _voucher['price'] _create_time = _voucher['create_time'] _expired_time = _voucher['expired_time'] _qrcode_url = _voucher['qrcode_url'] json = { "_id": _order_id, "vendor_id": vendor_id, "qrcode_url": _qrcode_url, "create_time": _create_time, "last_update_time": _timestamp, "amount": _amount, "expired_time": _expired_time, "price": _price, 'status': 1, "account_id": _account_id } # status=1, 已分配,未使用 voucher_dao.voucher_dao().create(json) self.render('wx/voucher-pay-success.html', vendor_id=vendor_id, voucher=_voucher)
def get(self, vendor_id, voucher_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got voucher_id %r in uri", voucher_id) ops = self.get_ops_info() _voucher = voucher_pay_dao.voucher_pay_dao().query_not_safe(voucher_id) # 转换成元 _voucher['amount'] = float(_voucher['amount']) / 100 _voucher['price'] = float(_voucher['price']) / 100 _voucher['expired_time'] = timestamp_date(_voucher['expired_time']) counter = self.get_counter(vendor_id) self.render('vendor/vouchers-pay-edit.html', vendor_id=vendor_id, ops=ops, counter=counter, voucher=_voucher)
def get(self, vendor_id, voucher_id): logging.info("got vendor_id %r in uri", vendor_id) voucher = voucher_pay_dao.voucher_pay_dao().query_not_safe(voucher_id) voucher['amount'] = float(voucher['amount']) / 100 voucher['price'] = float(voucher['price']) / 100 vendor_wx = vendor_wx_dao.vendor_wx_dao().query(vendor_id) wx_app_id = vendor_wx['wx_app_id'] wx_app_secret = vendor_wx['wx_app_secret'] wx_notify_domain = wx_app_info['wx_notify_domain'] logging.info("------------------------------------uri: " + self.request.uri) _access_token = getAccessTokenByClientCredential( wx_app_id, wx_app_secret) _jsapi_ticket = getJsapiTicket(_access_token) _sign = Sign(_jsapi_ticket, wx_notify_domain + self.request.uri).sign() logging.info("------------------------------------nonceStr: " + _sign['nonceStr']) logging.info("------------------------------------jsapi_ticket: " + _sign['jsapi_ticket']) logging.info("------------------------------------timestamp: " + str(_sign['timestamp'])) logging.info("------------------------------------url: " + _sign['url']) logging.info("------------------------------------signature: " + _sign['signature']) _account_id = self.get_secure_cookie("account_id") self.render('wx/voucher-pay-info.html', vendor_id=vendor_id, voucher=voucher, wx_app_id=wx_app_id, wx_notify_domain=wx_notify_domain, sign=_sign, account_id=_account_id, vendor_wx=vendor_wx)
def post(self): vendor_id = self.get_argument("vendor_id", "") logging.info("got vendor_id %r", vendor_id) voucher_id = self.get_argument("voucher_id", "") account_id = self.get_secure_cookie("account_id") _timestamp = time.time() # 一分钟内不能创建第二个订单, # 防止用户点击回退按钮,产生第二个订单 _old_orders = voucher_order_dao.voucher_order_dao().query_by_account( voucher_id, account_id) # if len(_old_orders) > 0: # for _old_order in _old_orders: # if (_timestamp - _old_order['create_time']) < 60: # return # # 订单申报数目 # _applicant_num = self.get_argument("applicant_num", 1) # 转换成元 _voucher = voucher_pay_dao.voucher_pay_dao().query_not_safe(voucher_id) _amount = _voucher['amount'] _price = _voucher['price'] _voucher_id = _voucher['_id'] _create_time = _voucher['create_time'] _expired_time = _voucher['expired_time'] _qrcode_url = _voucher['qrcode_url'] _customer = vendor_member_dao.vendor_member_dao().query_not_safe( vendor_id, account_id) try: _customer['account_nickname'] except: _customer['account_nickname'] = '' try: _customer['account_avatar'] except: _customer['account_avatar'] = '' _nickname = _customer['account_nickname'] _avatar = _customer['account_avatar'] # 创建一个代金券订单 _status = ORDER_STATUS_BF_INIT if _price == 0: _status = ORDER_STATUS_WECHAT_PAY_SUCCESS _order_id = str(uuid.uuid1()).replace('-', '') _timestamp = time.time() # 创建订单索引 order_index = { "_id": _order_id, "order_tyoe": "buy_voucher", "club_id": vendor_id, "item_type": "voucher", "item_id": _voucher_id, "item_name": _title, "distributor_type": "club", "distributor_id": guest_club_id, "create_time": _timestamp, "pay_type": "wxpay", "pay_status": _status, "total_amount": _amount, #已经转换为分,注意转为数值 } self.create_order(order_index) _order = { "_id": _order_id, "vendor_id": vendor_id, "account_id": account_id, "account_avatar": _avatar, "account_nickname": _nickname, "voucher_id": _voucher_id, "voucher_price": _price, "voucher_amount": _amount, "pay_type": "wxpay", "applicant_num": 1, "create_time": _timestamp, "last_update_time": _timestamp, 'status': _status, 'review': False } # status=99, 微信返回的支付状态 voucher_order_dao.voucher_order_dao().create(_order) num = voucher_order_dao.voucher_order_dao().count_not_review_by_vendor( vendor_id) budge_num_dao.budge_num_dao().update({ "_id": vendor_id, "voucher_order": num }) #创建微信订单 _total_amount = int(_voucher['price']) _timestamp = (int)(time.time()) if _total_amount != 0: # wechat 统一下单 # _openid = self.get_secure_cookie("wx_openid") # logging.info("got _openid %r", _openid) # 从comm中统一取 myinfo = self.get_myinfo_login() _openid = myinfo['login'] _store_id = 'Aplan' logging.info("got _store_id %r", _store_id) _product_description = "voucher" logging.info("got _product_description %r", _product_description) wx_app_info = vendor_wx_dao.vendor_wx_dao().query(vendor_id) wx_app_id = wx_app_info['wx_app_id'] logging.info("got wx_app_id %r in uri", wx_app_id) wx_mch_key = wx_app_info['wx_mch_key'] wx_mch_id = wx_app_info['wx_mch_id'] wx_notify_domain = wx_app_info['wx_notify_domain'] key = wx_mch_key nonceA = getNonceStr() logging.info("got nonceA %r", nonceA) #_ip = self.request.remote_ip _remote_ip = self.request.headers['X-Real-Ip'] logging.info("got _remote_ip %r", _remote_ip) total_fee = str(_total_amount) logging.info("got total_fee %r", total_fee) notify_url = wx_notify_domain + '/bf/wx/voucher-orders/notify' logging.info("got notify_url %r", notify_url) signA = getOrderSign(_remote_ip, notify_url, wx_app_id, wx_mch_id, nonceA, _openid, key, _store_id, _order_id, _product_description, total_fee) logging.info("got signA %r", signA) _xml = '<xml>' \ + '<appid>' + wx_app_id + '</appid>' \ + '<attach>' + _store_id + '</attach>' \ + '<body>' + _product_description + '</body>' \ + '<mch_id>' + wx_mch_id + '</mch_id>' \ + '<nonce_str>' + nonceA + '</nonce_str>' \ + '<notify_url>' + notify_url + '</notify_url>' \ + '<openid>' + _openid + '</openid>' \ + '<out_trade_no>' + _order_id + '</out_trade_no>' \ + '<spbill_create_ip>' + _remote_ip + '</spbill_create_ip>' \ + '<total_fee>' + total_fee + '</total_fee>' \ + '<trade_type>JSAPI</trade_type>' \ + '<sign>' + signA + '</sign>' \ + '</xml>' logging.info("got xml-------- %r", _xml) url = "https://api.mch.weixin.qq.com/pay/unifiedorder" http_client = HTTPClient() response = http_client.fetch(url, method="POST", body=_xml) logging.info("got response %r", response.body) _order_return = parseWxOrderReturn(response.body) logging.info("got _timestamp %r", str(_timestamp)) try: prepayId = _order_return['prepay_id'] except: _order_return['prepay_id'] = '' prepayId = '' logging.info("got prepayId %r", prepayId) try: nonceB = _order_return['nonce_str'] except: _order_return['nonce_str'] = '' nonceB = '' signB = getPaySign(_timestamp, wx_app_id, nonceB, prepayId, key) logging.info("got signB %r", signB) _order_return['pay_sign'] = signB _order_return['timestamp'] = _timestamp _order_return['app_id'] = wx_app_id _order_return['timestamp'] = _timestamp #_order_return['return_msg'] = 'OK' if (_order_return['return_msg'] == 'OK'): json = { '_id': _order_id, 'prepay_id': prepayId, 'status': ORDER_STATUS_WECHAT_UNIFIED_SUCCESS } else: json = { '_id': _order_id, 'prepay_id': prepayId, 'status': ORDER_STATUS_WECHAT_UNIFIED_FAILED } voucher_order_dao.voucher_order_dao().update(json) _voucher['amount'] = float(_voucher['amount']) / 100 _voucher['price'] = float(_voucher['price']) / 100 self.render('wx/voucher-pay-confirm.html', vendor_id=vendor_id, order_return=_order_return, voucher=_voucher, order=_order)
def get(self, vendor_id, voucher_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got voucher_id %r in uri", voucher_id) user_agent = self.request.headers["User-Agent"] lang = self.request.headers["Accept-Language"] wx_code = self.get_argument("code", "") logging.info("got wx_code=[%r] from argument", wx_code) wx_app_info = vendor_wx_dao.vendor_wx_dao().query(vendor_id) wx_app_id = wx_app_info['wx_app_id'] logging.info("got wx_app_id %r in uri", wx_app_id) wx_app_secret = wx_app_info['wx_app_secret'] wx_notify_domain = wx_app_info['wx_notify_domain'] if not wx_code: redirect_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wx_app_id + "&redirect_uri=" + wx_notify_domain + "/bf/wx/vendors/" + vendor_id + "/vouchers/" + voucher_id + "/buy/step1&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect" self.redirect(redirect_url) return accessToken = getAccessToken(wx_app_id, wx_app_secret, wx_code) access_token = accessToken["access_token"] logging.info("got access_token %r", access_token) wx_openid = accessToken["openid"] logging.info("got wx_openid %r", wx_openid) wx_userInfo = getUserInfo(access_token, wx_openid) nickname = wx_userInfo["nickname"] #nickname = unicode(nickname).encode('utf-8') logging.info("got nickname=[%r]", nickname) avatar = wx_userInfo['headimgurl'] logging.info("got avatar=[%r]", avatar) # 表情符号乱码,无法存入数据库,所以过滤掉 try: # UCS-4 Emoji = re.compile(u'[\U00010000-\U0010ffff]') nickname = Emoji.sub(u'\u25FD', nickname) # UCS-2 Emoji = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]') nickname = Emoji.sub(u'\u25FD', nickname) logging.info("got nickname=[%r]", nickname) except re.error: logging.error("got nickname=[%r]", nickname) nickname = "anonymous" url = API_DOMAIN + "/api/auth/wx/register" http_client = HTTPClient() random = str(uuid.uuid1()).replace('-', '') headers = {"Authorization": "Bearer " + random} _json = json_encode({ 'wx_openid': wx_openid, 'nickname': nickname, 'avatar': avatar }) response = http_client.fetch(url, method="POST", headers=headers, body=_json) logging.info("got response.body %r", response.body) data = json_decode(response.body) session_ticket = data['rs'] account_id = session_ticket['account_id'] self.set_secure_cookie("access_token", session_ticket['access_token']) self.set_secure_cookie("expires_at", str(session_ticket['expires_at'])) self.set_secure_cookie("account_id", account_id) # self.set_secure_cookie("wx_openid",wx_openid) timestamp = time.time() vendor_member = vendor_member_dao.vendor_member_dao().query_not_safe( vendor_id, account_id) if not vendor_member: memeber_id = str(uuid.uuid1()).replace('-', '') _json = { '_id': memeber_id, 'vendor_id': vendor_id, 'account_id': account_id, 'account_nickname': nickname, 'account_avatar': avatar, 'comment': '...', 'bonus': 0, 'history_bonus': 0, 'vouchers': 0, 'crets': 0, 'rank': 0, 'tour_leader': False, 'distance': 0, 'create_time': timestamp, 'last_update_time': timestamp } vendor_member_dao.vendor_member_dao().create(_json) logging.info("create vendor member %r", account_id) else: _json = { 'vendor_id': vendor_id, 'account_id': account_id, 'account_nickname': nickname, 'account_avatar': avatar, 'last_update_time': timestamp } vendor_member_dao.vendor_member_dao().update(_json) _voucher = voucher_pay_dao.voucher_pay_dao().query_not_safe(voucher_id) _voucher['amount'] = float(_voucher['amount']) / 100 _voucher['price'] = float(_voucher['price']) / 100 vendor_member = vendor_member_dao.vendor_member_dao().query_not_safe( vendor_id, account_id) if (vendor_member): try: vendor_member['account_nickname'] except: vendor_member['account_nickname'] = '' try: vendor_member['account_avatar'] except: vendor_member['account_avatar'] = '' _avatar = vendor_member['account_avatar'] _nickname = vendor_member['account_nickname'] self.render('wx/voucher-order-confirm.html', vendor_id=vendor_id, voucher=_voucher)
def get(self, vendor_id, voucher_id): wx_app_info = vendor_wx_dao.vendor_wx_dao().query(vendor_id) wx_app_id = wx_app_info['wx_app_id'] wx_notify_domain = wx_app_info['wx_notify_domain'] logging.info("got wx_app_id %r in uri", wx_app_id) redirect_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + wx_app_id + "&redirect_uri=" + wx_notify_domain + "/bf/wx/vendors/" + vendor_id + "/vouchers/" + voucher_id + "/buy/step1&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect" # FIXME 这里应改为从缓存取自己的access_token然后查myinfo是否存在wx_openid # 存在就直接用,不存在再走微信授权并更新用户信息 /api/myinfo-as-wx-user access_token = self.get_secure_cookie("access_token") logging.info("access_token %r======", access_token) if access_token: try: url = API_DOMAIN + "/api/myinfo-as-wx-user" http_client = HTTPClient() headers = {"Authorization": "Bearer " + access_token} response = http_client.fetch(url, method="GET", headers=headers) logging.info("got response.body %r", response.body) data = json_decode(response.body) user = data['rs'] account_id = user['_id'] avatar = user['avatar'] nickname = user['nickname'] timestamp = time.time() vendor_member = vendor_member_dao.vendor_member_dao( ).query_not_safe(vendor_id, account_id) if not vendor_member: memeber_id = str(uuid.uuid1()).replace('-', '') _json = { '_id': memeber_id, 'vendor_id': vendor_id, 'account_id': account_id, 'account_nickname': nickname, 'account_avatar': avatar, 'comment': '...', 'bonus': 0, 'history_bonus': 0, 'vouchers': 0, 'crets': 0, 'rank': 0, 'tour_leader': False, 'distance': 0, 'create_time': timestamp, 'last_update_time': timestamp } vendor_member_dao.vendor_member_dao().create(_json) logging.info("create vendor member %r", account_id) else: _json = { 'vendor_id': vendor_id, 'account_id': account_id, 'account_nickname': nickname, 'account_avatar': avatar, 'last_update_time': timestamp } vendor_member_dao.vendor_member_dao().update(_json) _voucher = voucher_pay_dao.voucher_pay_dao().query_not_safe( voucher_id) _voucher['amount'] = float(_voucher['amount']) / 100 _voucher['price'] = float(_voucher['price']) / 100 vendor_member = vendor_member_dao.vendor_member_dao( ).query_not_safe(vendor_id, account_id) if (vendor_member): try: vendor_member['account_nickname'] except: vendor_member['account_nickname'] = '' try: vendor_member['account_avatar'] except: vendor_member['account_avatar'] = '' _avatar = vendor_member['account_avatar'] _nickname = vendor_member['account_nickname'] self.render('wx/voucher-order-confirm.html', vendor_id=vendor_id, voucher=_voucher) except: self.redirect(redirect_url) else: self.redirect(redirect_url)
def get(self, vendor_id, voucher_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got voucher_id %r in uri", voucher_id) ops = self.get_ops_info() _voucher = voucher_pay_dao.voucher_pay_dao().query_not_safe(voucher_id) # 转换成元 _amount = _voucher['amount'] _price = _voucher['price'] _voucher_id = _voucher['_id'] _id = str(uuid.uuid1()).replace('-', '') _create_time = _voucher['create_time'] _expired_time = _voucher['expired_time'] _qrcode_url = _voucher['qrcode_url'] # account_id应由微信端传入 account_id = 'feece1648fa6484086700a83b0e8e540' _customer = vendor_member_dao.vendor_member_dao().query_not_safe( vendor_id, account_id) try: _customer['account_nickname'] except: _customer['account_nickname'] = '' try: _customer['account_avatar'] except: _customer['account_avatar'] = '' _nickname = _customer['account_nickname'] _avatar = _customer['account_avatar'] # 创建一个代金券订单 _timestamp = time.time() json = { "_id": _id, "vendor_id": vendor_id, "account_id": account_id, "account_avatar": _avatar, "account_nickname": _nickname, "voucher_id": _voucher_id, "voucher_price": _price, "voucher_amount": _amount, "pay_type": "wxpay", "applicant_num": 1, "create_time": _timestamp, "last_update_time": _timestamp, 'status': 1, 'review': False } voucher_order_dao.voucher_order_dao().create(json) # 每分配一个有偿代金券则生成一个普通代金券记录,方便个人中心查询 _timestamp = time.time() json = { "_id": _id, "vendor_id": vendor_id, "qrcode_url": _qrcode_url, "create_time": _create_time, "last_update_time": _timestamp, "amount": _amount, "expired_time": _expired_time, "price": _price, 'status': 1, "account_id": account_id } # status=1, 已分配,未使用 voucher_dao.voucher_dao().create(json) # 更新用户代金券 _customer_profile = vendor_member_dao.vendor_member_dao( ).query_not_safe(vendor_id, account_id) try: _customer_profile['vouchers'] except: _customer_profile['vouchers'] = 0 _vouchers_num = int(_customer_profile['vouchers']) + int(_amount) _json = { 'vendor_id': vendor_id, 'account_id': account_id, 'last_update_time': _timestamp, 'vouchers': _vouchers_num } vendor_member_dao.vendor_member_dao().update(_json) num = voucher_order_dao.voucher_order_dao().count_not_review_by_vendor( vendor_id) budge_num_dao.budge_num_dao().update({ "_id": vendor_id, "voucher_order": num }) counter = self.get_counter(vendor_id) self.redirect('/vendors/' + vendor_id + '/vouchers?status=0')