yandex-money / yandex-money-sdk-php
Yandex.Money API SDK for PHP
v3.0.9
2015-02-02 16:45 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: 3.7.*
This package is not auto-updated.
Last update: 2024-09-17 22:54:57 UTC
README
PHP Yandex.Money API SDK
要求
PHP 5.3 或更高版本
链接
入门
安装
- 将
"yandex-money/yandex-money-sdk-php": "3.0.*"
添加到您的应用的composer.json
文件中。或者将仓库克隆到您的项目中。 - 如果您使用 composer - 简单地使用
require_once 'vendor/autoload.php';
,否则粘贴以下代码// For payments from the Yandex.Money 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';
从 Yandex.Money 钱包发起支付
使用 Yandex.Money API 需要以下步骤
-
获取令牌 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, ));
无授权的银行卡支付
-
获取实例 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/