cakephp-biztech/cakephp-xero-oauth2 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cakephp-biztech/cakephp-xero-oauth2

Composer 安装命令:

composer require cakephp-biztech/cakephp-xero-oauth2

包简介

Xero Oauth2 API plugin for CakePHP 3.x

README 文档

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.

统计信息

  • 总下载量: 1.54k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-04