Example #1
0
def get_agent_setting():
    dbg('get_agent_setting')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        target_agent_id = data['target_agent_id']
    except:
        print_exception_info()
        raise ApiError('update agent setting error', error.ERROR_PARAM)

    agent_id = current_user.agent_id
    target_agent = dbapi.get_agent(id=target_agent_id)
    if not target_agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)

    agent_setting = dbapi.get_agent_setting(agent_id=target_agent_id)
    reply['data'] = {
        'min_withdraw': agent_setting.min_withdraw,
        'withdraw_fee': agent_setting.withdraw_fee,
        'wallet_pay_enable': agent_setting.wallet_pay_enable,
        'trans_url': agent_setting.trans_url
    }

    return make_response(jsonify(reply), status_code)
Example #2
0
def get_wallet_balance():
    dbg('get_wallet_balance')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        imei = data['imei']
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    device = dbapi.get_device(imei=imei)
    if not device:
        raise ApiError('ERROR_DEVICE_NOT_FOUND', error.ERROR_DEVICE_NOT_FOUND)

    user_id = g.oauth_user.user_id
    wallet = dbapi.get_wallet(role=0,
                              user_id=user_id,
                              agent_id=device.owner_agent_id)

    agent_setting = dbapi.get_agent_setting(agent_id=device.agent_id)
    wallet_pay_enable = 1
    if agent_setting and agent_setting.wallet_pay_enable == 0:
        wallet_pay_enable = 0

    reply['data'] = {
        'balance': wallet.balance,
        'wallet_pay_enable': wallet_pay_enable
    }
    return make_response(jsonify(reply), status_code)
Example #3
0
def send_trans_data_to_server(self, imei, msg):
    try:
        cur_count = self.request.retries
        dbg(cur_count)
        time_list = (5, 15, 30, 60, 120)
        device = dbapi.get_device(imei=imei)
        if device:
            agent_setting = dbapi.get_agent_setting(agent_id=device.agent_id)
            if agent_setting and agent_setting.trans_url:
                r = requests.post(agent_setting.trans_url,
                                  json={
                                      'imei': imei,
                                      'message': msg
                                  })
                if r.status_code == requests.codes.ok:
                    return 'ok'
                else:
                    dbg(r.text)
                    raise Exception('wrong data')
            else:
                dbg('no agent_setting or trans_url for agent_id: %d' %
                    device.agent_id)
                raise Exception('url error')
        else:
            dbg('no device: %s' % imei)
            raise Exception('no device')
    except:
        print_exception_info()
        if cur_count < len(time_list):
            countdown = time_list[cur_count]
            raise self.retry(countdown=countdown, max_retries=5)
        else:
            return 'error'
Example #4
0
def update_agent_setting():
    dbg('update_agent_setting')
    reply, status_code = {'code': 0, 'msg': ''}, 200

    try:
        data = request.get_json()
        target_agent_id = data['target_agent_id']
        update = data['update']
        dbg(update)
        keys = ('min_withdraw', 'withdraw_fee', 'wallet_pay_enable',
                'trans_url')
        for k in update:
            assert (k in keys)
    except:
        print_exception_info()
        raise ApiError('update agent setting error', error.ERROR_PARAM)

    if 'min_withdraw' in update and update['min_withdraw'] < 200:
        raise ApiError('ERROR_SETTING_WITH_DRAW',
                       error.ERROR_SETTING_WITH_DRAW)

    if 'withdraw_fee' in update and update['withdraw_fee'] < 0.006:
        raise ApiError('ERROR_SETTING_WITHDRAW_FEE',
                       error.ERROR_SETTING_WITHDRAW_FEE)

    agent_id = current_user.agent_id
    target_agent = dbapi.get_agent(id=target_agent_id)
    if not target_agent:
        raise ApiError('ERROR_AGENT_NOT_FOUND', error.ERROR_AGENT_NOT_FOUND)

    if target_agent.hook_agent_id != 0 and target_agent.hook_agent_id != agent_id:
        raise ApiError('ERROR_AGENT_NO_PERMISSION',
                       error.ERROR_AGENT_NO_PERMISSION)

    # 更新目标代理
    agent_setting = dbapi.get_agent_setting(agent_id=target_agent_id)
    if not agent_setting:
        agent_setting = dbapi.make_new_agent_setting(target_agent_id)
        db.session.commit()
    dbapi.update_agent_setting(agent_setting, **update)

    # 更新level3的代理
    agents3 = dbapi.get_agent(hook_agent_id=target_agent_id)
    for agent3 in agents3:
        agent_id = agent3.id
        agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
        if not agent_setting:
            agent_setting = dbapi.make_new_agent_setting(agent_id)
            db.session.commit()
        dbapi.update_agent_setting(agent_setting, **update)

        # 更新level2的代理
        agents2 = dbapi.get_agent(hook_agent_id=agent_id)
        for agent2 in agents2:
            agent_id = agent2.id
            agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
            if not agent_setting:
                agent_setting = dbapi.make_new_agent_setting(agent_id)
                db.session.commit()
            dbapi.update_agent_setting(agent_setting, **update)

            # 更新level1的代理
            agents1 = dbapi.get_agent(hook_agent_id=agent_id)
            for agent1 in agents1:
                agent_id = agent1.id
                agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
                if not agent_setting:
                    agent_setting = dbapi.make_new_agent_setting(agent_id)
                    db.session.commit()
                dbapi.update_agent_setting(agent_setting, **update)

    db.session.commit()

    return make_response(jsonify(reply), status_code)
