Esempio n. 1
0
        public async Task <CommandResult> Handle(DisconnectCommand request, CancellationToken cancellationToken)
        {
            var client = new XeroClient(_xeroConfig.Value, _httpClientFactory.CreateClient("xero"));

            var xeroToken = await _tokenRepository.Get();

            if (xeroToken is null)
            {
                return(CommandResult.Fail("No Connection Found"));
            }

            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                await _tokenRepository.Create(xeroToken);
            }

            var xeroTenant = xeroToken.Tenants[0];
            await client.DeleteConnectionAsync(xeroToken, xeroTenant);

            await _tokenRepository.Delete();

            return(CommandResult.Success);
        }
Esempio n. 2
0
        public async Task <ActionResult> Create(string Name, string EmailAddress)
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var contact = new Contact();

            contact.Name         = Name;
            contact.EmailAddress = EmailAddress;
            var contacts = new Contacts();

            contacts._Contacts = new List <Contact>()
            {
                contact
            };

            var AccountingApi = new AccountingApi();
            var response      = await AccountingApi.CreateContactsAsync(accessToken, xeroTenantId, contacts);

            return(RedirectToAction("Index", "ContactsInfo"));
        }
        // GET: /Organisation/
        public async Task <ActionResult> Index()
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var AccountingApi = new AccountingApi();
            var response      = await AccountingApi.GetOrganisationsAsync(accessToken, xeroTenantId);

            var organisation_info = new Organisation();

            organisation_info = response._Organisations[0];

            return(View(organisation_info));
        }
Esempio n. 4
0
        // GET: ContactsInfo
        public async Task <ActionResult> Index()
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            var serviceProvider   = new ServiceCollection().AddHttpClient().BuildServiceProvider();
            var httpClientFactory = serviceProvider.GetService <IHttpClientFactory>();

            XeroConfiguration XeroConfig = new XeroConfiguration
            {
                ClientId     = ConfigurationManager.AppSettings["XeroClientId"],
                ClientSecret = ConfigurationManager.AppSettings["XeroClientSecret"],
                CallbackUri  = new Uri(ConfigurationManager.AppSettings["XeroCallbackUri"]),
                Scope        = ConfigurationManager.AppSettings["XeroScope"],
                State        = ConfigurationManager.AppSettings["XeroState"]
            };

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var AccountingApi = new AccountingApi();
            var response      = await AccountingApi.GetContactsAsync(accessToken, xeroTenantId);

            var contacts = response._Contacts;

            return(View(contacts));
        }
Esempio n. 5
0
        // GET: /InvoiceSync/
        public async Task <ActionResult> Index()
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var AccountingApi = new AccountingApi();


            var sevenDaysAgo   = DateTime.Now.AddDays(-7).ToString("yyyy, MM, dd");
            var invoicesFilter = "Date >= DateTime(" + sevenDaysAgo + ")";

            var response = await AccountingApi.GetInvoicesAsync(accessToken, xeroTenantId, null, invoicesFilter);

            var invoices = response._Invoices;

            return(View(invoices));
        }
        public async Task <ActionResult> Create(string Name, string Number)
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var asset = new Asset()
            {
                AssetName   = Name,
                AssetNumber = Number
            };

            var AssetApi = new AssetApi();
            var response = await AssetApi.CreateAssetAsync(accessToken, xeroTenantId, asset);

            return(RedirectToAction("Index", "AssetsInfo"));
        }
