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

    user_id = current_user.id
    agent_id = current_user.agent_id
    wallet = dbapi.get_wallet(user_id=user_id, agent_id=agent_id)
    wechat_user = dbapi.get_wechat_user(admin_uid=user_id)
    nickname = ''
    if not wechat_user:
        nickname = '未绑定微信'
    else:
        nickname = wechat_user.nickname

    total_fee_sum = dbapi.get_pay_to_user(user_id=user_id, total=True)
    wallet = dbapi.get_wallet(user_id=user_id, agent_id=agent_id)
    receipt_sum = dbapi.get_agent_wallet_receipt_sum(wallet.id)

    reply['data'] = {
        'balance': int(wallet.balance),
        'withdrawable_balance': int(wallet.withdrawable_balance),
        'nickname': nickname,
        'total_fee_sum': total_fee_sum,
        'receipt_sum': receipt_sum,
    }
    return make_response(jsonify(reply), status_code)
Beispiel #2
0
 def wrapper(*args, **kws):
     # session['openid'] = 'oEMOtjoiv9HBQiAC7x7BnSDAUuVw'
     if 'openid' not in session:
         raise ApiError('do not login', error.ERROR_NO_LOGIN)
     wechat_user = dbapi.get_wechat_user(openid=session['openid'])
     if not wechat_user:
         raise ApiError('do not login', error.ERROR_NO_LOGIN)
     g.wechat_user = wechat_user
     return func(*args, **kws)
Beispiel #3
0
 def wrapper(*args, **kws):
     if 'has_openid' in session:
         session.pop('has_openid')
     # session['openid'] = 'oEMOtjoiv9HBQiAC7x7BnSDAUuVw'
     user_agent = request.headers.get('User-Agent')
     if 'MicroMessenger' in user_agent:
         if 'openid' not in session:
             raise ApiError('do not login', error.ERROR_NO_LOGIN)
         wechat_user = dbapi.get_wechat_user(openid=session['openid'])
         if not wechat_user:
             raise ApiError('do not login', error.ERROR_NO_LOGIN)
         g.wechat_user = wechat_user
         g.oauth_user = wechat_user
     else:
         if 'ali_user_id' not in session:
             raise ApiError('do not login', error.ERROR_NO_LOGIN)
         ali_user = dbapi.get_ali_user(ali_user_id=session['ali_user_id'])
         if not ali_user:
             raise ApiError('do not login', error.ERROR_NO_LOGIN)
         g.oauth_user = ali_user
         g.wechat_user = None
     return func(*args, **kws)
Beispiel #4
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)
Beispiel #5
0
def bindwechat(bind_id):
    code  = request.args.get('code')
    state = request.args.get('state')

    wechat_bind = dbapi.get_wechat_bind(id=int(bind_id))
    now = datetime.now()
    if not wechat_bind:
        return redirect(url_for('main.fail'))
    elif wechat_bind.expires_at < now:
        return render_template('fail.html', message='二维码已超时')

    bind_admin_uid = wechat_bind.admin_uid

    admin_agent = dbapi.get_agent(user_id=bind_admin_uid)
    agent_id = admin_agent.id


    # 第一次应该返回null
    wechat_config = dbapi.get_wechat_config(agent_id=int(agent_id))
    print("WechatConfig: %s" % wechat_config)
    appid       = wechat_config.appid
    appsecret   = wechat_config.appsecret

    print("WechatConfig here!")

    if code and state:

        succ, res = wechatsdk.get_auth_user_info(appid, appsecret, code)
        if succ:
            wechat_user = dbapi.get_wechat_user(openid=res['openid'])
            if not wechat_user:
                # 1. 创建用户
                user = dbapi.make_new_user(wechat_body=res)
                db.session.commit()
                if not user:
                    raise ApiError('login error', error.ERROR_INSERT_USER)
                # 2. 将旧的关联清除
                old_wechat_user = dbapi.get_wechat_user(admin_uid=bind_admin_uid)
                if old_wechat_user:
                    old_wechat_user = dbapi.update_wechat_user(old_wechat_user, admin_uid=0)
                    db.session.add(old_wechat_user)
                # 3. 创建新的关联
                res['user_id'] = user.id
                res['admin_uid'] = bind_admin_uid
                wechat_user = dbapi.make_new_wechat_user(res)
                db.session.commit()
                if not wechat_user:
                    raise ApiError('bindwechat error', error.ERROR_INSERT_WECHAT_USER)
            else:
                # 1. 将旧的关联清除
                old_wechat_user = dbapi.get_wechat_user(admin_uid=bind_admin_uid)
                if old_wechat_user:
                    old_wechat_user = dbapi.update_wechat_user(old_wechat_user, admin_uid=0)
                    db.session.add(old_wechat_user)
                # 2. 创建新的关联
                wechat_user = dbapi.update_wechat_user(wechat_user, admin_uid=bind_admin_uid)
                db.session.add(wechat_user)
                db.session.commit()

            # 绑定成功
            wechat_bind = dbapi.update_wechat_bind(wechat_bind, status=1)
            db.session.commit()

            return redirect(url_for('main.succ'))
        else:
            dbg(str(res))
            return redirect(url_for('main.fail'))

    elif not code and not state:
        redirect_uri = wechat_config.redirect_bind_url + bind_id
        url          = wechatsdk.gen_auth_url(appid, redirect_uri, state)
        dbg('url: %s' % url)
        return redirect(url)
Beispiel #6
0
def login(agent_id):
    dbg('login')
    code  = request.args.get('code')
    state = request.args.get('state')

    wechat_config = dbapi.get_wechat_config(agent_id=int(agent_id))
    appid       = wechat_config.appid
    appsecret   = wechat_config.appsecret

    dbg((appid, appsecret))

    if code and state:

        # succ, res = wechatsdk.get_auth_access_token(appid, appsecret, code)
        succ, res = wechatsdk.get_auth_user_info(appid, appsecret, code)
        if succ:
            user_info = res
            openid = res['openid']
            wechat_user = dbapi.get_wechat_user(openid=res['openid'])
            if not wechat_user:
                # # 1. 获取用户信息
                # user_info = wechatsdk.get_wechat_user_info(appid, appsecret, openid)
                # if not user_info:
                #     return 'error'

                # dbg(user_info)

                # 1. 创建用户
                user = dbapi.make_new_user(wechat_body=user_info)
                db.session.commit()
                if not user:
                    raise ApiError('login error', error.ERROR_INSERT_USER)

                # 2. 创建微信用户
                user_info['user_id'] = user.id
                user_info['admin_uid'] = 0    # 普通用户都为0
                wechat_user = dbapi.make_new_wechat_user(user_info)
                db.session.commit()
                if not wechat_user:
                    raise ApiError('login error', error.ERROR_INSERT_WECHAT_USER)
            else:
                user = dbapi.get_user(user_id=wechat_user.user_id)
                if user.nickname == "":
                    wechat_user = dbapi.update_wechat_user(wechat_user, wechat_body=user_info)
                    user = dbapi.update_user(user, wechat_body=user_info)
                    db.session.commit()

            session['openid'] = res.get('openid')
            session['has_openid'] = 1
            next_url = state
            return redirect(next_url)
        else:
            dbg(str(res))
            return 'error'

    elif not code and not state:
        redirect_uri = wechat_config.redirecturl + agent_id
        next_url     = request.args.get('next_url') or session['next_url']
        state        = next_url
        url          = wechatsdk.gen_auth_url(appid, redirect_uri, state)
        dbg('url: %s' % url)
        return redirect(url)