Delphi DLL 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


var
rest: HCkRest;
success: Boolean;
oauth1: HCkOAuth1;
rsaKey: HCkPrivateKey;
jsonReq: HCkJsonObject;
sbReq: HCkStringBuilder;
sbResponse: HCkStringBuilder;

begin
rest := CkRest_Create();

oauth1 := CkOAuth1_Create();
CkOAuth1_putConsumerKey(oauth1,'OAUTH1_CONSUMER_KEY');
CkOAuth1_putConsumerSecret(oauth1,'OAUTH1_CONSUMER_SECRET');
CkOAuth1_putToken(oauth1,'OAUTH1_TOKEN');
CkOAuth1_putTokenSecret(oauth1,'OAUTH1_TOKEN_SECRET');
CkOAuth1_putSignatureMethod(oauth1,'RSA-SHA1');
rsaKey := CkPrivateKey_Create();
//  Insert code here to load the RSA private key...
//  ...
//  then call SetRsaKey to provide the RSA key to the OAuth1 class.
success := CkOAuth1_SetRsaKey(oauth1,rsaKey);
CkRest_SetAuthOAuth1(rest,oauth1,False);

success := CkRest_Connect(rest,'api.xero.com',443,True,True);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

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

sbReq := CkStringBuilder_Create();
CkJsonObject_EmitSb(jsonReq,sbReq);

CkRest_AddHeader(rest,'Content-Type','application/json');

sbResponse := CkStringBuilder_Create();
success := CkRest_FullRequestSb(rest,'POST','/api.xro/2.0/Invoices',sbReq,sbResponse);
if (success <> True) then
  begin
    Memo1.Lines.Add(CkRest__lastErrorText(rest));
    Exit;
  end;

if (CkRest_getResponseStatusCode(rest) <> 200) then
  begin
    Memo1.Lines.Add('Received error response code: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
    Memo1.Lines.Add('Response body:');
    Memo1.Lines.Add(CkStringBuilder__getAsString(sbResponse));
    Exit;
  end;

Memo1.Lines.Add('Example Completed.');

CkRest_Dispose(rest);
CkOAuth1_Dispose(oauth1);
CkPrivateKey_Dispose(rsaKey);
CkJsonObject_Dispose(jsonReq);
CkStringBuilder_Dispose(sbReq);
CkStringBuilder_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"
    }
  ]
}