justcommunication-ru / paykeeper-sdk
paykeeper.ru 收款 PHP SDK
v0.1.2
2024-04-25 10:04 UTC
Requires
- ext-json: *
- guzzlehttp/guzzle: ^6.2 || ^7.0
- psr/log: *
Requires (Dev)
- monolog/monolog: ^3.5
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-09-25 10:59:40 UTC
README
PHP SDK 为 https://paykeeper.ru
安装
composer require justcommunication-ru/paykeeper-sdk
使用
use JustCommunication\PaykeeperSDK\PaykeeperAPIClient; $client = new PaykeeperAPIClient($url, $username, $password, $tokenHandler);
$url — 您的 Paykeeper 账户地址。每个客户均单独提供。例如:https://somename.server.paykeeper.ru
$username — 用户名
$password — 密码
$tokenHandler — 可选参数 – 令牌处理器。
令牌处理器
安全令牌仅限于一定时间,建议自动保存和更新。如果 API 返回了令牌验证错误,则会尝试自动更新此令牌并重新请求。
此参数是可选的,如果不传递,则将使用 InMemory 处理器。
可用的处理器
InMemoryTokenHandler
在内存中存储处理器,如果 PHP 脚本已终止,则在下一次请求时将请求新的令牌。
FileTokenHandler
在文件系统中存储令牌
use JustCommunication\PaykeeperSDK\TokenHandler\FileTokenHandler; $tokenHandler = new FileTokenHandler('/path/to/token.txt');
CallbackTokenHandler
允许通过回调读取和保存令牌的处理器
use JustCommunication\PaykeeperSDK\TokenHandler\CallbackTokenHandler; $tokenHandler = new CallbackTokenHandler(function () use ($db) { return $db->fetchColumn('SELECT token FROM tokens WHERE name = "paykeeper"'); }, function ($new_token) { $db->update('UPDATE tokens SET token = :token WHERE name = "paykeeper"', [ 'token' => $new_token ]); });
自定义处理器
为了实现自己的处理器,请实现接口 TokenHandlerInterface
方法
准备账单
use JustCommunication\PaykeeperSDK\API\Invoice\InvoicePreviewRequest; $invoicePreviewRequest = new InvoicePreviewRequest(); $invoicePreviewRequest ->setOrderId(123) ->setServiceName('Test service') ->setAmount(100) ; try { $response = $client->sendInvoicePreviewRequest($invoicePreviewRequest); header('Location: ' . $response->getInvoiceUrl()); // перенаправляем пользователя на страницу оплаты exit; } catch (PaykeeperAPIException $e) { // обработка ошибки }
获取账单信息
$invoice = $client->getInvoice($invoice_id); $invoice->getId(); $invoice->getStatus(); $invoice->getCreatedAt(); $invoice->getPaidAt(); $invoice->getPayAmount();
账单历史
use JustCommunication\PaykeeperSDK\API\Invoice\InvoiceListRequest; $invoiceListRequest = new InvoiceListRequest(); $invoiceListRequest->setStatuses([ $invoiceListRequest::STATUS_SENT, $invoiceListRequest::STATUS_PAID, $invoiceListRequest::STATUS_EXPIRED ]) ->setStartDate(new \DateTime('-10 days')) ->setEndDate(new \DateTime('+10 days')) ; $response = $client->sendInvoiceListRequest($invoiceListRequest); foreach ($response->getInvoices() as $invoice) { $invoice->getId(); $invoice->getStatus(); $invoice->getCreatedAt(); $invoice->getPaidAt(); $invoice->getPayAmount(); }
配置 HTTP 客户端
方法 #1:传递参数数组
use JustCommunication\PaykeeperSDK\PaykeeperAPIClient; $client = new PaykeeperAPIClient($url, $username, $password, $tokenHandler, [ 'proxy' => 'tcp://:8125', 'timeout' => 6, 'connect_timeout' => 4 ]);
可用参数列表: https://docs.guzzlephp.org/en/stable/request-options.html
方法 #2:传递自己的 \GuzzleHttp\Client
配置自己的 http 客户端
// Http клиент с логгированием всех запросов $stack = HandlerStack::create(); $stack->push(Middleware::log($logger, new MessageFormatter(MessageFormatter::DEBUG))); $httpClient = new \GuzzleHttp\Client([ 'handler' => $stack, 'timeout' => 6 ]);
并将其作为构造函数参数或设置器传递
use JustCommunication\PaykeeperSDK\PaykeeperAPIClient; $client = new PaykeeperAPIClient($url, $username, $password, $tokenHandler, $httpClient);
或设置器
use JustCommunication\PaykeeperSDK\PaykeeperAPIClient; $client = new PaykeeperAPIClient($url, $username, $password, $tokenHandler); $client->setHttpClient($httpClient);
日志记录
在 $client 中可以传递自己的 Psr\Logger。
$client->setLogger($someLogger);
默认情况下,日志记录已禁用(NullLogger)
测试
可以使用命令启动测试
vendor/bin/phpunit