diff --git a/src/classes/XeroAccountingApi.cls b/src/classes/XeroAccountingApi.cls index 551be92..8fd9775 100644 --- a/src/classes/XeroAccountingApi.cls +++ b/src/classes/XeroAccountingApi.cls @@ -31,6 +31,33 @@ public with sharing class XeroAccountingApi { return xeroContacts; } + /** + * @author Ben Edwards (ben@benedwards.co.nz) + * @description Method to return ALL contacts for your Xero environment + * @return List of Xero Contact wrapper classes + **/ + public static XeroContact getContact (String xeroContactId) { + + XeroContact xeroContact = new XeroContact(); + + // Execute the callout to the Contacts resource + HttpResponse response = XeroCalloutUtility.executeCallout('GET', 'Contacts/' + xeroContactId, null); + + // If successful response + if (response.getStatusCode() == 200) { + + // Use the parser to convert the response into Xero objects + xeroContact = XeroCalloutResponseParser.parseContacts(response.getBody())[0]; + } + else { + + // Raise error + throw new XeroAccountingApiException(response.getStatusCode() + ': ' + response.getBody()); + } + + return xeroContact; + } + /** * @author Ben Edwards (ben@benedwards.co.nz) * @description Method to create a Contact in Xero. diff --git a/src/classes/XeroAccountingApi.cls-meta.xml b/src/classes/XeroAccountingApi.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroAccountingApi.cls-meta.xml +++ b/src/classes/XeroAccountingApi.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroAccountingApiTest.cls-meta.xml b/src/classes/XeroAccountingApiTest.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroAccountingApiTest.cls-meta.xml +++ b/src/classes/XeroAccountingApiTest.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroAddress.cls-meta.xml b/src/classes/XeroAddress.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroAddress.cls-meta.xml +++ b/src/classes/XeroAddress.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroCalloutResponseParser.cls b/src/classes/XeroCalloutResponseParser.cls index aa1cde0..6cb567c 100644 --- a/src/classes/XeroCalloutResponseParser.cls +++ b/src/classes/XeroCalloutResponseParser.cls @@ -1,42 +1,49 @@ /** -* @author Ben Edwards (ben@benedwards.co.nz) +* @author Ben Edwards (ben@edwards.nz) * @description Class to parse responses from Xero callouts **/ public with sharing class XeroCalloutResponseParser { /** - * @author Ben Edwards (ben@benedwards.co.nz) + * @author Ben Edwards (ben@edwards.nz) * @description Method to convert JSON responses into Xero objects * @return Returns a Xero wrapper class object from a given response body **/ public static List parseContacts(String jsonBody) { - // Parse the JSON response - Map jsonResponseMap = (Map) JSON.deserializeUntyped(jsonBody); - // Re-serialize just the Contacts portion back to JSOn - String jsonSerialized = JSON.serialize((List) jsonResponseMap.get('Contacts')); - + String jsonSerialized = generateJsonStringForParsing (jsonBody, 'Contacts'); + // And finally parse the now re-serialized contact list back into Contact objects - return (List) JSON.deserialize(jsonSerialized, List.class); + return (List) JSON.deserializeStrict(jsonSerialized, List.class); } - /** - * @author Ben Edwards (ben@benedwards.co.nz) + * @author Ben Edwards (ben@edwards.nz) * @description Method to convert JSON responses into Xero objects * @return Returns a Xero wrapper class object from a given response body **/ public static List parseInvoices(String jsonBody) { - // Parse the JSON response - Map jsonResponseMap = (Map) JSON.deserializeUntyped(jsonBody); - // Re-serialize just the Contacts portion back to JSOn - String jsonSerialized = JSON.serialize((List) jsonResponseMap.get('Invoices')); + String jsonSerialized = generateJsonStringForParsing (jsonBody, 'Invoices'); // And finally parse the now re-serialized contact list back into Contact objects - return (List) JSON.deserialize(jsonSerialized, List.class); - } + return (List) JSON.deserializeStrict(jsonSerialized, List.class); + } + + /** + * @author Ben Edwards (ben@edwards.nz) + * @description Method to take the full JSON response and just return the object portion. Eg. The Contact and Invoice portion only + * @return JSON String with only specific object data + **/ + private static String generateJsonStringForParsing (String jsonBody, String objectName) { + + // Parse the JSON response + Map jsonResponseMap = (Map) JSON.deserializeUntyped(jsonBody); + + // Re-serialize just the object portion back to JSON + return JSON.serialize((List) jsonResponseMap.get(objectName)); + } } \ No newline at end of file diff --git a/src/classes/XeroCalloutResponseParser.cls-meta.xml b/src/classes/XeroCalloutResponseParser.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroCalloutResponseParser.cls-meta.xml +++ b/src/classes/XeroCalloutResponseParser.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroCalloutUtility.cls-meta.xml b/src/classes/XeroCalloutUtility.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroCalloutUtility.cls-meta.xml +++ b/src/classes/XeroCalloutUtility.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroContact.cls b/src/classes/XeroContact.cls index a628ccb..acc2708 100644 --- a/src/classes/XeroContact.cls +++ b/src/classes/XeroContact.cls @@ -11,6 +11,7 @@ public class XeroContact { public String LastName; public String EmailAddress; public String BankAccountDetails; + public String SkypeUserName; public XeroAddress[] Addresses; public XeroPhone[] Phones; public String UpdatedDateUTC; @@ -18,14 +19,45 @@ public class XeroContact { public Boolean IsSupplier; public Boolean IsCustomer; public String DefaultCurrency; + public Balances Balances; public ContactPersons[] ContactPersons; public Boolean HasAttachments; + public Attachments[] Attachments; public Boolean HasValidationErrors; - class ContactGroups { + public class ContactGroups { } - class ContactPersons { + public class ContactPersons { + + public String FirstName; + public String LastName; + public String EmailAddress; + public Boolean IncludeInEmails; + } + + public class Balances { + + public AccountsPayable AccountsPayable; + public AccountsReceivable AccountsReceivable; + } + + public class AccountsPayable { + public Decimal Overdue; //0 + public Decimal Outstanding; //0 + } + + public class AccountsReceivable { + public Decimal Overdue; //2625 + public Decimal Outstanding; //2625 } + public class Attachments { + + public String AttachmentID; + public String FileName; + public String Url; + public String MimeType; + public Integer ContentLength; + } } \ No newline at end of file diff --git a/src/classes/XeroContact.cls-meta.xml b/src/classes/XeroContact.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroContact.cls-meta.xml +++ b/src/classes/XeroContact.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroInvoice.cls-meta.xml b/src/classes/XeroInvoice.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroInvoice.cls-meta.xml +++ b/src/classes/XeroInvoice.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroOAuthUtility.cls b/src/classes/XeroOAuthUtility.cls index c30a960..1b51cc1 100644 --- a/src/classes/XeroOAuthUtility.cls +++ b/src/classes/XeroOAuthUtility.cls @@ -64,9 +64,7 @@ public with sharing class XeroOAuthUtility { // Add each paramaeter to the header string header = header + key + '="' +parameters.get(key)+ '", '; } - - system.debug('### ' + signature); - + // Add the OAuth signature and return. // Note: The signature needs to be URL encoded, as if it includes / and + these should be encoded correctly return header + 'oauth_signature="' + EncodingUtil.urlEncode(signature, 'UTF-8') + '"'; diff --git a/src/classes/XeroOAuthUtility.cls-meta.xml b/src/classes/XeroOAuthUtility.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroOAuthUtility.cls-meta.xml +++ b/src/classes/XeroOAuthUtility.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroPhone.cls-meta.xml b/src/classes/XeroPhone.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroPhone.cls-meta.xml +++ b/src/classes/XeroPhone.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active diff --git a/src/classes/XeroXmlUtility.cls-meta.xml b/src/classes/XeroXmlUtility.cls-meta.xml index d219ea1..38aa015 100644 --- a/src/classes/XeroXmlUtility.cls-meta.xml +++ b/src/classes/XeroXmlUtility.cls-meta.xml @@ -1,5 +1,5 @@ - 35.0 + 36.0 Active