stefan-ghivi/php-test

Mangopay api V2 的 PHP SDK

2.4.4 2016-09-27 09:11 UTC

This package is not auto-updated.

Last update: 2024-09-23 14:18:49 UTC


README

MangopaySDK 是一个用于与 Mangopay REST API 一起工作的 PHP 客户端库。

兼容性说明

  • 自 SDK 的 v2.1 版本起,您必须使用至少 API v2.01 的版本 (有关所需更改的更多信息)
  • 如果您在更新 SDK 后(尤其是更新到 SDK 的 v2.0 版本)遇到身份验证和/或临时令牌文件的问题,您可能只需删除您的临时文件(使用 $api->Config->TemporaryFolder 指定)——这允许它在下一次需要时正确生成

要求

要使用此 SDK,您至少需要以下内容

  • PHP v5.4
  • cURL(包含并启用在标准 PHP 发行版中)
  • OpenSSL(包含并启用在标准 PHP 发行版中)
  • psr/log v1.0
  • 您不需要使用 Composer,但强烈建议您这样做(特别是处理对 PSR Log 库的依赖关系)

使用 Composer 安装

您可以使用 Mangopay SDK 库作为您的项目中的依赖项,使用 Composer(这是首选技术)。如果您还没有安装 Composer,请遵循 这些安装说明。仓库中包含一个 composer.json 文件,它已从 Packagist 引用。

使用 Composer 安装既简单又可靠

第 1 步 - 通过执行以下命令将 Mangopay SDK 作为依赖项添加

you@yourhost:/path/to/your-project$ composer require mangopay/php-sdk-v2:^2.3

第 2 步 - 使用 Composer 更新您的依赖项

you@yourhost:/path/to/your-project$ composer update

第 3 步 - 最后,请确保将自动加载器包含到您的项目中

require_once '/path/to/your-project/vendor/autoload.php';

库已添加到您的依赖项中,并准备好使用。

不使用 Composer 的安装

项目试图遵守 PSR-4 规范,以便从文件路径自动加载类。命名空间前缀为 MangoPay\,基本目录为 /path/to/your-project/

