示例#1
0
    def test_balance_multiple(self):
        v = Decimal("1.12")
        v1 = BalanceKit.multiple_unit(v)
        v2 = BalanceKit.divide_unit(v1)
        self.assertEqual(v, v2)

        v = Decimal("1.1211")
        d = 10000
        v1 = BalanceKit.multiple_unit(v, d)
        v2 = BalanceKit.divide_unit(v1, d)
        self.assertEqual(v, v2)
示例#2
0
    def post(self):
        """
        获取用户余额
        """
        # uid = g.user.uid
        # merchant = g.user.merchant
        # account = g.user.account
        # is_active = g.user.is_active
        # state = g.user.state
        # ac_type = g.user.ac_type
        # login_pwd = g.user.login_pwd

        balance = UserBalance.query_balance(uid=g.user.uid,
                                            merchant=g.user.merchant).first()
        return ResponseUserBalance(
            bs_data=dict(balance=BalanceKit.divide_unit(balance.balance),
                         has_trade_pwd=g.user.has_trade_pwd())).as_response()
示例#3
0
    def post(self):
        """
        转账
        判断接受转账的用户是否存在
        判断是否存在支付密码
        校验老的支付密码
        判断余额是否足够
        执行转账操作
        """

        form, error = TransferForm().request_validate()
        if error:
            return error.as_response()

        # 从全局变量中取出用户ID,参考:verify_credential
        uid = g.user.uid

        if not g.user.has_permission(UserPermissionEnum.TRANSFER):
            return UserPermissionDeniedError().as_response()

        # 判断接受转账的用户是否存在
        bind_user = UserBindInfo.query_bind(form.merchant.data,
                                            form.number.data)
        if not bind_user:
            # 未绑定账号,验证手机号码是否正确
            account = form.join_phone_number()
            if not account:
                return AccountNotExistError(message="您输入的账号不存在").as_response()
            form.number.data = account
        else:
            # 使用绑定的手机号码
            form.number.data = bind_user.account

        user_info = User.query_user(form.merchant.data,
                                    account=form.number.data)
        if not user_info:
            return AccountNotExistError(message="账号(%s)不存在" %
                                        form.number.data).as_response()

        no_transfer_limit = UserBindInfo.query_bind_by_uid(user_info.uid)
        if not no_transfer_limit and form.amount.data > TRANSFER_AMOUNT_LIMIT:
            # 非绑定用户转账限额检查
            return UserPermissionDeniedError(
                message="单次转账额度不能超过%s" % TRANSFER_AMOUNT_LIMIT).as_response()

        # 判断是否是给自己转账
        if uid == user_info.uid:
            return TransferToMeError().as_response()

        # 判断是否存在支付密码
        user = User.query_user(form.merchant.data, uid)

        if not user.trade_pwd:
            return PaymentPwdNotExistError().as_response()

        cache = UserPaymentPasswordLimitCache(uid=uid)

        # 获取支付密码输入错误次数是否达到上限
        if cache.is_limited():
            return PaymentPasswordLimitedError().as_response()

        # 校验支付密码
        flag = User.verify_payment_password(
            form.merchant.data, uid=uid, password=form.payment_password.data)

        # 交易密码校验失败
        if not flag:
            cache.incr_times()
            times = cache.get_left_times()
            return PaymentPasswordError(message=PaymentPasswordError.message.
                                        format(times)).as_response()

        # 密码校验成功 删除密码输入错误记录
        cache.delete_cache()

        # 判断余额是否足够
        balance = UserBalance.query_balance(uid=uid,
                                            merchant=g.user.merchant).first()
        if BalanceKit.divide_unit(balance.balance) < form.amount.data:
            return AccountBalanceInsufficientError().as_response()

        # 执行转账动作
        flag, msg = UserBalanceEvent.transfer(from_user=g.user,
                                              to_user=user_info,
                                              merchant=form.merchant.data,
                                              amount=form.amount.data,
                                              comment=form.comment.data)

        # 设置失败的情况
        if not flag:
            return NoSourceError().as_response()

        return ResponseSuccess().as_response()