fakturan.nu / fakturan
Fakturan.nu REST API 客户端
1.1.0
2015-05-11 14:23 UTC
Requires
- guzzlehttp/guzzle: 5.2
Requires (Dev)
- php-vcr/php-vcr: 1.2.*
- phpunit/phpunit: 4.5
This package is not auto-updated.
Last update: 2024-09-28 16:21:05 UTC
README
#Fakturan.nu REST API 客户端 PHP 为 Fakturan.nu 的基于网络的软件提供 PHP API-client。文档可以在 API 文档 中查看。
##入门
###要求
- PHP 5.4+
###安装 您可以通过将以下行添加到 composer.json 文件的 require 块来通过 Composer 安装库
"fakturan.nu/fakturan": "1.1.*"
之后运行 composer install
。
###设置 您只需添加加密的 key_id
和 password
进行身份验证,然后即可开始使用!请记住在开发过程中使用 sandbox-mode。准备在生产环境中使用时,只需删除第三个参数。
Fakturan\Fakturan::setup('KEY_ID', 'PASSWORD', [ 'sandbox' => TRUE ]);
###示例 检索所有产品
Fakturan\Model\Product::all();
创建新产品
$new_product = new Fakturan\Product(); $new_product->name = 'My new shiny product'; $new_product->unit = 'KG'; $new_product->price = 450; $new_product->save();
获取 ID 为 54 的单个产品
$book = Fakturan\Product::find(54);
通过搜索获取单个产品
$product = Fakturan\Product::findBy(['product_code' => 2]);
编辑产品
$book = Fakturan\Product::find(54); $book->price = 25; $book->save();
删除产品
$product_to_be_deleted = Fakturan\Product::find(54); $product_to_be_deleted->destroy();
创建发票
// Find your client $client = Fakturan\Client::find(1); $invoice = new Fakturan\Invoice(); $invoice->client_id = $client->id; $invoice->date = date('Y-m-d'); // Find a product to use for templating $product = Fakturan\Product::find(24); // Add the product to the invoice. The second parameter can override the default values. // It is used to set the amount and makes it possible to add a discount. // See https://fakturan.nu/apidocs/2/invoices/create.html for possible attributes on rows. $invoice->addRow($product, ['amount' => 5]); // It is also possible to add rows without a preset product by sending an array instead: $row = [ 'product_name' => 'My custom product', 'unit' => 'PCS', 'price' => 500, 'amount' => 24 ]; $invoice->addRow($row) // Rows only consisting of text can also be added $invoice->addRow(['text_row' => true, 'text' => 'Performed customizations: purple flames']); $invoice->save();
###错误 根据问题类型,API 将返回不同类型的异常
Fakturan\Error\AccessDenied // Catches 401 (access denied) Fakturan\Error\ResourceNotFound // Catches 404 (Resource not found). Fakturan\Error\ConnectionFailed // Catches 407 (Connection to server failed). Fakturan\Error\ResourceInvalid // Catches 422 (Validation errors). Fakturan\Error\ClientError // Catches 400-499 (Client errors). Fakturan\Error\ServerError // Catches 500-599 (Server related issues). Fakturan\Error\FakturanException // Catches all of the above
因为它们都继承自 Fakturan\Error\FakturanException
,所以它们都可以在单个 catch-block 中捕获
try { // Methods that sends a request to the server } catch(Fakturan\Error\FakturanException $e) { echo $e->getMessage(); }
在生产环境中,我们建议您在使用 save
、update_attributes
和 delete
时始终使用 try-catch 块