public PaymentController(ILogger <PaymentController> logger, IBankProvider bank, IPaymentRequestStore paymentRequestStore)
 {
     _logger = logger;
     _bank   = bank;
     _store  = paymentRequestStore;
 }
Пример #2
0
        public static async Task Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                          .AddEnvironmentVariables();

            var configuration    = builder.Build();
            var clientId         = configuration["Client:ClientId"];
            var clientSecret     = configuration["Client:ClientSecret"];
            var baseApiAddress   = configuration["BaseApiAddress"]; //http://api-vnext.incontrl.io
            var subscriptionId   = configuration["SubscriptionId"];
            var subscriptionGuid = string.IsNullOrWhiteSpace(subscriptionId) ? Guid.Empty : new Guid(subscriptionId);
            var api = new IncontrlApi(clientId, clientSecret);

            api.Configure(baseApiAddress);
            api.LoginAsync(true).Wait();

            //cool ... ensure subscription here ...
            subscriptionGuid = await EnsureSubscriptionData(subscriptionGuid, api);

            //var subscriptionApi = api.Subscription(subscriptionGuid);

            var bankAccounts = api.Subscription(subscriptionGuid).BankAccounts().ListAsync().Result;
            var factory      = new BankProviderFactory();

            foreach (var bankAccount in bankAccounts.Items)
            {
                IBankProvider provider     = factory.Get(bankAccount.Provider.Name, bankAccount.Provider.Settings);
                var           transactions = await provider.GetTransactionsAsync(new BankTransactionSearchDocument());

                // i. save transactions to storage
                var savedTransactions = new List <BankTransaction>();
                foreach (var transaction in transactions)
                {
                    var savedTrans = await api.Subscription(subscriptionGuid).BankAccount(bankAccount.Id.Value).Transactions().CreateAsync(transaction);

                    // ii. get active invoices
                    var pendingInvoices = await api.Subscription(subscriptionGuid).Invoices().ListAsync(new ListOptions <InvoiceListFilter> {
                        Filter = new InvoiceListFilter {
                            Status = InvoiceStatus.Issued
                        }
                    });

                    foreach (var invoice in pendingInvoices.Items.ToList())
                    {
                        // iii. try to match (exact match please ...) invoices & transactions -> invoice.PaymentCode = transaction.Description

                        if (invoice.PaymentCode.Trim().Equals(savedTrans.Text.Trim()))
                        {
                            // a. add payments here ..
                            var payment = await api.Subscription(subscriptionGuid).BankAccount(bankAccount.Id.Value).Transaction(savedTrans.Id.Value).Payments().CreateAsync(new Payment {
                                InvoiceId = invoice.Id.Value, Amount = savedTrans.Amount
                            });

                            if (invoice.TotalPayable.Value == savedTrans.Amount)
                            {
                                // b. update the status now
                                InvoiceStatus invoiceStatus = await api.Subscription(subscriptionGuid).Invoice(invoice.Id.Value).Status().UpdateAsync(InvoiceStatus.Paid);

                                var url = $"http://api-vnext.incontrl.io/{invoice.PermaLink}";
                                OpenBrowser(url);
                                Console.ForegroundColor = ConsoleColor.Green;
                                Console.WriteLine($"We found a match !!! {payment.Amount:N2}");
                            }
                        }
                    }
                }

                //i. get bank accounts by subscriptionId
                //ii. create provider concrete class through BankProviderFactory
                //iii. get bank transactions by bank account
                //iv. store bank transactions to our storage
                //v. get invoices by subscriptionId
                //vi. matching .....

                Console.ReadKey();
            }
        }