voov/billingo-api-connector

Billingo API Connector

1.1.1 2019-09-24 07:01 UTC

This package is auto-updated.

Last update: 2024-09-17 17:31:12 UTC


README

此软件包是 Billingo API 2.0 的 PHP 连接器。完整的 API 文档可在 此处 获取。

安装

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

composer require voov/billingo-api-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 = 'Billingo\\API\\Connector';

    // 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 Billingo\API\Connector\HTTP\Request;

  $billingo = new Request([
	  '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);
      }    
  }