Shardul Virkar
Shardul Virkar

Reputation: 21

Creating a new invoice

I can see list of all invoices for organisations using the Xero OAuth 2 sample from Xero-NetStandard. I want to create a new invoice for a particular tenant/organisation, how do I create an invoice object and what should the POST Method look like? Below code is what I have so far :

        public async Task<string> InvoicesPostAsync() 
    {            
        var token = await _tokenStore.GetAccessTokenAsync(User.XeroUserId());
        var connections = await _xeroClient.GetConnectionsAsync(token);

        List<string> allinvoicenames = new List<string>();
        foreach (var connection in connections)
        {
            var tenantID = connection.TenantId.ToString();
            var request = (HttpWebRequest)WebRequest.Create("https://api.xero.com/api.xro/2.0/Invoices");

            var postData = "thing1=hello";
            var data = Encoding.ASCII.GetBytes(postData);
            request.Method = "POST";                
            request.Headers.Add("Authorization" , "Bearer "+ token);
            request.Headers.Add("Xero-tenant-id" , tenantID);
            request.ContentType = "application/x-www-form-urlencoded";
            request.Credentials = CredentialCache.DefaultCredentials;
            request.ContentLength = data.Length;

        using (var stream = request.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
            }
            var response = (HttpWebResponse)request.GetResponse();
        }

Upvotes: 0

Views: 429

Answers (1)

MJMortimer
MJMortimer

Reputation: 865

Out of curiosity, which OAuth2.0 sample are you referring to?

This sample app that I put together makes use of our new OAuth2.0 SDK - https://github.com/XeroAPI/xero-netstandard-oauth2-samples/tree/master/XeroOAuth2Sample

The same SDK can be used to create invoices. Below is a bit of sample code to do so

var invoice = new Invoice
{
    Type = Invoice.TypeEnum.ACCREC,
    Contact = new Contact
    {
        Name = "Some contact name"
    },
    Date = DateTime.Today,
    DueDate = DateTime.Today.AddDays(7),
    Status = Invoice.StatusEnum.DRAFT,
    LineItems = new List<LineItem>
    {
        new LineItem
        {
            Description = "Manchester United Scarf",
            Quantity = 1,
            UnitAmount = 24.99,
            AccountCode = "200"
        }
    }
};

var createdInvoice = await _accountingApi.CreateInvoiceAsync(accessToken, tenantId, invoice);

Upvotes: 1

Related Questions