Esempio n. 1
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");
                }
            }
        }
Esempio n. 2
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);
            }

        }