Code Monkey home page Code Monkey logo

xero-cfml-sample-app's Introduction

Sample App for Xero CFML SDK - connect to Xero Accounting API with CFML application servers.

Getting Started

Install sample app with CommandBox

  box install xero-cfml-sample-app

Alternatively, you can clone or download this repo into your wwwroot folder

Create a Xero User Account

Create a Xero user account for free. Xero does not have a designated "sandbox" for development. Instead you'll use the free Demo company for development. Learn how to get started with Xero Development Accounts.

Create a Xero Public App

Go to http://app.xero.com and login with your Xero user account to create a Xero Public app.

Update Config.json with Key and Secret

Look in the resources directory for your config.json file.

Refer to Xero Developer Center Getting Started if you haven't created your Xero App yet - this is where you'll find the Consumer Key and Secret.

Public Application

{ 
  "AppType" : "PUBLIC",
  "UserAgent": "YourAppName",
  "ConsumerKey" : "__YOUR_CONSUMER_KEY__",
  "ConsumerSecret" : "__YOUR_CONSUMER_KEY_SECRET__",
  "CallbackBaseUrl" : "http://localhost:8500",
  "CallbackPath" : "/xero-cfml-sample-app/callback.cfm"
}

Secure your config.json file

All configuration files and certificates are located in the resources directory. In a production environment, you will move this directory outside the webroot for security reasons.

Remember to update the pathToConfigJSON variable in your Application.cfc.

  <cfset pathToConfigJSON = getDirectoryFromPath(getCurrentTemplatePath()) & "resources/config.json"> 
  <cfset application.xero = CreateObject("component", "modules.xero-cfml.xero").init(pathToConfigJSON)>

Ready to Rock

Open your browser to http://localhost:8500/xero-cfml-sample-app and click the "Connect to Xero" button to start the oAuth flow.

Code behind this App

For Public Apps you'll use 3 legged oAuth, which involves a RequestToken Call, then redirecting the user to Xero to select a Xero org, then callback to your server where you'll swap the request token for your 30 min access tokens.

requesttoken.cfm

<cfscript>
try {
  application.xero.requestToken();
  location(application.xero.getAuthorizeUrl());
} 

catch(any e){
  if(e.ErrorCode EQ "401") {
    location("index.cfm?" & e.message);
  } else {
    writeDump(e);
    abort;
  }
}
</cfscript>	

callback.cfm

<cfscript>

try {
  application.xero.accessToken(aCallbackParams = cgi.query_string);
  location("get.cfm","false");
} 

catch(any e){
  if(e.ErrorCode EQ "401") {
    location("index.cfm?" & e.message);
  } else {
    writeDump(e);
    abort;
  }
}
</cfscript>

Methods

Each endpoint supports different set of methods - refer to Xero API Documentation

Below are examples of the types of methods you can call ....

Reading all objects from an endpoint

<cfscript>
  account=application.xero.getModel("Account"); 
  // Get all items 
  account.getAll();
  // After you getAll - you can loop over an Array of items (NOTE: Your object is not populated with the getAll method)
  account.getList();

  //After you getAll - Populate your object with the first item in the Array
  account.getObject(1); 

  //Get all using where clause
  account.getAll(where='Status=="ACTIVE"');

  //Get all using order param
  account.getAll(order='Name DESC');

  //Get all items modified since this date/time (i.e. 24 hours ago)
  dateTime24hoursAgo = DateAdd("d", -1, now());
  ifModifiedSince = DateConvert("local2utc", dateTime24hoursAgo);
  account.getAll(ifModifiedSince=ifModifiedSince);
</cfscript>   

Reading a single object from an endpoint.

<cfscript>  
  account=application.xero.getModel("Account");

  //Get an item by a specific ID (No need to getAll with this method)
  account.getById("XXXXXXXXXXXXXXXXX");
</cfscript>   

Creating objects on an endpoint

<cfscript>
  account=application.xero.getModel("Account");  

  account.setName("Dinner");
  account.setCode("4040");
  account.setType("CURRENT");
  account.create();
</cfscript>   

Update an object on an endpoint

<cfscript>
  account=application.xero.getModel("Account");  

  // Get all objects and set the first one in the Array to update
  account.getAll().getObject(1);
  account.setName("Meals");
  account.update();
</cfscript>   

Delete an object on an endpoint

<cfscript>
  account=application.xero.getModel("Account");  

  // Set the ID for the Account to Delete
  account.setAccountID("XXXXXXXXXXXXX");
  account.delete();
</cfscript>   

Archive an object on an endpoint

<cfscript>
  account=application.xero.getModel("Account"); 

  // Set the ID for the Account to Delete
  account.setAccountID("XXXXXXXXXXXXX");
  account.archive();
</cfscript>   

Void an object on an endpoint

<cfscript>
  invoice=application.xero.getModel("Invoice"); 

  // Set the ID for the Invoice to Void
  invoice.setInvoiceID("XXXXXXXXXXXXX");
  invoice.void();
</cfscript>   

Add to an object on an endpoint

<cfscript>
  trackingcategory=application.xero.getModel("TrackingCategory");

  // Set the ID for the Tracking Category
  trackingcategory.setTrackingCategoryID("XXXXXXXXXXXXX");
 
  trackingoption=application.xero.getModel("TrackingOption");
  trackingoption.setName("Foobar" &RandRange(1, 10000, "SHA1PRNG"));
  aTrackingOption = ArrayNew(1);
  aTrackingOption.append(trackingoption.toStruct());

  // Set the Array for Tracking Options
  trackingcategory.setOptions(aTrackingOption);
  trackingcategory.addOptions();
</cfscript>   

Remove from an object on an endpoint

<cfscript>
  trackingcategory=application.xero.getModel("TrackingCategory");

  // Set the ID for the Tracking Category
  trackingcategory.setTrackingCategoryID("XXXXXXXXXXXXX");
 
  trackingoptionToDelete=application.xero.getModel("TrackingOption").populate(trackingcategory.getOptions()[1]);  

  trackingcategory.setOptionId(trackingoptionToDelete.getTrackingOptionId()); 
  trackingcategory.deleteOption();        
</cfscript>   

Make API calls without Models

If you find yourself limited by the models, you can always hack your own raw API call.

  <cfset parameters = structNew()>
  <cfset body = "">
  <cfset ifModifiedSince = "">
  <cfset method = "GET">
  <cfset accept = "json/application">
  <cfset endpoint = "Organisation">

  <cfset sResourceEndpoint = "#application.xero.config.ApiBaseUrl##application.xero.config.ApiEndpointPath##endpoint#">
  
  <!--- Build and Call API, return new structure of XML results --->
  <cfset oRequestResult = application.xero.requestData(
    sResourceEndpoint = sResourceEndpoint,
    sOAuthToken = sOAuthToken,
    sOAuthTokenSecret= sOAuthTokenSecret,
    stParameters = parameters,
    sAccept = accept,
    sMethod = method,
    sIfModifiedSince = ifModifiedSince,
    sBody = body)>

  <cfdump var="#oRequestResult#" >

License

This software is published under the MIT License.

Copyright (c) 2014-2018 Xero Limited

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

xero-cfml-sample-app's People

Contributors

sidneyallen avatar cubiclabs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.