coingate / coingate-php
CoinGate PHP 库
Requires
- php: >=7.3.0
- ext-curl: *
- ext-json: *
Requires (Dev)
- phpstan/phpstan: ~1.4.10
- phpunit/phpunit: ^5.7 || ^9.0
- squizlabs/php_codesniffer: ~3.6.2
This package is auto-updated.
Last update: 2024-09-13 13:26:31 UTC
README
CoinGate PHP 库提供了从用 PHP 编写的应用程序方便地访问 CoinGate API 的途径。
需求
PHP 7.3.0 及更高版本。
Composer
您可以通过 Composer 安装库。运行以下命令
composer require coingate/coingate-php
手动安装
如果您不想使用 Composer,您可以下载 最新版本。然后,为了使用库,包含 init.php
文件。
require_once('/path/to/coingate-php/init.php');
依赖项
为了正常运行,库需要以下扩展
如果您使用 Composer,这些依赖项应该会自动处理。如果您手动安装,您需要确保这些扩展可用。
入门
您可以在 https://coingate.com 为生产环境和 https://sandbox.coingate.com 为测试(沙箱)注册 CoinGate 账户。
请注意,对于沙箱,您必须在 https://sandbox.coingate.com 上生成单独的 API 凭据。在 https://coingate.com 上生成的 API 凭据在沙箱模式下将不起作用。
CoinGate PHP 库的使用示例
$client = new \CoinGate\Client('YOUR_API_TOKEN');
为了使用沙箱模式,您需要将第二个参数设置为 true
。
$client = new \CoinGate\Client('YOUR_API_TOKEN', true);
如果您只计划使用公共 API 端点,则不需要身份验证。
$client = new CoinGate\Client(); // if needed you can set configuration parameters later $client->setApiKey('YOUR_API_TOKEN'); $client->setEnvironment('sandbox');
CoinGate API 的完整文档可以在 这里 找到
示例
使用此库的应用程序示例可以在 这里 找到
支付网关 API
创建订单
在 CoinGate 上创建订单,并将购物者重定向到发票(payment_url)。
$params = [ 'order_id' => 'YOUR-CUSTOM-ORDER-ID-115', 'price_amount' => 1050.99, 'price_currency' => 'USD', 'receive_currency' => 'EUR', 'callback_url' => 'https://example.com/payments?token=6tCENGUYI62ojkuzDPX7Jg', 'cancel_url' => 'https://example.com/cart', 'success_url' => 'https://example.com/account/orders', 'title' => 'Order #112', 'description' => 'Apple Iphone 13' ]; try { $order = $client->order->create($params); } catch (\CoinGate\Exception\ApiErrorException $e) { // something went wrong... // var_dump($e->getErrorDetails()); } echo $order->id;
结账
使用预先选择的支付货币(BTC、LTC、ETH 等)提交创建的订单。显示 payment_address 和 pay_amount 以供购物者查看或重定向到 payment_url。可用于白色标签发票。
$checkout = $client->order->checkout(7294, [ 'pay_currency' => 'BTC' ]);
获取订单
创建订单后,您将获得一个 ORDER ID。此 ID 将用于 GET ORDER 请求。
$order = $client->order->get(7294);
列出订单
检索所有放置的订单的信息。
$orders = $client->order->list([ 'created_at' => [ 'from' => '2022-01-25' ] ]);
公共 API
获取汇率
任何两种货币(法定货币或加密货币)的当前汇率。此端点是公共的,不需要身份验证。
$client->getExchangeRate('BTC', 'EUR');
列出汇率
CoinGate 为商家和交易者提供的当前汇率。此端点是公共的,不需要身份验证。
$client->listExchangeRates();
Ping
CoinGate API 的健康检查端点。此端点是公共的,不需要身份验证。
$client->ping();
IP 地址
获取 CoinGate 服务器的 IP 地址
$client->getIPAddresses();
货币
$client->getCurrencies(); // Crypto + Native + Merchant Pay $client->getCheckoutCurrencies(); // get Merchant Pay currencies only $client->getMerchantPayCurrencies(); // get Merchant Receive currencies only $client->getMerchantPayoutCurrencies();
平台
$client->getPlatforms();
自定义请求超时
要修改请求超时(连接或总时间,以秒为单位),您需要告诉 API 客户端使用除其默认值之外的 CurlClient。您将在那个 CurlClient 中设置超时。
// set up your tweaked Curl client $curl = new \CoinGate\HttpClient\CurlClient(); $curl->setTimeout(10); $curl->setConnectTimeout(5); // tell CoinGate Library to use the tweaked Curl client \CoinGate\Client::setHttpClient($curl); // use the CoinGate API client as you normally would
测试API连接
$result = \CoinGate\Client::testConnection('YOUR_API_TOKEN');
为了在沙盒模式下测试API连接,您需要将第二个参数设置为true
。
$result = \CoinGate\Client::testConnection('YOUR_API_TOKEN', true);
注意插件开发者
您正在编写一个集成CoinGate并嵌入我们库的插件吗?那么请使用setAppInfo函数来标识您的插件。例如
\CoinGate\Client::setAppInfo("MyAwesomePlugin", "1.0.0");
该方法应在发送任何请求到API之前调用一次。第二个参数是可选的。