Exemplo n.º 1
0
        /// <summary>
        /// Cancels action. Returns to previous view.
        /// </summary>
        public async void PopulateAccounts(object passwordEntry)
        {
            // Retrieve password from entry
            var passwordBox = passwordEntry as PasswordBox;
            var password    = passwordBox?.Password;

            // Reset account retrieval failed indicator
            AccountRetrievalFailed = false;

            // Clear current list of accounts
            AvailableAccounts = null;

            // Form Credentials
            var fiCredentials = new OFX.Types.Credentials(FinancialInstitutionUsername, password);

            try
            {
                // Retrieve accounts from fI
                AvailableAccounts =
                    await
                    UpdateService.EnumerateNewAccounts(SelectedFinancialInstitution, fiCredentials)
                    .ConfigureAwait(false);

                // If no accounts were retrieved, notify user
                NoAccountsRetrieved = !AvailableAccounts.Any();
            }
            catch (Exception)
            {
                // Error retrieving accounts
                AccountRetrievalFailed = true;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retrieve the list of accounts from a financial institution using OFX and return all accounts that are not already present in the database
        /// </summary>
        /// <param name="financialInstitution">Financial institution to query</param>
        /// <param name="fiCredentials">Credentials for financial institution account</param>
        /// <returns>List of accounts</returns>
        public static async Task <IEnumerable <Account> > EnumerateNewAccounts(
            OFX.Types.FinancialInstitution financialInstitution, OFX.Types.Credentials fiCredentials)
        {
            using (BackgroundTaskTracker.BeginTask("Retrieving Account Information"))
            {
                var ofxService     = new OFX2Service(financialInstitution, fiCredentials);
                var accountList    = new List <Account>();
                var ofxAccountList = await ofxService.ListAccounts().ConfigureAwait(false);

                // TODO: If ofxAccountList is null, raise a more detailed exception

                using (var dataService = new DataService())
                {
                    foreach (var ofxAccount in ofxAccountList)
                    {
                        // Convert from OFX account type to db account type and encode account id
                        AccountType accountType = AccountType.Checking;
                        string      accountId   = "";
                        if (ofxAccount.GetType() == typeof(OFX.Types.CheckingAccount))
                        {
                            accountType = AccountType.Checking;
                            accountId   = ((OFX.Types.CheckingAccount)ofxAccount).RoutingId + ":" + ofxAccount.AccountId;
                        }
                        else if (ofxAccount.GetType() == typeof(OFX.Types.SavingsAccount))
                        {
                            accountType = AccountType.Savings;
                            accountId   = ((OFX.Types.SavingsAccount)ofxAccount).RoutingId + ":" + ofxAccount.AccountId;
                        }
                        else if (ofxAccount.GetType() == typeof(OFX.Types.CreditCardAccount))
                        {
                            accountType = AccountType.Creditcard;
                            accountId   = ofxAccount.AccountId;
                        }

                        // Look for a matching account in the database
                        if (!dataService.GetAccountByFinancialId(accountId).Any())
                        {
                            // This account is not already in the DB, add to new account list
                            accountList.Add(new Account
                            {
                                AccountName =
                                    accountType + ":" +
                                    ofxAccount.AccountId.Substring(ofxAccount.AccountId.Length - 4),
                                AccountType = accountType.ToString(),
                                Currency    = "USD",
                                FiAccountId = accountId
                            });
                        }
                    }
                }

                // Return the finalized list of new accounts
                return(accountList);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Verify the provided account credentials. Raises an exception if validation fails
        /// </summary>
        /// <param name="financialInstitution">Financial institution to query</param>
        /// <param name="fiCredentials">Credentials for financial institution account</param>
        /// <returns>List of accounts</returns>
        public static async Task VerifyAccountCredentials(FinancialInstitution financialInstitution,
                                                          OFX.Types.Credentials fiCredentials)
        {
            using (BackgroundTaskTracker.BeginTask("Verifying Credentials"))
            {
                // Convert from data model FI into OFX FI
                var ofxFinancialInstitition = new OFX.Types.FinancialInstitution(financialInstitution.Name,
                                                                                 new Uri(financialInstitution.OfxUpdateUrl), financialInstitution.OfxOrganizationId,
                                                                                 financialInstitution.OfxFinancialUnitId);

                var ofxService = new OFX2Service(ofxFinancialInstitition, fiCredentials);

                // Call list accounts to validate credentials
                await ofxService.ListAccounts().ConfigureAwait(false);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Verify the user provided credentials against the configured FI. If they verify
        /// </summary>
        public async Task VerifyAndSaveCredentials(object passwordEntry)
        {
            // Retrieve password from entry
            var passwordBox = passwordEntry as PasswordBox;
            var password    = passwordBox?.Password;

            // Store the account we're updating in case it changes while we're validating
            var updateAccount = SelectedAccount;

            // Form credentials into proper type for verification
            var credentials = new OFX.Types.Credentials(FiUserName, password);

            // Verify credentials and update if verification fails
            try
            {
                await
                UpdateService.VerifyAccountCredentials(
                    SelectedAccount.FinancialInstitutionUser.FinancialInstitution,
                    credentials).ConfigureAwait(false);
            }
            catch (Exception)
            {
                // Verify failed
                CredentialsFailed   = true;
                CredentialsVerified = false;
                return;
            }

            // Verification OK
            CredentialsFailed = false;

            // Update FI user
            updateAccount.FinancialInstitutionUser.UserId   = credentials.UserId;
            updateAccount.FinancialInstitutionUser.Password = credentials.Password;

            using (var dataService = new DataService())
            {
                // Save to DB
                dataService.UpdateFiUser(updateAccount.FinancialInstitutionUser);
            }

            // Saved
            CredentialsVerified = true;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Cancels action. Returns to previous view.
        /// </summary>
        public async void PopulateAccounts(object passwordEntry)
        {
            // Retrieve password from entry
            var passwordBox = passwordEntry as PasswordBox;
            var password = passwordBox?.Password;

            // Reset account retrieval failed indicator
            AccountRetrievalFailed = false;

            // Clear current list of accounts
            AvailableAccounts = null;

            // Form Credentials
            var fiCredentials = new OFX.Types.Credentials(FinancialInstitutionUsername, password);

            try
            {
                // Retrieve accounts from fI
                AvailableAccounts =
                    await
                        UpdateService.EnumerateNewAccounts(SelectedFinancialInstitution, fiCredentials)
                            .ConfigureAwait(false);

                // If no accounts were retrieved, notify user
                NoAccountsRetrieved = !AvailableAccounts.Any();
            }
            catch (Exception)
            {
                // Error retrieving accounts
                AccountRetrievalFailed = true;
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Download OFX transactions for an account and merge them into the account transaction list
        /// </summary>
        /// <param name="account">Account configured with financial institution login information</param>
        public static async Task DownloadOfxTransactionsForAccount(Account account)
        {
            using (BackgroundTaskTracker.BeginTask("Downloading statements"))
            {
                // Default retrieval parameters
                OFX2Service       ofxService;
                OFX.Types.Account ofxAccount;
                var endTime   = DateTimeOffset.Now;
                var startTime = new DateTimeOffset(new DateTime(1997, 1, 1));

                using (var dataService = new DataService())
                {
                    // Retrieve matching account from DB - we need to get an entity in the current db session
                    var updateAccount = dataService.GetAccountById(account.AccountId);

                    // Form FI connection properties for transaction retrieval
                    var fi = new OFX.Types.FinancialInstitution(
                        updateAccount.FinancialInstitutionUser.FinancialInstitution.Name,
                        new Uri(updateAccount.FinancialInstitutionUser.FinancialInstitution.OfxUpdateUrl),
                        updateAccount.FinancialInstitutionUser.FinancialInstitution.OfxOrganizationId,
                        updateAccount.FinancialInstitutionUser.FinancialInstitution.OfxFinancialUnitId
                        );

                    // Form credentials for login
                    var credentials = new OFX.Types.Credentials(
                        updateAccount.FinancialInstitutionUser.UserId,
                        updateAccount.FinancialInstitutionUser.Password
                        );

                    // Create service
                    ofxService = new OFX2Service(fi, credentials);

                    // Create proper account type for this account
                    var accountType = (AccountType)account.AccountType;
                    if (accountType == AccountType.Checking)
                    {
                        // Split routing and account id from combined string
                        var accountIdComponents = account.FiAccountId.Split(':');
                        ofxAccount = new OFX.Types.CheckingAccount(accountIdComponents[0], accountIdComponents[1],
                                                                   "",
                                                                   true);
                    }
                    else if (accountType == AccountType.Savings)
                    {
                        // Split routing and account id from combined string
                        var accountIdComponents = account.FiAccountId.Split(':');
                        ofxAccount = new OFX.Types.SavingsAccount(accountIdComponents[0], accountIdComponents[1], "",
                                                                  true);
                    }
                    else //if (accountType == AccountType.CREDITCARD)
                    {
                        ofxAccount = new OFX.Types.CreditCardAccount(account.FiAccountId, "", true);
                    }

                    // Use the start time of the latest transaction if we have any
                    try
                    {
                        var lastTransaction =
                            (from transaction in updateAccount.Transactions
                             orderby transaction.Date descending
                             select transaction).First();
                        startTime = new DateTimeOffset(lastTransaction.Date);
                    }
                    catch (InvalidOperationException)
                    {
                        // No transactions - ignore and use default start date.
                    }
                }

                // Retrieve statement(s) (should only be one per protocol, but we can handle any number)
                try
                {
                    var ofxStatments =
                        await ofxService.GetStatement(ofxAccount, startTime, endTime).ConfigureAwait(false);

                    foreach (var ofxStatement in ofxStatments)
                    {
                        MergeStatementTransactionsIntoAccount(account, ofxStatement);
                    }
                }
                catch (OfxException ex)
                {
                    MessageBox.Show(ex.Message, "Error");
                }
                catch (InvalidOperationException ex)
                {
                    MessageBox.Show("The data provided by the financial institution could not be parsed.", "Error");
                }
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Verify the user provided credentials against the configured FI. If they verify
        /// </summary>
        public async Task VerifyAndSaveCredentials(object passwordEntry)
        {
            // Retrieve password from entry
            var passwordBox = passwordEntry as PasswordBox;
            var password = passwordBox?.Password;

            // Store the account we're updating in case it changes while we're validating
            var updateAccount = SelectedAccount;

            // Form credentials into proper type for verification
            var credentials = new OFX.Types.Credentials(FiUserName, password);

            // Verify credentials and update if verification fails
            try
            {
                
                await
                    UpdateService.VerifyAccountCredentials(
                        SelectedAccount.FinancialInstitutionUser.FinancialInstitution,
                        credentials).ConfigureAwait(false);
            }
            catch (Exception)
            {
                // Verify failed
                CredentialsFailed = true;
                CredentialsVerified = false;
                return;
            }

            // Verification OK
            CredentialsFailed = false;

            // Update FI user
            updateAccount.FinancialInstitutionUser.UserId = credentials.UserId;
            updateAccount.FinancialInstitutionUser.Password = credentials.Password;

            using (var dataService = new DataService())
            {
                // Save to DB
                dataService.UpdateFiUser(updateAccount.FinancialInstitutionUser);
            }

            // Saved
            CredentialsVerified = true;
        }
Exemplo n.º 8
0
        /// <summary>
        /// Download OFX transactions for an account and merge them into the account transaction list
        /// </summary>
        /// <param name="account">Account configured with financial institution login information</param>
        public static async Task DownloadOfxTransactionsForAccount(Account account)
        {
            using (BackgroundTaskTracker.BeginTask("Downloading statements"))
            {
                // Default retrieval parameters
                OFX2Service ofxService;
                OFX.Types.Account ofxAccount;
                var endTime = DateTimeOffset.Now;
                var startTime = new DateTimeOffset(new DateTime(1997, 1, 1));

                using (var dataService = new DataService())
                {
                    // Retrieve matching account from DB - we need to get an entity in the current db session
                    var updateAccount = dataService.GetAccountById(account.AccountId);

                    // Form FI connection properties for transaction retrieval
                    var fi = new OFX.Types.FinancialInstitution(
                        updateAccount.FinancialInstitutionUser.FinancialInstitution.Name,
                        new Uri(updateAccount.FinancialInstitutionUser.FinancialInstitution.OfxUpdateUrl),
                        updateAccount.FinancialInstitutionUser.FinancialInstitution.OfxOrganizationId,
                        updateAccount.FinancialInstitutionUser.FinancialInstitution.OfxFinancialUnitId
                        );

                    // Form credentials for login
                    var credentials = new OFX.Types.Credentials(
                        updateAccount.FinancialInstitutionUser.UserId,
                        updateAccount.FinancialInstitutionUser.Password
                        );

                    // Create service
                    ofxService = new OFX2Service(fi, credentials);

                    // Create proper account type for this account
                    var accountType = (AccountType) account.AccountType;
                    if (accountType == AccountType.Checking)
                    {
                        // Split routing and account id from combined string
                        var accountIdComponents = account.FiAccountId.Split(':');
                        ofxAccount = new OFX.Types.CheckingAccount(accountIdComponents[0], accountIdComponents[1],
                            "",
                            true);
                    }
                    else if (accountType == AccountType.Savings)
                    {
                        // Split routing and account id from combined string
                        var accountIdComponents = account.FiAccountId.Split(':');
                        ofxAccount = new OFX.Types.SavingsAccount(accountIdComponents[0], accountIdComponents[1], "",
                            true);
                    }
                    else //if (accountType == AccountType.CREDITCARD)
                    {
                        ofxAccount = new OFX.Types.CreditCardAccount(account.FiAccountId, "", true);
                    }

                    // Use the start time of the latest transaction if we have any
                    try
                    {
                        var lastTransaction =
                            (from transaction in updateAccount.Transactions
                                orderby transaction.Date descending
                                select transaction).First();
                        startTime = new DateTimeOffset(lastTransaction.Date);

                    }
                    catch (InvalidOperationException)
                    {
                        // No transactions - ignore and use default start date.
                    }

                }

                // Retrieve statement(s) (should only be one per protocol, but we can handle any number)
                var ofxStatments = await ofxService.GetStatement(ofxAccount, startTime, endTime).ConfigureAwait(false);

                if (!String.IsNullOrEmpty(ofxStatments.Item2) || !String.IsNullOrWhiteSpace(ofxStatments.Item2))
                {
                    MessageBox.Show(ofxStatments.Item2, "Error");
                }

                foreach (var ofxStatement in ofxStatments.Item1)
                    MergeStatementTransactionsIntoAccount(account, ofxStatement);
            }

        }