コード例 #1
0
    def get(self):
        data = accountParser.parse_args()
        user = UserModel.find_by_username(get_jwt_identity()['username'])
        if AccountModel.account_exists(user.id, data['number']) == True:
            account = AccountModel.find_by_account_number(data['number'])
            transactions = TransactionModel.find_all_by_account_id(account.id)

            for t in transactions:
                print(t.id, t.amount, t.type, t.account_id)
            return transactions
        return {
            'message':
            'Account does not exist or is not owned by current user.'
        }
コード例 #2
0
 def get(self):
     data = accountParser.parse_args()
     user = UserModel.find_by_username(get_jwt_identity()['username'])
     if AccountModel.account_exists(user.id, data['number']) == True:
         account = AccountModel.find_by_account_number(data['number'])
         return {
             'id': account.id,
             'account_number': account.account_number,
             'balance': account.balance
         }
     return {
         'message':
         'Account does not exist or is not owned by current user.'
     }
コード例 #3
0
    def post(self):
        data = depositParser.parse_args()
        user = UserModel.find_by_username(get_jwt_identity())

        try:
            account = AccountModel.find_by_account_number(data['account'])
            trans = TransactionModel(amount=data['amount'],
                                     type=TxType.CREDIT,
                                     account_id=account.id,
                                     counterpart_name=data['counterpart_name'],
                                     counterpart_acct=data['counterpart_acct'],
                                     fraud_flag=FraudType.NONE)
            trans.save_to_db()
            account.applyTransaction(trans)
            account.save_to_db()
            return jsonify({
                'id': account.id,
                'balance': str(account.balance),
                'owner': account.owner_id
            })
        except Exception as e:
            print(e)
            return {
                'message':
                'There was a problem processing the credit transaction.'
            }
コード例 #4
0
    def post(self):
        # TODO maybe restrict this feature to certain roles (admin only)
        data = accountParser.parse_args()
        identity = get_jwt_identity()
        print("{}".format(identity['role']))
        user = UserModel.find_by_username(identity['username'])
        print("Creating account for {}".format(user.username))
        newAccount = AccountModel(balance=0.0,
                                  owner_id=user.id,
                                  account_number=data['number'])

        try:
            newAccount.save_to_db()
            return {'message': 'Account created successfully'}
        except Exception as e:
            print(e)
            return {'message': 'Could not create account'}
コード例 #5
0
    def get(self):
        user = UserModel.find_by_username(get_jwt_identity()['username'])
        accounts = AccountModel.find_all_by_id(user.id)

        for a in accounts:
            print(a.id, a.account_number, a.balance)
        print(type(accounts))
        return accounts
コード例 #6
0
 def post(self):
     data = fraudParser.parse_args()
     user = UserModel.find_by_username(get_jwt_identity()['username'])
     if AccountModel.account_exists(user.id, data['account']) == True:
         account = AccountModel.find_by_account_number(data['account'])
         transaction = TransactionModel.find_by_id(account.id,
                                                   data['transaction'])
         if transaction != None:
             transaction.fraud_flag = FraudType.FLAG
             transaction.save_to_db()
             print(transaction.fraud_flag)
             return {
                 'message':
                 'Selected transaction has been flagged as fraud.'
             }
         return {
             'message':
             'Transaction does not exist or is not made by the specified account.'
         }
     return {
         'message':
         'Account does not exist or is not owned by current user.'
     }
コード例 #7
0
    def post(self):
        data = withdrawalParser.parse_args()

        if get_jwt_identity(
        )['role'] != 'BANK':  #maybe compare this using the enum instead of a string?
            return {'message': 'Only a bank can initiate a withdrawal.'}

        user = UserModel.find_by_username(get_jwt_identity())

        try:
            account = AccountModel.find_by_account_number(data['account'])
            #TODO Add logic to prevent account from going negative
            #     Good approach would be to do this in the applyTransaction() method. make it return a bool
            #     True signifies successful transaction, False means it failed (would go negative) then check
            #     the result here and react accordingly.
            trans = TransactionModel(amount=data['amount'],
                                     type=TxType.DEBIT,
                                     account_id=account.id,
                                     counterpart_name=data['counterpart_name'],
                                     counterpart_acct=data['counterpart_acct'],
                                     fraud_flag=FraudType.NONE)
            acctUser = UserModel.find_by_id(account.owner_id)
            trans.save_to_db()
            account.applyTransaction(trans)
            account.save_to_db()
            return {
                'id': account.id,
                'balance': str(account.balance),
                'owner': account.owner_id,
                'message': 'Withdrawal successful.',
                'counterpart_name': acctUser.username
            }
        except Exception as e:
            print(e)
            return {
                'message':
                'There was a problem processing the credit transaction.'
            }
