cakephp-biztech/cakephp-xero-oauth2

Xero Oauth2 API plugin for CakePHP 3.x

0.2.3 2021-04-05 12:38 UTC

This package is auto-updated.

Last update: 2024-11-13 11:00:40 UTC


README

Latest Stable Version Total Downloads License Latest Unstable Version

This plugin provides access to Xero OAuth2 API for CakePHP. This plugin is wrapper around Xero PHP official SDK.

Requirements

  • CakePHP 3.5 or greater
  • PHP 5.6 or greater

Installation

  1. You can install this plugin into your CakePHP application using composer

    composer require cakephp-biztech/cakephp-xero-oauth2
    
  2. After installation, load the plugin

    Plugin::load('XeroOauth2', ['routes' => true]);

    Or, you can load the plugin using the shell command:

    bin/cake plugin load -r XeroOauth2
    
  3. Run plugin migration to create table

    bin/cake migrations migrate -p XeroOauth2
    

Setup

Now create new file to set your Xero App details.

  1. Create new file xero_config.php in config directory:

    <?php
    
    return [
        'XeroOauth2' => [
            'clientId' => 'your-client-id',
            'clientSecret' => 'your-client-secret',
            'baseUri' => 'https://example.com',
            'scope' => [
                'openid',
                'email',
                'profile',
                'offline_access',
                'accounting.settings',
                'accounting.contacts',
                // Any other scopes needed for your application goes here
            ],
            'successUrl' => 'http://example.com/success'
        ]
    ];

    Note: Do not forget to replace "https://example.com" with your website URL in your config/xero_config.php file.

  2. After creating the configuration file, make sure to load the file in your bootstrap.php:

    Configure::load('xero_config', 'default');

Important:

When you create your Xero API App you must have to specify 'OAuth 2.0 redirect URI' to https://your-website.com/xero-oauth2/callback (replace "https://your-website.com" with your website URL).

Usage

This plugin ships with XeroOauth component which can be used to get the instance of:

  • \XeroAPI\XeroPHP\Api\AccountingApi via accountingApi() method
  • \XeroAPI\XeroPHP\Api\AssetApi via assetApi() method
  • \XeroAPI\XeroPHP\Api\IdentityApi via identityApi() method
  • \XeroAPI\XeroPHP\Api\ProjectApi via projectApi() method

Load XeroOauth component:

$this->loadComponent('XeroOauth2.XeroOauth');
$accountingApi = $this->XeroOauth->accountingApi();

Once you have an instance of \XeroAPI\XeroPHP\Api\AccountingApi::class you're dealing directly with Xero's official SDK.

The Accounting API requires we pass through a tenant ID on each request. You can get value of that using Storage component. Also, you can get additional token details from this component as well.

$this->loadComponent('XeroOauth2.Storage');
$tenantId = $this->Storage->getXeroTenantId();

Following examples shows how to get contacts from Accounting API:

src/Controller/ContactsController.php

<?php
namespace App\Controller;

class ContactsController extends AppController
{
    public function index()
    {
        $this->loadComponent('XeroOauth2.Storage');
        $this->loadComponent('XeroOauth2.XeroOauth');

        $tenantId = $this->Storage->getXeroTenantId();
        $accountingApi = $this->XeroOauth->accountingApi();

        foreach ($accountingApi->getContacts($tenantId)->getContacts() as $contact) {
            debug([
                'id' => $contact->getContactId(),
                'name' => $contact->getName(),
                'email' => $contact->getEmailAddress(),
            ]);
        }
    }
}

You can also check XeroAPI Oauth2 App repository's example file.

Reference

Issues

Feel free to submit issues and enhancement requests.