inferna/coinbase-commerce

Coinbase Commerce API 库

2.0.1 2021-05-14 18:42 UTC

This package is not auto-updated.

Last update: 2024-09-28 11:03:25 UTC


README

CircleCI

Coinbase Commerce

官方PHP库,用于Coinbase Commerce API

目录

PHP版本

支持PHP版本5.4及以上。

文档

更多详情请访问Coinbase API文档

要开始使用此库,请在Coinbase Commerce上注册账户。您可以在用户设置中找到您的API_KEY

接下来,初始化一个用于与API交互的Client。初始化客户端时,唯一必需的参数是apiKey,但您也可以传递baseUrlapiVersiontimeout。参数也可以在初始化后设置。

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 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->confirm();
}

事件

事件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($signature, $body, $sharedSecret);
    echo 'Successfully verified';
} catch (\Exception $exception) {
    echo $exception->getMessage();
    echo 'Failed';
}

测试和贡献

欢迎所有贡献!流程很简单:Fork这个仓库,做出你的更改,运行测试套件,然后提交pull请求。要运行测试,克隆仓库并执行以下命令

composer install
composer test

许可协议

Apache-2.0