Ejemplo n.º 1
0
def repay(card_info):
    '''
    call when user need to repay
    :param card_info:
    :return:
    '''
    if  card_info["freeze"]:# card is freeze
        functions.colordisplay("your card is frozen!Please refreeze first!", "red")
        return None
    card_number = card_info["card_number"]
    repay_change = card_info["credit"] - card_info["change"]
    if repay_change > 0:
        functions.colordisplay("You need to repay:%s yuan"%repay_change,"red")
    try:
        change = card_info["change"]
        print("Your change:%s yuan"%change)
        print("Your credit:%s yuan"%card_info["credit"])
        amount = input("\033[1;33;1m Input amount:>>\033[0m")
        interest = config.TransType["repay"]
        print("current interest:%s%%"%interest)
        new_change = calculate(amount, change, 'plus', interest)
        pay_auth = input("\033[1;33;1m Input pay password:>>\033[0m")
        if pay_auth == card_info["pay_password"]:
            data = api_func.db_hander("select","card_info")
            data[card_number]["change"] = new_change
            api_func.db_hander("modify", "card_info",data)# write in file
            print("new change:",new_change)
            log.set_log("transaction",20,"User %s repay successfully!"%card_info["username"])
        else:
            functions.colordisplay("Repay failed because of incorrect pay password!", "red")
            log.set_log("transaction",20,"User %s failed to repay,because of incorrect password" %card_info["username"])
    except Exception as e:
        log.set_log("errror",40,"User %s repay with error \n %s" %(card_info["username"],e))
Ejemplo n.º 2
0
def withdraw(card_info):
    '''
    call when user need to withdraw
    :param card_info: dic type
    :return:
    '''
    if  card_info["freeze"]:# card is freeze
        functions.colordisplay("your card is frozen!Please refreeze first!", "red")
        return None
    card_number = card_info["card_number"]
    try:
        change = card_info["change"]
        print("Your change:%s yuan"%change)
        amount = input("\033[1;33;1m Input amount:>>\033[0m")
        interest = config.TransType["withdraw"]
        print("Service fee %s%%:"%interest)
        new_change = calculate(amount, change, 'minus', interest)
        pay_auth = input("\033[1;33;1m Input pay password:>>\033[0m")
        if pay_auth == card_info["pay_password"]:
            data = api_func.db_hander("select", "card_info")
            data[card_number]["change"] = new_change
            api_func.db_hander("modify", "card_info", data)  # write in file
            print("Your new change:%s yuan"%new_change)
        else:
            functions.colordisplay("Repay failed because of incorrect pay password!", "red")
            log.set_log("transaction",20,"User %s withdraw failed,wrong pay password!"%card_info["username"])

    except Exception as e:
        functions.colordisplay(e, "red")
        log.set_log("error",40,"User %s withdraw failed, error!",card_info["username"])
Ejemplo n.º 3
0
def freeze(car_number):
    '''
    modify user card freeze flag to opposite state
    :param car_number:
    :return:
    '''
    data = api_func.db_hander("select", "card_info")
    flag = data[car_number]["freeze"]
    data[car_number]["freeze"] = not flag  # convert freeze status
    print("freeze/unfreeze!")
    api_func.db_hander("modify", "card_info", data)
Ejemplo n.º 4
0
def mag_change(car_number):
    '''
    modify user card change
    :param car_number:
    :return:
    '''
    try:
        data = api_func.db_hander("select", "card_info")
        print("Your current change:", data[car_number]["change"])
        change_input = input("\033[1;33;1m Input amount>>:\033[0m")
        data[car_number]["change"] = float(change_input)  # modify freeze flag
    except Exception as e:
        if e:
            functions.colordisplay("Input error!", "red")
        else:
            api_func.db_hander("modify", "card_info", data)
            print("Modify change successfully!")
Ejemplo n.º 5
0
def login():
    '''
    call while login
    :return: info of user, dic;else return None
    '''
    count = [0,""]
    while True:
        all_userdata = api_func.db_hander("select", "user_info")  # get user_info
        print("All users:")##########
        for item in all_userdata.keys():############
            print(item)##########
        username = input("\033[1;33;1mPlease input username:\033[0m")
        username = username.strip()
        log.set_log("access",20,"User %s attempts to login!"%username)
        if username in all_userdata.keys():
            userdata = all_userdata[username] # get specific user
            psd = input("\033[1;33;1mPlease input password:\033[0m")
            psd = psd.strip()
            if psd == userdata["password"]:
                if userdata["locked"]:#locked
                    print("\033[1;31;1m User has been locked!!\033[0m")
                else:
                    log.set_log("access",20,"User %s  login successfully!" % username)
                    print("\033[1;33;1m Login accept!\033[0m")
                    return userdata
            else: #password is incorrect
                colordisplay("password is incorrect!","red")
                log.set_log("access",20,"User %s failed to login!" % username)
                if count[1] == username:# tha same user
                    count[0] += 1
                    if count[0] >= 3:#more than 3 times
                        colordisplay("User %s is locked because of attempting too many times!","red")
                        api_func.db_hander("modify","user_info",all_userdata)# write in file
                        log.set_log("access",20,"User %s is locked because of attempting too many times!" % username)
                else:# another user
                    count[1] = username
                    count[0] += 1

        elif username == "b": # back
            break
        else:
            print("\033[1;31;1m Can not find such user!!!\033[0m")
    return None
