soleniye/yoomoney-sdk-php

1.0.0 2021-05-22 13:40 UTC

This package is auto-updated.

Last update: 2024-09-22 22:05:22 UTC


README

要求

PHP 5.3 或更高版本

链接

Yoomoney API 页面:俄语英语

入门

安装

  1. "soleniye/yoomoney-sdk-php": "1.0.*" 添加到您的应用程序的 composer.json 文件中。或者克隆仓库到您的项目。
  2. 如果您使用 composer,只需使用 require_once 'vendor/autoload.php';,否则粘贴以下代码
    require_once '/path/to/cloned/repo/lib/api.php';

从 Yoomoney 钱包进行付款

使用 Yoomoney API 需要以下步骤

  1. 获取 token URL 并将用户浏览器重定向到 Yoomoney 服务。注意:client_idredirect_uriclient_secret 是在您在 Yoomoney API 中注册应用程序时获得的常量。

    use \YooMoney\API;
    
    $auth_url = API::buildObtainTokenUrl($client_id, $redirect_uri, $scope);
  2. 之后,用户填写 Yoomoney HTML 表单,用户被重定向回 REDIRECT_URI?code=CODE

  3. 您应立即将 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;
  4. 现在您可以使用 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,
    ));

侧注

  1. 在以下情况下库会抛出异常
    • 响应状态不等于 2**
    • I/O 错误(参见 requests
  2. 如果您注册应用程序并填写 CLIENT_SECRET 项,那么您应该明确提供 $client_secret,其中 $client_secret=NULL
  3. 您应该将所有传递的布尔值用引号括起来(因为否则 PHP 会将它们转换为数字)。例如
API($access_token).requestPayment(array(
    test_payment => "true",
    // other params
));

运行测试

  1. 克隆此仓库。
  2. 安装 composer。
  3. 运行 composer install
  4. 确保 phpunit 可执行文件存在于您的 $PATH 中。
  5. 创建 tests/constants.php 文件,包含 CLIENT_IDCLIENT_SECRETACCESS_TOKEN 常量。
  6. 运行测试 phpunit --bootstrap vendor/autoload.php tests/