/// <summary> /// Retrieve a statement for a single bank account. Includes transaction details. /// </summary> /// <param name="account">The bank account to retrieve statement data for</param> /// <param name="startDate">Start of date range for transactions</param> /// <param name="endDate">End of date range for transactions</param> /// <returns>List of statements containing the requested data.</returns> public async Task <Tuple <IEnumerable <Statement>, string> > GetStatement(Types.BankAccount account, DateTimeOffset startDate, DateTimeOffset endDate) { // Ensure service catalog is populated await PopulateServiceProfiles(); // Retrieve request profile for this message set var requestProfile = GetMessageSetRequestProfile(typeof(BankMessageSetV1)); // Form specification of the account to retrieve information for var bankAccount = getOFXBankAccount(account); // Form transaction inclusion specification for date range. Always include transaction details var transactionInclusion = new IncTransaction { DTSTART = OFXUtils.DateToOFX(startDate), DTEND = OFXUtils.DateToOFX(endDate), INCLUDE = BooleanType.Y }; // Wrap in statement request var statementRequest = new StatementRequest { BANKACCTFROM = bankAccount, INCTRAN = transactionInclusion }; // Wrap in transaction var transactionRequest = new StatementTransactionRequest { TRNUID = GetNextTransactionId(), STMTRQ = statementRequest }; // Wrap in messageset var messageSet = new BankRequestMessageSetV1 { Items = new AbstractRequest[1] { transactionRequest } }; // Gather all message sets in the request var requestMessageSets = new List <AbstractTopLevelMessageSet> { CreateSignonRequest(userCredentials, requestProfile), messageSet }; // Send to service and await response Protocol.OFX response = await new Transport(requestProfile.ServiceEndpoint).sendRequestAsync(requestMessageSets.ToArray()); // TODO: Check response for errors string errorMessage; // Extract statement data and return return(Tuple.Create(Statement.CreateFromOFXResponse(response, out errorMessage), errorMessage)); }
/// <summary> /// Populates a signon request for this service using the provided user credentials. /// Some OFX calls can use special credentials different than a FI account holder, so this method /// allows the caller to specify credentials /// </summary> /// <param name="credentials">Authentication credentials to use for this request. If no credentials are specified, the default user credentials for the service are used.</param> /// <param name="requestProfile">Request profile specifying communication parameters for this request</param> /// <returns></returns> protected SignonRequestMessageSetV1 CreateSignonRequest(Credentials credentials = null, MessageSetRequestProfile requestProfile = null) { // If no credentials are specified, use the default user credentials assigned when the service was constructed if (credentials == null) { credentials = userCredentials; } // If no request profile is specified, use the default if (requestProfile == null) { requestProfile = defaultMessageSetProfile; } // Populate the FinancialInstitution data var fi = new FinancialInstitution { ORG = financialInstitution.OrganizationId, FID = financialInstitution.FinancialId }; // Populate a signon request with the current date and provided user credentials var signonRequest = new SignonRequest { DTCLIENT = OFXUtils.DateToOFX(new DateTimeOffset(DateTime.Now)), ItemsElementName = new ItemsChoiceType[2] { ItemsChoiceType.USERID, ItemsChoiceType.USERPASS }, Items = new string[2] { credentials.UserId, credentials.Password }, FI = fi, LANGUAGE = LanguageEnum.ENG, APPID = appId, APPVER = appVer }; // Wrap the signon request in a signon message set, per protocol var signonMessageSet = new SignonRequestMessageSetV1 { SONRQ = signonRequest }; // Return the fully populated request return(signonMessageSet); }