Git Product home page Git Product logo

apex-for-xero's Introduction

Apex-for-Xero

Deploy to Salesforce

(deploy from Github from https://githubsfdeploy.herokuapp.com/app/githubdeploy/benedwards44/Apex-for-Xero)

This application contains Apex utilities for accessing the Xero REST APIs.

These classes are set up to use the Private Application type.

The aim of this project is to act as a starting point for accessing Xero APIs via Apex. I wanted to share this project as I spent a lot of time getting the authentication working for Xero, as Apex doesn't have a standard OAuth 1.0 library and there wasn't much detail online about it.

For more information about the Xero APIs, check out: http://developer.xero.com/documentation/api/api-overview/

Quick Setup

  1. Create an Auth. Provider within your Salesforce Org:

    1. Setup -> Security Controls -> Auth. Providers -> New
    2. Provider Type = "Open ID Connect"
    3. Name = "Xero"
    4. Consumer Key = "ABC" (this is temporary, we will update this with the Xero Consumer Key once we have it)
    5. Consumer Secret = "ABC" (as above)
    6. Authorize Endpoint URL = "https://login.xero.com/identity/connect/authorize"
    7. Token Endpoint URL = "https://login.xero.com/identity/connect/token"
    8. Default Scopes: "openid profile email offline_access accounting.transactions accounting.contacts" (you can see all scopes here https://developer.xero.com/documentation/oauth2/scopes)

    Leave everything as is. Click save and then copy the generated "Callback URL". Eg. https://login.salesforce.com/services/authcallback/00D2v000003QVUrCAO/Xero

  2. Create the Xero App:

    1. https://app.xero.com/ -> My Apps -> New app
    2. App name = Your unique name
    3. Company or application URL = Can be anything, suggest either your Salesforce Org URL or your company website
    4. OAuth 2.0 redirect URI = Paste in the "Callback URL" copied from step 1 above
    5. Take the generated Client Id and Client Secret and paste into the Auth. Provider created above
  3. Create the Salesforce Named Credential:

    1. Setup -> Named Credential -> New Named Credential
    2. Label = "Xero"
    3. Name = "Xero"
    4. URL = "https://api.xero.com"
    5. Identity Type = "Named Principal"
    6. Authentication Protocol = "OAuth 2.0"
    7. Authentication Provider = "Xero" (the provider created in step 1)
    8. Start Authentication Flow on Save = Checked (this will trigger the OAuth process to Xero)
  4. Deploy this package to a Salesforce environment (Deploy to Org)

  5. You now need to retrieve the Xero Tenant ID and store in the Custom Label:

    1. Run the Apex method XeroAPI.getXeroTenantId();
    2. Copy the returned value
    3. Update to the label: Setup -> Create -> Custom Labels -> Xero_Tenant_Id -> Edit -> Paste in value from above
  6. You can now access Xero API resources via Apex. Eg... XeroAPI.getContacts();

Usage

Once the above steps are complete, you can now access the example methods to access the Xero API resources. There are currently only a few pre-built methods set up to start using. Please use these as a base and extend as necessary.

Get Contacts

This method queries all contacts in your Xero org. To execute, simply run:

XeroAPI.getContacts();

And a list of type XeroContact is returned.

Create Contact

// Send Contact to Xero
Account myAccount = [SELECT Name, ... FROM Account];
XeroAPI.sendAccount(myAccount);

You can view example JSON requests here

Get Invoices

This method queries all invoices in your Xero org. To execute, run:

XeroAPI.getInvoices();

Create Invoice

This method creates an invoice for the given XML:

XeroInvoice newInvoice = new XeroInvoice();
newInvoice.Date_x = system.today();
... // Add additional Invoice details based on the XeroInvoice wrapper

// Send Invoice to Xero
XeroAPI.sendInvoice(XeroXmlUtility.serialize(newInvoice, 'Invoice'));

You can view example JSON requests here

Contributing

Feel free to fork this repo and use as you wish. I'd welcome anyone to add additional methods and add to this project, I will do the same as I go.

apex-for-xero's People

Contributors

benedwards44 avatar gordienoye avatar

Watchers

James Cloos avatar

Forkers

superzak1

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.