Blog Detail

27

Jul
Access Xero Accounting System Using an Eloquent-like Syntax cover image

arrow_back Access Xero Accounting System Using an Eloquent-like Syntax

Xero Laravel allows developers to access the Xero accounting system using an Eloquent-like syntax.

Please note that this version of Xero Laravel supports the Xero OAuth 2.0 implementation. Older Xero apps using OAuth 1.x are no longer supported.

Installation

Xero Laravel can be easily installed using Composer. Just run the following command from the root of your project.

composer require langleyfoxall/xero-laravel

Setup

First, run the following artisan command from the root of your project. This will publish the package configuration file.

php artisan vendor:publish --provider="LangleyFoxall\XeroLaravel\Providers\XeroLaravelServiceProvider"

You now need to populate the config/xero-laravel-lf.php file with the credentials for your Xero app(s). You can create apps and find the required credentials in the My Apps section of your Xero account.

If you only intend to use one Xero app, the standard configuration file should be sufficient. All you will need to do is add the following variables to your .env file.

XERO_CLIENT_ID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XERO_CLIENT_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XERO_REDIRECT_URI=https://example.com/xero-callback

OAuth 2.0 flow

In order for users to make use of your Xero app, they must first give your app permission to access their Xero account. To do this, your web application must do the following.

  • Redirect the user to the Xero authorization URL.
  • Capture the response from Xero, and obtain an access token.
  • Retrieve the list of tenants (typically Xero organizations), and let the user select one.
  • Store the access token and selected tenant ID against the user’s account for future use.
  • Before using the access token, check if it has expired and refresh it if necessary.

Usage

To use Xero Laravel, you first need to get retrieve your user’s stored access token and tenant id. You can use these to create a new XeroApp object which represents your Xero application.

use LangleyFoxall\XeroLaravel\XeroApp;
use League\OAuth2\Client\Token\AccessToken;

$user = auth()->user(); 

$xero = new XeroApp(
            new AccessToken(json_decode($user->xero_oauth_2_access_token)),
            $user->xero_tenant_id
        );

You can then immediately access Xero data using Eloquent-like syntax. The following code snippet shows the available syntax. When multiple results are returned from the API they will be returned as Laravel Collection.

# Retrieve all contacts
$contacts = $xero->contacts()->get();                               
$contacts = $xero->contacts;

# Retrieve contacts filtered by name
$contacts = $xero->contacts()->where('Name', 'Bank West')->get();

# Retrieve an individual contact filtered by name
$contact = $xero->contacts()->where('Name', 'Bank West')->first();

# Retrieve an individual contact by its GUID
$contact = $xero->contacts()->find('34xxxx6e-7xx5-2xx4-bxx5-6123xxxxea49');

# Retrieve multiple contact by their GUIDS
$contacts = $xero->contacts()->find([
    '34xxxx6e-7xx5-2xx4-bxx5-6123xxxxea49',
    '364xxxx7f-2xx3-7xx3-gxx7-6726xxxxhe76',
]);

For more details, You can visit Github.

Closing Note:

We are highly results-driven experts having experience in the grounds of Laravel web application development, CRM software, SAAS applications, and eCommerce web apps. So If you want to develop a custom web app you can contact us or Start a project with us.

Published at : 27-07-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project