Esempio n. 7
0
        public async Task <ActionResult> Create(string Name, string LineDescription, string LineQuantity, string LineUnitAmount, string LineAccountCode)
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var contact = new Contact();

            contact.Name = Name;

            var line = new LineItem()
            {
                Description = LineDescription,
                Quantity    = decimal.Parse(LineQuantity),
                UnitAmount  = decimal.Parse(LineUnitAmount),
                AccountCode = LineAccountCode
            };

            var lines = new List <LineItem>()
            {
                line
            };

            var invoice = new Invoice()
            {
                Type      = Invoice.TypeEnum.ACCREC,
                Contact   = contact,
                Date      = DateTime.Today,
                DueDate   = DateTime.Today.AddDays(30),
                LineItems = lines
            };

            var invoiceList = new List <Invoice>();

            invoiceList.Add(invoice);

            var invoices = new Invoices();

            invoices._Invoices = invoiceList;

            var AccountingApi = new AccountingApi();
            var response      = await AccountingApi.CreateInvoicesAsync(accessToken, xeroTenantId, invoices);

            var updatedUTC = response._Invoices[0].UpdatedDateUTC;

            return(RedirectToAction("Index", "InvoiceSync"));
        }
        public async Task <ActionResult> Create(string firstName, string lastName, string DateOfBirth)
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            // var contact = new Contact();
            // contact.Name = Name;
            // contact.EmailAddress = EmailAddress;
            // var contacts = new Contacts();
            // contacts._Contacts = new List<Contact>() { contact };

            DateTime dob = DateTime.Today.AddYears(-20);

            HomeAddress homeAddress = new HomeAddress()
            {
                AddressLine1 = "6 MeatMe Street",
                AddressLine2 = " ",
                Region       = State.VIC,
                City         = "Long Island",
                PostalCode   = "9999",
                Country      = "New York"
            };

            Employee employee = new Employee()
            {
                FirstName   = firstName,
                LastName    = lastName,
                DateOfBirth = dob,
                HomeAddress = homeAddress
            };

            var employees = new List <Employee>()
            {
                employee
            };

            var PayrollAUApi = new PayrollAuApi();
            var response     = await PayrollAUApi.CreateEmployeeAsync(accessToken, xeroTenantId, employees);

            return(RedirectToAction("Index", "EmployeesInfo"));
        }
Esempio n. 9
0
        public async Task <IActionResult> RefreshAsync(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Authorization/Refresh")] HttpRequest req
            , ILogger log
            )
        {
            var xeroToken = await _tokenStore.GetStoredToken();

            xeroToken = (XeroOAuth2Token)await _client.RefreshAccessTokenAsync(xeroToken);

            await _tokenStore.StoreToken(xeroToken);

            return(new JsonResult(xeroToken));
        }
Esempio n. 10
0
        public async Task <ActionResult> Create(string Name, string EmailAddress)
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            var serviceProvider   = new ServiceCollection().AddHttpClient().BuildServiceProvider();
            var httpClientFactory = serviceProvider.GetService <IHttpClientFactory>();

            XeroConfiguration XeroConfig = new XeroConfiguration
            {
                ClientId     = ConfigurationManager.AppSettings["XeroClientId"],
                ClientSecret = ConfigurationManager.AppSettings["XeroClientSecret"],
                CallbackUri  = new Uri(ConfigurationManager.AppSettings["XeroCallbackUri"]),
                Scope        = ConfigurationManager.AppSettings["XeroScope"],
                State        = ConfigurationManager.AppSettings["XeroState"]
            };

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var contact = new Contact
            {
                Name         = Name,
                EmailAddress = EmailAddress
            };

            var contacts = new Contacts();

            contacts._Contacts = new List <Contact>()
            {
                contact
            };

            var AccountingApi = new AccountingApi();
            var response      = await AccountingApi.CreateContactsAsync(accessToken, xeroTenantId, contacts);

            return(RedirectToAction("Index", "ContactsInfo"));
        }
        // GET: /IdentityInfo/
        public async Task <ActionResult> Index()
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken = xeroToken.AccessToken;

            var IdentityApi = new IdentityApi();
            var response    = await IdentityApi.GetConnectionsAsync(accessToken);

            var connections = response;

            return(View(connections));
        }
        public async Task <ActionResult> Delete(string connectionId)
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken      = xeroToken.AccessToken;
            Guid   connectionIdGuid = Guid.Parse(connectionId);

            var IdentityApi = new IdentityApi();
            await IdentityApi.DeleteConnectionAsync(accessToken, connectionIdGuid);

            TokenUtilities.DestroyToken();

            return(RedirectToAction("Index", "Home"));
        }
        // GET: /EmployeesInfo/
        public async Task <ActionResult> Index()
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value, httpClientFactory);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var PayrollAUApi = new PayrollAuApi();
            var response     = await PayrollAUApi.GetEmployeesAsync(accessToken, xeroTenantId);

            var employees = response._Employees;

            return(View(employees));
        }
Esempio n. 14
0
        // GET /Authorization/Disconnect
        public async Task <ActionResult> Disconnect()
        {
            var client = new XeroClient(XeroConfig.Value, httpClientFactory);

            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken = xeroToken.AccessToken;
            Tenant xeroTenant  = xeroToken.Tenants[0];

            await client.DeleteConnectionAsync(xeroToken, xeroTenant);

            TokenUtilities.DestroyToken();

            return(RedirectToAction("Index", "Home"));
        }
