daniel-spravtsev / yoomoney-sdk-php
PHP版Yoomoney API SDK (基于yandex.money SDK分叉)
v3.1.1
2024-02-13 11:58 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
README
需求
PHP 5.3 或更高版本
链接
入门
安装
- 将
"daniel-spravtsev/yoomoney-sdk-php": "3.0.*"添加到您的应用程序的composer.json文件中。或者将仓库克隆到您的项目中。 - 如果您使用 composer,请简单地使用
require_once 'vendor/autoload.php';,否则粘贴以下代码// For payments from the Yoomoney wallet require_once '/path/to/cloned/repo/lib/api.php'; // For payments from bank cards without authorization require_once '/path/to/cloned/repo/lib/external_payment.php';
来自Yoomoney钱包的支付
使用Yoomoney API需要以下步骤
-
获取token URL并将用户的浏览器重定向到Yandex.Money服务。注意:
client_id、redirect_uri、client_secret是在您在 Yandex.Money API 中注册应用时获得的常量。use \YandexMoney\API; $auth_url = API::buildObtainTokenUrl($client_id, $redirect_uri, $scope);
-
之后,用户填写 Yandex.Money 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;
-
现在您可以使用 Yandex.Money 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, ));
无需授权的银行卡支付
-
获取 instance-id(通常每个客户只有一个。您可以将其存储在数据库中)。
use \YandexMoney\ExternalPayment; $response = ExternalPayment::getInstanceId($client_id); if($response->status == "success") { $instance_id = $response->instance_id; } else { // throw exception with $response->error message }
-
发起支付请求
// make instance $external_payment = ExternalPayment($instance_id); $payment_options = array( // pattern_id, etc.. ); $response = $external_payment->request($payment_options); if($response->status == "success") { $request_id = $response->request_id; } else { // throw exception with $response->message }
-
使用 process-payment 处理请求
$process_options = array( "request_id" => $request_id // other params.. ); $result = $external_payment->process($process_options); // process $result according to docs
备注
- 库在以下情况下抛出异常
- 响应状态不等于 2**
- I/O 错误(见 requests)
- 如果您注册了应用并填写了
CLIENT_SECRET项,则应在$client_secret=NULL的位置明确提供$client_secret - 您应该将所有传递的布尔值用引号括起来(因为 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/