neto737 / coinbase-commerce
Coinbase Commerce API 库
Requires
- php: >=7.4
- guzzlehttp/guzzle: ^7.0
- guzzlehttp/psr7: ^2.6
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- phpunit/phpunit: ^9.0
README
这是 Coinbase Commerce API 的官方 PHP 库的分支。
注意:官方仓库没有积极维护。
如果官方仓库有更新,这个分支将被删除或相应更新。在用 composer
更新包时出现错误,这可能表明官方仓库已更新,该分支已被移除。
目录
PHP 版本
支持 PHP 版本 7.4 及以上。
文档
更多详细信息请访问 Coinbase API 文档。
要开始使用这个库,请在 Coinbase Commerce 上注册一个账户。您可以在用户设置中找到您的 API_KEY
。
接下来初始化一个 Client
用于与 API 交互。初始化客户端时,唯一的必需参数是 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 方法返回一个 ApiResource
,它表示 API 的 JSON 响应。当响应数据解析为对象时,将自动使用适当的 ApiResource
子类。
客户端支持处理常见的 API 错误和警告。在 API 的任何交互过程中发生的所有错误都将引发异常。
安装
如果您已经在项目中有一个 composer.json
文件,您可以通过预编辑 composer.json
文件(步骤 2)来跳过 步骤 3,然后完成 步骤 1。
步骤 1:使用 composer
安装官方包
composer require coinbase/coinbase-commerce
步骤 2:将此分支添加到版本控制系统(VCS)
composer.json
文件应像这样要求包
"require": { "coinbase/coinbase-commerce": "^1.0" }
编辑 composer.json
以要求包如下所示
"require": { "coinbase/coinbase-commerce": "dev-master" }, "repositories": [ { "type": "vcs", "url": "git@github.com:IceQ1337/coinbase-commerce-php" } ]
步骤 3:使用 composer
更新源文件
composer update coinbase/coinbase-commerce
使用
use CoinbaseCommerce\ApiClient; //Make sure you don't store your API Key in your source code! ApiClient::init('API_KEY');
结账
结账 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);
收费
收费 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();
}
发票
发票API文档 更多关于如何使用费用的示例可以在examples/Resources/InvoiceExample.php
文件中找到
加载发票资源类
use CoinbaseCommerce\Resources\Invoice;
检索
$invoiceObj = Invoice::retrieve(<invoice_id>);
创建
$invoiceData = [ 'business_name' => 'Crypto Account LLC', 'customer_email' => 'customer@test.com', 'customer_name' => 'Test Customer', 'local_price' => [ 'amount' => '100.00', 'currency' => 'USD' ], 'memo' => 'Taxes and Accounting Services' ]; Invoice::create($invoiceData); // or $invoiceObj = new Invoice(); $invoiceObj->business_name = 'Crypto Account LLC'; $invoiceObj->customer_email = 'customer@test.com'; $invoiceObj->customer_name = 'Test Customer'; $invoiceObj->local_price = [ 'amount' => '100.00', 'currency' => 'USD' ]; $invoiceObj->memo = 'Taxes and Accounting Services'; $invoiceObj->save();
列表
$list = Invoice::getList(); foreach($list as $invoice) { var_dump($list); } $pagination = $list->getPagination();
获取所有发票
$allInvoices = Invoice::getAll();
解析发票
解析已被标记为未解决的发票。
注意:只有带有未解决费用的发票才能成功解析。
$invoiceObj = Invoice::retrieve(<charge_id>);
if ($invoiceObj) {
$invoiceObj->resolve();
}
作废发票
作废已创建的发票。
注意:只有状态为OPEN
或VIEWED
的发票才能被作废。一旦检测到付款,发票将无法再被作废。
$invoiceObj = Invoice::retrieve(<invoice_id>);
if ($invoiceObj) {
$invoiceObj->void();
}
事件
事件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为其发送到您的端点的webhook事件签名,这使得您可以验证并确认它们并非由其他人发送。您可以在examples/Webhook
文件夹中找到一个使用Express实现此功能的简单示例。
验证签名头
use CoinbaseCommerce\Webhook; try { Webhook::verifySignature($body, $signature, $sharedSecret); echo 'Successfully verified'; } catch (\Exception $exception) { echo $exception->getMessage(); echo 'Failed'; }
测试和贡献
欢迎任何和所有的贡献!过程很简单:fork这个仓库,做出您的修改,运行测试套件,然后提交一个pull request。要运行测试,克隆仓库并运行以下命令
composer install
composer test
许可证
Apache-2.0