Example #1
0
    def post(cls):
        # data is received in the form like {"mobile_number = "*****", "amount" = "***"}
        data = request.get_json()
        wallet = WalletModel.find_by_mobile_number(data["mobile_number"])
        if wallet is None:
            wallet = WalletModel()
            return {
                "message":
                gettext("WALLET_NOT_FOUND").format(data["mobile_number"])
            }, 400

        if not wallet.kyc_status:
            return {
                "message":
                gettext("KYC_NOT_DONE").format(data["mobile_number"])
            }, 400

        if wallet.amount < data["amount"]:
            return {
                "message":
                gettext("NOT_ENOUGH_BALANCE").format(data["mobile_number"])
            }, 400

        wallet.reduce_amount(data["amount"])
        return {"message": gettext("PAYMENT_SUCCESSFUL")}, 200
Example #2
0
    def get(cls):
        """
        Checks whether wallet exists or not. If it exists Then checks whether Kyc is True or not.
        :param : data is received in the form of payload like {"mobile_number": "value"}
        :return: encrypted response
            if there is some error in finding wallet or kyc not done then return {"message": "encrypted message"}
            else return wallet data like {"amount": "encrypted value",
                                          "kyc": "Encrypted value",
                                          "mobile_number": "Encrypted Value"
                                          }
        """
        data = request.get_json()
        data = Decryption.decrypt(request.get_json())
        mobile_number = data["mobile_number"]
        wallet = WalletModel.find_by_mobile_number(mobile_number)
        if wallet is None:
            return Encryption.encrypt(
                {"message":
                 gettext("WALLET_NOT_FOUND").format(mobile_number)}), 404

        if not wallet.kyc_status:
            return Encryption.encrypt(
                {"message":
                 gettext("KYC_NOT_DONE").format(mobile_number)}), 404

        return Encryption.encrypt(wallet_schema.dump(wallet)), 200
Example #3
0
    def put(cls):
        """
        payload = {"mobile_number = "*****", "amount" = "***"}
        :return:
        """
        data = request.get_json()
        # data = Decyption.decrypt(request.get_json())
        wallet = WalletModel.find_by_mobile_number(data["mobile_number"])
        if wallet is None:
            return Encryption.encrypt({
                "message":
                gettext("WALLET_NOT_FOUND").format(data["mobile_number"])
            }), 404

        if not wallet.kyc_status:
            return Encryption.encrypt({
                "message":
                gettext("KYC_NOT_DONE").format(data["mobile_number"])
            }), 404

        if wallet.amount < data["amount"]:
            return Encryption.encrypt({
                "message":
                gettext("NOT_ENOUGH_BALANCE").format(data["mobile_number"])
            }), 404

        wallet.reduce_amount(float(data["amount"]))
        try:
            wallet.save_to_db()
        except:
            return Encryption.encrypt(
                {"message": gettext("ERROR_IN_SENDING_MONEY")}), 500
        return Encryption.encrypt({"message":
                                   gettext("PAYMENT_SUCCESSFUL")}), 200
Example #4
0
    def get(cls):
        data = request.get_json()
        wallet = WalletModel.find_by_mobile_number(data["mobile_number"])
        if wallet is None:
            return {
                "message": gettext("WALLET_NOT_FOUND").format(mobile_number)
            }, 400

        if not wallet.kyc_status:
            return {
                "message": gettext("KYC_NOT_DONE").format(mobile_number)
            }, 400

        return wallet_schema.dump(wallet), 200
Example #5
0
 def put(cls):
     data = request.get_json()
     wallet = WalletModel.find_by_mobile_number(data["mobile_number"])
     if wallet:
         wallet.add_amount(data["amount"])
     else:
         wallet = wallet_schema.load(data)
     try:
         wallet.save_to_db()
     except:
         return {
             "message":
             gettext("ERROR_IN_SAVING_WALLET").format(data["mobile_number"])
         }, 500
     return wallet_schema.dump(wallet), 200
Example #6
0
 def put(cls):
     data = request.get_json()
     # data = Decyption.decrypt(request.get_json())
     wallet = WalletModel.find_by_mobile_number(data["mobile_number"])
     if wallet is None:
         return Encryption.encrypt({
             "message":
             gettext("WALLET_NOT_FOUND").format(data[mobile_number])
         }), 400
     try:
         wallet.add_amount(float(data["amount"]))
         wallet.save_to_db()
     except:
         return Encryption.encrypt(
             {"message": gettext("ERROR_IN_ADDING_MONEY")}), 500
     return Encryption.encrypt(
         {"message": gettext("MONEY_ADDED_SUCCESSFULLY")}), 200
