コード例 #1
0
        public async Task TestGetStatement()
        {
            // Retrieve accounts using provided credentials
            IEnumerable <OFX.Types.Account> accounts = await AuthenticatedOfxService.ListAccounts();

            // At least 1 account must be returned
            Assert.IsNotNull(accounts);
            Assert.IsTrue(accounts.Any());

            // Retrieve transactions for the last 30 days
            var startTime = DateTimeOffset.Now.Subtract(new TimeSpan(30, 0, 0, 0));

            // Retrieve statements for each account
            foreach (var account in accounts)
            {
                // Retrieve statement for the account.
                var statements =
                    await AuthenticatedOfxService.GetStatement((OFX.Types.CreditCardAccount) account, startTime, DateTimeOffset.Now);

                // Should only be one statement
                Assert.Equals(statements.Count(), 1);

                // Iterates statements and transactions, records to debug file and raises exception if the data is invalid
                DumpStatement(statements);
            }
        }
コード例 #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)
                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");
                }
            }
        }