rpsimao/invoicexpress-api
Composer 安装命令:
composer require rpsimao/invoicexpress-api
包简介
Laravel Package to interact with InvoiceXpress API
README 文档
README
Laravel InvoiceXpress API
Laravel package to interact with InvoiceXpress API
Tested with Laravel 5.5.*
Table of Contents
1 - Installation
Via Composer
$ composer require rpsimao/invoicexpress-api
In your config/app.php, register Providers and the Facade (Not needed for Laravel 5.5 upwards)
'providers' => [ .... rpsimao\InvoiceXpressAPI\InvoiceXpressAPIServiceProvider::class, ..... 'aliases' => [ ..... 'InvoiceXpressClients' => rpsimao\InvoiceXpressAPI\InvoiceXpressAPIFacade::class, .....
1.1 - Publish configuration
$ php artisan vendor:publish --tag=ivxapi-config
In the configuration file, all the API endpoints are accessible, so for example you need to generate an invoice PDF:
config(invoicexpress.enpoints.invoice.generate_pdf);
All endpoints are generic like: 'api/pdf/{invoice-id}.xml', so there is a helper function for replacing the generic endpoint with the real value:
endpoint_replace(['the-real-value'], config(invoicexpress.enpoints.invoice.generate_pdf));
The first argument MUST be an array, and the number of the itens to replace, must match the items to be replaced in the endpoint. If not an exception is raised, and a fatal error is thrown.
1.2 - Migrations
$ php artisan vendor:publish --tag=ivxapi-migrations $ php artisan migrate
2 - Configuration
Add to your .env file your API Key and Account name
INVOICEXPRESS_API_KEY=
INVOICEXPRESS_ACCOUNT_NAME=
These will be read by the config file:
.... 'api_key' => env('INVOICEXPRESS_API_KEY'), 'account_name' => env('INVOICEXPRESS_ACCOUNT_NAME'), ....
If you do not want to put your API key in the .env file, or prefer to get it on every request, you can call the
getAPIKey()method. This way you can change the API key in your account frequently and not need to update the app.In the config file
invoicexpress, there are 2 empty fields['username', 'password']so you can put the username/password there, if you want.
$client = new InvoiceXpressAPI(); $api_key = $client->getAPIKey('my-username', 'my-password'); .... //later in the query .... $client->setQuery(['api_key' => $api_key]); ....
3 - Usage
Check the documentation for the params of the actions.
There are 2 Classes for working with the API:
3.1 - Eloquent Model
It has one custom function, for retrieve all your customers and put them into the DB.
You can make a cron job for retrieving them periodically.
//Accepts a flag (true or false[default]) InvoiceXpressClients::getAllClientsFromAPI(true);
If you pass the true flag, the function inserts the clients into the database. False or none, returns an array with all your clients.
If the client already exists, it updates the values.
3.1.1 - One-to-One relationship with Laravel::Auth()
If you wish to have a relationship between the InvoiceXpress and your app Users, do the following:
$ php artisan vendor:publish --tag=ivxapi-migrateauth $ php artisan migrate
In your Users Model, add the following method:
class User extends Model { ....... //Get the InvoiceXpress Client record associated with the user. public function invoicexpress() { return $this->hasOne('InvoiceXpressClients'); }
You now have a one-to-one relationship. Now you only have to insert the user_id in the InvoiceXpress table.
3.2 - Interact with the API
use rpsimao\InvoiceXpressAPI\Service\InvoiceXpressAPI; //Making a GET REQUEST $client = new InvoiceXpressAPI(); $client->setMethod('GET'); $client->setUrl(config('invoicexpress.my_url')); $client->setEndpoint(config('invoicexpress.endpoints.clients.list_all')); $client->setQuery(['api_key' => config('invoicexpress.api_key')]); $response = $client->talkToAPI(); ..... // Another GET Request to generate a PDF for an invoice $client = new InvoiceXpressAPI(); $client->setMethod('get'); $client->setUrl(config('invoicexpress.my_url')); $client->setEndpoint( endpoint_replace(['12759480'], config('invoicexpress.endpoints.invoices.generate_pdf')) ); $client->setQuery([ 'api_key' => config('invoicexpress.api_key'), 'invoice-id' => '12759480', 'second_copy' => true ]); $response = $client->talkToAPI(); //Making a POST REQUEST // Creating a new Client client = new InvoiceXpressAPI(); $client->setMethod('post'); $client->setUrl(config('invoicexpress.my_url')); $client->setEndpoint( config('invoicexpress.endpoints.clients.create')); $client->setQuery([ 'api_key' => config('invoicexpress.api_key'), 'client' => [ 'name' => 'My name', 'code' => 'My Client Code', 'email' => 'client@email.com' //.... insert more values .... ] ]); $response = $client->talkToAPI(); //Do whatever you need with the response //Making a PUT REQUEST $client = new InvoiceXpressAPI(); $client->setMethod('put'); $client->setUrl(config('invoicexpress.my_url')); $client->setEndpoint(endpoint_replace(['123456789'], config('invoicexpress.endpoints.clients.update'))); $client->setQuery([ 'api_key' => config('invoicexpress.api_key'), 'client-id' => '123456789', 'client' => [ 'name' => 'My awesome Client', 'code' => '123', 'phone' => 999888777 //.... insert more values .... ] ]); $response = $client->talkToAPI(); //Do whatever you need with the response
4 - Tests
Currently there are 4 tests available.
- A GET Request to Retrieve all Clients
- A PUT Request to Update Client Information
- A GET Request for retrieve your API key
- A GET Request for generate a Invoice PDF
For them to work, you have to fill with you own credentials | data:
class GetTest extends TestCase { // Use your own credentials|data to run the tests protected $url = ''; protected $api_key = ''; protected $username = ''; protected $password = ''; protected $client_id = ''; protected $client_name = ''; protected $client_code = ''; protected $client_phone = ''; protected $invoice = ''; .......
Then you can run the tests:
$ cd your-laravel-project-folder
$ vendor/bin/phpunit vendor/rpsimao/invoicexpress-api
If all goes well, you should receive:
OK (4 tests, 4 assertions)
5 - Messages
By default all Error / Success messages are returned in XML format.
If you wish to change to JSON, just add the setMsgFormat() method and pass the JSON flag:
..... $client = new InvoiceXpressAPI(); $client->setMsgFormat('json'); ......
5.1 - Error messages
The api_code and api_msg are the real messages that the InvoiceXpress API returns, the others are just for debugging.
The debugging tags only appears if in the .env file: APP_DEBUG=true
This is how the Error Messages are returned:
XML
<?xml version="1.0"?> <response> <api_code>500</api_code> <api_msg>Server error: `GET https://mycompany.app.invoicexpress.com/api/pdf/1234567.xml?api_key=11111abc2222def33333&invoice-id=1234567` resulted in a `500 Internal Server Error` response: An error occured on the server. We have been notified.</api_msg> <file>/Code/testapi/vendor/rpsimao/invoicexpress-api/src/Service/InvoiceXpressAPI.php</file> <line>408</line> <message>simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '&lt;' not found</message> </response>
JSON
{
"api_code":"500",
"api_msg":"Server error: `GET https:\/\/mycompany.app.invoicexpress.com\/api\/pdf\/1234567.xml?api_key=11111abc2222def33333&invoice-id=1234567` resulted in a `500 Internal Server Error` response:\nAn error occured on the server. We have been notified.\n\n",
"file":"\/Code\/testapi\/vendor\/rpsimao\/invoicexpress-api\/src\/Service\/InvoiceXpressAPI.php",
"line":385,
"message":"simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found"
}
5.2 - Success Messages
This is how the Success Messages are returned:
XML
<?xml version="1.0"?> <response> <api_code>200</api_code> <api_msg>OK</api_msg> <api_values><pdfUrl>https://invoicexpress-downloads.s3.amazonaws.com/store_00000_Factura-0000-a.pdf?AWSAccessKeyId=AAKKAAKKK&Expires=1501762080&Signature=vmePXICWhUBRLygwVO6y2Lx6x4M%3D&response-content-disposition=attachment%3B%20filename%3DFactura-0000-a.pdf&response-content-type=application%2Fpdf</pdfUrl></api_values> </response>
JSON
{
"response": {
"api_code": "200",
"api_msg": "OK",
"api_values": { "pdfUrl": "https://invoicexpress-downloads.s3.amazonaws.com/store_00000_Factura-0000-a.pdf?AWSAccessKeyId= AAKKAAKKK&Expires=1501762080&Signature=vmePXICWhUBRLygwVO6y2Lx6x4M%3D&response-content-disposition=attachment%3B%20filename%3DFactura-0000-a.pdf&response-content-type=application%2Fpdf" }
}
}
rpsimao/invoicexpress-api 适用场景与选型建议
rpsimao/invoicexpress-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 139 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 07 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「laravel」 「invoicexpress」 「invoicexpress-api」 「rpsimao」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rpsimao/invoicexpress-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rpsimao/invoicexpress-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rpsimao/invoicexpress-api 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple PHP wrapper for InvoiceXpress API
A PSR-7 compatible library for making CRUD API endpoints
Alfabank REST API integration
Laravel Package to interact with InvoiceXpress API
Laravel package for Accurate Online API integration.
A lightweight plain-PHP framework for database-backed CRUD APIs.
统计信息
- 总下载量: 139
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-24