Example #7
0
    def get(cls):
        """
        Checks whether wallet exists or not. If it exists Then checks whether Kyc is done or not.
        :param mobile_number:
        :return:
        """
        data = request.get_json()
        # data = Decyption.decrypt(request.get_json())
        mobile_number = data["mobile_number"]
        wallet = WalletModel.find_by_mobile_number(mobile_number)
        if wallet is None:
            return Encryption.encrypt(
                {"message":
                 gettext("WALLET_NOT_FOUND").format(mobile_number)}), 404

        if not wallet.kyc_status:
            return Encryption.encrypt(
                {"message":
                 gettext("KYC_NOT_DONE").format(mobile_number)}), 404

        return Encryption.encrypt(wallet_schema.dump(wallet)), 200
Example #8
0
 def put(cls):
     """
         This method is used mainly during rollback to add amount back in the wallet if the transaction fails.
         payload = {"mobile_number": "********", "amount":13131}
         returns a encrypted message.
     """
     data = request.get_json()
     data = Decryption.decrypt(request.get_json())
     wallet = WalletModel.find_by_mobile_number(data["mobile_number"])
     if wallet is None:
         return Encryption.encrypt({
             "message":
             gettext("WALLET_NOT_FOUND").format(data["mobile_number"])
         }), 400
     try:
         wallet.add_amount(data["amount"])
         wallet.save_to_db()
     except:
         return Encryption.encrypt(
             {"message": gettext("ERROR_IN_ADDING_MONEY")}), 500
     return Encryption.encrypt(
         {"message": gettext("MONEY_ADDED_SUCCESSFULLY")}), 200
Example #9
0
    def put(cls):
        """
        This method is mainly used for checking whether enough money is present in the wallet during payment or not.
        If enough amount exists then it reduces the amount for that person in the database
        and return encrypted message.
        payload = {"mobile_number = "*****", "amount" = "***"}
        :return: {"message": "Encrypted message"}
        """
        data = request.get_json()
        data = Decryption.decrypt(request.get_json())
        wallet = WalletModel.find_by_mobile_number(data["mobile_number"])
        if wallet is None:
            return Encryption.encrypt({
                "message":
                gettext("WALLET_NOT_FOUND").format(data["mobile_number"])
            }), 404

        if not wallet.kyc_status:
            return Encryption.encrypt({
                "message":
                gettext("KYC_NOT_DONE").format(data["mobile_number"])
            }), 404

        if wallet.amount < data["amount"]:
            return Encryption.encrypt({
                "message":
                gettext("NOT_ENOUGH_BALANCE").format(data["mobile_number"])
            }), 404

        wallet.reduce_amount(data["amount"])
        try:
            wallet.save_to_db()
        except:
            return Encryption.encrypt(
                {"message": gettext("ERROR_IN_SENDING_MONEY")}), 500
        return Encryption.encrypt({"message":
                                   gettext("PAYMENT_SUCCESSFUL")}), 200
Example #10
0
    def put(self, id):
        if WalletModel.find_by_id(id):
            return {'err': "A wallet with id '{}' already exists".format(id)}, 400
        
        data = Wallet.parser.parse_args()

        initial_funding = data['amount']
        if(initial_funding < 10.00):
            return {"err": "Wallet must be created with a minimum balance of 10"}, 400
        wallet = WalletModel(id, 0)

        try:
            wallet.save_to_db()
            self.__recordTransaction(id, amount, datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
        except:
            return {"err": "An error occured while creating the wallet"}, 500
        
        return wallet.json(), 201
Example #11
0
 def post(cls):
     """
     This endpoint is mainly for Testing purposes...
     payload = {"mobile_number = "*****", "amount" = 8090 , "kyc_status" : true/false}
     """
     data = request.get_json()
     # data = Decyption.decrypt(request.get_json())
     wallet = WalletModel.find_by_mobile_number(data["mobile_number"])
     if wallet:
         return {
             "message":
             gettext("WALLET_EXIST").format(data["mobile_number"])
         }, 400
     else:
         data["amount"] = float(data["amount"])
         wallet = wallet_schema.load(data)
     try:
         wallet.save_to_db()
     except:
         return {
             "message":
             gettext("ERROR_IN_SAVING_WALLET").format(data["mobile_number"])
         }, 500
     return wallet_schema.dump(wallet), 201
Example #12
0
    def post(self, id):
        #Acquires lock to ensure strong consistency if multiple users try to transact
        sem.acquire()
        data = Wallet.parser.parse_args()
        wallet = WalletModel.find_by_id(id)
        print(data['amount'])

        if wallet is None:
            sem.release()
            return {'err': 'There is no wallet associated with this id'}, 404
        else:
            try:
                amount = data['amount']
                if ((amount < 0) and (wallet.balance + amount < 10.00)):
                    sem.release()
                    return {'err': 'Minimum balance of 10 must be kept in the wallet'}, 400
                wallet.balance += amount
                wallet.save_to_db()
                self.__recordTransaction(id, amount, datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
            except:
                sem.release()
                return {'err': 'An error occured while carrying out the transaction'}, 500
        sem.release()
        return wallet.json()
Example #13
0
    def get(self, id):
        wallet = WalletModel.find_by_id(id)

        if wallet:
            return wallet.json()
        return {'err': 'There is no wallet associated with this id'}, 404