pixelmindai / coinbase-commerce
Coinbase Commerce API 库
Requires
- php: >=7.4
- guzzlehttp/guzzle: ~5.0|~6.0|~7.0
- psr/http-message: ^1.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-09-06 18:12:02 UTC
README
注意:此存储库未积极维护。
这是 Coinbase Commerce API 的官方 PHP 库。
目录
PHP 版本
支持 PHP 版本 5.4 及以上。
文档
更多详细信息请访问 Coinbase API 文档。
要开始使用此库,请在 Coinbase Commerce 上注册账户。您可以从用户设置中找到您的 API_KEY
。
接下来,初始化一个用于与 API 交互的 Client
。初始化客户端时,只需要传递 apiKey
参数,但您也可以传递 baseUrl
、apiVersion
和 timeout
参数。参数也可以在初始化后设置
use CoinbaseCommerce\ApiClient; //Make sure you don't store your API Key in your source code! $apiClientObj = ApiClient::init(<API_KEY>); $apiClientObj->setTimeout(3);
禁用 SSL 检查
$apiClientObj->verifySsl(false);
API 资源类提供以下静态方法: list, all, create, retrieve, updateById, deleteById
。此外,API 资源类还提供以下实例方法: save, delete, insert, update
。
每个 API 方法返回一个表示 API JSON 响应的 ApiResource
。当响应数据解析成对象时,将自动使用适当的 ApiResource
子类。
客户端支持处理常见的 API 错误和警告。所有在 API 交互过程中发生的错误都将作为异常抛出。
安装
使用 composer
安装
composer require fossil01/coinbase-commerce
用法
use CoinbaseCommerce\ApiClient; //Make sure you don't store your API Key in your source code! ApiClient::init('API_KEY');
结账
Checkouts API 文档 更多关于如何使用结账的示例可以在 examples/Resources/CheckoutExample.php
文件中找到
加载结账资源类
use CoinbaseCommerce\Resources\Checkout;
检索
$checkoutObj = Checkout::retrieve(<checkout_id>);
创建
$checkoutData = [ 'name' => 'The Sovereign Individual', 'description' => 'Mastering the Transition to the Information Age', 'pricing_type' => 'fixed_price', 'local_price' => [ 'amount' => '100.00', 'currency' => 'USD' ], 'requested_info' => ['name', 'email'] ]; $newCheckoutObj = Checkout::create($checkoutData); // or $newCheckoutObj = new Checkout(); $newCheckoutObj->name = 'The Sovereign Individual'; $newCheckoutObj->description = 'Mastering the Transition to the Information Age'; $newCheckoutObj->pricing_type = 'fixed_price'; $newCheckoutObj->local_price = [ 'amount' => '100.00', 'currency' => 'USD' ]; checkoutObj->requested_info = ['name', 'email']; checkoutObj->save();
更新
$checkoutObj = new Checkout(); $checkoutObj->id = <checkout_id>; $checkoutObj->name = 'new name'; $checkoutObj->save(); // or $newParams = [ 'name' => 'New name' ]; Checkout::updateById(<checkout_id>, $newParams});
删除
$checkoutObj = new Checkout(); $checkoutObj->id = <checkout_id>; $checkoutObj->delete(); // or Checkout::deleteById(<checkout_id>);
列表
列表方法返回 ApiResourceList 对象。
$params = [ 'limit' => 2, 'order' => 'desc' ]; $list = Checkout::getList($params); foreach($list as $checkout) { var_dump($checkout); } // Get number of items in list $count = $list->count(); // or $count = count($list); // Get number of all checkouts $countAll = $list->countAll(); // Get pagination $pagination = $list->getPagination(); // To load next page with previous setted params(in this case limit, order) if ($list->hasNext()) { $list->loadNext(); foreach($list as $checkout) { var_dump($checkout); } }
获取所有结账
$params = [ 'order' => 'desc' ]; $allCheckouts = Checkout::getAll($params);
收费
Charges API 文档 更多关于如何使用收费的示例可以在 examples/Resources/ChargeExample.php
文件中找到
加载收费资源类
use CoinbaseCommerce\Resources\Charge;
检索
$chargeObj = Charge::retrieve(<charge_id>);
创建
$chargeData = [ 'name' => 'The Sovereign Individual', 'description' => 'Mastering the Transition to the Information Age', 'local_price' => [ 'amount' => '100.00', 'currency' => 'USD' ], 'pricing_type' => 'fixed_price' ]; Charge::create($chargeData); // or $chargeObj = new Charge(); $chargeObj->name = 'The Sovereign Individual'; $chargeObj->description = 'Mastering the Transition to the Information Age'; $chargeObj->local_price = [ 'amount' => '100.00', 'currency' => 'USD' ]; $chargeObj->pricing_type = 'fixed_price'; $chargeObj->save();
列表
$list = Charge::getList(); foreach($list as $charge) { var_dump($list); } $pagination = $list->getPagination();
获取所有收费
$allCharges = Charge::getAll();
解决一个收费
解决一个之前标记为未解决的收费。
$chargeObj = Charge::retrieve(<charge_id>);
if ($chargeObj) {
$chargeObj->resolve();
}
取消一个收费
取消一个之前创建的收费。注意:只有新的收费可以被成功取消。一旦检测到支付,收费就不能再取消。
$chargeObj = Charge::retrieve(<charge_id>);
if ($chargeObj) {
$chargeObj->cancel();
}
事件
Events API 文档 更多关于如何使用事件的示例可以在 examples/Resources/EventExample.php
文件中找到
加载事件资源类
use CoinbaseCommerce\Resources\Event;
检索
$eventObj = Event::retrieve(<event_id>);
列表
$listEvent = Event::getList(); foreach($listEvent as $event) { var_dump($event); } $pagination = $listEvent->getPagination();
获取所有事件
$allEvents = Event::getAll();
警告
要谨慎对待警告。如果已配置,库将记录所有警告到标准的 PSR-3 日志记录器。
use CoinbaseCommerce\ApiClient; //Make sure you don't store your API Key in your source code! $apiClientObj = ApiClient::init(<API_KEY>); $apiClientObj->setLogger($logger);
Webhooks
币安商业(Coinbase Commerce)对其发送到您的端点的事件进行签名,允许您验证并确认这些事件不是由其他人发送的。您可以在 examples/Webhook
文件夹中找到一个使用 Express 的简单示例。
验证签名头
use CoinbaseCommerce\Webhook; try { Webhook::verifySignature($signature, $body, $sharedSecret); echo 'Successfully verified'; } catch (\Exception $exception) { echo $exception->getMessage(); echo 'Failed'; }
测试和贡献
欢迎任何形式的贡献!流程很简单:Fork 此存储库,进行您的更改,运行测试套件,然后提交拉取请求。要运行测试,请克隆存储库并运行以下命令
composer install
composer test
许可协议
Apache-2.0