def top_up(): ''' 卡充值接口 :return: ''' data = json.loads(request.form.get('data')) user_id = g.user_id card_no = data.get('card_no') top_money = data.get('top_money') if not check_float(top_money): results = {"code": RET.SERVERERROR, "msg": "充值金额不能为小数!"} return jsonify(results) balance = SqlData().search_user_field('balance', user_id) if float(top_money) > balance: results = {"code": RET.SERVERERROR, "msg": "账户余额不足!"} return jsonify(results) money = str(int(top_money) * 100) resp = QuanQiuFu().trans_account_recharge(card_no, money) resp_code = resp.get('resp_code') if resp_code == '0000': try: detail = resp.get('response_detail') remain = int(detail.get('balance')) / 100 SqlData().update_card_remain('remain', remain, card_no) except Exception as e: logging.error('充值后更新卡余额失败!' + str(e)) top_money = int(top_money) # 查询账户操作前的账户余额 before_balance = SqlData().search_user_field('balance', user_id) # 计算要扣除多少钱 do_money = top_money - top_money * 2 # 直接更新账户余额,不计算理论余额,用sql更新本次操作费用 SqlData().update_balance(do_money, user_id) # 查询扣除后的余额 balance = SqlData().search_user_field('balance', user_id) n_time = xianzai_time() SqlData().insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.TOP_UP, 1, card_no, float(top_money), 0, before_balance, balance, user_id) return jsonify({"code": RET.OK, "msg": "充值成功!请刷新界面!"}) else: resp_msg = resp.get('resp_msg') s = '充值卡余额失败,状态码: ' + resp_code + ',信息: ' + resp_msg logging.error(s) return jsonify({"code": RET.SERVERERROR, "msg": "充值失败!请联系服务商解决!"})
def create_card(): if request.method == 'GET': user_id = g.user_id min_top = SqlData.search_user_field('min_top', user_id) create_price = SqlData.search_user_field('create_price', user_id) context = dict() context['min_top'] = min_top context['create_price'] = create_price return render_template('user/create_card.html', **context) if request.method == 'POST': # 判断是否是子账号用户 vice_id = g.vice_id if vice_id: auth_dict = RedisTool.hash_get('svb_vice_auth', vice_id) if auth_dict is None: return jsonify({ 'code': RET.SERVERERROR, 'msg': '抱歉您没有权限执行此操作!' }) c_card = auth_dict.get('c_card') if c_card == 'F': return jsonify({ 'code': RET.SERVERERROR, 'msg': '抱歉您没有权限执行此操作!' }) data = json.loads(request.form.get('data')) top_money = data.get('top_money') label = data.get('label') card_num = data.get('card_num') user_id = g.user_id user_data = SqlData.search_user_index(user_id) create_price = user_data.get('create_card') min_top = user_data.get('min_top') max_top = user_data.get('max_top') balance = user_data.get('balance') card_num = int(card_num) if card_num > 10: results = {"code": RET.SERVERERROR, "msg": "批量开卡数量不得超过10张!"} return jsonify(results) if not check_float(top_money): results = {"code": RET.SERVERERROR, "msg": "充值金额不能为小数!"} return jsonify(results) # 本次开卡需要的费用,计算余额是否充足 money_all = (int(top_money) + create_price) * card_num if money_all > balance: results = { "code": RET.SERVERERROR, "msg": "本次消费金额:" + str(money_all) + ",账号余额不足!" } return jsonify(results) # 计算充值金额是否在允许范围 # if not min_top <= int(top_money) <= max_top: if not min_top <= int(top_money): results = {"code": RET.SERVERERROR, "msg": "充值金额不在允许范围内!"} return jsonify(results) # 该处修改开卡 try: data_list = [] cents = int(top_money) * 100 for i in range(card_num): data = svb.create_card(cents) if data: # 开卡费用 n_time = xianzai_time() card_number = data.get('card_number') cvc = data.get('cvc') expiry = data.get('expiry') card_id = data.get('card_id') last4 = data.get('last4') valid_starting_on = data.get('valid_start_on') valid_ending_on = data.get('valid_end_on') # 插入卡信息 SqlData.insert_card(card_number, cvc, expiry, card_id, last4, valid_starting_on, valid_ending_on, label, 'T', int(top_money), user_id) # 扣去开卡费用 before_balance = SqlData.search_user_field( 'balance', user_id) create_price_do_money = float( create_price) - float(create_price) * 2 SqlData.update_balance(create_price_do_money, user_id) balance = SqlData.search_user_field("balance", user_id) # balance = before_balance - create_price SqlData.insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.CREATE_CARD, card_number, create_price, before_balance, balance, user_id) # 扣去充值费用 before_balance = SqlData.search_user_field( 'balance', user_id) top_money = int(top_money) top_money_do_money = top_money - top_money * 2 SqlData.update_balance(top_money_do_money, user_id) balance = SqlData.search_user_field("balance", user_id) n_time = xianzai_time() SqlData.insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.TOP_UP, card_number, top_money, before_balance, balance, user_id) data_list.append(card_num) return jsonify({ "code": RET.OK, "msg": "成功开卡" + str(len(data_list)) + "张! 账户余额为: $" + str(balance) }) except Exception as e: logging.error(str(e)) return jsonify({ "code": RET.SERVERERROR, "msg": "网络繁忙, 开卡失败, 请稍后再试" })
def top_up(): # 判断是否是子账号用户 vice_id = g.vice_id if vice_id: auth_dict = RedisTool.hash_get('svb_vice_auth', vice_id) if auth_dict is None: return render_template('user/no_auth.html') c_card = auth_dict.get('top_up') if c_card == 'F': return render_template('user/no_auth.html') if request.method == 'GET': card_number = request.args.get('card_number') context = dict() context['card_number'] = card_number return render_template('user/card_top.html', **context) if request.method == 'POST': data = json.loads(request.form.get('data')) user_id = g.user_id card_number = request.args.get('card_number') top_money = data.get('top_money') user_data = SqlData.search_user_index(user_id) before_balance = user_data.get('balance') if not check_float(top_money): results = {"code": RET.SERVERERROR, "msg": "充值金额不能为小数!"} return jsonify(results) if int(top_money) > before_balance: results = { "code": RET.SERVERERROR, "msg": "本次消费金额:" + str(top_money) + ",账号余额不足!" } return jsonify(results) card_status = SqlData.search_one_card_status(card_number) if not card_status: return jsonify({'code': RET.SERVERERROR, 'msg': "该卡已注销,不支持此操作!"}) card_id = SqlData.search_card_field('card_id', card_number) card_detail = svb.card_detail(card_id) if card_detail: available_balance = card_detail.get('data').get( 'available_balance') now_balance = available_balance + int(top_money) * 100 res = svb.update_card(card_id, now_balance) if res: top_money = int(top_money) top_money_do_money = top_money - top_money * 2 SqlData.update_balance(top_money_do_money, user_id) n_time = xianzai_time() balance = SqlData.search_user_field('balance', user_id) SqlData.insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.TOP_UP, card_number, top_money, before_balance, balance, user_id) return jsonify({ 'code': RET.OK, 'msg': '充值成功!账户余额:$ ' + str(balance) + ",卡余额:$ " + str(now_balance / 100) }) return jsonify({'code': RET.SERVERERROR, 'msg': '网络繁忙,请稍后重试!'}) else: return jsonify({'code': RET.SERVERERROR, 'msg': '网络繁忙,请稍后重试!'})
def xls_top(): ''' 表格批量充值接口 :return: ''' if request.method == 'POST': try: file = request.files.get('file') filename = file.filename file_path = save_file(file, filename, DIR_PATH.XLS_PATH) data = xlrd.open_workbook(file_path, encoding_override='utf-8') table = data.sheets()[0] nrows = table.nrows # 行数 ncols = table.ncols # 列数 row_list = [table.row_values(i) for i in range(0, nrows)] # 所有行的数据 col_list = [table.col_values(i) for i in range(0, ncols)] # 所有列的数据 # 定义返回信息 results = {'code': '', 'msg': ''} user_id = g.user_id # 判断是否填写充值信息或者大于一百次充值 if len(row_list) <= 1 or len(row_list) > 101: results['code'] = RET.OK results['msg'] = '请规范填写内容后上传!(单次批量充值不能超过100次)' return jsonify(results) # 判断总充值金额是否满足本次充值总额 money_list = col_list[1][1:] sum_money = 0 for m in money_list: if not check_float(m): results['code'] = RET.SERVERERROR results['msg'] = '充值金额不能为小数: ' + str(m) return jsonify(results) try: sum_money += int(m) except: results['code'] = RET.SERVERERROR results['msg'] = '请填写正确的充值金额!' return jsonify(results) balance = SqlData().search_user_field('balance', user_id) if sum_money > balance: results['code'] = RET.SERVERERROR results['msg'] = '账户余额不足,请充值后重试!' return jsonify(results) # 判断卡号是否规范!判断卡号是否属于该用户!判断充值金额是否符合要求 _card = row_list[1:] for card_list in _card: card_no = card_list[0].strip() if len(card_no) != 16: results['code'] = RET.SERVERERROR results['msg'] = '卡号不规范: ' + card_no return results account_id = SqlData().search_card_field('account_id', card_no) if not account_id or account_id != user_id: results['code'] = RET.SERVERERROR results['msg'] = '没有该卡号: ' + card_no return results # 给每张卡做充值 for card_list in _card: card_no = card_list[0].strip() top_money = int(card_list[1]) balance = SqlData().search_user_field('balance', user_id) if float(top_money) > balance: results['code'] = RET.SERVERERROR results['msg'] = '充值卡号: ' + card_no + ", 失败! 账户余额不足!" return jsonify(results) money = str(int(top_money) * 100) # 防止API异常,异常则重复充值3次,直到充值成功,3次仍是失败则退出本次充值 top_num = 0 while True: resp = QuanQiuFu().trans_account_recharge(card_no, money) resp_code = resp.get('resp_code') if resp_code == '0000': top_money = int(top_money) # 查询账户操作前的账户余额 before_balance = SqlData().search_user_field('balance', user_id) # 计算要扣除多少钱 do_money = top_money - top_money * 2 # 直接更新账户余额,不计算理论余额,用sql更新本次操作费用 SqlData().update_balance(do_money, user_id) # 查询扣除后的余额 balance = SqlData().search_user_field('balance', user_id) n_time = xianzai_time() SqlData().insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.TOP_UP, 1, card_no, float(top_money), 0, before_balance, balance, user_id) break elif top_num > 2: resp_msg = resp.get('resp_msg') s = '充值卡余额失败,状态码: ' + resp_code + ',信息: ' + resp_msg logging.error(s) results['code'] = RET.SERVERERROR results['msg'] = "卡号: " + card_no + ", 充值是失败!请尝试单笔充值!" return jsonify(results) else: top_num += 1 results['code'] = RET.OK results['msg'] = MSG.OK return jsonify(results) except Exception as e: logging.error(str(e)) results = {'code': RET.SERVERERROR, 'msg': MSG.SERVERERROR} return jsonify(results)
def create_card(): ''' 单卡开卡 :return: ''' data = json.loads(request.form.get('data')) card_name = data.get('card_name') top_money = data.get('top_money') label = data.get('label') user_id = g.user_id user_data = SqlData().search_user_index(user_id) create_price = user_data.get('create_card') min_top = user_data.get('min_top') max_top = user_data.get('max_top') balance = user_data.get('balance') if not check_float(top_money): results = {"code": RET.SERVERERROR, "msg": "充值金额不能为小数!"} return jsonify(results) # 本次开卡需要的费用,计算余额是否充足 money_all = int(top_money) + create_price if money_all > balance: results = {"code": RET.SERVERERROR, "msg": "本次消费金额:" + str(money_all) + ",账号余额不足!"} return jsonify(results) # 计算充值金额是否在允许范围 if not min_top <= int(top_money) <= max_top: results = {"code": RET.SERVERERROR, "msg": "充值金额不在允许范围内!"} return jsonify(results) try: # my_lock.acquire() activation = SqlData().search_activation() if not activation: return jsonify({"code": RET.SERVERERROR, "msg": "请联系服务商添加库存!"}) pay_passwd = "04A5E788" SqlData().update_card_info_field('card_name', 'USING', activation) # my_lock.acquire() # 开卡及更新相关信息(批量开卡为同一流程步骤) resp = QuanQiuFu().create_card(activation, pay_passwd) resp_code = resp.get('resp_code') if resp_code != '0000' and resp_code != '0079': resp_msg = resp.get('resp_msg') s = '卡激活失败! 状态码: ' + resp_code + ',信息: ' + resp_msg + '激活码为: ' + activation logging.error(s) return jsonify({"code": RET.SERVERERROR, "msg": resp_msg}) SqlData().update_card_info_field('account_id', user_id, activation) card_no = resp.get('response_detail').get('card_no') # 查询当次开卡是否免费 free_num = SqlData().search_user_field('free', user_id) # 有免费开卡数量则,设置开卡费用为0元,没有则获取设置的单价 if free_num > 0: # 设置开卡单价,并更新可免费开卡数量 create_price = 0 SqlData().update_remove_free(user_id) elif free_num == 0: create_price = SqlData().search_user_field('create_price', user_id) # 查询账户操作前的账户余额 before_balance = SqlData().search_user_field('balance', user_id) do_money = create_price - create_price * 2 # 直接更新账户余额,不计算理论余额,用sql更新本次操作费用 SqlData().update_balance(do_money, user_id) balance = SqlData().search_user_field('balance', user_id) n_time = xianzai_time() SqlData().insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.CREATE_CARD, 1, card_no, create_price, 0, before_balance, balance, user_id) # 查询卡信息,及更新相关信息 resp_card_info = QuanQiuFu().query_card_info(card_no) if resp_card_info.get('resp_code') != '0000': expire_date = '' card_verify_code = '' else: re_de = resp_card_info.get('response_detail') expire_date = re_de.get('expire_date') card_verify_code = re_de.get('card_verify_code') SqlData().update_card_info(card_no, pay_passwd, n_time, card_name, label, expire_date, card_verify_code, user_id, activation) money = str(int(float(top_money) * 100)) # 给卡里充值金额,及更新相关信息 resp = QuanQiuFu().trans_account_recharge(card_no, money) resp_code = resp.get('resp_code') if resp_code == '0000': top_money = float(top_money) SqlData().update_card_remain('remain', top_money, card_no) # 查询账户操作前的账户余额 before_balance = SqlData().search_user_field('balance', user_id) do_money_top = top_money - top_money * 2 # 直接更新账户余额,不计算理论余额,用sql更新本次操作费用 SqlData().update_balance(do_money_top, user_id) balance = SqlData().search_user_field('balance', user_id) n_time = xianzai_time() SqlData().insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.TOP_UP, 1, card_no, top_money, 0, before_balance, balance, user_id) return jsonify({"code": RET.OK, "msg": "开卡成功!请刷新界面!"}) else: resp_msg = resp.get('resp_msg') s = '充值卡余额失败,状态码: ' + resp_code + ',信息: ' + resp_msg logging.error(s) return jsonify({"code": RET.SERVERERROR, "msg": "开卡成功,充值失败!"}) except Exception as e: logging.error(e) results = {"code": RET.SERVERERROR, "msg": MSG.SERVERERROR} return jsonify(results)
def create_some(): ''' 批量开卡接口 :return: ''' # print(session.get('create')) data = json.loads(request.form.get('data')) card_num = data.get('card_num') name_status = data.get('n') content = data.get('content') limit = data.get('limit') label = data.get('label') user_id = g.user_id user_data = SqlData().search_user_index(user_id) create_price = user_data.get('create_card') min_top = user_data.get('min_top') max_top = user_data.get('max_top') balance = user_data.get('balance') card_num = int(card_num) if card_num > 20: results = {"code": RET.SERVERERROR, "msg": "批量开卡数量不得超过20张!"} return jsonify(results) if name_status == "write": name_list = content.split("|") if len(name_list) < card_num: results = {"code": RET.SERVERERROR, "msg": "名字数量小于建卡数量!"} return jsonify(results) else: name_list = make_name(card_num) if not check_float(limit): results = {"code": RET.SERVERERROR, "msg": "充值金额不能为小数!"} return jsonify(results) sum_money = card_num * int(limit) + card_num * create_price # 本次开卡需要的费用,计算余额是否充足 if sum_money > balance: results = {"code": RET.SERVERERROR, "msg": "本次消费金额:" + str(sum_money) + ",账号余额不足!"} return jsonify(results) # 计算充值金额是否在允许范围 if not min_top <= int(limit) <= max_top: results = {"code": RET.SERVERERROR, "msg": "充值金额不在允许范围内!"} return jsonify(results) act_count = SqlData().search_activation_count() if act_count < card_num: results = {"code": RET.SERVERERROR, "msg": "请联系服务商添加库存!"} return jsonify(results) # 查询是否有免费开卡的数量(如果没有,就不必在循环开卡的时候反复查询,浪费资源) free = SqlData().search_user_field('free', user_id) try: for i in range(card_num): # my_lock.acquire() activation = SqlData().search_activation() if not activation: return jsonify({"code": RET.SERVERERROR, "msg": "请联系服务商添加库存!"}) SqlData().update_card_info_field('card_name', 'USING', activation) # my_lock.release() pay_passwd = "04A5E788" resp = QuanQiuFu().create_card(activation, pay_passwd) resp_code = resp.get('resp_code') # print(resp_code) if resp_code != '0000' and resp_code != '0079': resp_msg = resp.get('resp_msg') s = '激活卡失败,状态码: ' + resp_code + ',信息: ' + resp_msg + ',激活码为:' + activation logging.error(s) return jsonify({"code": RET.SERVERERROR, "msg": resp_msg}) SqlData().update_card_info_field('account_id', user_id, activation) card_no = resp.get('response_detail').get('card_no') # 如果有免费开卡数量,则每次开卡查询免费数量,没有则不必每次查询 if free > 0: # 查询当次开卡是否免费 free_num = SqlData().search_user_field('free', user_id) # 有免费开卡数量则,设置开卡费用为0元,没有则获取设置的单价 if free_num > 0: # 设置开卡单价,并更新可免费开卡数量 create_price = 0 SqlData().update_remove_free(user_id) elif free_num == 0: create_price = SqlData().search_user_field('create_price', user_id) # 查询账户操作前的账户余额 before_balance = SqlData().search_user_field('balance', user_id) do_money = create_price - create_price * 2 # 直接更新账户余额,不计算理论余额,用sql更新本次操作费用 SqlData().update_balance(do_money, user_id) balance = SqlData().search_user_field('balance', user_id) n_time = xianzai_time() SqlData().insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.CREATE_CARD, 1, card_no, create_price, 0, before_balance, balance, user_id) resp_card_info = QuanQiuFu().query_card_info(card_no) # print(resp_card_info) if resp_card_info.get('resp_code') != '0000': expire_date = '' card_verify_code = '' else: re_de = resp_card_info.get('response_detail') expire_date = re_de.get('expire_date') card_verify_code = re_de.get('card_verify_code') card_name = name_list.pop() SqlData().update_card_info(card_no, pay_passwd, n_time, card_name, label, expire_date, card_verify_code, user_id, activation) money = str(int(limit) * 100) resp = QuanQiuFu().trans_account_recharge(card_no, money) resp_code = resp.get('resp_code') # print(resp) if resp_code == '0000': top_money = int(limit) # 更新卡缓存余额 SqlData().update_card_remain('remain', top_money, card_no) # 查询账户操作前的账户余额 before_balance = SqlData().search_user_field('balance', user_id) do_money_top = top_money - top_money * 2 # 直接更新账户余额,不计算理论余额,用sql更新本次操作费用 SqlData().update_balance(do_money_top, user_id) balance = SqlData().search_user_field('balance', user_id) n_time = xianzai_time() SqlData().insert_account_trans(n_time, TRANS_TYPE.OUT, DO_TYPE.TOP_UP, 1, card_no, top_money, 0, before_balance, balance, user_id) else: resp_msg = resp.get('resp_msg') s = '充值卡余额失败,状态码: ' + resp_code + ',信息: ' + resp_msg logging.error(s) card_num = str(i+1) s = "成功开卡"+card_num+"张,充值第"+card_num+"失败!请单独充值卡号:"+card_no+"!" return jsonify({"code": RET.SERVERERROR, "msg": s}) return jsonify({"code": RET.OK, "msg": "成功开卡"+str(card_num)+"张!请刷新界面!"}) except Exception as e: logging.error(e) results = {"code": RET.SERVERERROR, "msg": MSG.SERVERERROR} return jsonify(results)