0

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();
        }

1 Answers1

1

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);
MJMortimer
  • 865
  • 5
  • 10
  • I used the same sample app from the link which you have mentioned. I'll try the code out, thanks for the help. – Shardul Virkar Jan 22 '20 at 21:30
  • I was able to add an invoice using the code above. One more thing, if I wanted to update an already present invoice and change the status to PAID, is it possible to do that? – Shardul Virkar Jan 23 '20 at 16:05
  • You can't directly update an invoice to PAID. For an invoice to have its status updated to PAID it's remaining balance must be 0, so you'll need to create Payments/credit notes/prepayments/overpayments and allocate the balance of the invoice to them until the invoice has no remaining balance left. – MJMortimer Jan 23 '20 at 17:42