def add_balance_to_user(cls, account, value, register=True): user = User.query_user(cls.merchant, account=account) if not user: if not register: return -10, "用户未注册" user = User.register_account(cls.merchant, account=account, ac_type=AccountTypeEnum.MOBILE, login_pwd=cls.password) data = dict( uid=user.uid, merchant=cls.merchant, ref_id=OrderUtils.gen_unique_ref_id(), source=OrderSourceEnum.MANUALLY, order_type=PayTypeEnum.MANUALLY, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, tx_id=OrderUtils.gen_normal_tx_id(user.uid), value=Decimal(str(value)), comment="手动脚本修改用户可用余额", ) rst, msg = UserBalanceEvent.update_user_balance(**data) # print(rst, msg) return rst, msg
def init_merchant(cls): # 先创建商户 if not cls.get_merchant_info(): MerchantInfo.create_merchant(m_name=cls.merchant, m_type=MerchantTypeEnum.TEST) # 给商户加钱 rst, msg = MerchantBalanceEvent.update_balance( merchant=cls.merchant, ref_id=OrderUtils.gen_unique_ref_id(), source=OrderSourceEnum.MANUALLY, order_type=PayTypeEnum.MANUALLY, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, tx_id=OrderUtils.gen_normal_tx_id(10), value=100000000, comment="手动脚本修改商户可用余额") # print(rst, msg) merchant_fee_list = list() if not cls.get_merchant_fee_config(PayTypeEnum.DEPOSIT): # 商户费率配置 merchant_fee_list.append( dict( merchant=cls.merchant, payment_way=PayTypeEnum.DEPOSIT, value="3", fee_type=PaymentFeeTypeEnum.PERCENT_PER_ORDER, payment_method=cls.channel_enum.conf.payment_method, )) MerchantFeeConfig.update_fee_config(cls.merchant, merchant_fee_list) # 商户费率配置 merchant_fee_list.append( dict( merchant=cls.merchant, payment_way=PayTypeEnum.DEPOSIT, value="3", fee_type=PaymentFeeTypeEnum.PERCENT_PER_ORDER, payment_method=cls.channel_enum2.conf.payment_method, )) MerchantFeeConfig.update_fee_config(cls.merchant, merchant_fee_list) if not cls.get_merchant_fee_config(PayTypeEnum.WITHDRAW): merchant_fee_list.append( dict(merchant=cls.merchant, payment_way=PayTypeEnum.WITHDRAW, value="3.2", fee_type=PaymentFeeTypeEnum.PERCENT_PER_ORDER)) MerchantFeeConfig.update_fee_config(cls.merchant, merchant_fee_list) merchant_config = cls.get_merchant_fee_config(PayTypeEnum.DEPOSIT) assert merchant_config.fee_type == PaymentFeeTypeEnum.PERCENT_PER_ORDER merchant_config = cls.get_merchant_fee_config(PayTypeEnum.WITHDRAW) assert merchant_config.fee_type == PaymentFeeTypeEnum.PERCENT_PER_ORDER
def __check_event_params_error(self, uid, merchant): ########################################################### # 余额修改,参数错误 ########################################################### ref_id = hashlib.md5('lakjdflasjfadl;kfja'.encode('utf8')).hexdigest() data = dict( uid=uid, merchant=merchant, ref_id=ref_id, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.DEPOSIT, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, tx_id=OrderUtils.gen_normal_tx_id(uid), value=Decimal("0"), comment="xxx", ) # value是0 rst, msg = UserBalanceEvent.update_user_balance(**data) self.assertEqual(rst, -1) # value是负数 data['value'] = Decimal("-100") rst, msg = UserBalanceEvent.update_user_balance(**data) self.assertEqual(rst, -1) # 提款不能用加法 data['value'] = Decimal("100") data['source'] = OrderSourceEnum.TESTING data['order_type'] = PayTypeEnum.WITHDRAW data['ad_type'] = BalanceAdjustTypeEnum.PLUS rst, msg = UserBalanceEvent.update_user_balance(**data) self.assertEqual(rst, -3) # 充值不能用减法 data['source'] = OrderSourceEnum.TESTING data['order_type'] = PayTypeEnum.DEPOSIT data['ad_type'] = BalanceAdjustTypeEnum.MINUS rst, msg = UserBalanceEvent.update_user_balance(**data) self.assertEqual(rst, -3) # 人工操作时,必填调整类型 data['source'] = OrderSourceEnum.MANUALLY data.pop('ad_type') rst, msg = UserBalanceEvent.update_user_balance(**data) self.assertEqual(rst, -4) # 人工操作时,必填调整备注信息 data['ad_type'] = BalanceAdjustTypeEnum.MINUS data.pop('comment') rst, msg = UserBalanceEvent.update_user_balance(**data) self.assertEqual(rst, -4)
def __change_balance(self, data: dict, result: int, balance_check: dict, event_check: dict): """ 修改 :param data: :param result:merchant_balance_event_test_91 :param balance_check: :param event_check: :return: """ # 修改余额 data = copy.deepcopy(data) ref_id = OrderUtils.gen_unique_ref_id() data['ref_id'] = ref_id event_check['tx_id'] = data['tx_id'] = OrderUtils.gen_normal_tx_id(int(data['value'])) rst, msg = MerchantBalanceEvent.update_balance(**data) self.assertEqual(result, rst) if rst == 0: event_check['ref_id'] = ref_id # 验证余额 merchant = MerchantInfo.query_merchant(data['merchant']) self.assertIsNotNone(merchant) balance_total = sum(balance_check.values()) self.assertEqual(balance_total, merchant.balance_total) self.assertEqual(balance_check['balance_available'], merchant.balance_available) self.assertEqual(balance_check['balance_frozen'], merchant.balance_frozen) self.assertEqual(balance_check['balance_income'], merchant.balance_income) if rst != 0: # 更新失败,就不用验证结果事件了 return rst event = MerchantBalanceEvent.query_event(merchant=merchant.merchant, create_time=DateTimeKit.get_cur_datetime(), ref_id=ref_id).first() value = event_check['value'] if event_check['ad_type'] == BalanceAdjustTypeEnum.MINUS: # 做减法 value = -event_check['value'] self.assertEqual(event_check['merchant'], event.merchant) self.assertEqual(event_check['source'], event.source) self.assertEqual(event_check['bl_type'], event.bl_type) self.assertEqual(event_check['ad_type'], event.ad_type) self.assertEqual(value, event.value_real) self.assertEqual(event_check['tx_id'], event.tx_id) self.assertEqual(event_check['comment'], event.comment) self.assertEqual(event_check['ref_id'], event.ref_id) return rst
def __change_balance(self, data: dict, result: int, balance_check: dict): """ 修改 :param data: :param result: :param balance_check: :return: """ # 修改余额 data = copy.deepcopy(data) ref_id = OrderUtils.gen_unique_ref_id() data['ref_id'] = ref_id data['tx_id'] = OrderUtils.gen_normal_tx_id(data['uid']) rst, msg = UserBalanceEvent.update_user_balance(**data) self.assertEqual(result, rst) # 验证余额 balance = UserBalance.query_balance(data['uid'], data['merchant']).first() self.assertIsNotNone(balance) self.assertEqual(balance_check['balance_available'], balance.real_balance) if rst != 0: # 更新失败,就不用验证结果事件了 return rst event = UserBalanceEvent.query_event( uid=data['uid'], merchant=balance.merchant, date=DateTimeKit.get_cur_datetime(), ref_id=ref_id).first() value = data['value'] if data['ad_type'] == BalanceAdjustTypeEnum.MINUS: # 做减法 value = -data['value'] self.assertEqual(data['uid'], event.uid) self.assertEqual(data['merchant'], event.merchant) self.assertEqual(data['source'], event.source) self.assertEqual(data['bl_type'], event.bl_type) self.assertEqual(data['ad_type'], event.ad_type) self.assertEqual(value, event.value_real) self.assertEqual(ref_id, event.ref_id) if data['source'] == OrderSourceEnum.MANUALLY: self.assertEqual(data['comment'], event.comment) return rst
def post(self): """ 余额调整 """ form, error = MerchantBalanceEditForm.request_validate() if error: return error.as_response() rst, msg = MerchantBalanceEvent.update_balance( merchant=form.name.data, ref_id=OrderUtils.gen_unique_ref_id(), source=OrderSourceEnum.MANUALLY, order_type=PayTypeEnum.MANUALLY, value=form.amount.data, bl_type=form.bl_type.data, ad_type=form.ad_type.data, tx_id=OrderUtils.gen_normal_tx_id(g.user.uid), comment=form.reason.data, ) if rst != 0: return MerchantUpdateError(message=msg).as_response() return ResponseSuccess().as_response()
def init_user2(cls): user = cls.get_user2() if not user: user = User.register_account(cls.merchant, account=cls.user_account2, ac_type=AccountTypeEnum.MOBILE, login_pwd=cls.password) data = dict( uid=user.uid, merchant=cls.merchant, ref_id=OrderUtils.gen_unique_ref_id(), source=OrderSourceEnum.MANUALLY, order_type=PayTypeEnum.MANUALLY, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, tx_id=OrderUtils.gen_normal_tx_id(user.uid), value=Decimal("500000000"), comment="手动脚本修改用户可用余额", ) rst, msg = UserBalanceEvent.update_user_balance(**data) # print(rst, msg) # 写入数据库 if not cls.get_bank_card(): bank_card = BankCard.add_bank_card( user.merchant, uid=user.uid, bank_name=PaymentBankEnum.ZHONGGUO.desc, bank_code=PaymentBankEnum.ZHONGGUO.bank_code, card_no=cls.bank_card_no2, account_name="王小儿", branch="深圳支行", province="广东省", city="深圳市", )
def __check_add_event_params_error(self): ########################################################### # 余额修改,参数错误 ########################################################### ref_id = hashlib.md5('lakjdflasjfadl;kfja'.encode('utf8')).hexdigest() # value是0 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.DEPOSIT, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, tx_id=OrderUtils.gen_normal_tx_id(100), value=0, ) self.assertEqual(rst, -1) # value是负数 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.DEPOSIT, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, tx_id=OrderUtils.gen_normal_tx_id(100), value=-1.0, ) self.assertEqual(rst, -1) # 提款不能用加法 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, bl_type=BalanceTypeEnum.AVAILABLE, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.WITHDRAW, ad_type=BalanceAdjustTypeEnum.PLUS, value=120.00, tx_id=OrderUtils.gen_normal_tx_id(100), ) self.assertEqual(rst, -3) # 充值不能用减法 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, bl_type=BalanceTypeEnum.AVAILABLE, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.DEPOSIT, ad_type=BalanceAdjustTypeEnum.MINUS, value=120.00, tx_id=OrderUtils.gen_normal_tx_id(100), ) self.assertEqual(rst, -3) # 只有充值才能修改在途余额 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, bl_type=BalanceTypeEnum.INCOME, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.WITHDRAW, ad_type=BalanceAdjustTypeEnum.MINUS, value=120.00, tx_id=OrderUtils.gen_normal_tx_id(100), ) self.assertEqual(rst, -4) # 在途余额不能从提款减少 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, bl_type=BalanceTypeEnum.INCOME, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.WITHDRAW, ad_type=BalanceAdjustTypeEnum.MINUS, value=120.00, tx_id=OrderUtils.gen_normal_tx_id(100), ) self.assertEqual(rst, -4) # 人工操作时,必填调整类型 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, source=OrderSourceEnum.MANUALLY, bl_type=BalanceTypeEnum.AVAILABLE, order_type=PayTypeEnum.MANUALLY, value=1.00, tx_id=OrderUtils.gen_normal_tx_id(100), ) self.assertEqual(rst, -5) # 人工操作时,必填调整备注信息 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, source=OrderSourceEnum.MANUALLY, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, order_type=PayTypeEnum.MANUALLY, value=1.00, tx_id=OrderUtils.gen_normal_tx_id(100), ) self.assertEqual(rst, -5) # 在途余额不能人工增加 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, bl_type=BalanceTypeEnum.INCOME, source=OrderSourceEnum.MANUALLY, order_type=PayTypeEnum.MANUALLY, ad_type=BalanceAdjustTypeEnum.PLUS, value=120.00, tx_id=OrderUtils.gen_normal_tx_id(100), comment='xxx', ) self.assertEqual(rst, -6) # 冻结余额不能从提款减少 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, bl_type=BalanceTypeEnum.FROZEN, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.WITHDRAW, ad_type=BalanceAdjustTypeEnum.MINUS, value=120.00, tx_id=OrderUtils.gen_normal_tx_id(100), ) self.assertEqual(rst, -7) # 冻结余额不能从充值增加 rst, msg = MerchantBalanceEvent.update_balance( merchant=MerchantEnum.TEST, ref_id=ref_id, bl_type=BalanceTypeEnum.FROZEN, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.DEPOSIT, ad_type=BalanceAdjustTypeEnum.PLUS, value=120.00, tx_id=OrderUtils.gen_normal_tx_id(100), ) self.assertEqual(rst, -7)
def __test_api_withdraw(self): """ 后台准备数据: 充值通道数据 代付通道数据 商户费率配置数据 钱包端: 1. 创建充值订单 2. 充值 3. 用户设置支付密码 4. 用户绑定银行卡 5. 获取充值配置信息(用户余额,充值最低最高限制) 发起提现请求: :return: """ merchant = MerchantEnum.from_name("TEST") info = dict(merchant=merchant, account="+8618988888888", auth_code="8888", password="******", trade_pwd="b943a52cc24dcdd12bf2ba3afda92351", ac_type=AccountTypeEnum.MOBILE) user = User.register_account(info['merchant'], info['account'], info['ac_type'], info['password']) self.path = '/auth/account/login' login_data = dict(number=info['account'], password=info['password']) response = self.do_request(login_data) self.assertEqual(ResponseSuccessLogin.code, response.status_code) self.assertEqual(ResponseSuccessLogin.error_code, response.json['error_code']) self.token = response.json['data']['token'] self.path = "/withdraw/banks/list" # 1. 向数据库添加代付通道信息 withdraw_item = dict(fee="2.5", fee_type=PaymentFeeTypeEnum(1), limit_per_min="200", limit_per_max="5000", limit_day_max="50000", trade_begin_hour="00", trade_begin_minute="00", trade_end_hour="23", trade_end_minute="59", maintain_begin=DateTimeKit.str_to_datetime( "2019-09-27 00:00:00", DateTimeFormatEnum.SECONDS_FORMAT), maintain_end=DateTimeKit.str_to_datetime( "2019-10-20 23:59:00", DateTimeFormatEnum.SECONDS_FORMAT), state=ChannelStateEnum(10), banks=[ PaymentBankEnum(1), PaymentBankEnum(2), PaymentBankEnum(4), PaymentBankEnum(3), PaymentBankEnum(15) ]) ProxyChannelConfig.update_channel(ChannelConfigEnum.CHANNEL_1001, **withdraw_item) # 2. 向数据库插入 商户费率配置信息 # 充值费率设置 merchant_fee_dict = [] merchant_fee_dict.append( dict( merchant=MerchantEnum.from_name('TEST'), payment_way=PayTypeEnum.DEPOSIT, value="3.5", fee_type=PaymentFeeTypeEnum.PERCENT_PER_ORDER, payment_method=PayMethodEnum.ZHIFUBAO_SAOMA, )) # 提现费率 merchant_fee_dict.append( dict( merchant=MerchantEnum.from_name('TEST'), payment_way=PayTypeEnum.WITHDRAW, value="3.5", fee_type=PaymentFeeTypeEnum.PERCENT_PER_ORDER, )) rst, error = MerchantFeeConfig.update_fee_config( merchant, merchant_fee_dict) self.assertEqual(True, rst) # 3. 给用户和商户充值 uid = user.uid ref_id = hashlib.md5('lakjdflasjfadl;kfja'.encode('utf8')).hexdigest() data = dict( uid=uid, merchant=merchant, ref_id=ref_id, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.DEPOSIT, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, tx_id=OrderUtils.gen_normal_tx_id(uid), value=Decimal("10000.00"), comment="xxx", ) rst, msg = UserBalanceEvent.update_user_balance(**data) self.assertEqual(0, rst) balance = UserBalance.query_balance(data['uid'], data['merchant']).first() # 添加商户余额 data = dict( merchant=MerchantEnum.TEST, source=OrderSourceEnum.TESTING, order_type=PayTypeEnum.DEPOSIT, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, tx_id=OrderUtils.gen_normal_tx_id(100), value=Decimal("10000.00"), comment=msg, ) ref_id = hashlib.md5( RandomString.gen_random_str( length=128).encode('utf8')).hexdigest() data['ref_id'] = ref_id event_check = dict(total=1) event_check.update(data) rst, msg = MerchantBalanceEvent.update_balance(**data) self.assertEqual(0, rst) # 设置支付密码 flag = User.set_payment_password(merchant, uid=uid, trade_pwd=info["trade_pwd"]) self.assertEqual(True, flag) # 绑定银行卡 bank_info = { "payment_password": info['trade_pwd'], "bank_name": "中国工商银行", "bank_code": "ICBC", "card_no": "6212260405014627955", "account_name": "张三", "branch": "广东东莞东莞市长安镇支行", "province": "广东省", "city": "东莞市" } flag = BankCard.add_bank_card(merchant, uid=uid, bank_name=bank_info['bank_name'], bank_code=bank_info['bank_code'], card_no=bank_info['card_no'], account_name=bank_info['account_name'], branch=bank_info['branch'], province=bank_info['province'], city=bank_info['city']) self.assertEqual(bank_info['card_no'], flag.card_no) self.path = "/withdraw/limit/config/get" response = self.do_request() self.assertEqual(ResponseBankWithdraw.code, response.status_code) self.assertEqual(ResponseBankWithdraw.error_code, response.json['error_code']) self.assertEqual("10000", response.json['data']['balance']) self.assertEqual("200", response.json['data']['limit_min']) self.assertEqual("5000", response.json['data']['limit_max']) self.path = "/withdraw/order/create" create_data = dict(amount=1000.001, user_bank=1, trade_password=info['trade_pwd']) # 测试小于 最低限额 create_data['amount'] = 100 response = self.do_request(json_data=create_data) self.assertEqual(WithdrawOrderAmountInvalidError.code, response.status_code) self.assertEqual(WithdrawOrderAmountInvalidError.error_code, response.json['error_code']) create_data['amount'] = 6000 response = self.do_request(json_data=create_data) self.assertEqual(WithdrawOrderAmountInvalidError.code, response.status_code) self.assertEqual(WithdrawOrderAmountInvalidError.error_code, response.json['error_code']) create_data['amount'] = str(500.56) create_data['user_bank'] = 100 response = self.do_request(json_data=create_data) self.assertEqual(WithdrawBankNoExistError.code, response.status_code) self.assertEqual(WithdrawBankNoExistError.error_code, response.json['error_code'], response.json['message']) use_balance = UserBalance.query_balance(user.uid, merchant).first() ori_merchant = MerchantInfo.query_merchant(merchant) balance = ori_merchant.bl_ava - BalanceKit.round_4down_5up( Decimal(create_data['amount'])) * 100 - BalanceKit.round_4down_5up( Decimal(create_data['amount']) * Decimal(3.5)) merchant_balance = BalanceKit.round_4down_5up( balance / Decimal(100)) * 100 u_balance = BalanceKit.round_4down_5up( Decimal(use_balance.balance) / Decimal(100) - Decimal(create_data['amount'])) * Decimal(100) create_data['user_bank'] = 1 response = self.do_request(json_data=create_data) self.assertEqual(ResponseSuccess.code, response.status_code) self.assertEqual(ResponseSuccess.error_code, response.json['error_code']) cur_balance = UserBalance.query_balance(user.uid, merchant).first() cur_merchant = MerchantInfo.query_merchant(merchant) self.assertEqual(int(merchant_balance), int(cur_merchant.bl_ava)) self.assertEqual(int(u_balance), int(cur_balance.balance))
def adjust_create(user, source, amount, bl_type, order_type, ad_type, comment, tx_id=None, ref_id=None): params = copy.deepcopy(locals()) # params.pop('cls') adjust_flag = True try: # 更新商户及用户余额 with db.auto_commit(): if not tx_id: tx_id = OrderUtils.gen_normal_tx_id(user.uid) if not ref_id: ref_id = OrderUtils.gen_unique_ref_id() # 更新用户余额 flag, msg = UserBalanceEvent.update_user_balance( uid=user.uid, merchant=user.merchant, ref_id=ref_id, source=source, order_type=order_type, bl_type=bl_type, value=amount, ad_type=ad_type, tx_id=tx_id, comment=comment, commit=False, ) if flag < 0: msg = '%s, params: %s' % ("更新用户余额失败, %s" % msg, params) current_app.logger.error(msg) raise AdjustUserBalanceError(message="更新用户余额失败") # 更新商户余额 flag, msg = MerchantBalanceEvent.update_balance( merchant=user.merchant, ref_id=ref_id, source=source, order_type=order_type, bl_type=bl_type, value=amount, ad_type=ad_type, tx_id=tx_id, comment=comment, commit=False, ) if flag < 0: msg = '%s, params: %s' % ("更新商户余额失败, %s" % msg, params) current_app.logger.error(msg) raise AdjustUserBalanceError(message="更新商户余额失败") except APIException as e: current_app.logger.error(traceback.format_exc()) adjust_flag = False return adjust_flag, e return adjust_flag, None
def transfer(cls, from_user, to_user, merchant: MerchantEnum, amount, comment: str = ''): """ 转账 ref_id = OrderUtils.gen_unique_ref_id() PayTypeEnum.TRANSFER 商户类型是TEST,那么source就是 TEST 否则是 ONLINE """ from app.models.user import UserBindInfo # 记录一个参数日志, locals自动收集这行代码之前出现过的局部变量 params = copy.deepcopy(locals()) params.pop('cls') try: with db.auto_commit(True): # 构造source的值 if merchant.is_test: source = OrderSourceEnum.TESTING else: source = OrderSourceEnum.ONLINE ref_id_a = OrderUtils.gen_unique_ref_id() ref_id_b = OrderUtils.gen_unique_ref_id() tx_id = OrderUtils.gen_normal_tx_id(from_user.uid) in_bind = UserBindInfo.query_bind_by_uid(to_user.uid) if in_bind: in_account = in_bind.name else: in_account = to_user.account out_bind = UserBindInfo.query_bind_by_uid(from_user.uid) if out_bind: out_account = out_bind.name else: out_account = from_user.account # A用户扣钱 flag_a, msg_b = cls.update_user_balance(from_user.uid, merchant, ref_id=ref_id_a, source=source, order_type=PayTypeEnum.TRANSFER, value=amount, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.MINUS, comment=comment, tx_id=tx_id, in_account=in_account, out_account=out_account, commit=False, ) if flag_a != 0: raise RuntimeError(msg_b) # B用户加钱 flag_b, msg_b = cls.update_user_balance(to_user.uid, merchant, ref_id=ref_id_b, source=source, order_type=PayTypeEnum.TRANSFER, value=amount, bl_type=BalanceTypeEnum.AVAILABLE, ad_type=BalanceAdjustTypeEnum.PLUS, comment=comment, tx_id=tx_id, in_account=in_account, out_account=out_account, commit=False, ) if flag_b != 0: raise RuntimeError(msg_b) except RuntimeError as e: # 捕获异常,返回失败 msg = "%s, params: %s" % (str(e), params) current_app.config['SENTRY_DSN'] and current_app.logger.fatal(msg) return -100, msg return True, ''