def make_up(): ''' 补全卡信息接口(卡列表头部工具栏最右边那个笑脸) :return: ''' try: user_id = g.user_id sql = "WHERE cvv='' OR expire='' AND account_id={}".format(str(user_id)) info = SqlData().search_card_info_admin(sql) if not info: return jsonify({'code': RET.OK, 'msg': '无卡缺失信息!'}) for i in info: card_no = i.get('card_no') card_no = card_no.strip() 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('cvv', card_verify_code, card_no) SqlData().update_card_info_card_no('expire', expire_date, card_no) return jsonify({'code': RET.OK, 'msg': MSG.OK}) except Exception as e: logging.error(str(e)) return jsonify({'code': RET.OK, 'msg': MSG.SERVERERROR})
def refund_balance(): try: data = json.loads(request.form.get('data')) card_no = json.loads(request.form.get('card_no')) pay_passwd = SqlData().search_card_field('pay_passwd', card_no) if "-" in str(data): return jsonify({'code': RET.SERVERERROR, 'msg': '请输入正确金额!'}) refund_money = str(round(float(data) * 100)) resp = QuanQiuFu().trans_account_cinsume(card_no, pay_passwd, refund_money) resp_code = resp.get('resp_code') resp_msg = resp.get('resp_msg') results = {"code": RET.OK, "msg": MSG.OK} if resp_code == "0000": user_id = g.user_id refund = SqlData().search_user_field('refund', user_id) hand_money = round(refund * float(data), 2) do_money = round(float(data) - hand_money, 2) before_balance = SqlData().search_user_field('balance', user_id) # 更新账户余额 SqlData().update_balance(do_money, user_id) balance = SqlData().search_user_field('balance', user_id) # 将退款金额转换为负数 do_money = do_money - do_money * 2 n_time = xianzai_time() SqlData().insert_account_trans(n_time, TRANS_TYPE.IN, DO_TYPE.REFUND, 1, card_no, do_money, hand_money, before_balance, balance, user_id) # 更新客户充值记录 pay_num = sum_code() t = xianzai_time() SqlData().insert_top_up(pay_num, t, do_money, before_balance, balance, user_id, '退款') results['msg'] = resp_msg else: resp_msg = resp.get('resp_msg') s = '卡余额领回失败,状态码: ' + resp_code + ',信息: ' + resp_msg logging.error(s) results['code'] = RET.SERVERERROR results['msg'] = resp_msg return jsonify(results) except Exception as e: logging.error(str(e)) results = {"code": RET.SERVERERROR, "msg": MSG.SERVERERROR} return jsonify(results)
def card_lock(): try: card_no = request.args.get('card_no') resp = QuanQiuFu().card_status_query(card_no) resp_code = resp.get('resp_code') if resp_code != "0000": return jsonify({'code': RET.SERVERERROR, 'msg': '服务器繁忙请稍后在试!'}) resp_detail = resp.get('response_detail') card_status = resp_detail.get('card_status') pay_passwd = SqlData().search_card_field('pay_passwd', card_no) if card_status == "00": # 挂失 do_type = DO_TYPE.CARD_LOCK elif card_status == '11': # 解挂 do_type = DO_TYPE.CARD_OPEN else: return jsonify({'code': RET.SERVERERROR, 'msg': '服务器繁忙请稍后在试!'}) resp = QuanQiuFu().card_loss(card_no, pay_passwd, do_type) resp_code = resp.get('resp_code') if resp_code == '0000': return jsonify({'code': RET.OK, 'msg': MSG.OK}) else: return jsonify({'code': RET.SERVERERROR, 'msg': '服务器繁忙请稍后在试!'}) except Exception as e: logging.error(str(e)) return jsonify({'code': RET.SERVERERROR, 'msg': MSG.SERVERERROR})
def one_detail(): ''' 卡的详情界面接口(交易记录,和余额) :return ''' try: context = dict() card_no = request.args.get('card_no') resp = QuanQiuFu().query_card_info(card_no) if resp.get('resp_code') == '0000': detail = resp.get('response_detail') freeze_fee_all = detail.get('freeze_fee_all') balance = detail.get('balance') f_freeze = int(freeze_fee_all)/100 f_balance = int(balance)/100 remain = round(f_balance - f_freeze, 2) context['balance'] = f_balance context['freeze_fee_all'] = f_freeze context['remain'] = remain resp = QuanQiuFu().auth_trade_query(card_no) if resp.get('resp_code') == '0000': result_set = resp.get('response_detail').get('result_set') info_list = list() for i in result_set: info_dict = dict() info_dict['trade_no'] = i.get('trade_no') if i.get('merchant_name') == "香港龙日实业有限公司": info_dict['merchant_name'] = "全球付" else: info_dict['merchant_name'] = i.get('merchant_name') trans_type = i.get('trans_type')[0:2] if trans_type == '01': info_dict['trans_type'] = '充值' elif trans_type == '02': info_dict['trans_type'] = '消费' else: info_dict['trans_type'] = '暂未定义消费类型' status_code = i.get('trans_status') info_dict['trans_status'] = TRANS_STATUS.get(status_code) info_dict['trans_amount'] = i.get('trans_amount') info_dict['trans_currency_type'] = i.get('trans_currency_type') # info_dict['trans_local_time'] = i.get('trans_local_time') info_dict['trans_local_time'] = i.get('app_time') info_dict['auth_settle_amount'] = i.get('auth_settle_amount') info_dict['settle_amount'] = i.get('settle_amount') info_dict['settle_currency_type'] = i.get('settle_currency_type') info_list.append(info_dict) context['pay_list'] = info_list return render_template('user/card_detail.html', **context) except Exception as e: logging.error((str(e))) return jsonify({'code': RET.SERVERERROR, 'msg': MSG.SERVERERROR})
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 loop(card_info): global info card_no = card_info.get('card_no') try: resp = QuanQiuFu().query_card_info(card_no) if resp.get('resp_code') == '0000': detail = resp.get('response_detail') freeze_fee_all = detail.get('freeze_fee_all') balance = detail.get('balance') f_freeze = int(freeze_fee_all) / 100 f_balance = int(balance) / 100 remain = round(f_balance - f_freeze, 2) else: msg = resp.get('resp_msg') remain = msg except: remain = '查询失败!' try: card_status = QuanQiuFu().card_status_query(card_no) if card_status.get('resp_code') == '0000': detail = card_status.get('response_detail') card_status = detail.get('card_status') if card_status == '00': c_s = '正常' elif card_status == '11': c_s = '冻结' elif card_status == '99': c_s = '注销' elif card_status == '10': c_s = '锁定' else: c_s = card_status else: c_s = card_status.get('resp_msg') except: c_s = '查询失败!' card_info['remain'] = remain card_info['card_status'] = c_s info.append(card_info)
def card_lock(): ''' 卡(锁定/解锁接口) :return: ''' try: card_no = request.args.get('card_no') resp = QuanQiuFu().card_status_query(card_no) resp_code = resp.get('resp_code') if resp_code != "0000": return jsonify({'code': RET.SERVERERROR, 'msg': '服务器繁忙请稍后在试!'}) resp_detail = resp.get('response_detail') card_status = resp_detail.get('card_status') pay_passwd = SqlData().search_card_field('pay_passwd', card_no) # 卡正常状态就为挂失,其他状态默认解挂 if card_status == "00": # 挂失 do_type = DO_TYPE.CARD_LOCK # elif card_status == '11': # 解挂 else: do_type = DO_TYPE.CARD_OPEN # else: # return jsonify({'code': RET.SERVERERROR, 'msg': '服务器繁忙请稍后在试!'}) resp = QuanQiuFu().card_loss(card_no, pay_passwd, do_type) resp_code = resp.get('resp_code') if resp_code == '0000': card_str = '' if card_status == '00': card_str = '冻结' if card_status == '11': card_str = '正常' SqlData().update_card_info_card_no('status', card_str, card_no) return jsonify({'code': RET.OK, 'msg': MSG.OK}) else: return jsonify({'code': RET.SERVERERROR, 'msg': '服务器繁忙请稍后在试!'}) except Exception as e: logging.error(str(e)) return jsonify({'code': RET.SERVERERROR, 'msg': MSG.SERVERERROR})
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)