public IncontrlApiTests() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables() // Configure your user secrets in the following location. You may need to create the directory first. // %APPDATA%\microsoft\UserSecrets\<userSecretsId>\secrets.json .AddUserSecrets <IncontrlApiTests>(); _configuration = builder.Build(); _api = new IncontrlApi(_configuration["AppId"], _configuration["ApiKey"], new Uri("http://localhost:20202"), new Uri("https://identity.incontrl.io")); }
public static async Task <Guid> EnsureSubscriptionData(Guid subscriptionGuid, IncontrlApi api) { // i. create subscription, deserialize subscription from disk Subscription subscription = null; if (Guid.Empty.Equals(subscriptionGuid)) { var subscriptionJson = File.ReadAllText(@"Resources\create_subscription.json"); var subscriptionRequest = Newtonsoft.Json.JsonConvert.DeserializeObject <CreateSubscriptionRequest>(subscriptionJson); subscriptionRequest.Company.LegalName += DateTime.Now.Second; subscriptionRequest.Company.Name += DateTime.Now.Second; subscription = await api.Subscriptions().CreateAsync(subscriptionRequest); } else { subscription = await api.Subscription(subscriptionGuid).GetAsync(); } var subscriptionApi = api.Subscription(subscription.Id.Value); // ii. ensure bank account for the subscription var bankAccounts = await subscriptionApi.BankAccounts().ListAsync(); if (null == bankAccounts || 0 == bankAccounts.Count) { var bankAccount = await subscriptionApi.BankAccounts().CreateAsync(new BankAccount { Bank = "NBG", Baseline = new Balance { Amount = 1000, Date = DateTime.Now }, Code = "1234", Name = "Main Bank Account", Number = "edw tha paei to iban", Provider = new TransactionProviderConfig { Name = "nbg", Settings = new { provider = "NBG", provider_id = "NBG.gr", user_id = "9d7f2ef4-7262-4429-a487-7979e4a76447", username = "******", sandbox_id = "inContrl", application_id = "inContrl", account_id = "2cebef85-bf5b-4d1d-8727-d4441750d21d", bank_id = "DB173089-A8FE-43F1-8947-F1B2A8699829", nbgApiUrl = "https://apis.nbg.gr/public/nbgapis/obp/v3.0.1" } } }); } // iii. ensure invoices in subscription var existingInvoices = await subscriptionApi.Invoices().ListAsync(new ListOptions <InvoiceListFilter> { Size = 3, Sort = "Date-" }); if (existingInvoices.Count == 0) { var product = await subscriptionApi.Products().CreateAsync(new CreateProductRequest { Amount = 450, Name = "My Precious", Taxes = new List <Tax> { new Tax { Name = "VAT", Rate = 0.24M, IsSalesTax = true } } }); var company = await subscriptionApi.Organisations().CreateAsync(new CreateOrganisationRequest { Email = "*****@*****.**", LegalName = "INDICE OE", Name = "Indice", LineOfBusiness = "Independent Software Vendor", TaxCode = "GR99", TaxOffice = "ΣΤ' ΑΘΗΝΩΝ", Website = "http://www.indice.gr", Address = new Address { CountryCode = "GR", Line1 = "22 Iakchou str.", City = "Athens", ZipCode = "11854" } }); await subscriptionApi.Invoices().CreateAsync(new CreateInvoiceRequest { PaymentCode = "171021000001", CurrencyCode = "EUR", Date = DateTime.Now.AddSeconds(-4), Status = InvoiceStatus.Issued, Recipient = new Recipient { Organisation = company }, Lines = new List <InvoiceLine> { new InvoiceLine { Description = "This is a Nice expensive item", DiscountRate = 0.05, UnitAmount = 450, Product = product, Taxes = product.Taxes } } }); await subscriptionApi.Invoices().CreateAsync(new CreateInvoiceRequest { PaymentCode = "171021000001", CurrencyCode = "EUR", Date = DateTime.Now.AddSeconds(-2), Status = InvoiceStatus.Issued, Recipient = new Recipient { Organisation = company }, Lines = new List <InvoiceLine> { new InvoiceLine { Description = "This is a Nice expensive item 2", DiscountRate = 0.05, UnitAmount = 450, Product = product, Taxes = product.Taxes, Quantity = 2 } } }); var incoive = await subscriptionApi.Invoices().CreateAsync(new CreateInvoiceRequest { PaymentCode = "171021000001", CurrencyCode = "EUR", Date = DateTime.Now, Status = InvoiceStatus.Issued, Recipient = new Recipient { Organisation = company }, Lines = new List <InvoiceLine> { new InvoiceLine { Description = "This is a Nice expensive item 3", DiscountRate = 0.05, UnitAmount = 450, Product = product, Taxes = product.Taxes, Quantity = 0.5 } } }); var invoices = await subscriptionApi.Invoices().ListAsync(new ListOptions <InvoiceListFilter> { Size = 3, Sort = "Date-" }); foreach (var invoice in invoices.Items) { var url = $"{api.ApiAddress}/{invoice.PermaLink}"; OpenBrowser(url); } } return(subscription.Id.Value); }
public async Task SyntaxTest() { var api = new IncontrlApi("{my-app-id}", "{my-api-key}"); var subscriptionId = Guid.NewGuid(); var subscriptionAlias = "my-subscription"; var contactId = Guid.NewGuid(); var documentId = Guid.NewGuid(); var documentTypeId = Guid.NewGuid(); var organisationId = Guid.NewGuid(); var productId = Guid.NewGuid(); var bankAccountId = Guid.NewGuid(); var transactionId = Guid.NewGuid(); var paymentOptionId = Guid.NewGuid(); var appId = Guid.NewGuid(); var taxId = Guid.NewGuid(); await api.LoginAsync("{my-username}", "{my-password}"); #region Subscriptions // GET: /subscriptions var subscriptions = await api.Subscriptions() .ListAsync(); // POST: /subscriptions var newSubscription = await api.Subscriptions() .CreateAsync(new CreateSubscriptionRequest { }); // GET: /subscriptions/{subscriptionId} var subscription = await api.Subscriptions(subscriptionId) .GetAsync(); // GET: /subscriptions/{subscriptionId} subscription = await api.Subscriptions(subscriptionAlias) .GetAsync(); // GET: /subscriptions/{subscriptionId}/company var company = await api.Subscriptions(subscriptionId) .Company() .GetAsync(); // PUT: /subscriptions/{subscriptionId}/company company = await api.Subscriptions(subscriptionId) .Company() .UpdateAsync(new UpdateCompanyRequest { }); // GET: /subscriptions/{subscriptionId}/contact var subscriptionContact = await api.Subscriptions(subscriptionId) .Contact() .GetAsync(); // PUT: /subscriptions/{subscriptionId}/contact subscriptionContact = await api.Subscriptions(subscriptionId) .Contact() .UpdateAsync(new Contact()); // GET: /subscriptions/{subscriptionId}/members var subscriptionMembers = await api.Subscriptions(subscriptionId) .Members() .ListAsync(); // GET: /subscriptions/{subscriptionId}/metrics var subscriptionMetrics = await api.Subscriptions(subscriptionId) .Metrics() .ListAsync(); // GET: /subscriptions/{subscriptionId}/plan var subscriptionPlan = await api.Subscriptions(subscriptionId) .Plan() .GetAsync(); // PUT: /subscriptions/{subscriptionId}/plan/services/{serviceId} var updatedService = await api.Subscriptions(subscriptionId) .Plan() .Services(Guid.NewGuid()) .UpdateAsync(new UpdateServiceRequest { Enabled = true, Settings = new AadeMyDataSettings { } }); // PUT: /subscriptions/{subscriptionId}/plan await api.Subscriptions(subscriptionId) .Plan() .UpdateAsync(new Plan { }); // GET: /subscriptions/{subscriptionId}/status var status = await api.Subscriptions(subscriptionId) .Status() .GetAsync(); // PUT: /subscriptions/{subscriptionId}/status status = await api.Subscriptions(subscriptionId) .Status() .UpdateAsync(new SubscriptionStatus { }); // PUT: /subscriptions/{subscriptionId}/time-zone subscription = await api.Subscriptions(subscriptionId) .TimeZone() .UpdateAsync(new UpdateSubscriptionTimeZoneRequest { }); // GET: /subscriptions/all var allSubscriptions = await api.Subscriptions(globalAccess : true) .ListAsync(); // GET: /subscriptions/all/metrics var metrics = await api.Subscriptions() .Metrics() .ListAsync(); // GET: /subscriptions/{subscriptionId}/activity var activity = await api.Subscriptions(subscriptionId) .Activity() .GetAsync(); // GET: /subscriptions/activity var overallActivity = await api.Subscriptions().ListAsync(); // POST: /subscriptions/{subscriptionId}/invite var invitation = await api.Subscriptions(subscriptionId) .Invitation() .SendAsync("*****@*****.**"); // POST: /subscriptions/{subscriptionId}/accept-invitation await api.Subscriptions() .Invitation("invitationId") .AcceptAsync("memberId"); // DELETE: /subscriptions/{subscriptionId} await api.Subscriptions(subscriptionAlias).DeleteAsync(); #endregion #region Contacts // GET: /subscriptions/{subscriptionId}/contacts var contacts = await api.Subscriptions(subscriptionId) .Contacts() .ListAsync(); // POST: /subscriptions/{subscriptionId}/contacts var newContact = await api.Subscriptions(subscriptionId) .Contacts() .CreateAsync(new Contact { }); // GET: /subscriptions/{subscriptionId}/contacts/{contactId} var contact = await api.Subscriptions(subscriptionId) .Contacts(contactId) .GetAsync(); // PUT: /subscriptions/{subscriptionId}/contacts/{contactId} contact = await api.Subscriptions(subscriptionId) .Contacts(contactId) .UpdateAsync(new Contact()); // GET: /subscriptions/{subscriptionId}/contacts/{contactId}/companies var contactCompanies = await api.Subscriptions(subscriptionId) .Contacts(contactId) .Companies() .ListAsync(); #endregion #region Documents // GET: /subscriptions/{subscriptionId}/documents var documents = await api.Subscriptions(subscriptionId).Documents().ListAsync(new ListOptions <DocumentListFilter> { Page = 1, Size = 25, Filter = new DocumentListFilter { TypeId = new[] { Guid.NewGuid() }, CustomerReference = string.Empty } }, summary: true); // POST: /subscriptions/{subscriptionId}/documents var createdDocument = await api.Subscriptions(subscriptionId).Documents().CreateAsync(new CreateDocumentRequest { }); // DELETE: /subscriptions/{subscriptionId}/documents/{documentId} await api.Subscriptions(subscriptionId).Documents(documentId).DeleteAsync(); // GET: /subscriptions/{subscriptionId}/documents/{documentId} var document = await api.Subscriptions(subscriptionId).Documents(documentId).GetAsync(); // PUT: /subscriptions/{subscriptionId}/documents/{documentId} document = await api.Subscriptions(subscriptionId).Documents(documentId).UpdateAsync(new UpdateDocumentRequest { }); // GET: /subscriptions/{subscriptionId}/documents/{documentId}/payments var documentPayments = await api.Subscriptions(subscriptionId).Documents(documentId).Payments().ListAsync(); // POST: /subscriptions/{subscriptionId}/documents/{documentId}/payments var newDocumentPayment = await api.Subscriptions(subscriptionId).Documents(documentId).Payments().CreateAsync(new Payment { }); // DELETE: /subscriptions/{subscriptionId}/documents/{documentId}/payments/{transactionId} await api.Subscriptions(subscriptionId).Documents(documentId).Payments().Transactions(transactionId).DeleteAsync(); // GET: /subscriptions/{subscriptionId}/documents/{documentId}/payments/{transactionId}/approval await api.Subscriptions(subscriptionId).Documents(documentId).Payments().Transactions(transactionId).Approval().UpdateAsync(new UpdateApprovalRequest { }); // GET: /subscriptions/{subscriptionId}/documents/{documentId}/status var documentStatus = await api.Subscriptions(subscriptionId) .Documents(documentId) .Status() .GetAsync(); // PUT: /subscriptions/{subscriptionId}/documents/{documentId}/status documentStatus = await api.Subscriptions(subscriptionId) .Documents(documentId) .Status() .UpdateAsync(new UpdateDocumentStatusRequest { }); // GET: /subscriptions/{subscriptionId}/documents/{documentId}/trackings var documentTrackings = await api.Subscriptions(subscriptionId) .Documents(documentId) .Trackings() .ListAsync(); // POST: /subscriptions/{subscriptionId}/documents/{documentId}/trackings var createdDocumentTracking = await api.Subscriptions(subscriptionId) .Documents(documentId) .Trackings() .CreateAsync(new CreateDocumentTrackingRequest { Recipient = string.Empty }); // PUT: /subscriptions/{subscriptionId}/documents/{documentId}/trackings await api.Subscriptions(subscriptionId) .Documents(documentId) .Trackings("") .UpdateAsync(new UpdateDocumentTrackingRequest { }); // GET: /subscriptions/{subscriptionId}/documents/{documentId}/type var documentType = await api.Subscriptions(subscriptionId).Documents(documentId).Type().GetAsync(); // PUT: /subscriptions/{subscriptionId}/documents/{documentId}/type documentType = await api.Subscriptions(subscriptionId).Documents(documentId).Type().UpdateAsync(new UpdateDocumentDocumentType { TypeId = Guid.NewGuid() }); // GET: documents/{documentId}.{format?} var documentDocument = await api.Subscriptions(subscriptionId).Documents(documentId).As(DocumentFormat.Pdf).DownloadAsync(); // POST: subscriptions/{subscriptionId:subscription}/my-data/cancel var aadeCancelResult = await api.Subscriptions(subscriptionId).Documents(documentId).MyData().CancelAsync(); #endregion #region Document Types // GET: /subscriptions/{subscriptionId}/document-types var documentTypes = await api.Subscriptions(subscriptionId) .DocumentTypes() .ListAsync(); // POST: /subscriptions/{subscriptionId}/document-types var newDocumentType = await api.Subscriptions(subscriptionId) .DocumentTypes() .CreateAsync(new CreateDocumentTypeRequest { }); // DELETE: /subscriptions/{subscriptionId}/document-types/{documentTypeId} await api.Subscriptions(subscriptionId) .DocumentTypes(documentTypeId) .DeleteAsync(); // GET: /subscriptions/{subscriptionId}/document-types/{documentTypeId} var subscriptionDocumentType = await api.Subscriptions(subscriptionId) .DocumentTypes(documentTypeId) .GetAsync(); // PUT: /subscriptions/{subscriptionId}/document-types/{documentTypeId} subscriptionDocumentType = await api.Subscriptions(subscriptionId) .DocumentTypes(documentTypeId) .UpdateAsync(new UpdateDocumentTypeRequest { }); // GET: /subscriptions/{subscriptionId}/document-types/{documentTypeId}/payment-options var documentTypePaymentOptions = await api.Subscriptions(subscriptionId) .DocumentTypes(documentTypeId) .PaymentOptions() .ListAsync(); // POST: /subscriptions/{subscriptionId}/document-types/{documentTypeId}/payment-options var createdDocumentTypePaymentOption = await api.Subscriptions(subscriptionId) .DocumentTypes(documentTypeId) .PaymentOptions() .CreateAsync(new PaymentOption { }); // DELETE: /subscriptions/{subscriptionId}/document-types/{documentTypeId}/payment-options/{paymentOptionId} await api.Subscriptions(subscriptionId) .DocumentTypes(documentId) .PaymentOptions(paymentOptionId) .DeleteAsync(); // GET: /subscriptions/{subscriptionId}/document-types/{documentTypeId}/template var documentTypeTemplate = await api.Subscriptions(subscriptionAlias) .DocumentTypes(documentTypeId) .Template() .DownloadAsync(); // POST: /subscriptions/{subscriptionId}/document-types/{documentTypeId}/template await api.Subscriptions(subscriptionId) .DocumentTypes(documentTypeId) .Template() .UploadAsync(File.OpenRead(""), string.Empty); #endregion #region Organisations // GET: /subscriptions/{subscriptionId}/organisations var organisations = await api.Subscriptions(subscriptionId) .Organisations() .ListAsync(); // POST: /subscriptions/{subscriptionId}/organisations var newOrganisation = await api.Subscriptions(subscriptionId) .Organisations() .CreateAsync(new Organisation { }); // GET: /subscriptions/{subscriptionId}/organisations/{organisationId} var organisation = await api.Subscriptions(subscriptionId) .Organisations(organisationId) .GetAsync(); // PUT: /subscriptions/{subscriptionId}/organisations/{organisationId} organisation = await api.Subscriptions(subscriptionId) .Organisations(organisationId) .UpdateAsync(new UpdateOrganisationRequest { }); #endregion #region Products // GET: /subscriptions/{subscriptionId}/products var products = await api.Subscriptions(subscriptionId) .Products() .ListAsync(); // POST: /subscriptions/{subscriptionId}/products var newProduct = await api.Subscriptions(subscriptionId) .Products() .CreateAsync(new Product { }); // GET: /subscriptions/{subscriptionId}/products/{productId} var product = await api.Subscriptions(subscriptionId) .Products(productId) .GetAsync(); // PUT: /subscriptions/{subscriptionId}/products/{productId} product = await api.Subscriptions(subscriptionId) .Products(productId) .UpdateAsync(new Product { }); #endregion #region Payment Options // GET: /subscriptions/{subscriptionId}/payment-options var paymentOptions = await api.Subscriptions(subscriptionId) .PaymentOptions() .ListAsync(); // POST: /subscriptions/{subscriptionId}/payment-options var createdPaymentOption = await api.Subscriptions(subscriptionId) .PaymentOptions() .CreateAsync(new PaymentOption { }); // GET: /subscriptions/{subscriptionId}/payment-options/{paymentOptionId} var paymentOption = await api.Subscriptions(subscriptionId) .PaymentOptions(paymentOptionId) .GetAsync(); // PUT: /subscriptions/{subscriptionId}/payment-options/{paymentOptionId} paymentOption = await api.Subscriptions(subscriptionId) .PaymentOptions(paymentOptionId) .UpdateAsync(new PaymentOption { }); // GET: /subscriptions/{subscriptionId}/payment-options/{paymentOptionId}/transactions var transactions = await api.Subscriptions(subscriptionId) .PaymentOptions(paymentOptionId) .Transactions() .ListAsync(); // POST: /subscriptions/{subscriptionId}/payment-options/{paymentOptionId}/transactions var createdTransaction = await api.Subscriptions(subscriptionId) .PaymentOptions(paymentOptionId) .Transactions() .CreateAsync(new Transaction { }); // GET: /subscriptions/{subscriptionId}/payment-options/{paymentOptionId}/transactions/{transactionId} var transaction = await api.Subscriptions(subscriptionId) .PaymentOptions(paymentOptionId) .Transactions(transactionId) .GetAsync(); // GET: /subscriptions/{subscriptionId}/payment-options/{paymentOptionId}/transactions/{transactionId}/payments var payments = await api.Subscriptions(subscriptionId) .PaymentOptions(paymentOptionId) .Transactions(transactionId) .Payments() .ListAsync(); // POST: /subscriptions/{subscriptionId}/payment-options/{paymentOptionId}/transactions/{transactionId}/payments var createdPayment = await api.Subscriptions(subscriptionId) .PaymentOptions(paymentOptionId) .Transactions(transactionId) .Payments() .CreateAsync(new Payment { }); // POST: /subscriptions/{subscriptionId}/payment-options/{paymentOptionId}/transactions/bulk await api.Subscriptions(subscriptionId) .PaymentOptions(paymentOptionId) .Transactions() .BulkCreateAsync(new BulkLoadTransactionsRequest { }); #endregion #region Apps / Users // GET: api/apps var myApps = await api.Apps() .ListAsync(); // GET: api/apps/{appId} var app = await api.App(appId) .GetAsync(); //GET: api/apps/all/webhooks var webHooks = await api.Apps() .WebHooks() .ListAsync(); //GET: api/apps/members var members = await api.Apps() .Members() .ListAsync(new MemberRequest()); var users = await api.Users() .ListAsync(new MemberRequest()); #endregion #region Lookups var timezones = await api.Lookups() .TimeZones() .ListAsync(new ListOptions { Page = 1, Size = 100 }); #endregion #region Taxes var taxes = await api.Subscriptions(subscriptionId) .Taxes() .ListAsync(); var newTax = await api.Subscriptions(subscriptionId) .Taxes() .CreateAsync(new TaxDefinition()); var tax = await api.Subscriptions(subscriptionId) .Taxes(taxId) .GetAsync(); var updatedTax = api.Subscriptions(subscriptionId) .Taxes(taxId) .UpdateAsync(new TaxDefinition()); await api.Subscriptions(subscriptionId) .Taxes(taxId) .DeleteAsync(); #endregion #region Reports var reports = await api.Subscriptions().Reports().ListAsync(ReportType.Paid, ReportingFrequency.Yearly); var generatedReport = await api.Subscriptions(subscriptionId).Reports().UpsertAsync(ReportType.Issued, ReportingFrequency.Semesterly, new Document { }); var generatedPastReport = await api.Subscriptions(subscriptionId).PastReports().CreateAsync(ReportType.Paid, ReportingFrequency.Monthly, 1, 2018); #endregion }
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(); } }