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): logging.info("got vendor_id %r in uri", vendor_id) _account_id = self.get_secure_cookie("account_id") _customer_profile = vendor_member_dao.vendor_member_dao().query_not_safe(vendor_id, _account_id) if not _customer_profile: _customer_profile = {"crets":0} else: try: _customer_profile['crets'] except: _customer_profile['crets'] = 0 _before = time.time() _crets = cret_dao.cret_dao().query_pagination_by_account(vendor_id, _account_id, _before, PAGE_SIZE_LIMIT) for _cret in _crets: _cret['create_time'] = timestamp_friendly_date(_cret['create_time']) _activity = activity_dao.activity_dao().query(_cret['activity_id']) _cret['activity_title'] = _activity['title'] _cret['activity_bk_img_url'] = _activity['bk_img_url'] self.render('wx/my-certs.html', vendor_id=vendor_id, crets=_crets, customer_profile=_customer_profile)
def get(self, vendor_id, cret_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got cret_id %r in uri", cret_id) ops = self.get_ops_info() _cret = cret_dao.cret_dao().query(cret_id) _customer_profile = vendor_member_dao.vendor_member_dao().query_not_safe(vendor_id, _cret['account_id']) try: _customer_profile['account_nickname'] except: _customer_profile['account_nickname'] = '' try: _customer_profile['account_avatar'] except: _customer_profile['account_avatar'] = '' counter = self.get_counter(vendor_id) self.render('vendor/customer-cret-info.html', vendor_id=vendor_id, ops=ops, counter=counter, profile=_customer_profile, cret=_cret)
def post(self, vendor_id): logging.info("got vendor_id %r in uri", vendor_id) ops = self.get_ops_info() keys_value = self.get_argument("search_keys", "") customers = vendor_member_dao.vendor_member_dao().query_by_keys(vendor_id,keys_value) for _customer_profile in customers: _customer_profile['create_time'] = timestamp_datetime(_customer_profile['create_time']); try: _customer_profile['account_nickname'] except: _customer_profile['account_nickname'] = '' try: _customer_profile['account_avatar'] except: _customer_profile['account_avatar'] = '' logging.info("got account_avatar %r", _customer_profile['account_avatar']) try: _customer_profile['comment'] except: _customer_profile['comment'] = '' try: _customer_profile['bonus'] except: _customer_profile['bonus'] = 0 logging.info("got bonus %r", _customer_profile['bonus']) try: _customer_profile['history_bonus'] except: _customer_profile['history_bonus'] = 0 logging.info("got history_bonus %r", _customer_profile['history_bonus']) try: _customer_profile['vouchers'] except: _customer_profile['vouchers'] = 0 # 转换成元 _customer_profile['vouchers'] = float(_customer_profile['vouchers']) / 100 try: _customer_profile['distance'] except: _customer_profile['distance'] = 0 try: _customer_profile['rank'] except: _customer_profile['rank'] = 0 try: _customer_profile['crets'] except: _customer_profile['crets'] = 0 counter = self.get_counter(vendor_id) self.render('vendor/customers.html', vendor_id=vendor_id, ops=ops, counter=counter, customers=customers, keys_value=keys_value)
def get(self, vendor_id): logging.info("got vendor_id %r in uri", vendor_id) _account_id = self.get_secure_cookie("account_id") _customer_profile = vendor_member_dao.vendor_member_dao().query_not_safe(vendor_id, _account_id) if not _customer_profile: _customer_profile = {'vouchers':0} else: try: _customer_profile['vouchers'] except: _customer_profile['vouchers'] = 0 # 有效的代金券 _before = time.time() _status = 1 # 未使用 new_voucher_amount = 0 #新的有效代金券总数 _vouchers = voucher_dao.voucher_dao().query_pagination_by_vendor(vendor_id, _account_id, _status, _before, PAGE_SIZE_LIMIT) for _data in _vouchers: logging.info("got voucher======== %r", _data) new_voucher_amount = new_voucher_amount + _data['amount'] # 转换成元 _data['amount'] = float(_data['amount']) / 100 _data['expired_time'] = timestamp_friendly_date(_data['expired_time']) # 修改个人代金券信息 if new_voucher_amount < 0: new_voucher_amount = 0 _timestamp = time.time(); _json = {'vendor_id':vendor_id, 'account_id':_account_id, 'last_update_time':_timestamp, 'vouchers':new_voucher_amount} vendor_member_dao.vendor_member_dao().update(_json) _customer_profile['vouchers'] = new_voucher_amount # 转换成元 _customer_profile['vouchers'] = float(_customer_profile['vouchers']) / 100 self.render('wx/my-vouchers.html', vendor_id=vendor_id, vouchers_num=_customer_profile['vouchers'], vouchers=_vouchers)
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, cret_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got cret_id %r in uri", cret_id) _cret = cret_dao.cret_dao().query(cret_id) _customer_profile = vendor_member_dao.vendor_member_dao().query_not_safe(vendor_id, _cret['account_id']) _activity = activity_dao.activity_dao().query(_cret['activity_id']) _cret['activity_title'] = _activity['title'] self.render('wx/cert-info.html', vendor_id=vendor_id, cret=_cret, profile=_customer_profile)
def get(self, vendor_id, task_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got task_id %r in uri", task_id) ops = self.get_ops_info() _task = task_dao.task_dao().query(task_id) _task['create_time'] = timestamp_datetime(_task['create_time']) _customers = vendor_member_dao.vendor_member_dao().query_pagination( vendor_id, 0, PAGE_SIZE_LIMIT) # 已分配过该任务的用户剔除 new_customers = [] for _customer in _customers: _account_id = _customer['account_id'] _personal_task = personal_task_dao.personal_task_dao( ).query_by_task_account(task_id, _account_id) if not _personal_task: new_customers.append(_customer) _customers = new_customers for _customer in _customers: _customer['create_time'] = timestamp_datetime( _customer['create_time']) try: _customer['account_nickname'] except: _customer['account_nickname'] = '' try: _customer['account_avatar'] except: _customer['account_avatar'] = '' try: _customer['rank'] = int(_customer['rank']) except: _customer['rank'] = 0 try: _customer['comment'] except: _customer['comment'] = '' counter = self.get_counter(vendor_id) self.render('vendor/task-allocate.html', vendor_id=vendor_id, ops=ops, counter=counter, task=_task, customers=_customers)
def get(self, vendor_id): logging.info("got vendor_id %r in uri", vendor_id) _status = self.get_argument("status", "") logging.info("got _status %r", _status) _status = int(_status) iBefore = 0 sBefore = self.get_argument("before", "") #格式 2016-06-01 22:36 if sBefore != "": iBefore = float(datetime_timestamp(sBefore)) else: iBefore = time.time() account_id = self.get_secure_cookie("account_id") if (account_id != ""): _vouchers = voucher_dao.voucher_dao().query_pagination_by_vendor( vendor_id, account_id, _status, iBefore, PAGE_SIZE_LIMIT) logging.info("got _account_id %r", account_id) else: _vouchers = voucher_dao.voucher_dao().query_pagination_by_status( vendor_id, _status, iBefore, PAGE_SIZE_LIMIT) for _data in _vouchers: # 转换成元 _data['amount'] = float(_data['amount']) / 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'] _json = JSON.dumps(_vouchers, default=json_util.default) logging.info("got _vouchers %r", _json) self.write(_json) self.finish()
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() _account_id = self.get_argument("account_id", "") logging.info("got _account_id %r", _account_id) _timestamp = time.time() json = { "_id": voucher_id, "last_update_time": _timestamp, "account_id": _account_id, "status": 1 } # status=1, 已分配 voucher_dao.voucher_dao().update(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 _voucher = voucher_dao.voucher_dao().query_not_safe(voucher_id) _vouchers_num = int(_customer_profile['vouchers']) + int( _voucher['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) self.redirect('/vendors/' + vendor_id + '/vouchers?status=0')
def get(self, vendor_id): # 从comm中统一取 myinfo = self.get_myinfo_login() account_id = myinfo['account_id'] nickname = myinfo['nickname'] avatar =myinfo['avatar'] 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) customer_profile = vendor_member_dao.vendor_member_dao().query_not_safe(vendor_id, account_id) try: customer_profile['bonus'] except: customer_profile['bonus'] = 0 # 金额转换成元 # customer_profile['bonus'] = float(customer_profile['bonus']) / 100 # logging.info("got bonus %r", customer_profile['bonus']) # 转换成元 try: customer_profile['vouchers'] except: customer_profile['vouchers'] = 0 customer_profile['vouchers'] = float(customer_profile['vouchers']) / 100 # 加上任务数量 personal_tasks = personal_task_dao.personal_task_dao().query_by_vendor_account(vendor_id,account_id) customer_profile['tasks'] = len(personal_tasks) self.render('wx/personal-center.html', vendor_id=vendor_id, profile=customer_profile)
def get(self, vendor_id, activity_id): logging.info("got vendor_id %r in uri", vendor_id) logging.info("got activity_id %r in uri", activity_id) _array = apply_dao.apply_dao().query_by_activity(activity_id) for data in _array: _member = vendor_member_dao.vendor_member_dao().query_not_safe( vendor_id, data['account_id']) try: logging.info("got account_avatar %r", _member['account_avatar']) data['account_avatar'] = _member['account_avatar'] except: logging.warn("got account_avatar is null") docs_list = list(_array) logging.info("got json %r", JSON.dumps(docs_list, default=json_util.default)) self.write(JSON.dumps(docs_list, default=json_util.default)) self.finish()
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_dao.voucher_dao().query_not_safe(voucher_id) # 转换成元 _voucher['amount'] = float(_voucher['amount']) / 100 _voucher['expired_time'] = timestamp_date(_voucher['expired_time']) _customers = vendor_member_dao.vendor_member_dao().query_pagination( vendor_id, 0, PAGE_SIZE_LIMIT) for _customer in _customers: _customer['create_time'] = timestamp_datetime( _customer['create_time']) try: _customer['account_nickname'] except: _customer['account_nickname'] = '' try: _customer['account_avatar'] except: _customer['account_avatar'] = '' try: _customer['rank'] = int(_customer['rank']) except: _customer['rank'] = 0 try: _customer['comment'] except: _customer['comment'] = '' counter = self.get_counter(vendor_id) self.render('vendor/vouchers-allocate.html', vendor_id=vendor_id, ops=ops, counter=counter, voucher=_voucher, customers=_customers)
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): logging.info("got vendor_id %r in uri", vendor_id) # _session_ticket = self.get_secure_cookie("session_ticket") _keys = self.get_argument("keysValue", "") _type = self.get_argument("searchType", "") if (_type == 'order'): _array = order_dao.order_dao().query_by_order_keys( vendor_id, _keys) elif (_type == 'title'): _array = apply_dao.apply_dao().query_by_title_keys( vendor_id, _keys) elif (_type == 'nickname'): _array = order_dao.order_dao().query_by_nickname_keys( vendor_id, _keys) elif (_type == 'date'): keys_array = _keys.split('~') begin_keys = float(date_timestamp(keys_array[0])) end_keys = float(date_timestamp(keys_array[1])) logging.info("got begin_keys>>>>>>>>>>> %r in uri", begin_keys) logging.info("got end_keys>>>>>>>>>>> %r in uri", end_keys) _array = order_dao.order_dao().query_by_time_keys( vendor_id, begin_keys, end_keys) else: _array = [] if (_array): for order in _array: _activity = activity_dao.activity_dao().query( order['activity_id']) order['activity_title'] = _activity['title'] # order['activity_amount'] = _activity['amount'] if not order['base_fees']: order['activity_amount'] = 0 else: for base_fee in order['base_fees']: # 价格转换成元 order['activity_amount'] = float(base_fee['fee']) / 100 logging.info("got activity_title %r", order['activity_title']) order['create_time'] = timestamp_datetime(order['create_time']) customer_profile = vendor_member_dao.vendor_member_dao( ).query_not_safe(vendor_id, order['account_id']) if (customer_profile): try: customer_profile['account_nickname'] except: customer_profile['account_nickname'] = '' try: customer_profile['account_avatar'] except: customer_profile['account_avatar'] = '' order['account_nickname'] = customer_profile[ 'account_nickname'] order['account_avatar'] = customer_profile[ 'account_avatar'] else: order['account_nickname'] = '' order['account_avatar'] = '' try: order['bonus'] except: order['bonus'] = 0 # 价格转换成元 order['bonus'] = float(order['bonus']) / 100 try: order['prepay_id'] except: order['prepay_id'] = '' try: order['transaction_id'] except: order['transaction_id'] = '' try: order['payed_total_fee'] except: order['payed_total_fee'] = 0 for ext_fee in order['ext_fees']: # 价格转换成元 ext_fee['fee'] = float(ext_fee['fee']) / 100 for insurance in order['insurances']: # 价格转换成元 insurance['fee'] = float(insurance['fee']) / 100 for _voucher in order['vouchers']: # 价格转换成元 _voucher['fee'] = float(_voucher['fee']) / 100 _cret = cret_dao.cret_dao().query_by_account( order['activity_id'], order['account_id']) if _cret: logging.info("got _cret_id %r", _cret['_id']) order['cret_id'] = _cret['_id'] else: order['cret_id'] = None # order['activity_amount'] = float(_activity['amount']) / 100 if not order['base_fees']: order['activity_amount'] = 0 else: for base_fee in order['base_fees']: # 价格转换成元 order['activity_amount'] = float(base_fee['fee']) / 100 order['total_amount'] = float(order['total_amount']) / 100 order['payed_total_fee'] = float( order['payed_total_fee']) / 100 _json = json_encode(_array) logging.info("got _json %r", _json) self.write(JSON.dumps(_json, default=json_util.default)) self.finish()
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')
def get(self, vendor_id): logging.info("got vendor_id %r in uri", vendor_id) # _session_ticket = self.get_secure_cookie("session_ticket") _keys = self.get_argument("keysValue", "") _type = self.get_argument("searchType", "") if (_type == 'title'): _array = apply_dao.apply_dao().query_by_title_keys( vendor_id, _keys) elif (_type == 'nickname'): _array = apply_dao.apply_dao().query_by_nickname_keys( vendor_id, _keys) elif (_type == 'date'): keys_array = _keys.split('~') begin_keys = float(date_timestamp(keys_array[0])) end_keys = float(date_timestamp(keys_array[1])) _array = apply_dao.apply_dao().query_by_time_keys( vendor_id, begin_keys, end_keys) logging.info("got begin_keys--------- %r in uri", begin_keys) logging.info("got end_keys--------- %r in uri", end_keys) else: _array = [] if (_array): for _apply in _array: _activity = activity_dao.activity_dao().query( _apply['activity_id']) _apply['activity_title'] = _activity['title'] logging.info("got activity_title %r", _apply['activity_title']) _apply['create_time'] = timestamp_datetime( _apply['create_time']) customer_profile = vendor_member_dao.vendor_member_dao( ).query_not_safe(vendor_id, _apply['account_id']) if (customer_profile): try: customer_profile['account_nickname'] except: customer_profile['account_nickname'] = '' try: customer_profile['account_avatar'] except: customer_profile['account_avatar'] = '' _apply['account_nickname'] = customer_profile[ 'account_nickname'] _apply['account_avatar'] = customer_profile[ 'account_avatar'] else: _apply['account_nickname'] = '' _apply['account_avatar'] = '' if _apply['gender'] == 'male': _apply['gender'] = u'男' else: _apply['gender'] = u'女' _json = json_encode(_array) logging.info("got _json %r", _json) self.write(JSON.dumps(_json, default=json_util.default)) self.finish()
def post(self): vendor_id = self.get_argument("vendor_id", "") logging.info("got vendor_id %r", vendor_id) activity_id = self.get_argument("activity_id", "") logging.info("got activity_id %r", activity_id) _account_id = self.get_secure_cookie("account_id") guest_club_id = self.get_argument("guest_club_id") logging.info("got guest_club_id %r", guest_club_id) access_token = self.get_access_token() # 取得自己的最后一笔订单 params = {"filter":"account", "account_id":_account_id, "page":1, "limit":1,} url = url_concat(API_DOMAIN + "/api/orders", params) 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) rs = data['rs'] orders = rs['data'] _timestamp = time.time() # 一分钟内不能创建第二个订单, # 防止用户点击回退按钮,产生第二个订单 if len(orders) > 0: for order in orders: if (_timestamp - order['create_time']) < 60: self.redirect('/bf/wx/orders/wait') return # 订单总金额 _total_amount = self.get_argument("total_amount", 0) logging.info("got _total_amount %r", _total_amount) # 价格转换成分 _total_amount = int(float(_total_amount) * 100) logging.info("got _total_amount %r", _total_amount) # 订单申报数目 _applicant_num = self.get_argument("applicant_num", 1) # 活动金额,即已选的基本服务项金额 amount = 0 actual_payment = 0 quantity = int(_applicant_num) logging.info("got quantity %r", quantity) _activity = self.get_activity(activity_id) logging.info("got _activity %r", _activity) # _activity = activity_dao.activity_dao().query(activity_id) _bonus_template = bonus_template_dao.bonus_template_dao().query(activity_id) bonus_points = int(_bonus_template['activity_shared']) #基本服务 _base_fee_ids = self.get_body_argument("base_fees", []) logging.info("got _base_fee_ids %r", _base_fee_ids) # 转为列表 _base_fee_ids = JSON.loads(_base_fee_ids) _base_fees = [] base_fee_template = _activity['base_fee_template'] for _base_fee_id in _base_fee_ids: for template in base_fee_template: if _base_fee_id == template['_id']: _base_fee = {"_id":_base_fee_id, "name":template['name'], "fee":template['fee']} _base_fees.append(_base_fee) activity_amount = template['fee'] amount = amount + int(template['fee']) * quantity actual_payment = actual_payment + int(template['fee']) * quantity break; logging.info("got actual_payment %r", actual_payment) # 附加服务项编号数组 # *** 接受json数组用这个 *** _ext_fee_ids = self.get_body_argument("ext_fees", []) logging.info("got _ext_fee_ids %r", _ext_fee_ids) # 转为列表 _ext_fee_ids = JSON.loads(_ext_fee_ids) _ext_fees = [] ext_fee_template = _activity['ext_fee_template'] for _ext_fee_id in _ext_fee_ids: for template in ext_fee_template: if _ext_fee_id == template['_id']: _ext_fee = {"_id":_ext_fee_id, "name":template['name'], "fee":template['fee']} _ext_fees.append(_ext_fee) amount = amount + int(template['fee']) * quantity actual_payment = actual_payment + int(template['fee']) * quantity break; logging.info("got actual_payment %r", actual_payment) # 保险选项,数组 _insurance_ids = self.get_body_argument("insurances", []) _insurance_ids = JSON.loads(_insurance_ids) _insurances = [] _insurance_templates = insurance_template_dao.insurance_template_dao().query_by_vendor(vendor_id) for _insurance_id in _insurance_ids: for _insurance_template in _insurance_templates: if _insurance_id == _insurance_template['_id']: _insurance = {"_id":_insurance_id, "name":_insurance_template['title'], "fee":_insurance_template['amount']} _insurances.append(_insurance) amount = amount + int(_insurance['fee']) * quantity actual_payment = actual_payment + int(_insurance['fee']) * quantity break; logging.info("got actual_payment %r", actual_payment) #代金券选项,数组 _vouchers_ids = self.get_body_argument("vouchers", []) _vouchers_ids = JSON.loads(_vouchers_ids) _vouchers = [] for _vouchers_id in _vouchers_ids: logging.info("got _vouchers_id %r", _vouchers_id) _voucher = voucher_dao.voucher_dao().query_not_safe(_vouchers_id) _json = {'_id':_vouchers_id, 'fee':_voucher['amount']} _vouchers.append(_json) actual_payment = actual_payment - int(_json['fee']) * quantity logging.info("got actual_payment %r", actual_payment) # 积分选项,数组 _bonus = 0 _bonus_array = self.get_body_argument("bonus", []) if _bonus_array: _bonus_array = JSON.loads(_bonus_array) if len(_bonus_array) > 0: _bonus = _bonus_array[0] # 价格转换成分 _bonus = - int(float(_bonus) * 100) logging.info("got _bonus %r", _bonus) points = _bonus actual_payment = actual_payment + points logging.info("got actual_payment %r", actual_payment) _order_id = str(uuid.uuid1()).replace('-', '') _status = ORDER_STATUS_BF_INIT if actual_payment == 0: _status = ORDER_STATUS_WECHAT_PAY_SUCCESS # 创建订单索引 order_index = { "_id": _order_id, "order_type": "buy_activity", "club_id": vendor_id, "item_type": "activity", "item_id": activity_id, "item_name": _activity['title'], "distributor_type": "club", "distributor_id": guest_club_id, "create_time": _timestamp, "pay_type": "wxpay", "pay_status": _status, "quantity": quantity, "amount": amount, #已经转换为分,注意转为数值 "actual_payment": actual_payment, #已经转换为分,注意转为数值 "base_fees": _base_fees, "ext_fees": _ext_fees, "insurances": _insurances, "vouchers": _vouchers, "points_used": points, "bonus_points": bonus_points, # 活动奖励积分 "booking_time": _activity['begin_time'], } self.create_order(order_index) # budge_num increase self.counter_increase(vendor_id, "activity_order") self.counter_increase(activity_id, "order") # TODO notify this message to vendor's administrator by SMS 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_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'] _timestamp = (int)(time.time()) if actual_payment != 0: # wechat 统一下单 myinfo = self.get_myinfo_login() _openid = myinfo['login'] _store_id = 'Aplan' logging.info("got _store_id %r", _store_id) _product_description = _activity['activity_id'] logging.info("got _product_description %r", _product_description) #_ip = self.request.remote_ip _remote_ip = self.request.headers['X-Real-Ip'] _order_return = wx_wrap.getUnifiedOrder(_remote_ip, wx_app_id, _store_id, _product_description, wx_notify_domain, wx_mch_id, wx_mch_key, _openid, _order_id, actual_payment, _timestamp) # wx统一下单记录保存 _order_return['_id'] = _order_return['prepay_id'] self.create_symbol_object(_order_return) # 微信统一下单返回成功 order_unified = None if(_order_return['return_msg'] == 'OK'): order_unified = {'_id':_order_id,'prepay_id': _order_return['prepay_id'], 'pay_status': ORDER_STATUS_WECHAT_UNIFIED_SUCCESS} else: order_unified = {'_id':_order_id,'prepay_id': _order_return['prepay_id'], 'pay_status': ORDER_STATUS_WECHAT_UNIFIED_FAILED} # 微信统一下单返回成功 # TODO: 更新订单索引中,订单状态pay_status,prepay_id self.update_order_unified(order_unified) # FIXME, 将服务模板转为字符串,客户端要用 _servTmpls = _activity['ext_fee_template'] _activity['json_serv_tmpls'] = json_encode(_servTmpls); _activity['begin_time'] = timestamp_friendly_date(float(_activity['begin_time'])) # timestamp -> %m月%d 星期%w _activity['end_time'] = timestamp_friendly_date(float(_activity['end_time'])) # timestamp -> %m月%d 星期%w # 金额转换成元 # _activity['amount'] = float(activity_amount) / 100 for base_fee in order_index['base_fees']: # 价格转换成元 order_index['activity_amount'] = float(base_fee['fee']) / 100 self.render('wx/order-confirm.html', vendor_id=vendor_id, return_msg=response.body, order_return=_order_return, activity=_activity, order_index=order_index) else: #actual_payment == 0: # FIXME, 将服务模板转为字符串,客户端要用 _servTmpls = _activity['ext_fee_template'] _activity['json_serv_tmpls'] = tornado.escape.json_encode(_servTmpls); _activity['begin_time'] = timestamp_friendly_date(float(_activity['begin_time'])) # timestamp -> %m月%d 星期%w _activity['end_time'] = timestamp_friendly_date(float(_activity['end_time'])) # timestamp -> %m月%d 星期%w # 金额转换成元 # _activity['amount'] = float(activity_amount) / 100 for base_fee in order_index['base_fees']: # 价格转换成元 order_index['activity_amount'] = float(base_fee['fee']) / 100 # 如使用积分抵扣,则将积分减去 if order_index['points_used'] < 0: # 修改个人积分信息 bonus_points = { 'org_id':vendor_id, 'org_type':'club', 'account_id':_account_id, 'account_type':'user', 'action': 'buy_item', 'item_type': 'item', 'item_id': activity_id, 'item_name': _activity['title'], 'bonus_type':'bonus', 'points': points, 'order_id': order_index['_id'] } self.create_points(bonus_points) # self.points_decrease(vendor_id, order_index['account_id'], order_index['points_used']) # 如使用代金券抵扣,则将代金券减去 for _voucher in _vouchers: # status=2, 已使用 voucher_dao.voucher_dao().update({'_id':_voucher['_id'], 'status':2, 'last_update_time':_timestamp}) _customer_profile = vendor_member_dao.vendor_member_dao().query_not_safe(vendor_id, order_index['account_id']) # 修改个人代金券信息 _voucher_amount = int(_customer_profile['vouchers']) - int(_voucher['fee']) if _voucher_amount < 0: _voucher_amount = 0 _json = {'vendor_id':vendor_id, 'account_id':order_index['account_id'], 'last_update_time':_timestamp, 'vouchers':_voucher_amount} vendor_member_dao.vendor_member_dao().update(_json) # send message to wx 公众号客户 by template wx_access_token = wx_wrap.getAccessTokenByClientCredential(WX_APP_ID, WX_APP_SECRET) logging.info("got wx_access_token %r", wx_access_token) # 通过wxpub,给俱乐部操作员发送通知 ops = self.get_club_ops_wx(vendor_id) for op in ops: wx_openid = op['binding_id'] logging.info("got wx_openid %r", wx_openid) wx_wrap.sendOrderPayedToOpsMessage(wx_access_token, WX_NOTIFY_DOMAIN, wx_openid, order_index) self.render('wx/order-confirm.html', vendor_id=vendor_id, return_msg='OK', order_return={'timestamp':_timestamp, 'nonce_str':'', 'pay_sign':'', 'prepay_id':'', 'app_id': wx_app_id, 'return_msg':'OK'}, activity=_activity, order_index=order_index)
def post(self): logging.info("POST %r", self.request.uri) # 返回参数 #<xml> # <appid><![CDATA[wxaa328c83d3132bfb]]></appid>\n # <attach><![CDATA[Aplan]]></attach>\n # <bank_type><![CDATA[CFT]]></bank_type>\n # <cash_fee><![CDATA[1]]></cash_fee>\n # <fee_type><![CDATA[CNY]]></fee_type>\n # <is_subscribe><![CDATA[Y]]></is_subscribe>\n # <mch_id><![CDATA[1340430801]]></mch_id>\n # <nonce_str><![CDATA[jOhHjqDfx9VQGmU]]></nonce_str>\n # <openid><![CDATA[oy0Kxt7zNpZFEldQmHwFF-RSLNV0]]></openid>\n # <out_trade_no><![CDATA[e358738e30fe11e69a7e00163e007b3e]]></out_trade_no>\n # <result_code><![CDATA[SUCCESS]]></result_code>\n # <return_code><![CDATA[SUCCESS]]></return_code>\n # <sign><![CDATA[6291D73149D05F09D18C432E986C4DEB]]></sign>\n # <time_end><![CDATA[20160613083651]]></time_end>\n # <total_fee>1</total_fee>\n # <trade_type><![CDATA[JSAPI]]></trade_type>\n # <transaction_id><![CDATA[4007652001201606137183943151]]></transaction_id>\n #</xml> _xml = self.request.body logging.info("got return_body %r", _xml) _pay_return = parseWxPayReturn(_xml) # wx支付结果记录保存 _pay_return['_id'] = _pay_return['transaction_id'] self.create_symbol_object(_pay_return) logging.info("got result_code %r", _pay_return['result_code']) logging.info("got total_fee %r", _pay_return['total_fee']) logging.info("got time_end %r", _pay_return['time_end']) logging.info("got transaction_id %r", _pay_return['transaction_id']) logging.info("got out_trade_no %r", _pay_return['out_trade_no']) pay_id = _pay_return['out_trade_no'] logging.info("got pay_id %r", pay_id) _result_code = _pay_return['result_code'] if _result_code == 'SUCCESS' : # 查询过去是否填报,有则跳过此步骤。主要是防止用户操作回退键,重新回到此页面 trade_no = pay_id order_index = None if len(pay_id) == 30: # item trade_no = pay_id[0:24] logging.info("got trade_no %r", trade_no) order_index = self.get_order_index_by_trade_no(trade_no) else: # activity, len(trade_no)==32 order_index = self.get_order_index(trade_no) _order_id = order_index['_id'] logging.info("got order_index=[%r]", order_index) # 用于更新积分、优惠券 vendor_id = order_index['club_id'] if order_index['pay_status'] == 30: return else: # 调用微信支付接口,返回成功 # TODO: 更新订单索引中,订单状态pay_status,transaction_id,payed_total_fee order_payed = { '_id':_order_id, "pay_status": ORDER_STATUS_WECHAT_PAY_SUCCESS, 'transaction_id':_pay_return['transaction_id'], 'actual_payment':_pay_return['total_fee'] } self.update_order_payed(order_payed) logging.info("update_order_payed=[%r]", order_payed) # 购买商品活动奖励积分 # bonus_points = { # 'org_id':vendor_id, # 'org_type':'club', # 'account_id':order_index['account_id'], # 'account_type':'user', # 'action': 'get_bonus_points_buy_item', # 'item_type': order_index['item_type'], # 'item_id': order_index['item_id'], # 'item_name': order_index['item_name'], # 'bonus_type':'bonus', # 'points': _pay_return['total_fee'], # 'order_id': order_index['_id'] # } # self.create_points(bonus_points) # 如使用积分抵扣,则将积分减去 points = int(order_index['points_used']) if points < 0: # 修改个人积分信息 bonus_points = { 'org_id':vendor_id, 'org_type':'club', 'account_id':order_index['account_id'], 'account_type':'user', 'action': 'consumer_reward_points_buy_item', 'item_type': order_index['item_type'], 'item_id': order_index['item_id'], 'item_name': order_index['item_name'], 'bonus_type':'bonus', 'points': points, 'order_id': order_index['_id'] } self.create_points(bonus_points) # self.points_increase(vendor_id, order_index['account_id'], bonus) logging.info("used bonus_points=[%r]", bonus_points) # 如使用代金券抵扣,则将代金券减去 _vouchers = order_index['vouchers'] for _voucher in _vouchers: # status=2, 已使用 voucher_dao.voucher_dao().update({'_id':_voucher['_id'], 'status':2, 'last_update_time':_timestamp}) _customer_profile = mongodao().query_vendor_member_not_safe(vendor_id, order_index['account_id']) # 修改个人代金券信息 _voucher_amount = int(_customer_profile['vouchers']) - int(_voucher['fee']) if _voucher_amount < 0: _voucher_amount = 0 _json = {'vendor_id':vendor_id, 'account_id':order_index['account_id'], 'last_update_time':_timestamp, 'vouchers':_voucher_amount} vendor_member_dao.vendor_member_dao().update(_json) logging.info("used _vouchers=[%r]", _vouchers) # send message to wx 公众号客户 by template wx_access_token = wx_wrap.getAccessTokenByClientCredential(WX_APP_ID, WX_APP_SECRET) logging.info("got wx_access_token %r", wx_access_token) # 通过wxpub,给俱乐部操作员发送通知 ops = self.get_club_ops_wx(vendor_id) for op in ops: wx_openid = op['binding_id'] logging.info("got wx_openid %r", wx_openid) if order_index['order_type'] == "buy_activity": wx_wrap.sendActivityOrderPayedToOpsMessage(wx_access_token, WX_NOTIFY_DOMAIN, wx_openid, order_index) elif order_index['order_type'] == "buy_item": logging.info("sendItemOrderPayedToOpsMessage=[%r]", WX_MESSAGE_TEMPLATE) if WX_MESSAGE_TEMPLATE == "kkfcps": wx_wrap.sendItemOrderPayedToOpsMessage_kkfcps(wx_access_token, WX_NOTIFY_DOMAIN, wx_openid, order_index) else: wx_wrap.sendItemOrderPayedToOpsMessage(wx_access_token, WX_NOTIFY_DOMAIN, wx_openid, order_index) # 如果是分销的订单,给分销商加上积分 if order_index['distributor_id'] != DEFAULT_USER_ID: logging.info("distributor_id=[%r]", order_index['distributor_id']) # 获取分销商返点信息 club = self.get_club_basic_info(vendor_id) logging.info("club=[%r]", club) league_id = club['league_id'] discount_rate = self.get_discount_rate(league_id, order_index['item_id']) logging.info("discount_rate=[%r]", discount_rate) points = order_index['actual_payment'] * discount_rate / 100 logging.info("points=[%r]", points) # 修改分销商积分信息 bonus_points = { 'org_id':vendor_id, 'org_type':'club', 'account_id':order_index['distributor_id'], 'account_type':'club', 'action': 'resale_item', 'item_type': order_index['item_type'], 'item_id': order_index['item_id'], 'item_name': order_index['item_name'], 'bonus_type':'bonus', 'points': points, 'order_id': order_index['_id'] } self.create_points(bonus_points) # TODO 如果有分销上线,将1%的积分奖励给上线 higher_level = self.get_higher_level(order_index['club_id'], order_index['account_id']) logging.info("got higher_level=[%r]", higher_level) if higher_level: actual_payment = _pay_return['total_fee'] points = int(int(actual_payment) / 100) # TODO test # points = actual_payment if points > 0: bonus_points = { 'org_id':vendor_id, 'org_type':'club', 'account_id':higher_level['higher_level'], 'account_type':'user', 'action': 'lower_level_buy_item', 'item_type': order_index['item_type'], 'item_id': order_index['item_id'], 'item_name': order_index['item_name'], 'bonus_type':'bonus', 'points': points, 'order_id': order_index['_id'] } self.create_points(bonus_points) # TODO 发送消息给上级获得积分,下级购买商品 higher_login = self.get_club_user_wx(vendor_id, higher_level['higher_level']) if higher_login: higher_openid = higher_login['_id'] nickname = order_index['nickname'] text = u"您的朋友 " +nickname+ u" 购买商品,您获得 " +points+ u" 个积分" wx_wrap.sendMessageToCustomer(wx_access_token, higher_openid, text) else: # 调用微信支付接口,返回成功 # TODO: 更新订单索引中,订单状态pay_status,transaction_id,payed_total_fee order_payed = {'_id':_order_id, "pay_status": ORDER_STATUS_WECHAT_PAY_FAILED, 'transaction_id':DEFAULT_USER_ID, 'actual_payment':0} self.update_order_payed(order_payed)
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) 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)