洪水x92 / kh-gateway
v1.0.0
2024-05-15 15:14 UTC
Requires
- php: >=8.1
- ext-curl: *
- ext-openssl: *
- json-mapper/laravel-package: ^2.5
Requires (Dev)
- laravel/pint: ^1.15
- phpunit/phpunit: ^10
This package is auto-updated.
Last update: 2024-09-15 16:14:55 UTC
README
此包提供了一种简单的方式与K&H银行网关API交互。此包与K&H银行没有关联。
使用官方文档了解完整的支付流程: K&H官方文档
要求
- PHP 8.1或更高版本
安装
您可以通过composer安装此包
composer require floodx92/kh-gateway
用法
首先,您需要实例化CurlHttpClient
和OpenSSLAdapter
类。您始终可以使用new ClassName()
或ClassName::make()
来实例化一个类。
use Floodx92\KhGateway\Util\CurlHttpClient; use Floodx92\KhGateway\Crypto\OpenSSLAdapter; use Floodx92\KhGateway\ApiService; use Floodx92\KhGateway\Storage\FileKeyStorage; // Instantiate the Http client, with the API URL $client = CurlHttpClient::make(ApiService::SANDBOX_URL); //client = CurlHttpClient::make(ApiService::PRODUCTION_URL); // For the OpenSSLAdapter, first we need a KeyStore $keyStore = FileKeyStorage::make( 'path/to/your/private.key', 'path/to/your/public.key', //optionally you can pass a passphrase as last argument ) $adapter = OpenSSLAdapter::make($keyStore, $verifySignature = true);
然后,您可以使用ApiService
类向API发送请求。
use Floodx92\KhGateway\ApiService; use Floodx92\KhGateway\Model\Request\EchoRequest; use Floodx92\KhGateway\Exception\HttpException; $api = ApiService::make($adapter, $client); // Send an Echo request $echoRequest = EchoRequest::make($merchantId = 'ABC123456') try { $echoResult = $api->echo($echoRequest); echo $echoResult->resultCode; } catch (HttpException $e) { // Handle the exception }
错误处理
所有方法在发生cURL错误或收到HTTP错误状态码时都可能抛出HttpException
。您应该在代码中捕获此异常并适当处理。
待办事项
- 添加测试
- 添加更多文档
- 改善响应的JSON映射
贡献
有关详细信息,请参阅CONTRIBUTING。
许可证
此包是开源软件,根据MIT许可证授权。