toopago/coinpayments-php

针对 CoinPayments.net v1 API 的 PHP 封装。

v1.4 2018-09-15 00:00 UTC

This package is auto-updated.

Last update: 2024-09-29 05:36:08 UTC


README

需求

建议使用较新的 PHP 版本。此库是在 PHP v7.2.8-1+ 环境中编写的。我们测试代码的最小 PHP 版本是 v 5.5+。我们的 PHP 版本兼容性测试使用了 PHP CodeSnifferPHP 兼容性 的组合。

拥有一个设置好 API 密钥对的 CoinPayments.net 账户(公开 & 私有)。有关最新说明,请访问我们的 在线 API 文档 以了解如何设置 API 密钥并获得您账户 API 密钥页面的链接。

请参阅下面的测试部分,以获取测试 LTCT 或主网硬币的特定命令的附加需求。

请注意,此封装假定请求的 API 响应格式为 JSON。另一种选择是 XML,但此封装未记录或支持 XML 格式。默认情况下,除非另有指定,否则每个 API 调用都将返回 JSON。

安装

GitHub

此 CoinPayments.net API 封装可以直接下载或使用 GitHub 进行克隆。要在您的项目中使用它,请克隆此存储库或将 ZIP 文件下载到您选择的目录。

所需的最小文件位于源文件夹中,包括 CoinpaymentsAPI.phpCoinpaymentsValidator.phpCoinpaymentsCurlRequest.php。使用预定义的 API 密钥和测试变量所需的第 4 个文件是 keys.php。有一个 keys_example.php 文件,可以填充并重命名为 keys.php。此密钥文件用于将您的公开和私有 API 密钥传递给封装,以便调用 Coinpayments.net 平台。您也可以手动将密钥传递给 CoinpaymentsAPI 类,而不是使用 keys.php 文件。应将 keys.php 文件放置在 /src 目录中。

Composer

要使用 Composer 安装,请运行以下命令 composer require coinpaymentsnet/coinpayments-php,然后在您想使用封装类的地方包含以下行。

require_once('your_project_path_to/vendor/autoload.php');

注意 /examples 目录不使用 Composer 自动加载。

当使用 Composer 自动加载类时,应将您的 keys.php 文件定义在 vendor 目录之外,以免在升级包时覆盖它。

Packagist.org 包 URL

示例

在浏览器中预览 /examples 目录中的脚本,一旦您已在 keys.php 文件中设置了公开和私有密钥。这显然需要使用像 Apache 这样的 Web 服务器来提供这些示例 PHP 文件。有关与这些示例和您的账户交互的附加测试说明,请参阅以下内容。某些示例要求您在示例文件中填写初始变量才能执行其中包含的脚本。

用法

最简单的示例是检索基本账户信息。

// For manual wrapper usage include the following require:
require('/your_installation_path_to/src/CoinpaymentsAPI.php');

/** Scenario: Retrieve basic user account information.**/
// Either include the sample keys.php file (once populated) or manually set $public_key and $private_key variables
require('/your_installation_path_to/src/keys.php');

// Create a new API wrapper instance and call to the get basic account information command.
try {
    $cps_api = new CoinpaymentsAPI($private_key, $public_key, 'json');
    $information = $cps_api->GetBasicInfo();
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
    exit();
}

// Check for success of API call
if ($cps_api['error'] == 'ok') {
    // Prepare start of sample HTML output
    $output = '<table><tbody><tr><td>Username</td><td>Merchant ID</td><td>Email</td><td>Public Name</td></tr>';
    $output .= '<tr><td>' . $cps_api['result']['username'] . '</td><td>' . $cps_api['result']['merchant_id'] . '</td><td>' . $cps_api['result']['email'] . '</td><td>' . $cps_api['result']['public_name'] . '</td></tr>';
    // Close the sample output HTML and echo it onto the page
    $output .= '</tbody></table>';
    echo $output;
} else {
    // Throw an error if both API calls were not successful
    echo 'There was an error returned by the API call: ' . $balances['error'] . '<br>Rates API call status: ' . $rates['error'];
}

