private static void SetAccountInformation(XmlDocument doc, FIOTransactionResult result) { XmlNode infoNode = doc.DocumentElement.SelectSingleNode("/AccountStatement/Info"); if (infoNode.HasChildNodes) { string accountNumberString = doc.DocumentElement.SelectSingleNode("/AccountStatement/Info/accountId").InnerText; long accountNumber; if (long.TryParse(accountNumberString, out accountNumber)) { result.AccountNumber = accountNumber; } result.IBAN = doc.DocumentElement.SelectSingleNode("/AccountStatement/Info/iban") != null?doc.DocumentElement.SelectSingleNode("/AccountStatement/Info/iban").InnerText : ""; result.BIC = doc.DocumentElement.SelectSingleNode("/AccountStatement/Info/bic") != null?doc.DocumentElement.SelectSingleNode("/AccountStatement/Info/bic").InnerText : ""; } }
public static FIOTransactionResult GetFIOTransactions(Project project, string token, ApplicationDbContext context, bool loadOnlyAccountInfo, DateTime from) { FIOTransactionResult result = new FIOTransactionResult(); project.Owner.TransactionsLastUpdate = DateTime.Now; try { string url = FioBankHelper.TRANSACTIONS_DATE_PREFIX + token + FioBankHelper.GetDatePostfix(from, DateTime.Now); WebClient client = new WebClient(); string data = ""; try { data = client.DownloadString(url); } catch (Exception e) { result.Error = true; return(result); } if (string.IsNullOrEmpty(data)) { result.Error = true; return(result); } XmlDocument doc = new XmlDocument(); doc.LoadXml(data); if (loadOnlyAccountInfo) { SetAccountInformation(doc, result); result.Error = false; return(result); } XmlNode transactionList = doc.DocumentElement.SelectSingleNode("/AccountStatement/TransactionList"); result.Transactions = new List <FioTransaction>(); if (!transactionList.HasChildNodes) { result.Error = false; return(result); } XmlNodeList transactions = doc.DocumentElement.SelectNodes("/AccountStatement/TransactionList/Transaction"); foreach (XmlNode transactionNode in transactions) { result.Log += "TransactionStart | "; string transactionIDString = transactionNode.SelectSingleNode("column_22").InnerText; result.Log += "ID1: *" + transactionIDString + "* | "; long transactionID; if (!long.TryParse(transactionIDString, NumberStyles.Any, CultureInfo.InvariantCulture, out transactionID)) { result.Log += "ID2|"; result.Error = true; return(result); } result.Log += "ID3: *" + transactionID + "* | "; //Ammount decimal ammount; XmlNode ammountNode = transactionNode.SelectSingleNode("column_1"); if (ammountNode != null && !string.IsNullOrEmpty(ammountNode.InnerText) && !string.IsNullOrWhiteSpace(ammountNode.InnerText)) { if (ammountNode.InnerText.Contains('-')) { continue; } result.Log += "Ammount1: *" + ammountNode.InnerText + "* | "; if (!decimal.TryParse(ammountNode.InnerText, NumberStyles.Any, CultureInfo.InvariantCulture, out ammount)) { double ammountDouble; if (double.TryParse(ammountNode.InnerText.Replace('.', ','), out ammountDouble)) { result.Log += "Ammount2: *" + ammountDouble + "* | "; ammount = Convert.ToDecimal(ammountDouble); } else { result.Log += "Ammount3| "; result.Error = true; return(result); } } } else { ammount = 0; } result.Log += "Ammount4: *" + ammount + "* | "; //Variable symbol long variableSymbol; XmlNode variableSymbolNode = transactionNode.SelectSingleNode("column_5"); if (variableSymbolNode != null && !string.IsNullOrEmpty(variableSymbolNode.InnerText) && !string.IsNullOrWhiteSpace(variableSymbolNode.InnerText)) { result.Log += "VS1: *" + variableSymbolNode.InnerText + "* | "; if (!long.TryParse(variableSymbolNode.InnerText, out variableSymbol)) { result.Log += "VS2|"; result.Error = true; return(result); } } else { variableSymbol = 0; } result.Log += "VS1: *" + variableSymbol + "* | "; //Date DateTime date; XmlNode dateNode = transactionNode.SelectSingleNode("column_0"); if (dateNode != null && !string.IsNullOrEmpty(dateNode.InnerText) && !string.IsNullOrWhiteSpace(dateNode.InnerText)) { if (!DateTime.TryParse(dateNode.InnerText, out date)) { result.Error = true; return(result); } } else { date = DateTime.Now; } //Bank number int bankNumber; XmlNode bankNumberNode = transactionNode.SelectSingleNode("column_3"); if (bankNumberNode != null && !string.IsNullOrEmpty(bankNumberNode.InnerText) && !string.IsNullOrWhiteSpace(bankNumberNode.InnerText)) { if (!int.TryParse(bankNumberNode.InnerText, out bankNumber)) { result.Error = true; return(result); } } else { bankNumber = 0; } string note = transactionNode.SelectSingleNode("column_25") != null?transactionNode.SelectSingleNode("column_25").InnerText : ""; string accountNumber = transactionNode.SelectSingleNode("column_2") != null?transactionNode.SelectSingleNode("column_2").InnerText : ""; FioTransaction fioTransaction = new FioTransaction() { AccountNumber = accountNumber, BankCode = bankNumber, Amount = ammount, Date = date, TransactionID = transactionID, Note = note, VariableSymbol = variableSymbol }; result.Log += fioTransaction.ToString() + " ||| "; result.Transactions.Add(fioTransaction); } } catch (Exception e) { result.Log += e.Message + " |||| " + e.StackTrace; result.Error = true; return(result); } result.Error = false; return(result); }