otisz/billingo-connector

此包已被废弃,不再维护。作者建议使用 otisz/laravel-billingo 包。

Billingo API 连接器

1.3.1 2019-05-19 16:27 UTC

This package is auto-updated.

Last update: 2022-02-01 13:16:29 UTC


README

此包是 Billingo API 2.0 的 PHP 连接器。完整的 API 文档可在 此处 查看。

安装

安装连接器最简单的方法是使用 Composer

composer require otisz/billingo-connector

然后使用您框架的自动加载,或简单地添加

<?php
  require 'vendor/autoload.php';

手动安装

如果您想完全不使用 Composer,您可以从存储库下载源代码并使用任何 PSR-4 兼容的自动加载器。

注册自己的自动加载器也是一种方法(尽管不推荐)

<?php
// Source: http://www.php-fig.org/psr/psr-4/examples/
spl_autoload_register(function ($class) {

    // project-specific namespace prefix
    $prefix = 'Otisz\\BillingoConnector';

    // base directory for the namespace prefix
    $base_dir = __DIR__ . '/src/';

    // does the class use the namespace prefix?
    $len = strlen($prefix);
    if (strncmp($prefix, $class, $len) !== 0) {
        // no, move to the next registered autoloader
        return;
    }

    // get the relative class name
    $relative_class = substr($class, $len);

    // replace the namespace prefix with the base directory, replace namespace
    // separators with directory separators in the relative class name, append
    // with .php
    $file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

    // if the file exists, require it
    if (file_exists($file)) {
        require $file;
    }
});

入门

您可以通过创建一个新的 Request 实例来开始向 Billingo API 发送请求

<?php
  use Otisz\BillingoConnector\Connector;

  $billingo = new Connector([
	  'public_key' => 'YOUR_PUBLIC_KEY',
	  'private_key' => 'YOUR_PRIVATE_KEY'
  ]);

Request 类负责处理您的应用程序与 Billingo API 服务器之间的通信,JWT 授权在后台处理。

JWT 时间偏差

为了调整客户端和 API 服务器之间的一些时间偏差,您可以在创建新实例时设置 leeway 参数。偏差以秒为单位,默认值为 60。这会修改 JWT 的 nbfiatexp 声明,因此默认偏差的情况下,令牌在签发时间前后一分钟内有效。

常规使用

获取资源

<?php
// Return the first page of the clients
$clients = $billingo->get('clients');
// Return the next page
$clients = $billingo->get('clients', ['page' => 2]);

// Return one client
$client = $billingo->get('clients/123456789');

保存资源

<?php
// save a new client
$clientData = [
  "name" => "Gigazoom LLC.",
  "email" => "rbrooks5@amazon.com",
  "billing_address" => [
      "street_name" => "Moulton",
      "street_type" => "Terrace",
      "house_nr" => "2797",
      "city" => "Preston",
      "postcode" => "PR1",
      "country" => "United Kingdom"
  ]
];
$billingo->post('clients', $clientData);

更新资源

<?php
// save client
$billingo->put('clients/123456789', $newData);

删除资源

<?php
// delete client
$billingo->delete('clients/123456789');

下载发票

您可以将生成的发票以 PDF 格式下载

当传递第二个参数时,您可以指定一个文件名或一个使用 fopen 打开的资源,其中 PDF 将被保存。否则将返回一个 GuzzleHttp\Psr7\Stream,您可以从中读取。

<?php
  $billingo->downloadInvoice('123456789', 'filename.pdf');

使用流接口

<?php
  $invoice = $billingo->downloadInvoice('123456789');
  if($invoice->isReadable()) {
      while(!$invoice->eof()) {
          echo $invoice->read(1);
      }    
  }