coolpay / coolpay-php-client
用于与支付服务提供商 CoolPay 通信的 PHP-SDK
Requires
- php: >=5.3.3
- ext-curl: *
Requires (Dev)
- phpunit/phpunit: 4.8.*
- satooshi/php-coveralls: 1.0.*
- squizlabs/php_codesniffer: 2.8.*
This package is auto-updated.
Last update: 2024-09-20 23:39:03 UTC
README
coolpay-php-client
是 CoolPay API 的官方客户端。CoolPay API 允许您以安全可靠的方式接受支付。此包目前支持 CoolPay v10
api。
安装
Composer
如果您使用 Composer 管理项目依赖项,只需在项目的 composer.json 文件中添加 coolpay/coolpay-php-client 依赖项即可。以下是一个 composer.json 文件的示例,其中仅定义了 CoolPay 最新稳定版本的依赖项:
{
"require": {
"coolpay/coolpay-php-client": "1.0.*"
}
}
手动上传
如果您无法使用 Composer 和 Composer 自动加载器提供的所有好处,您可以上传 /CoolPay/
到您的网站空间。然而,那时您需要自己管理类的自动加载。
使用方法
在开始之前,您应该在 CoolPay 上注册自己并获取访问凭证。如果您还没有,请 点击这里 申请。
创建一个新的客户端
首先,您应该创建一个客户端实例,该实例是无名的或通过 CoolPay 提供的 api_key
或登录凭证授权的。
初始化匿名客户端
<?php use CoolPay\CoolPay; try { $client = new CoolPay(); } catch (Exception $e) { //... } ?>
使用 CoolPay Api Key 初始化客户端
<?php use CoolPay\CoolPay; try { $api_key = 'xxx'; $client = new CoolPay(":{$api_key}"); } catch (Exception $e) { //... } ?>
或者,您可以提供类似以下登录凭证
<?php use CoolPay\CoolPay; try { $qp_username = 'xxx'; $qp_password = 'xxx'; $client = new CoolPay("{$qp_username}:{$qp_password}"); } catch (Exception $e) { //... } ?>
API 调用
之后,您可以调用 CoolPay API 中描述的任何方法,对应于相应的 HTTP 方法和端点。目前支持以下方法:get
、post
、put
、patch
和 delete
。
// Get all payments $payments = $client->request->get('/payments'); // Get specific payment $payments = $client->request->get('/payments/{id}'); // Create payment $form = array( 'order_id' => $order_id, 'currency' => $currency, ... ); $payments = $client->request->post('/payments', $form); $status = $payments->httpStatus(); if ($status == 201) { // Successful created }
处理响应
获取 HTTP 状态码
$response = $client->request->get('/payments'); $status = $response->httpStatus(); if ($status == 200) { // Successful request }
返回的响应对象支持 3 种返回响应体的方式:asRaw()
、asObject
和 asArray()
。
// Get the HTTP status code, headers and raw response body. list($status_code, $headers, $response_body) = $client->request->get('/payments')->asRaw(); // Get the response body as an object $response_body = $client->request->get('/payments')->asObject(); // Get the response body as an array $response_body = $client->request->get('/payments')->asArray(); // Example usage $payments = $client->request->get('/payments')->asArray(); foreach($payments as $payment) { //... }
有关 API 响应的更多信息,请参阅 http://www.coolpay.com/docs。
测试
使用 Composer 创建自动加载器
$ composer install $ phpunit