コード例 #1
0
    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)
        _expired_time = self.get_argument("expired_time", "")
        logging.info("got _expired_time %r", _expired_time)

        # 转换为分(整数)
        _amount = float(_amount) * 100
        # 转换为秒
        _expired_time = date_timestamp(_expired_time)

        _timestamp = time.time()
        json = {
            "_id": voucher_id,
            "last_update_time": _timestamp,
            "amount": _amount,
            "expired_time": _expired_time
        }
        voucher_dao.voucher_dao().update(json)

        self.redirect('/vendors/' + vendor_id + '/vouchers?status=0')
コード例 #2
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)
        _expired_time = self.get_argument("expired_time", "")
        logging.info("got _expired_time %r", _expired_time)

        # 转换为分(整数)
        _amount = float(_amount) * 100
        # 转换为秒
        _expired_time = date_timestamp(_expired_time)

        _id = str(uuid.uuid1()).replace('-', '')
        _timestamp = time.time()
        json = {
            "_id": _id,
            "vendor_id": vendor_id,
            "create_time": _timestamp,
            "last_update_time": _timestamp,
            "amount": _amount,
            "expired_time": _expired_time,
            "price": 0,
            'status': 0
        }  # status=0, 未分配
        voucher_dao.voucher_dao().create(json)

        self.redirect('/vendors/' + vendor_id + '/vouchers?status=0')
コード例 #3
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)
コード例 #4
0
    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)
コード例 #5
0
    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()
コード例 #6
0
    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')
コード例 #7
0
    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'])

        counter = self.get_counter(vendor_id)
        self.render('vendor/vouchers-free-edit.html',
                    vendor_id=vendor_id,
                    ops=ops,
                    counter=counter,
                    voucher=_voucher)
コード例 #8
0
    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)
コード例 #9
0
    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)
コード例 #10
0
    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)
コード例 #11
0
    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)
コード例 #12
0
    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')