如果您不使用 PSR-4 或 Composer,安装过程就像下载库并将它存储在任何您希望在项目中包含的位置一样(别忘了包括所需的库依赖项

    require_once '/path/to/your-project/MangoPay/Autoloader.php';

或者,您可以从 发布页面(查找 mangopay2-php-sdk-[RELEASE_NAME].zip 文件)下载整个包(即包含所有必需的依赖项)。解压您下载的 zip 文件,并在您的项目中包含自动加载器

    require_once '/path/to/your-project/mangopay2-php-sdk/vendor/autoload.php';

许可证

MangopaySDK 在 MIT 许可证下分发,请参阅 LICENSE 文件

单元测试

测试放置在 /path/to/your-project/tests/ 下。/tests/suites/all.php 组合运行所有测试。您也可以使用 /tests/cases/*.php 中的任何一个来运行单个测试用例。

联系方式

使用GitHub上的问题跟踪器报告错误或建议功能。

账户创建

您可以注册一个免费沙盒账户,或注册一个生产账户(请注意,您的生产账户验证可能需要几天时间,所以请提前考虑好您真正上线的时间)。

配置

使用上述注册过程中的凭据信息,您应该设置$api->Config->ClientId为您的Mangopay ClientId,并将$api->Config->ClientPassword设置为您的Mangopay密码。

您还需要在$api->Config->TemporaryFolder中设置一个文件夹路径,SDK需要将临时文件存储在这里。这个路径应该在您的www文件夹之外。可以是/tmp//var/tmp/或任何PHP可以写入的位置。您必须在沙盒和生产环境中使用不同的文件夹。

$api->Config->BaseUrl默认设置为沙盒环境。要启用生产环境,将其设置为https://api.mangopay.com

require_once '/path/to/your-project/vendor/autoload.php';
$api = new MangoPay\MangoPayApi();

// configuration
$api->Config->ClientId = 'your-client-id';
$api->Config->ClientPassword = 'your-client-password';
$api->Config->TemporaryFolder = '/some/path/';
//$api->Config->BaseUrl = 'https://api.mangopay.com';//uncomment this to use the production environment

//uncomment any of the following to use a custom value (these are all entirely optional)
//$api->Config->CurlResponseTimeout = 20;//The cURL response timeout in seconds (its 30 by default)
//$api->Config->CurlConnectionTimeout = 60;//The cURL connection timeout in seconds (its 80 by default)
//$api->Config->CertificatesFilePath = ''; //Absolute path to file holding one or more certificates to verify the peer with (if empty, there won't be any verification of the peer's certificate)

// call some API methods...
try {
    $users = $api->Users->GetAll(); 
} catch(MangoPay\Libraries\ResponseException $e) {
    // handle/log the response exception with code $e->GetCode(), message $e->GetMessage() and error(s) $e->GetErrorDetails()
} catch(MangoPay\Libraries\Exception $e) {
    // handle/log the exception $e->GetMessage()
}

示例用法

require_once '/path/to/your-project/vendor/autoload.php';
$api = new MangoPay\MangoPayApi();

// configuration
$api->Config->ClientId = 'your-client-id';
$api->Config->ClientPassword = 'your-client-password';
$api->Config->TemporaryFolder = '/some/path/';

// get some user by id
try {
    $john = $api->Users->Get($someId);
} catch(MangoPay\Libraries\ResponseException $e) {
    // handle/log the response exception with code $e->GetCode(), message $e->GetMessage() and error(s) $e->GetErrorDetails()
} catch(MangoPay\Libraries\Exception $e) {
    // handle/log the exception $e->GetMessage()
}

// change and update some of his data
$john->LastName .= " - CHANGED";
try {
    $api->Users->Update($john);
} catch(MangoPay\Libraries\ResponseException $e) {
    // handle/log the response exception with code $e->GetCode(), message $e->GetMessage() and error(s) $e->GetErrorDetails()
} catch(MangoPay\Libraries\Exception $e) {
    // handle/log the exception $e->GetMessage()
}

// get all users (with pagination)
$pagination = new MangoPay\Pagination(1, 8); // get 1st page, 8 items per page
try {
    $users = $api->Users->GetAll($pagination);
} catch(MangoPay\Libraries\ResponseException $e) {
    // handle/log the response exception with code $e->GetCode(), message $e->GetMessage() and error(s) $e->GetErrorDetails()
} catch(MangoPay\Libraries\Exception $e) {
    // handle/log the exception $e->GetMessage()
}

// get his bank accounts
$pagination = new MangoPay\Pagination(2, 10); // get 2nd page, 10 items per page
try {
    $accounts = $api->Users->GetBankAccounts($john->Id, $pagination);
} catch(MangoPay\Libraries\ResponseException $e) {
    // handle/log the response exception with code $e->GetCode(), message $e->GetMessage() and error(s) $e->GetErrorDetails()
} catch(MangoPay\Libraries\Exception $e) {
    // handle/log the exception $e->GetMessage()
}

在Symfony项目中使用Composer的示例用法

您可以将Mangopay功能集成到您的Symfony项目中的Service中。

MangoPayService.php

<?php

namespace Path\To\Service;

use MangoPay;


class MangoPayService
{

    private $mangoPayApi;

    public function __construct()
    {
        $this->mangoPayApi = new MangoPay\MangoPayApi();
        $this->mangoPayApi->Config->ClientId = 'your-client-id';
        $this->mangoPayApi->Config->ClientPassword = 'your-client-password';
        $this->mangoPayApi->Config->TemporaryFolder = '/some/path/';    
        //$this->mangoPayApi->Config->BaseUrl = 'https://api.sandbox.mangopay.com';
    }
    
    /**
     * Create Mangopay User
     * @return MangopPayUser $mangoUser
     */
    public function getMangoUser()
    {
        
        $mangoUser = new \MangoPay\UserNatural();
        $mangoUser->PersonType = "NATURAL";
        $mangoUser->FirstName = 'John';
        $mangoUser->LastName = 'Doe';
        $mangoUser->Birthday = 1409735187;
        $mangoUser->Nationality = "FR";
        $mangoUser->CountryOfResidence = "FR";
        $mangoUser->Email = 'john.doe@mail.com';

        //Send the request
        $mangoUser = $this->mangoPayApi->Users->Create($mangoUser);

        return $mangoUser;
    }
}

日志记录

MangoPay使用PSR3 LoggerInterface。您可以为API提供自己的日志记录器。以下是一个展示Monolog集成的示例。

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

...

$logger = new Logger('sample-logger');
$logger->pushHandler(new StreamHandler($logConfig['path'], Logger::DEBUG));

$this->mangoPayApi = new MangoPay\MangoPayApi();
$this->mangoPayApi->setLogger($logger);