fullscreeninteractive/silverstripe-xero

安装: 71

依赖项: 0

建议者: 0

安全: 0

星标: 1

关注者: 2

分支: 1

开放问题: 0

类型:silverstripe-vendormodule

1.1.1 2022-10-16 19:38 UTC

This package is auto-updated.

Last update: 2024-08-30 01:32:23 UTC


README

维护者联系方式

安装

composer require "fullscreeninteractive/silverstripe-xero"

文档

calcinai/xero-php 提供轻量级包装,并增加了 Silverstripe 对认证和通过 OAuth 连接应用程序的支持。

要设置,请注册 Xero 应用程序并定义 clientId 和 clientSecret 作为环境变量。

XERO_CLIENT_ID='123'
XERO_CLIENT_SECRET='123'

一旦那些 API 密钥可用,在 设置 管理员下将出现一个新标签页,用于连接到 Xero。按照提示将所选账户链接到您的 Silverstripe 网站。

更新访问令牌

访问令牌有效期为 30 天,提供了一个计划好的队列作业 (RefreshXeroTokenJob),可以定期更新访问令牌。

在 Xero 中设置应用程序

  1. 前往 https://developer.xero.com/app/manage/
  2. 创建一个新的 Web App
  3. Redirect URI 设置为 https://www.yoursite.com/connectXero

注意 Silverstripe 中的 connectXero 端点仅对 ADMIN 用户开放。

与 API 交互

/** @var \XeroPHP\Application **/
$app = XeroFactory::singleton()->getApplication();

通过 https://github.com/calcinai/xero-php 集成与 API。有关创建发票等更多信息,请参阅该页面。

从成员创建 Xero 联系人

<?php

use Psr\Log\LoggerInterface;
use SilverStripe\ORM\DataExtension;
use FullscreenInteractive\SilverStripeXero\XeroFactory;
use SilverStripe\Core\Injector\Injector;

class XeroMemberExtension extends DataExtension
{
    private static $db = [
        'XeroContactID' => 'Varchar'
    ];

    public function updateCMSFields(FieldList $fields)
    {
        $fields->makeFieldReadonly('XeroContactID');
    }

    public function getXeroContact()
    {
        try {
            $xero = XeroFactory::singleton()->getApplication();
        } catch (Throwable $e) {
            $xero = null;
        }

        if ($xero) {
            try {
                $contact = null;

                if ($id = $this->owner->XeroContactID) {
                    $contact = $xero->loadByGUID('Accounting\\Contact', $id);
                }

                if (!$contact) {
                    $existing = $xero->load('Accounting\\Contact')
                        ->where('EmailAddress!=null AND EmailAddress.Contains("' . trim($this->owner->Email) . '")')
                        ->execute();

                    if (count($existing) > 1) {
                        $contact = $existing->offsetGet(0);
                    }
                }

                if (!$contact) {
                    // create the record
                    $contact = new \XeroPHP\Models\Accounting\Contact();
                    $contact->setName($this->owner->Name);
                    $contact->setFirstName($this->owner->FirstName);
                    $contact->setLastName($this->owner->Surname);
                    $contact->setEmailAddress($this->owner->Email);

                    try {
                        $xero->save($contact);
                    } catch (Exception $e) {
                        if (strpos($e->getMessage(), 'Already assigned to') !== false) {
                            $contact->setName($this->owner->Name . ' ' . date('Y-m-d'));

                            try {
                                $xero->save($contact);
                            } catch (Exception $e) {
                                Injector::inst()->get(LoggerInterface::class)->warning($e);
                            }
                        }
                    }
                }

                return $contact;
            } catch (Exception $e) {
                Injector::inst()->get(LoggerInterface::class)->error($e);
            }
        }
    }
}