Ejemplo n.º 6
0
def transfer(card_info):
    '''
    call when user need to transfer
    :param card_info: dic type
    :return: None
    '''
    if  card_info["freeze"]:# card is freeze
        functions.colordisplay("your card is frozen!Please refreeze first!","red")
    try:
        cur_card_number = card_info["card_number"]
        all_card_info = api_func.db_hander("select","card_info")
        print("current all cards: ")
        for key in all_card_info.keys():
            print(key)
        print("Your card num is:%s" %card_info["card_number"])
        change = card_info["change"]
        print("change:", change)
        trans_num = input("\033[1;33;1m Input card number you want to transfer:>>\033[0m")
        if trans_num in all_card_info.keys():
            amount = input("\033[1;33;1m Input amount:>>\033[0m")
            interest = config.TransType["transfer"]
            new_change = calculate(amount, change, 'minus', interest)
            pay_auth = input("\033[1;33;1m Input pay password:>>\033[0m")
            if pay_auth == card_info["pay_password"]:
                if trans_num in all_card_info.keys():
                    all_card_info[cur_card_number]["change"] -= new_change
                    all_card_info[trans_num]["change"] += float(change)  # receive account no interest
                    api_func.db_hander("modify", "card_info", all_card_info)  # Write in file
                    print("Your change remain:%s yuan"%all_card_info[cur_card_number]["change"])
                    log.set_log("transaction",20,"User %s transfer successfully!"%card_info["username"])

                else:
                    functions.colordisplay("Can not find the card number!","red")
            else:
                functions.colordisplay("Pay password is incorrect!","red")
                log.set_log("transaction",20,"User %s transfer failed,wrong pay_password!"%card_info["username"])
        else:
            functions.colordisplay("card does not exist!","red")

    except Exception as e:
        functions.colordisplay(e, "red")
        log.set_log("error",40,"User %s transfer failed, error!"%card_info["username"])
    return None
Ejemplo n.º 7
0
def find_user(username):
    '''
    find where the user has credit card ,return the  card number,else return None
    :param username:
    :return:
    '''
    userdata = api_func.db_hander("select","card_info")
    for items in userdata.values():
        if username == items["username"]:
            return items["card_number"]
    else:
        return None
Ejemplo n.º 8
0
def store_payment(order_num, money, username):
    '''
    This is pay api for store.According order number and order price to pay,
    return True if success,else return False.
    :param order_num:
    :param money:
    :param username:
    :return:
    '''
    card_number = creditcard.find_user(username)  # find card num by username
    print("Found your credit card:\033[1;34;1m%s\033[0m" % card_number)
    print("order:", order_num)
    print("aomount:%s yuan" % money)
    data = api_func.db_hander("select", "card_info")
    if data[card_number]["freeze"]:
        functions.colordisplay("This card is frozen! payment is denied!",
                               "red")
    else:
        user_card = data[card_number]  # get user card info
        pay_psd = input("\033[1;33;1m Input pay password:\033[0m")
        if pay_psd == user_card["pay_password"]:
            if user_card["change"] < money:
                functions.colordisplay("Your balance is not enough!", "red")
            else:
                data[card_number]["change"] -= money
                api_func.db_hander("modify", "card_info", data)  #write in file
                log.set_log("transaction", 20,
                            "User %s pay successfully!" % username)
                return True
        else:
            functions.colordisplay("Sorry,pay password is incorrect!!!", "red")
            log.set_log(
                "transaction", 20,
                "User %s failed to pay because of incorrect pay_password!" %
                username)
    return False
Ejemplo n.º 9
0
def card_center(login_data):
    '''

    :param user_data:
    :return:
    '''
    if login_data["auth"]:  # user is login
        flag_card = True
        while flag_card:
            card_number = creditcard.find_user(login_data["name"])
            if not card_number:  # user does not bind credit card
                functions.colordisplay("can not find user in db", "red")
                flag_card = False
            else:
                all_cards = api_func.db_hander("select", "card_info")
                current_card = all_cards[card_number]  # get current user info
                functions.colordisplay(menu_credit, "yellow")
                menu_choice = functions.inpmsg("Input >>:",
                                               ('1', '2', '3', '4', '5', '6',
                                                '7', '8', '9', 'b')).strip()
                if menu_choice == '1':  #User Info
                    print("Current user: [%s]" % login_data["name"])
                elif menu_choice == '2':  #Card Info
                    print("Your card number: [%s]" % card_number)
                    detail = functions.inpmsg("[m] to display more...")
                    if detail == "m":
                        for k, v in current_card:
                            print(k, ':', v)

                elif menu_choice == '3':  # Repay
                    creditcard.repay(current_card)
                elif menu_choice == '4':  #Withdraw
                    creditcard.withdraw(current_card)
                elif menu_choice == '5':  #Transfer
                    creditcard.transfer(current_card)
                elif menu_choice == '6':  #Freeze
                    freeze(current_card["card_number"])
                elif menu_choice == '7':  #See my Bill
                    api_func.see_bill(auth_tool["name"])
                elif menu_choice == '8':  #Manage credit
                    mag_credit(current_card["card_number"])
                elif menu_choice == '9':  #Manage change
                    mag_change(current_card["card_number"])
                else:  # menu_choice == 'b'---Back
                    flag_card = False

    else:
        functions.colordisplay("Sorry,you need to login first!", "red")