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 <C_CkRestW.h>
#include <C_CkOAuth1W.h>
#include <C_CkPrivateKeyW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    HCkRestW rest;
    BOOL success;
    HCkOAuth1W oauth1;
    HCkPrivateKeyW rsaKey;
    HCkJsonObjectW jsonReq;
    HCkStringBuilderW sbReq;
    HCkStringBuilderW sbResponse;

    rest = CkRestW_Create();

    oauth1 = CkOAuth1W_Create();
    CkOAuth1W_putConsumerKey(oauth1,L"OAUTH1_CONSUMER_KEY");
    CkOAuth1W_putConsumerSecret(oauth1,L"OAUTH1_CONSUMER_SECRET");
    CkOAuth1W_putToken(oauth1,L"OAUTH1_TOKEN");
    CkOAuth1W_putTokenSecret(oauth1,L"OAUTH1_TOKEN_SECRET");
    CkOAuth1W_putSignatureMethod(oauth1,L"RSA-SHA1");
    rsaKey = CkPrivateKeyW_Create();
    //  Insert code here to load the RSA private key...
    //  ...
    //  then call SetRsaKey to provide the RSA key to the OAuth1 class.
    success = CkOAuth1W_SetRsaKey(oauth1,rsaKey);
    CkRestW_SetAuthOAuth1(rest,oauth1,FALSE);

    success = CkRestW_Connect(rest,L"api.xero.com",443,TRUE,TRUE);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth1W_Dispose(oauth1);
        CkPrivateKeyW_Dispose(rsaKey);
        return;
    }

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

    sbReq = CkStringBuilderW_Create();
    CkJsonObjectW_EmitSb(jsonReq,sbReq);

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

    sbResponse = CkStringBuilderW_Create();
    success = CkRestW_FullRequestSb(rest,L"POST",L"/api.xro/2.0/Invoices",sbReq,sbResponse);
    if (success != TRUE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkOAuth1W_Dispose(oauth1);
        CkPrivateKeyW_Dispose(rsaKey);
        CkJsonObjectW_Dispose(jsonReq);
        CkStringBuilderW_Dispose(sbReq);
        CkStringBuilderW_Dispose(sbResponse);
        return;
    }

    if (CkRestW_getResponseStatusCode(rest) != 200) {
        wprintf(L"Received error response code: %d\n",CkRestW_getResponseStatusCode(rest));
        wprintf(L"Response body:\n");
        wprintf(L"%s\n",CkStringBuilderW_getAsString(sbResponse));
        CkRestW_Dispose(rest);
        CkOAuth1W_Dispose(oauth1);
        CkPrivateKeyW_Dispose(rsaKey);
        CkJsonObjectW_Dispose(jsonReq);
        CkStringBuilderW_Dispose(sbReq);
        CkStringBuilderW_Dispose(sbResponse);
        return;
    }

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


    CkRestW_Dispose(rest);
    CkOAuth1W_Dispose(oauth1);
    CkPrivateKeyW_Dispose(rsaKey);
    CkJsonObjectW_Dispose(jsonReq);
    CkStringBuilderW_Dispose(sbReq);
    CkStringBuilderW_Dispose(sbResponse);

    }

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