cakephp-biztech / cakephp-xero-oauth2
Xero Oauth2 API plugin for CakePHP 3.x
Installs: 1 489
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 1
Type:cakephp-plugin
Requires
- cakephp/cakephp: ^3.5
- xeroapi/xero-php-oauth2: ^2.1
Requires (Dev)
- phpunit/phpunit: ^5.7.14|^6.0
README
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
-
You can install this plugin into your CakePHP application using composer
composer require cakephp-biztech/cakephp-xero-oauth2
-
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
-
Run plugin migration to create table
bin/cake migrations migrate -p XeroOauth2
Setup
Now create new file to set your Xero App details.
-
Create new file
xero_config.php
inconfig
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. -
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
viaaccountingApi()
method\XeroAPI\XeroPHP\Api\AssetApi
viaassetApi()
method\XeroAPI\XeroPHP\Api\IdentityApi
viaidentityApi()
method\XeroAPI\XeroPHP\Api\ProjectApi
viaprojectApi()
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
- Official Xero PHP SDK: https://github.com/XeroAPI/xero-php-oauth2
- Example: https://github.com/XeroAPI/xero-php-oauth2-app/blob/master/example.php
Issues
Feel free to submit issues and enhancement requests.