コード例 #8
0
    def post(self):
        global current_component
        #Always start by checking whether current_component is filled out or not
        if len(current_component) == 0:
            current_component = check_login_list()
            if len(current_component) == 0:
                return {
                    'message':
                    'There was a problem with the current component functionality.'
                }
        data = cardParser.parse_args()

        if get_jwt_identity(
        )['role'] != 'CC':  #maybe compare this using the enum instead of a string?
            return {
                'message':
                'Only a credit card processor can initiate a card processing transaction.'
            }

        user = UserModel.find_by_username(get_jwt_identity()['username'])

        destAccount = AccountModel.find_by_account_number(data['destination'])
        destUser = UserModel.find_by_id(destAccount.owner_id)
        depositTrans = TransactionModel(amount=Decimal(data['amount']),
                                        type=TxType.CREDIT,
                                        account_id=destAccount.id,
                                        counterpart_acct=data['source'][-4:],
                                        fraud_flag=FraudType.NONE)

        # First we have to confirm that the destination account resides at this bank
        # If not, send an error message.
        if not destAccount.account_number.startswith(str(
                current_component[5])):
            return {
                'message':
                'Cannot process a card transaction that does not terminate at this bank.'
            }

        # Next, figure out if this transaction takes place within this bank or across different banks
        if not data['source'].startswith(str(current_component[5])):
            # For a back-to-bank transaction
            for bank in current_component[4]:
                if len(bank) != 1:
                    if data['source'].startswith(str(bank[3])):
                        #cc_login is a helper function in helper.py used to take care of logging into the correct bank
                        token = bnk_login(bank[1], current_component[3],
                                          bank[2])
                        header = {
                            "Authorization": "Bearer " + token['access_token'],
                            "Content-Type": "application/json"
                        }
                        # Make the request to the remote bank
                        bankResponse = requests.post(
                            bank[1] + "/bank/withdrawal",
                            json={
                                'amount': data['amount'],
                                'account': data['source'],
                                'counterpart_name': destUser.username,
                                'counterpart_acct':
                                destAccount.account_number[-4:]
                            },
                            headers=header,
                            verify=False).json()
                        print("Withdrawal request response: ")
                        print(bankResponse['message'])
                        if bankResponse['message'] == "Withdrawal successful.":
                            depositTrans.counterpart_name = bankResponse[
                                'counterpart_name']
                            destAccount.applyTransaction(depositTrans)
                            depositTrans.save_to_db()
                            destAccount.save_to_db()
                            return {
                                'message': 'Card processed successfully 2.'
                            }
                        else:
                            return {
                                'message':
                                'Insufficient funds in source account 2.'
                            }
                        break
        else:
            # For a local transaction
            srcAccount = AccountModel.find_by_account_number(data['source'])
            withdrawalTrans = TransactionModel(
                amount=Decimal(data['amount']),
                type=TxType.DEBIT,
                account_id=srcAccount.id,
                counterpart_acct=destAccount.account_number[-4:],
                fraud_flag=FraudType.NONE)
            try:
                withdrawalTrans.counterpart_name = destUser.username
                debitOk = srcAccount.applyTransaction(withdrawalTrans)
                if debitOk:
                    srcUser = UserModel.find_by_id(srcAccount.owner_id)
                    depositTrans.counterpart_name = srcUser.username
                    destAccount.applyTransaction(depositTrans)
                    depositTrans.save_to_db()
                    withdrawalTrans.save_to_db()
                    srcAccount.save_to_db()
                    destAccount.save_to_db()
                    return {'message': 'Card processed successfully.'}
                else:
                    return {'message': 'Insufficient funds in source account.'}

            except Exception as e:
                print(e)
                return {
                    'message':
                    'There was a problem processing this card transaction.'
                }