测试

要运行测试文件夹中的 PHPUnit 测试并在 /examples 目录中预览脚本,需要完整填写 keys.php 文件。以下说明了测试命令的不同需求,命令格式如下

  • raw_api_command_name | wrapperFunctionName()

只填充了公钥和私钥,以下命令可以进行测试。要使以下命令返回非空结果,需要确保相应的资源(资金)已存入(存款)、更新(货币接受设置)或创建(地址)在您的CoinPayments.net账户中。

  • get_basic_info | GetBasicInfo()
  • rates | GetRates()GetRatesWithAccepted()GetShortRates()GetShortRatesWithAccepted
  • balances | GetCoinBalances()GetAllCoinBalances()
  • get_deposit_address | GetDepositAddress()
  • get_callback_address | GetOnlyCallbackAddress()GetCallbackAddressWithIpn()
  • convert_limits | GetConversionLimits()

请注意,若要测试以下命令,您需要在账户中设置IPN URL或在命令中传递IPN URL,以便测试您的服务器在收到返回地址的付款通知。更多信息请参考我们关于即时支付通知系统的文档。

以下附加变量(API密钥下方的变量)需要设置第二个开发者CoinPayments.net账户以测试以下命令,假设货币为LTCT。或者,您也可以使用其他地址或钱包来测试这些命令,只要该货币得到CoinPayments.net平台的支持。请参阅支持的货币

  • create_transaction | CreateSimpleTransaction()CreateSimpleTransactionWithConversion()CreateComplexTransaction()CreateCustomTransaction()
  • create_transfer | CreateMerchantTransfer()
  • get_withdrawal_history | GetWithdrawalHistory()
  • get_withdrawal_info | GetWithdrawalInformation()
  • create_withdrawal & create_mass_withdrawal | CreateWithdrawal() & CreateMassWithdrawal() * 使用地址,而不是$PayByName,请参阅下文。

目前我们的API不允许在没有生产资产的情况下测试货币转换或$PayByName命令,因此以下命令需要平台上的生产环境数据。例如,未认领和已认领的$PayByName标签或可转换货币。我们的开发团队已使用生产环境数据(资产)运行这些测试,以便将这些命令纳入代码测试覆盖率。仍然建议在发布任何集成之前,使用生产环境测试这些命令。这需要少量主网硬币进行转换,并至少购买一个$PayByName标签进行认领,发送资金并检索信息(以测试所有可能的命令)。

  • convert | ConvertCoins()
  • get_conversion_info | GetConversionInformation()
  • get_pbn_info | GetProfileInformation()
  • get_pbn_list | GetTagList()
  • update_pbn_tag | UpdateTagProfile()
  • claim_pbn_tag | ClaimPayByNameTag()
  • create_transfer | CreatePayByNameTransfer()
  • create_withdrawal & create_mass_withdrawal | CreateWithdrawal() & CreateMassWithdrawal() * 使用$PayByName代替地址

PHPUnit测试

要从/tests目录运行测试,请运行此命令:phpunit CoinpaymentsAPITest,并使用您选择的输出类型标志。或者,您可以为PHPStorm等IDE配置,使其为您运行CoinpaymentsAPITest.php中的测试。在开发此包装器时,我们使用了PHPUnit v7.3.1。

交易费率

使用任何主网币进行的测试交易将产生常规交易和网络费用,因此我们强烈建议您在测试API集成时使用LiteCoin Testnet(LTCT)。

贡献

如果在您使用此包装器的工作中遇到错误或有一些建议可以帮助改进它以便其他人使用,欢迎您在此存储库中创建一个Github问题,它将被我们的开发团队成员之一审查。CoinPayments.net的漏洞赏金计划不涵盖此包装器。

许可证

MIT - 查看LICENSE.md