covergenius/xcover-php
Composer 安装命令:
composer require covergenius/xcover-php
包简介
XCover API SDK for PHP
关键字:
README 文档
README
XCover SDK for PHP
XCover SDK simplifies XCover API integration in PHP applications.
The library is based on Guzzle 7 HTTP client and offers the following features:
- AuthMiddleware performs authentication
- JsonResponseMiddleware provides convenient
jsonmethod on the Guzzle responses - HTTP abstraction in XCover class
- Automatic response status code validation and custom exception classes
Installation
XCover SDK is available on Packagist and the recommended way of installing it is via Composer.
V1 releases are for PHP 7.2 or higher (but less than 8)
V2 releases are for PHP 7.4 as well as 8.0 and higher
composer require covergenius/xcover-php
Usage
Basic usage
use XCoverClient\Config; use XCoverClient\XCover; // Instantiate client $client = new XCover(new Config([ 'baseUrl' => env('BASE_URL'), 'apiPrefix' => env('API_PREFIX'), 'apiKey' => env('API_KEY'), 'apiSecret' => env('API_SECRET'), 'partner' => env('PARTNER_CODE'), ])); // Quote request $quoteResponse = $client->createQuote( [ 'request' => [ [ 'policy_type' => 'event_ticket_protection', 'policy_type_version' => 1, 'policy_start_date' => '2019-12-01T17:59:00.831+00:00', 'event_datetime' => '2019-12-25T21:00:00+00:00', 'event_name' => 'Ariana Grande', 'event_location' => 'The O2', 'number_of_tickets' => 2, 'total_ticket_price' => 100, 'resale_ticket' => false, 'event_country' => 'GB' ] ], 'currency' => 'GBP', 'customer_country' => 'GB', 'customer_region' => 'London', 'customer_language' => 'en' ] ); $quotePackage = $quoteResponse->json(); // Quote package array will contain all information required to display the insurance offering echo $quotePackage['id']; // 'JWFFM-M3W3Y-INS' echo $quotePackage['total_price']; // 5.00 echo $quotePackage['quotes'][0]['price']; // 5.00 echo $quotePackage['quotes'][0]['tax']['total_tax']; // 1.00 echo $quotePackage['quotes'][0]['tax']['total_tax_formatted']; // '£ 1.00' echo $quotePackage['quotes'][0]['content']['title']; // 'Ticket Protection' echo $quotePackage['quotes'][0]['content']['description']; // 'Covers the purchase cost of tickets (up to a maximum of £500) if you are unable to attend a booked event as result of an unexpected circumstance.' echo $quotePackage['quotes'][0]['pds_url']; // 'https://xcover.com/en/pds/JWFFM-M3W3Y-INS?policy_type=event_ticket_protection_v1' // To report insurance booking echo $quotePackage['quotes'][0]['id']; // '40e9859d-9a2c-47fb-a0a1-5d121fc68fdd' $bookingResponse = $client->createBooking($quotePackage['id'], [ 'quotes' => [ [ 'id' => '40e9859d-9a2c-47fb-a0a1-5d121fc68fdd', ] ], 'policyholder' => [ 'first_name' => 'John', 'last_name' => 'Doe', 'email' => 'john.doe@gmail.com', 'age' => 30, 'country' => 'GB' ] ] ); // Booking has the same id as quote package and a similar structure $booking = $bookingResponse->json(); echo $booking['id']; // 'JWFFM-M3W3Y-INS' echo $booking['status']; // 'CONFIRMED' echo $booking['total_price']; // 5.00
Please refer to tests/XCoverTest.php file for more examples.
XCover class provides methods for all operations listed in XCover API Documentation.
Customising Guzzle client
To customise Guzzle configuration you can pass custom client instance in XCover constructor.
Guzzle 6 client instance is immutable so you need to prepare it beforehand and add all mandatory config options, e.g:
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use XCoverClient\Config; use XCoverClient\Middleware\AuthMiddleware; use XCoverClient\Middleware\JsonResponseMiddleware; use XCoverClient\XCover; // Add XCover mandatory middlewares $handlerStack = HandlerStack::create(); $handlerStack->push( new AuthMiddleware([ 'apiKey' => $this->config->apiKey(), 'apiSecret' => $this->config->apiSecret(), ]), 'auth' ); $handlerStack->push(new JsonResponseMiddleware, 'json_response'); // You can add your custom middlewares here // You can add your custom options to the Guzzle's Client constructor below $client = new Client([ 'handler' => $handlerStack, 'headers' => [ 'Content-Type' => 'application/json', 'X-Api-Key' => $this->config->apiKey(), ], ]); // Now pass it to XCover constructor as second argument $client = new XCover(new Config([ 'baseUrl' => env('BASE_URL'), 'apiPrefix' => env('API_PREFIX'), 'apiKey' => env('API_KEY'), 'apiSecret' => env('API_SECRET'), 'partner' => env('PARTNER_CODE'), ]), $client);
It is also possible to extend XCover Class and override createDefaultClient function.
TODO
There are a few features which we will add in the future releases:
- Async calls using Guzzle's promises
- CircuitBreaker middleware
- BYO HTTP Client
Testing
PHPUnit with PHP-VCR are used to run tests.
Tests are run on every push and pull request to the repository.
Run the below command to run tests
composer test
Code Style
PHP_CodeSniffer is used for coding standard. We currently follow PSR-1 and PSR-2 as a standard.
Lint checks are run on every push and pull request.
Run the below command to check for any code style issues.
composer lint
You can view the php-cs rules at phpcs.xml
Static Analysis
PHPStan is used for static analysis of code. PHPStan is a PHP Static Analysis Tool. PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code.
Static Analysis is run on every push and pull request
run the command below to check the code with PHPStan
composer analyse
Changelog
You can view the changelog at CHANGELOG.md
Security
You can iew the security policy at SECURITY.md
Licence
You can view license information at LICENSE
covergenius/xcover-php 适用场景与选型建议
covergenius/xcover-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51.57k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 01 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「insurance」 「xcover」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 covergenius/xcover-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 covergenius/xcover-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 covergenius/xcover-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
A money wrapper class
Insurance library: CASCO, Green card
haimianbao insurance SDK
Assur Connect API client library for PHP.
A simple and fast UK National Insurance Number Validator
统计信息
- 总下载量: 51.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-01-03