ASP.net POST Xero Invoice

Murray Brown
Murray Brown used Ask the Experts™
on
In Visual Studio ASP.net C#  I have used the first bit of code before to send information via an API
The second bit of code is an example of how to send an invoice to the Xero accounting system.
How I merge the two to send an invoice to Xero from a C# project

public string PostEmployeeHoursEdit(List<LineEdit> DataLines, int StoreID)
{
    HttpWebResponse Resp;
    try
    {
        string url = "http://www.restaurantmagicbox.com/platform/api/";
        string API_Method = "EmployeeHours";

        string data = "";
        data = Newtonsoft.Json.JsonConvert.SerializeObject(DataLines);

        string Auth = this.txtUsername.Text + ":" + this.txtPassword.Text;
        string webAddress = url + API_Method + "/" + StoreID + "/0";
        HttpWebRequest request = HttpWebRequest.Create(webAddress);
        request.Method = WebRequestMethods.Http.Post;
        request.Headers.Add("Authorization", Auth);
        request.ContentType = "application/json";
        request.ContentLength = data.Length;

        StreamWriter writer = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII);
        writer.Write(data);
        writer.Close();


        Resp = request.GetResponse();
        Stream stream = request.GetResponse().GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        string response = reader.ReadToEnd();

        return "OK";
    }
    catch (WebException wex)
    {
        WebResponse response = wex.Response;
        HttpStatusCode statusCode;

        HttpWebResponse httpResponse = (HttpWebResponse)response;
        statusCode = httpResponse.StatusCode;
        return statusCode + " " + httpResponse.StatusDescription;
    }
}

Open in new window


{
  "Type": "ACCREC",
  "Contact": { 
    "ContactID": "eaa28f49-6028-4b6e-bb12-d8f6278073fc" 
  },
  "Date": "\/Date(1518685950940+0000)\/",
  "DueDate": "\/Date(1518685950940+0000)\/",
  "DateString": "2009-05-27T00:00:00",
  "DueDateString": "2009-06-06T00:00:00",
  "LineAmountTypes": "Exclusive",
  "LineItems": [
    {
      "Description": "Consulting services as agreed (20% off standard rate)",
      "Quantity": "10",
      "UnitAmount": "100.00",
      "AccountCode": "200",
      "DiscountRate": "20"
    }
  ]
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Global Support Services Engineer
Commented:
From what I understand you're asking is how to use the C# code to send in the format of the second code to Xero.  If so, you'll just need to create a class object to pass to the JSON serializer.  Here's an example of class code:

public class XeroDTO
{
    public string Type { get; set; }
    public Contact Contact { get; set; }
    public List<Items> LineItems { get; set; }

    public XeroDTO()
	{
	}
}

public class Contact
{
    public string ContactID { get; set; }
}

public class Items
{
    public string Description { get; set; }
    public int Quantity { get; set; }
}

Open in new window

The serializer will transform the class properties to the JSON equivalent.  You just need to use the right type in accordance with what is used in the JSON sample.

So for the method, just replace the list object with the class:
public string PostEmployeeHoursEdit(Xero DataLines, int StoreID)
{...}

Open in new window


NOTE: not all the class examples I gave are accurate to your JSON file, I just added samples to give an idea what you can do and what it looks like.
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial