Objective-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


#import <CkoRest.h>
#import <CkoOAuth1.h>
#import <CkoPrivateKey.h>
#import <CkoJsonObject.h>
#import <CkoStringBuilder.h>

CkoRest *rest = [[CkoRest alloc] init];
BOOL success;

CkoOAuth1 *oauth1 = [[CkoOAuth1 alloc] init];
oauth1.ConsumerKey = @"OAUTH1_CONSUMER_KEY";
oauth1.ConsumerSecret = @"OAUTH1_CONSUMER_SECRET";
oauth1.Token = @"OAUTH1_TOKEN";
oauth1.TokenSecret = @"OAUTH1_TOKEN_SECRET";
oauth1.SignatureMethod = @"RSA-SHA1";
CkoPrivateKey *rsaKey = [[CkoPrivateKey alloc] init];
//  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 useQueryParams: NO];

success = [rest Connect: @"api.xero.com" port: [NSNumber numberWithInt: 443] tls: YES autoReconnect: YES];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

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

CkoStringBuilder *sbReq = [[CkoStringBuilder alloc] init];
[jsonReq EmitSb: sbReq];

[rest AddHeader: @"Content-Type" value: @"application/json"];

CkoStringBuilder *sbResponse = [[CkoStringBuilder alloc] init];
success = [rest FullRequestSb: @"POST" uriPath: @"/api.xro/2.0/Invoices" requestBody: sbReq responseBody: sbResponse];
if (success != YES) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

if ([rest.ResponseStatusCode intValue] != 200) {
    NSLog(@"%@%d",@"Received error response code: ",[rest.ResponseStatusCode intValue]);
    NSLog(@"%@",@"Response body:");
    NSLog(@"%@",[sbResponse GetAsString]);
    return;
}

NSLog(@"%@",@"Example Completed.");

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