Esempio n. 15
0
        // GET: /InvoiceSync/
        public async Task <ActionResult> Index()
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var AccountingApi = new AccountingApi();

            var response = await AccountingApi.GetInvoicesAsync(accessToken, xeroTenantId);

            var invoices = response._Invoices;

            return(Json(invoices));
        }
        // GET: /Assets/
        public async Task <ActionResult> Index()
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            var AssetApi = new AssetApi();

            var response = await AssetApi.GetAssetsAsync(accessToken, xeroTenantId, AssetStatusQueryParam.DRAFT);

            var assetItems = response.Items;

            return(View(assetItems));
        }
Esempio n. 17
0
        // GET: /InvoicePdf/
        public async Task <ActionResult> Index()
        {
            var xeroToken  = TokenUtilities.GetStoredToken();
            var utcTimeNow = DateTime.UtcNow;

            if (utcTimeNow > xeroToken.ExpiresAtUtc)
            {
                var client = new XeroClient(XeroConfig.Value);
                xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);

                TokenUtilities.StoreToken(xeroToken);
            }

            string accessToken  = xeroToken.AccessToken;
            string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

            Guid invoiceId = Guid.Parse("d5654fc7-be8e-42e5-802a-9f5ab9d2476a");

            var AccountingApi = new AccountingApi();

            var response = await AccountingApi.GetInvoiceAsPdfAsync(accessToken, xeroTenantId, invoiceId);

            return(new FileStreamResult(response, "application/pdf"));
        }
        public void Run(string name)
        {
            try
            {
                Console.WriteLine(name + ": Starting ");
                var clientId = _config["ClientId"];

                XeroClient client = null;
                IXeroToken token  = null;
                lock (_lockObj)
                {
                    client = new XeroClient(new XeroConfiguration {
                        ClientId = clientId
                    });

                    if (System.IO.File.Exists("Token.json"))
                    {
                        var savedJson = System.IO.File.ReadAllText("Token.json");
                        token = JsonConvert.DeserializeObject <XeroOAuth2Token>(savedJson);
                    }

                    if (token == null)
                    {
                        token = new XeroOAuth2Token {
                            RefreshToken = _config["RefreshToken"]
                        }
                    }
                    ;

                    IXeroToken newToken = Task.Run(() => client.RefreshAccessTokenAsync(token)).GetAwaiter().GetResult();
                    if (newToken != null)
                    {
                        token = newToken;
                    }

                    var json = JsonConvert.SerializeObject(token, Formatting.Indented);
                    System.IO.File.WriteAllText("Token.json", json);
                }

                Console.WriteLine(name + ": Token refreshed");
                var accessToken = token.AccessToken;

                var tenant = "";
                try
                {
                    var conRes = Task.Run(() => client.GetConnectionsAsync(token)).GetAwaiter().GetResult();
                    tenant = conRes[0].TenantId.ToString();
                    Console.WriteLine(name + ": Tenants");
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    SerializeException(e);
                    return;
                }

                var api = new AccountingApi {
                    ExceptionFactory = CustomExceptionFactory
                };

                Console.WriteLine(name + ": New token process");

                var response     = Task.Run(() => api.GetInvoicesAsyncWithHttpInfo(accessToken, tenant)).GetAwaiter().GetResult();
                var respInvoices = Task.Run(() => api.GetInvoicesAsyncWithHttpInfo(accessToken, tenant, page: 1, where : "")).GetAwaiter().GetResult();
                var invDate      = respInvoices.Data._Invoices[0].Date;

                var recurring = Task.Run(() => api.GetRepeatingInvoicesAsyncWithHttpInfo(accessToken, tenant)).GetAwaiter().GetResult();
                var status    = recurring.Data._RepeatingInvoices[0].Status;
                var schedule  = recurring.Data._RepeatingInvoices[0].Schedule;

                Console.WriteLine(name + ": Complete");
            }
            catch (CustomApiException ce)
            {
                Console.WriteLine(name + ": Failed: " + ce.Message);
                SerializeException(ce);
                Console.WriteLine(ce);
                var limits = new XeroLimits(ce.Headers);
            }
            catch (Exception e)
            {
                Console.WriteLine(name + ": Failed: " + e.Message);
                SerializeException(e);
            }
        }