yandex-money/yandex-money-sdk-php

Yandex.Money API SDK for PHP

v3.0.9 2015-02-02 16:45 UTC

This package is not auto-updated.

Last update: 2024-09-17 22:54:57 UTC


README

Build Status

PHP Yandex.Money API SDK

要求

PHP 5.3 或更高版本

链接

  1. Yandex.Money API 页面: 俄语英语
  2. 示例应用

入门

安装

  1. "yandex-money/yandex-money-sdk-php": "3.0.*" 添加到您的应用的 composer.json 文件中。或者将仓库克隆到您的项目中。
  2. 如果您使用 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 需要以下步骤

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

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

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

无授权的银行卡支付

  1. 获取实例 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
    }
  2. 发起支付请求

    // 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
    }
  3. 使用 process-payment 处理请求

    $process_options = array(
        "request_id" => $request_id
        // other params..
    );
    $result = $external_payment->process($process_options);
    // process $result according to docs

注意事项

  1. 在以下情况下库会抛出异常
    • 响应状态不等于 2**
    • I/O 错误(请参阅 requests
  2. 如果您注册了应用并填写了 CLIENT_SECRET 项,那么您应该在 $client_secret=NULL 处明确提供 $client_secret
  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/