Exemplo n.º 1
0
        static void Main(string[] args)
        {
            var configRoot = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

            var monzoProvider = new MonzoProvider(configRoot);

            var accessToken = monzoProvider.GetAccessToken().Result;

            var client = new MonzoClient(accessToken.Value);

            var accounts = client.GetAccountsAsync().Result;

            var currentAccount = accounts.Single(x => !x.Closed);

            var balance = client.GetBalanceAsync(currentAccount.Id).Result;

            var pots = client.GetPotsAsync().Result;

            var targetPot = pots.First(x => !x.Deleted); // TODO - support multiple pots

            var isLive = args.Length > 0 && args[0] == "live";

            var amountToDeposit = isLive ? balance.Value : 1;

            var random = new Random().Next();

            var deDupeId = $"{DateTime.UtcNow}-{random}";

            WriteLine($"Previous pot balance: {(decimal)targetPot.Balance / 100}{balance.Currency}");
            WriteLine($"Transferring {(decimal)amountToDeposit / 100}{balance.Currency} into the '{targetPot.Name}' pot");

            var response = client.DepositIntoPotAsync(targetPot.Id, currentAccount.Id, amountToDeposit, deDupeId).Result;

            WriteLine($"Success! New balance is {(decimal)response.Balance / 100}{response.Currency}");
        }
Exemplo n.º 2
0
        private static async Task <IList <Transaction> > GetTransactions(AppSettings config, int iteration = 0)
        {
            var monzo = new MonzoHelper(config);

            IList <Transaction> transactions;

            try
            {
                using (var client = new MonzoClient(monzo.AccessToken.Value))
                {
                    var accounts = await client.GetAccountsAsync();

                    var selectedAccount = accounts.FirstOrDefault(a => a.Type == AccountType.uk_retail) ?? accounts[0];

                    transactions = await client
                                   .GetTransactionsAsync(selectedAccount.Id, "merchant", monzo.PaginationOptions);
                };
            }
            catch (MonzoException)
            {
                if (iteration >= 2)
                {
                    throw new Exception("Unable to refresh access token");
                }

                await monzo.RefreshToken();

                transactions = await GetTransactions(config, ++iteration);
            }

            return(transactions);
        }
Exemplo n.º 3
0
        private async Task MonzoClientAuthorisation(string accessToken)
        {
            // fetch transactions etc
            using (var client = new MonzoClient(accessToken))
            {
                var accounts = await client.GetAccountsAsync();

                //var pots = await client.GetPotsAsync();
                var balance = await client.GetBalanceAsync(accounts[0].Id);

                var savingsBalance = await client.GetBalanceAsync(accounts[0].Id);

                var getTransactions = (await client.GetTransactionsAsync(accounts[0].Id, expand: "merchant"))
                                      .OrderByDescending(x => x.Created)
                                      .Take(150)
                                      .ToList();

                var spentToday = getTransactions
                                 .Where(x => x.Created.Date == DateTime.UtcNow.Date && x.Amount < 0)
                                 .Sum(x => x.Amount / 100m);

                var transactions = new List <MonzoTransaction>();

                foreach (var trans in getTransactions)
                {
                    string settled = string.IsNullOrEmpty(trans.Settled) ? "" : trans.Settled.Substring(0, trans.Settled.Length - 5);

                    transactions.Add(new MonzoTransaction
                    {
                        Id            = trans.Id,
                        Amount        = trans.Amount,
                        Created       = trans.Created,
                        Name          = trans.Merchant?.Name ?? trans.CounterParty?.Name ?? trans.Description,
                        Description   = trans.Merchant?.Id ?? trans.CounterParty?.Name ?? trans.Description,
                        Logo          = trans.Merchant?.Logo,
                        Category      = trans.Category,
                        Notes         = trans.Notes,
                        Settled       = settled,
                        DeclineReason = trans.DeclineReason
                    });
                }

                // store in temporary table
                var monzo = new MyFinances.Models.Monzo
                {
                    Balance        = balance.Value / 100m,
                    SavingsBalance = savingsBalance.Value / 100m,
                    AccountNo      = accounts[0].AccountNumber,
                    SortCode       = accounts[0].SortCode,
                    SpentToday     = spentToday,
                    Transactions   = transactions
                };

                await monzoService.InsertMonzoAccountSummary(monzo);
            }
        }
Exemplo n.º 4
0
        public async Task <SankeyResponse> Transactions()
        {
            var accessToken = JWT.Decode <AccessToken>(this.Request.Cookies["jwt"], this.settings.EncryptKey, JwsAlgorithm.HS256);

            using (var client = new MonzoClient(accessToken.Value, this.settings.BaseUrl, this.settings.ApiSubDomain))
            {
                var accounts = await client.GetAccounts();

                var transactions = await client.GetTransactions(accounts.Where(x => !x.Closed).Select(x => x.ID).ToArray(), DateTime.Now.AddMonths(-1), DateTime.Now);

                return(this.transactionService.GetSankeyDataForTransactions(transactions));
            }
        }
Exemplo n.º 5
0
        public async Task <string> ReadStringDataManual()
        {
            var emailEbayFactor = new EbayEmailFactory();

            var orderList = emailEbayFactor.GetOrderInfoFromEbayEmail();

            var updateToken = await Monzo.UpdateToken();

            var token      = updateToken.Value;
            var monzo      = new MonzoClient(token);
            var getAccount = await monzo.GetAccountsAsync();

            var accountId = getAccount[1].Id;

            var sinceDays = new PaginationOptions {
                SinceTime = DateTime.UtcNow.AddDays(-7)
            };
            var transactions = await monzo.GetTransactionsAsync(accountId, "merchant", sinceDays);

            var blobStorage = new BlobStorage();
            var _container  = await blobStorage.Create();

            foreach (var transaction in transactions)
            {
                if (transaction.Notes.Contains("#ebay") && !transaction.Notes.Contains("#automated"))
                {
                    var findEmail = orderList.Where(x => x.Price == transaction.Amount && x.OrderDate.Date.DayOfYear >= transaction.Created.Date.DayOfYear);

                    if (findEmail.Count() > 0)
                    {
                        var order = findEmail.First();

                        // Do Matching
                        var newNote = new Dictionary <string, string>()
                        {
                            { "notes", $"{order.Name} \n #ebay #automated" }
                        };

                        //upload to azure
                        using (var client = new WebClient())
                        {
                            client.DownloadFile(order.Image, "temp.jpg");
                        }

                        var uploadedImageURL = "";

                        using (FileStream stream = new FileStream("temp.jpg", FileMode.Open, FileAccess.Read))
                        {
                            var blockBlob = _container.GetBlockBlobReference($"{transaction.Id}-ebay.jpg");
                            blockBlob.Properties.ContentType = "image/jpeg";
                            stream.Seek(0, SeekOrigin.Begin);
                            await blockBlob.UploadFromStreamAsync(stream);

                            uploadedImageURL = blockBlob.StorageUri.PrimaryUri.ToString();
                        }

                        await monzo.CreateAttachmentAsync(transaction.Id, uploadedImageURL, "image/jpeg");

                        await monzo.AnnotateTransactionAsync(transaction.Id, newNote);
                    }
                }
            }

            return("ok");
        }