Example #5
0
def add_agent():
    dbg('add_agent')
    reply, status_code = {'code': 0, 'msg': ''}, 200
    try:
        data = request.get_json()
        salesman = data['salesman']
        level = data['level']
        slevel = data['slevel']
        name = data['name']
        email = data['email']
        phone = data['phone']
        address = data['address']
        remark = data['remark']
        expandable = data['expandable']
        withdrawable = data['withdrawable']
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    target_agent = dbapi.get_agent(phone=phone)
    if target_agent:
        raise ApiError('ERROR_PHONE_EXISTS', error.ERROR_PHONE_EXISTS)

    target_agent = dbapi.get_agent(email=email)
    if target_agent:
        raise ApiError('ERROR_EMAIL_EXISTS', error.ERROR_EMAIL_EXISTS)

    agent_id = current_user.agent_id
    agent = dbapi.get_agent(id=agent_id)
    if not agent:
        raise ApiError('ERROR_AGENT_NO_PERMISSION not agent',
                       error.ERROR_AGENT_NO_PERMISSION)
    if not agent.expandable:
        raise ApiError('ERROR_AGENT_NO_PERMISSION not expandable',
                       error.ERROR_AGENT_NO_PERMISSION)
    if salesman == 0 and agent.salesman == 0 and level >= agent.level:
        raise ApiError('ERROR_AGENT_NO_PERMISSION not allowed level',
                       error.ERROR_AGENT_NO_PERMISSION)
    if agent.salesman == 1 and slevel < agent.slevel:
        raise ApiError('ERROR_AGENT_NO_PERMISSION not allowed slevel',
                       error.ERROR_AGENT_NO_PERMISSION)

    openluat_user = dbapi.get_openluat_user(phone=phone)
    if not openluat_user:
        openluat_user = dbapi.make_new_openluat_user(name, email, phone)
        db.session.commit()

    hook_agent_id = agent_id
    new_agent = dbapi.make_new_agent(salesman, openluat_user.id, hook_agent_id,
                                     level, slevel, expandable, withdrawable,
                                     name, phone, email, address, remark)
    db.session.commit()

    cur_agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
    if not cur_agent_setting:
        cur_agent_setting = dbapi.make_new_agent_setting(agent_id)

    min_withdraw = cur_agent_setting.min_withdraw
    withdraw_fee = cur_agent_setting.withdraw_fee
    wallet_pay_enable = cur_agent_setting.wallet_pay_enable
    agent_setting = dbapi.make_new_agent_setting(
        new_agent.id,
        min_withdraw=min_withdraw,
        withdraw_fee=withdraw_fee,
        wallet_pay_enable=wallet_pay_enable)

    # 默认微信公众号对接
    old_agent_wechat = dbapi.get_agent_wechat(agent_id=agent_id)
    agent_wechat = dbapi.make_new_agent_wechat(
        new_agent.id, old_agent_wechat.wechat_config_id)

    # 默认阿里生活号对接
    old_agent_ali = dbapi.get_agent_ali(agent_id=agent_id)
    agent_ali = dbapi.make_new_agent_ali(new_agent.id,
                                         old_agent_ali.ali_config_id)

    db.session.commit()

    return make_response(jsonify(reply), status_code)
Example #6
0
def top_agent_get_wechat_withdraw():
    dbg('top_agent_get_wechat_withdraw')
    reply ,status_code = {'code': 0, 'msg': ''}, 200

    try:
        page  = int(request.args.get('page', 1))
        psize = int(request.args.get('psize', 10))
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    agent_id = current_user.agent_id
    user_id  = current_user.id

    wechat_agent_id = get_top_wechat_agent_id(agent_id)
    if wechat_agent_id != agent_id:
        raise ApiError('ERROR_AGENT_NO_PERMISSION', error.ERROR_AGENT_NO_PERMISSION)

    agent_setting = dbapi.get_agent_setting(agent_id=wechat_agent_id)

    count, res = dbapi.get_pay_to_user(wechat_agent_id=wechat_agent_id,
        page=page, psize=psize)
    data = []
    sum_dict = {}
    if count:
        for pay_to_user in res:
            openluat_user_id = pay_to_user.openluat_user_id
            agent = dbapi.get_agent(user_id=openluat_user_id)
            if agent:
                wallet = dbapi.get_wallet(user_id=openluat_user_id, agent_id=agent_id)
                name = agent.name
                agent_id = agent.id
                if agent_id in sum_dict:
                    dbg('sum in dict')
                    total_fee_sum = sum_dict[agent_id]['total_fee_sum']
                    receipt_sum = sum_dict[agent_id]['receipt_sum']
                else:
                    total_fee_sum = dbapi.get_pay_to_user(user_id=openluat_user_id, total=True)
                    receipt_sum = dbapi.get_agent_wallet_receipt_sum(wallet.id)
                    sum_dict[agent_id] = {
                        'total_fee_sum': total_fee_sum,
                        'receipt_sum': receipt_sum
                    }
            else:
                dbg('agent not found for openluat_user_id: %s' % openluat_user_id)
                name = ''
                total_fee_sum = 0
                receipt_sum = 0

            info = {
                'time': int(pay_to_user.ctime.timestamp()),
                # 'to_nickname': pay_to_user.to_nickname,
                'payment_no': pay_to_user.payment_no,
                'name': name,
                'total_fee': pay_to_user.total_fee,
                'wechat_fee': math.ceil(pay_to_user.total_fee * agent_setting.withdraw_fee),
                'wechat_fee_rate': agent_setting.withdraw_fee,
                'fee': pay_to_user.total_fee - math.ceil(pay_to_user.total_fee * agent_setting.withdraw_fee),
                'total_fee_sum': total_fee_sum,
                'receipt_sum': receipt_sum,
                'balance': int(wallet.balance),
                'withdrawable_balance': int(wallet.withdrawable_balance),
                'status': pay_to_user.status
            }
            data.append(info)

    reply = {
        'count': count,
        'pays': data
    }

    return make_response(jsonify(reply), status_code)
