Unicode C++ Xero: Create an Invoice

Back to Index

Example of a draft sales ( ACCREC) invoice with enough detail to be approved once it's been created.

Documentation: https://developer.xero.com/documentation/api/invoices


#include <CkRestW.h>
#include <CkOAuth1W.h>
#include <CkPrivateKeyW.h>
#include <CkJsonObjectW.h>
#include <CkStringBuilderW.h>

void ChilkatSample(void)
    {
    CkRestW rest;
    bool success;

    CkOAuth1W oauth1;
    oauth1.put_ConsumerKey(L"OAUTH1_CONSUMER_KEY");
    oauth1.put_ConsumerSecret(L"OAUTH1_CONSUMER_SECRET");
    oauth1.put_Token(L"OAUTH1_TOKEN");
    oauth1.put_TokenSecret(L"OAUTH1_TOKEN_SECRET");
    oauth1.put_SignatureMethod(L"RSA-SHA1");
    CkPrivateKeyW rsaKey;
    //  Insert code here to load the RSA private key...
    //  ...
    //  then call SetRsaKey to provide the RSA key to the OAuth1 class.
    success = oauth1.SetRsaKey(rsaKey);
    rest.SetAuthOAuth1(oauth1,false);

    success = rest.Connect(L"api.xero.com",443,true,true);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    //  The following code creates the JSON request body.
    //  The JSON created by this code is shown below.
    CkJsonObjectW jsonReq;
    jsonReq.UpdateString(L"Type",L"ACCREC");
    jsonReq.UpdateString(L"Contact.ContactID",L"eaa28f49-6028-4b6e-bb12-d8f6278073fc");
    jsonReq.UpdateString(L"Date",L"/Date(1518685950940+0000)/");
    jsonReq.UpdateString(L"DueDate",L"/Date(1518685950940+0000)/");
    jsonReq.UpdateString(L"DateString",L"2009-05-27T00:00:00");
    jsonReq.UpdateString(L"DueDateString",L"2009-06-06T00:00:00");
    jsonReq.UpdateString(L"LineAmountTypes",L"Exclusive");
    jsonReq.UpdateString(L"LineItems[0].Description",L"Consulting services as agreed (20% off standard rate)");
    jsonReq.UpdateString(L"LineItems[0].Quantity",L"10");
    jsonReq.UpdateString(L"LineItems[0].UnitAmount",L"100.00");
    jsonReq.UpdateString(L"LineItems[0].AccountCode",L"200");
    jsonReq.UpdateString(L"LineItems[0].DiscountRate",L"20");

    CkStringBuilderW sbReq;
    jsonReq.EmitSb(sbReq);

    rest.AddHeader(L"Content-Type",L"application/json");

    CkStringBuilderW sbResponse;
    success = rest.FullRequestSb(L"POST",L"/api.xro/2.0/Invoices",sbReq,sbResponse);
    if (success != true) {
        wprintf(L"%s\n",rest.lastErrorText());
        return;
    }

    if (rest.get_ResponseStatusCode() != 200) {
        wprintf(L"Received error response code: %d\n",rest.get_ResponseStatusCode());
        wprintf(L"Response body:\n");
        wprintf(L"%s\n",sbResponse.getAsString());
        return;
    }

    wprintf(L"Example Completed.\n");
    }

Sample JSON Request Body

{
  "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"
    }
  ]
}