synergitech/exact-php-client

此包已被放弃,不再维护。未建议替代包。

Exact API 的 PHP 客户端

4.0.2 2017-10-19 17:12 UTC

README

Build Status

PHP 客户端库,用于使用 Exact Online API。

注意:对于 Guzzle 6 使用 v2,对于 Guzzle 3 使用 v1。

Composer 安装

可以通过 Composer 安装此 PHP 客户端。

composer require picqer/exact-php-client

使用方法

  1. 在 Exact App Center 设置应用以获取凭据
  2. 从您的应用授权集成
  3. 解析回调并完成连接设置
  4. 使用库进行操作

步骤 1-3 在设置时只需执行一次。

在 Exact App Center 设置应用以获取凭据

在 Exact App Center 设置一个应用以获取您的 Client IDClient Secret。您还需要设置正确的 Callback URL 以使 OAuth 舞蹈工作。

从您的应用授权集成

以下是一个示例 authorize() 函数。

$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('CALLBACK_URL'); // Same as entered online in the App Center
$connection->setExactClientId('CLIENT_ID');
$connection->setExactClientSecret('CLIENT_SECRET');
$connection->redirectForAuthorization();

这将使用户重定向到 Exact 进行登录并授权您的集成与他们的帐户。

解析回调并完成连接设置

Exact 将重定向回您提供的 callback url。回调将接收一个 code 参数。这是 OAuth 的授权代码。存储此代码。

创建一个新的 Exact 连接,以便库可以交换代码并获取 accesstokenrefreshtokenaccesstoken 是一个临时令牌,它允许您的应用与 Exact 进行通信。refreshtoken 是一个令牌,用于获取一个新的 accesstoken,同时也刷新 refreshtoken。库将为您处理所有这些。以下是一个可能的 connect() 函数,它返回 API 连接。

$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('CALLBACK_URL');
$connection->setExactClientId('CLIENT_ID');
$connection->setExactClientSecret('CLIENT_SECRET');

if (getValue('authorizationcode')) // Retrieves authorizationcode from database
	$connection->setAuthorizationCode(getValue('authorizationcode'));

if (getValue('accesstoken')) // Retrieves accesstoken from database
	$connection->setAccessToken(unserialize(getValue('accesstoken')));

if (getValue('refreshtoken')) // Retrieves refreshtoken from database
	$connection->setRefreshToken(getValue('refreshtoken'));

if (getValue('expires_in'))  // Retrieves expires timestamp from database
	$connection->setTokenExpires(getValue('expires_in'));

// Make the client connect and exchange tokens
try {
	$connection->connect();
} catch (\Exception $e)
{
	throw new Exception('Could not connect to Exact: ' . $e->getMessage());
}

// Save the new tokens for next connections
setValue('accesstoken', serialize($connection->getAccessToken()));
setValue('refreshtoken', $connection->getRefreshToken());

// Optionally, save the expiry-timestamp. This prevents exchanging valid tokens (ie. saves you some requests)
setValue('expires_in', $connection->getTokenExpires());

关于部门(行政)

默认情况下,库将使用用户的默认行政。这意味着当用户在 Exact Online 中切换行政时,库也将开始使用此行政。

使用库进行操作(示例)

// Optionally set administration, otherwise use the current administration of the user
$connection->setDivision(123456);

// Create a new account
$account = new Account($connection);
$account->AddressLine1 = $customer['address'];
$account->AddressLine2 = $customer['address2'];
$account->City = $customer['city'];
$account->Code = $customer['customerid'];
$account->Country = $customer['country'];
$account->IsSales = 'true';
$account->Name = $customer['name'];
$account->Postcode = $customer['zipcode'];
$account->Status = 'C';
$account->save();


// Add a product in Exact
$item = new Item($connection);
$item->Code = $productcode;
$item->CostPriceStandard = $costprice;
$item->Description = $name;
$item->IsSalesItem = true;
$item->SalesVatCode = 'VH';
$item->save();


// Retrieve an item
$item = new Item($connection);
$item->find(ID);

// List items
$item = new Item($connection);
$item->get();

// List items with filter (using a filter always returns a collection)
$item = new Item($connection);
$items = $item->filter("Code eq '$productcode'"); // Uses filters as described in Exact API docs (odata filters)

// Create new invoice with invoice lines
$items[] = [
	'Item'      => $itemId,
	'Quantity'  => $orderproduct['amount'],
	'UnitPrice' => $orderproduct['price']
];

$salesInvoice = new SalesInvoice($this->connection());
$salesInvoice->InvoiceTo = $customer_code;
$salesInvoice->OrderedBy = $customer_code;
$salesInvoice->YourRef = $orderId;
$salesInvoice->SalesInvoiceLines = $items;

连接到荷兰以外的其他 Exact 国家

根据 Exact 开发者指南 选择正确的基 URL。

<?php
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('CALLBACK_URL');
$connection->setExactClientId('CLIENT_ID');
$connection->setExactClientSecret('CLIENT_SECRET');
$connection->setBaseUrl('https://start.exactonline.de');

查看 src/Picqer/Financials/Exact 了解所有可用的实体。

Webhooks

可以通过 WebhookSubscription 实体来管理 webhook 订阅。

对于验证传入的 webhook 调用,您可以使用 Authenticatable 特性。将 authenticate 方法提供完整的 JSON 请求和 Exact 提供的 Webhook secret,它将返回 true 或 false。

故障排除

'Picqer\Financials\Exact\ApiException' 错误信息:'错误 400:请向查询字符串添加 $select 或 $top=1 语句。

在某些特定情况下,遗憾的是 Exact API 文档中未对此进行说明,这是必需的。可能是为了防止请求过载。遇到此错误时,您需要添加一个 select 或 top。select 用于提供您想要提取的字段列表,$top=1 限制结果为一条项目。

示例

仅返回 EntryID 和 FinancialYear。

$test = new GeneralJournalEntry($connection);
var_dump($test->filter('', '', 'EntryID, FinancialYear'));

$top=1 添加方式如下

$test = new GeneralJournalEntry($connection);
var_dump($test->filter('', '', '', ['$top'=> 1]));

认证错误

'致命错误:未捕获的异常:无法连接到 Exact:客户端错误:POST https://start.exactonline.nl/api/oauth2/token 导致 400 Bad Request 响应:Bad Request in /var/www/html/oauth_call_connect.php:61 Stack trace: #0 {main} thrown in /var/www/html/oauth_call_connect.php on line 61`'

此错误发生是因为您在重定向 URL 中获得的代码仅对一次调用有效。当您使用一个 "已使用" 的代码再次调用认证过程时,您会得到这个错误。请确保您只使用 Exact Online 提供的代码一次以获取访问令牌。

代码示例

例如,请参阅: example/example.php

待办事项

  • 当前实体不包含所有可用属性。如果您需要它们,请随时提交包含或扩展实体的 PR。使用 greasemonkey 或 tampermonkey 中的 userscript.js 生成一致且完整的实体。