启用简单的 Fortnox 集成

1.0.13 2020-10-03 06:05 UTC

README

Build Status

Fortie

一个简单的 Fortnox PHP 包,包括 Laravel Service Provider 以便在 Laravel 中轻松集成。

安装

安装此包的最简单方法是使用 Composer。

composer require wetcat/fortie

或者将 "wetcat/fortie": "^1.0" 添加到您的 composer.json 文件中。

如果您没有 Composer,您应该 安装它

Laravel

在 Laravel 中,使用 Fortie 的最简单方法是添加 ServiceProvider,在 config/app.php 中将以下行添加到 providers 数组中。这在使用 artisan 命令之前是必需的。

<?php

return [
    
    ...

    'providers' => [

        ...

        Wetcat\Fortie\FortieServiceProvider::class,

    ],

    ...

];

配置

在 Laravel 中,您可以使用 php artisan vendor:publish --provider="Wetcat\Fortie\FortieServiceProvider" --tag="config" 命令发布配置文件,之后该文件应位于 app/fortie.php 中。使用 Fortnox 注册时提供的详细信息。

访问令牌 在注册时不会提供,您需要使用您的 授权码客户端密钥 分别获取。有关此过程的更多信息,请参阅此处

配置文件应如下所示

<?php 

return [

  'default' => [

    // Your specific access token
    'access_token'   => '<your access token>',

    // Your specific client secret
    'client_secret'  => '<your client secret>',

    // The type you're sending
    'content_type'   => 'application/json',

    // The type you're accepting as response
    'accepts'        => 'application/json', 

    // The URL to the Fortnox API
    'endpoint'       => 'https://api.fortnox.se/3/',
    
    // Rate limit (number of requests per second)
    'rate_limit'  => 4,
  ],

];

请注意,XML 目前尚不支持,该包可以读取并将 Fortnox 的响应尝试转换为 XML 结构,但在向 Fortnox 发送数据时,它始终发送 json。

使用

在包含 ServiceProvider 后,您可以在您的 BaseController 中使用依赖注入,以使 Fortie 可用于所有控制器。

[...]

use Wetcat\Fortie\Fortie;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected $fortie;

    public function __construct(Fortie $fortie)
    {
        $this->fortie = $fortie;
    }

}

只需调用 $this->fortie->... 来访问 Fortie。

class MyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        dd($this->fortie->accounts()->all());
    }

    [...]

}

当然,您也可以将 Fortie 注入任何 Laravel 控制器方法,以限制应用程序范围内对 Fortnox 的访问。

[...]

use Wetcat\Fortie\Fortie;

class MyController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Fortie $fortie)
    {
        dd($fortie->accounts()->all());
    }

    [...]

}

非 Laravel

在 Laravel 中,ServiceProvider 负责使用配置文件设置 Fortie,如果用于 Laravel 之外,则需要使用 Fortnox 提供的配置实例化 Fortie。

$fortie = new Fortie(
    $endpoint,
    $access_token,
    $client_secret,
    $content_type,
    $accepts
);

访问令牌

要获取访问令牌(用于您的集成),您需要使用 authroization-code 请求。

在 Laravel 中,您可以使用 Artisan 命令轻松获取访问令牌。

php artisan fortie:activate code=<your authorization code>

提供商

该包配置了多个提供商,每个提供商都映射到 REST API 中的特定端点。例如,账户 映射到 accounts() 方法。

$arrayOfAccounts = $fortie->accounts()->all();

有关所有提供商的用法详细信息,请参阅 Wiki

当前实现的提供商

  • 缺席交易
  • 账户图表
  • 账户
  • 存档
  • 文章文件连接
  • 文章
  • 文章 URL 连接
  • 出勤交易
  • 公司设置
  • 合同递延收益
  • 合同
  • 合同模板
  • 成本中心
  • 货币
  • 客户
  • 员工
  • 财政年度
  • 收件箱
  • 发票递延收益
  • 发票付款
  • 发票
  • 标签
  • 锁定期间
  • 支付方式
  • 报价
  • 订单
  • 预定义账户
  • 预定义凭证系列
  • 价格表
  • 价格
  • 打印模板
  • 项目
  • 工资交易
  • 调度时间
  • 供应商发票应计
  • 供应商发票外部URL连接
  • 供应商发票文件连接
  • 供应商发票付款
  • 供应商发票
  • 供应商
  • 税收减免
  • 交货条款
  • 支付条款
  • 可信邮件发送者
  • 单位
  • 凭单文件连接
  • 凭单
  • 凭单系列
  • 交货方式

依赖关系

此包由以下依赖项构建。

故障排除

如果您遇到cURL错误(特别是cURL错误56),您可以检查Stackoverflow上的(这个)[http://stackoverflow.com/a/26538127/388320]

许可证

版权所有2015 安德烈亚斯·戈兰松

根据Apache许可证版本2.0(“许可证”)许可;除非遵守许可证规定,否则您不得使用此文件。您可以在以下位置获取许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则根据许可证分发的软件按“原样”分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言管理权限和限制,请参阅许可证。