Example #7
0
def wechat_withdraw():
    dbg('wechat_withdraw')
    reply ,status_code = {'code': 0, 'msg': ''}, 200

    try:
        data    = request.get_json()
        fee     = data['fee']
        desc    = data['desc']
        remark  = data['remark']
        pswd    = data['pswd']
        assert(isinstance(fee, int))
    except:
        print_exception_info()
        raise ApiError('ERROR_PARAM', error.ERROR_PARAM)

    if not current_user.verify_password(pswd):
        raise ApiError('ERROR_WRONG_PSWD', error.ERROR_WRONG_PSWD)

    agent_id = current_user.agent_id
    agent_setting = dbapi.get_agent_setting(agent_id=agent_id)
    if fee < agent_setting.min_withdraw:
        dbg('fee: %d\tmin_withdraw: %d' % (fee, agent_setting.min_withdraw))
        raise ApiError('ERROR_WITHDRAW_FEE', error.ERROR_WITHDRAW_FEE)

    wallet = dbapi.get_wallet(user_id=current_user.id, agent_id=agent_id)
    if fee > wallet.withdrawable_balance:
        dbg('fee: %d\twithdrawable_balance: %d' % (fee, wallet.withdrawable_balance))
        raise ApiError('ERROR_WITHDRAW_FEE', error.ERROR_WITHDRAW_FEE)

    user_id = current_user.id
    wechat_user = dbapi.get_wechat_user(admin_uid=user_id)
    if not wechat_user:
        # TODO 错误类型需要优化
        raise ApiError('ERROR_NO_USER', error.ERROR_NO_USER)
    to_openid = wechat_user.openid
    to_nickname = wechat_user.nickname
    max_id  = dbapi.get_max_pay_to_user_id()
    str_max_id = '%04d' % max_id
    if len(str_max_id) > 4:
        str_max_id = str_max_id[-4:]
    trade_no = 'P' + str(int(time.time())) + str_max_id

    wechat_agent_id = get_top_wechat_agent_id(agent_id)
    if not wechat_agent_id:
        raise ApiError('ERROR_TOP_WECHAT_NOT_FOUND', error.ERROR_NO_USER)

    pay_to_user = dbapi.make_new_pay_to_user(wechat_agent_id, user_id, to_openid, to_nickname, trade_no,
        fee, desc, remark)
    db.session.commit()

    kwargs = {
        'openid': to_openid,
        'trade_no': trade_no,
        'money': fee - math.ceil(fee*agent_setting.withdraw_fee),   # 向上取整
        'desc': desc,
        'agent_id': agent_id
    }
    err, result = wechatpay.pay_to_user(**kwargs)
    if err:
        pay_to_user = dbapi.update_pay_to_user(pay_to_user, status=2,
            notify_res=str(err))
        db.session.add(pay_to_user)
        db.session.commit()
        raise ApiError('wechat withdraw error', error.ERROR_PAY_TO_USER_FAIL)

    payment_no = result['payment_no']
    pay_to_user = dbapi.update_pay_to_user(pay_to_user, status=1,
        notify_res=str(result), payment_no=payment_no)
    db.session.add(pay_to_user)

    wallet_id = wallet.id
    trade_type = dbapi.WALLET_TRADE_TYPE_WECHAT_WITHDRAW
    receipt = -fee
    withdrawable_receipt = -fee
    wallet_receipt = dbapi.make_new_wallet_receipt(user_id, agent_id, wallet_id,
        trade_type, receipt, withdrawable_receipt, remark, payment_no)

    balance = wallet.balance - fee
    withdrawable_balance = wallet.withdrawable_balance - fee
    wallet = dbapi.update_wallet(wallet, balance=balance, withdrawable_balance=withdrawable_balance)
    db.session.add(wallet)
    db.session.commit()

    return make_response(jsonify(reply), status_code)