soleniye / yoomoney-sdk-php
PHP Yoomoney API SDK
1.0.0
2021-05-22 13:40 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
README
要求
PHP 5.3 或更高版本
链接
入门
安装
- 将
"soleniye/yoomoney-sdk-php": "1.0.*"添加到您的应用程序的composer.json文件中。或者克隆仓库到您的项目。 - 如果您使用 composer,只需使用
require_once 'vendor/autoload.php';,否则粘贴以下代码require_once '/path/to/cloned/repo/lib/api.php';
从 Yoomoney 钱包进行付款
使用 Yoomoney API 需要以下步骤
-
获取 token URL 并将用户浏览器重定向到 Yoomoney 服务。注意:
client_id、redirect_uri、client_secret是在您在 Yoomoney API 中注册应用程序时获得的常量。use \YooMoney\API; $auth_url = API::buildObtainTokenUrl($client_id, $redirect_uri, $scope);
-
之后,用户填写 Yoomoney HTML 表单,用户被重定向回
REDIRECT_URI?code=CODE。 -
您应立即将
CODE交换为ACCESS_TOKEN。$access_token_response = API::getAccessToken($client_id, $code, $redirect_uri, $client_secret=NULL); if(property_exists($access_token_response, "error")) { // process error } $access_token = $access_token_response->access_token;
-
现在您可以使用 Yoomoney API。
$api = new API($access_token); // get account info $acount_info = $api->accountInfo(); // check status // get operation history with last 3 records $operation_history = $api->operationHistory(array("records"=>3)); // check status // make request payment $request_payment = $api->requestPayment(array( "pattern_id" => "p2p", "to" => $money_wallet, "amount_due" => $amount_due, "comment" => $comment, "message" => $message, "label" => $label, )); // check status // call process payment to finish payment $process_payment = $api->processPayment(array( "request_id" => $request_payment->request_id, ));
侧注
- 在以下情况下库会抛出异常
- 响应状态不等于 2**
- I/O 错误(参见 requests)
- 如果您注册应用程序并填写
CLIENT_SECRET项,那么您应该明确提供$client_secret,其中$client_secret=NULL - 您应该将所有传递的布尔值用引号括起来(因为否则 PHP 会将它们转换为数字)。例如
API($access_token).requestPayment(array( test_payment => "true", // other params ));
运行测试
- 克隆此仓库。
- 安装 composer。
- 运行
composer install。 - 确保
phpunit可执行文件存在于您的$PATH中。 - 创建
tests/constants.php文件,包含CLIENT_ID、CLIENT_SECRET和ACCESS_TOKEN常量。 - 运行测试
phpunit --bootstrap vendor/autoload.php tests/。