ambitionphp/coinbase

Laravel 对 Coinbase 钱包 API v2 的包装

dev-master 2021-12-17 01:19 UTC

This package is auto-updated.

Last update: 2024-09-17 08:00:19 UTC


README

安装

使用 Composer 安装库。如果您不熟悉 Composer 或通用依赖管理器,请阅读Composer 文档

composer require galal/coinbase
Provider => Galal\Coinbase\CoinbaseServiceProvider::class,
Aliase => 'Coinbase'=>Galal\Coinbase\Facades\Coinbase::class,

要开始,您需要发布所有供应商资产

$ php artisan vendor:publish

认证

API 密钥

将您自己的 Coinbase 账户的 API 密钥和密钥添加到 config/coinbase.php 中。

use Galal\Coinbase\Facades\Coinbase;

$client = Coinbase::client();

OAuth2

使用 OAuth2 认证访问除您自己的账户之外的其他用户账户。此库不处理握手过程,并在初始化时假设您有一个访问令牌。您可以使用 [OAuth2 客户端](如 [league/oauth2-client])来处理握手过程。

use Galal\Coinbase\Facades\Coinbase;

// with a refresh token
$client = Coinbase::clientUsingOauth($accessToken, $refreshToken);

// without a refresh token
$client = Coinbase::clientUsingOauth($accessToken);

双因素认证

在某些情况下,发送资金端点需要 2FA 令牌。当需要时,会抛出特定异常。

use Galal\Coinbase\Facades\Coinbase;

$transaction = Coinbase::transaction()->send([
    'toEmail' => 'test@test.com',
    'bitcoinAmount' => 1
]);

$account = Coinbase::client()->getPrimaryAccount();
try {
    Coinbase::client()->createAccountTransaction($account, $transaction);
} catch (TwoFactorRequiredException $e) {
    // show 2FA dialog to user and collect 2FA token

    // retry call with token
    Coinbase::client()->createAccountTransaction($account, $transaction, [
        Coinbase::param()::TWO_FACTOR_TOKEN => '123456',
    ]);
}

警告

明智的做法是注意警告。如果配置了标准 PSR-3 日志记录器,库会将所有警告记录到该日志记录器。

use Galal\Coinbase\Facades\Coinbase;

$client = Coinbase::clientWithLogging($logger);

您还可以通过使用 expand 参数来请求 API 在初始请求中返回扩展资源。

$deposit = $this->client->getAccountDeposit($account, $depositId, [
    Coinbase::param()::EXPAND = ['transaction'],
]);

在创建新资源时可以使用资源引用,从而避免从 API 请求资源的开销。

$deposit = Coinbase::deposit([
    'paymentMethod' => Coinbase::paymentMethod()->reference($paymentMethodId)
]);

// or use the convenience method
$deposit = Coinbase::deposit([
    'paymentMethodId' => $paymentMethodId
]);

响应

有多种方法可以访问原始响应数据。首先,每个资源对象都有一个 getRawData() 方法,您可以使用它来访问任何未映射到对象属性的任何字段。

$data = $deposit->getRawData();

最后 HTTP 响应的原始数据也存在于客户端对象上。

$data = $client->decodeLastResponse();

活动记录方法

该库包括对资源对象上的活动记录方法的支持。您必须在启动应用程序时启用此功能。

$client->enableActiveRecord();

启用后,您可以在资源对象上调用活动记录方法。

$transactions = $account->getTransactions([
    Coinbase::param()::FETCH_ALL => true,
]);

使用方法

列出支持的本地货币

$currencies = $client->getCurrencies();

列出汇率

$rates = $client->getExchangeRates();

购买价格

$buyPrice = $client->getBuyPrice('BTC-USD');

销售价格

$sellPrice = $client->getSellPrice('BTC-USD');

现货价格

$spotPrice = $client->getSpotPrice('BTC-USD');

当前服务器时间

$time = $client->getTime();

获取授权信息

$auth = $client->getCurrentAuthorization();

查找用户信息

$user = $client->getUser($userId);

获取当前用户

$user = $client->getCurrentUser();

更新当前用户

$user->setName('New Name');
$client->updateCurrentUser($user);

列出所有账户

$accounts = $client->getAccounts();

列出账户详情

$account = $client->getAccount($accountId);

列出主账户详情

$account = $client->getPrimaryAccount();

将账户设为主账户

$client->setPrimaryAccount($account);

创建新的比特币账户

$account = Coinbase::account([
    'name' => 'New Account'
]);
$client->createAccount($account);

更新账户

$account->setName('New Account Name');
$client->updateAccount($account):

删除账户

$client->deleteAccount($account);

列出账户的接收地址

$addresses = $client->getAccountAddresses($account);

获取接收地址信息

$address = $client->getAccountAddress($account, $addressId);

列出地址的交易

$transactions = $client->getAddressTransactions($address);

创建新的接收地址

$address = Coinbase::address([
    'name' => 'New Address'
]);
$client->createAccountAddress($account, $address);

列出交易

$transactions = $client->getAccountTransactions($account);

获取交易信息

$transaction = $client->getAccountTransaction($account, $transactionId);

发送资金

$transaction = Coinbase::transaction()->send([
    'toBitcoinAddress' => 'ADDRESS',
    'amount'           => new Money(5, CurrencyCode::USD),
    'description'      => 'Your first bitcoin!',
    'fee'              => '0.0001' // only required for transactions under BTC0.0001
]);

$client->createAccountTransaction($account, $transaction);

将资金转账到新账户

$fromAccount = Coinbase::account()->reference($accountId);

$toAccount = Coinbase::account()([
    'name' => 'New Account'
]);
$client->createAccount($toAccount);

$transaction = Coinbase::transaction()->transfer([
    'to'            => $toAccount,
    'bitcoinAmount' => 1,
    'description'   => 'Your first bitcoin!'
]);

$client->createAccountTransaction($fromAccount, $transaction);

请求资金

$transaction = Coinbase::transaction()->request([
    'amount'      => Coinbase::money(8, CurrencyCode::USD),
    'description' => 'Burrito'
]);

$client->createAccountTransaction($transaction);

重新发送请求

$account->resendTransaction($transaction);

取消请求

$account->cancelTransaction($transaction);

满足请求

$account->completeTransaction($transaction);

列出购买

$buys = $client->getAccountBuys($account);

获取购买信息

$buy = $client->getAccountBuy($account, $buyId);

购买比特币

$buy = Coinbase::buy([
    'bitcoinAmount' => 1
]);

$client->createAccountBuy($account, $buy);

提交购买

只有在您在创建购买时传递 commit=false 时才需要这样做。

$client->createAccountBuy($account, $buy, [Coinbase::param()::COMMIT => false]);
$client->commitBuy($buy);

列出销售

$sells = $client->getAccountSells($account);

获取销售信息

$sell = $client->getAccountSell($account, $sellId);

出售比特币

$sell = Coinbase::sell([
    'bitcoinAmount' => 1
]);

$client->createAccountSell($account, $sell);

提交销售

只有在您在创建销售时传递 commit=false 时才需要这样做。

$client->createAccountSell($account, $sell, [Coinbase::param()::COMMIT => false]);
$client->commitSell($sell);

列出存款

$deposits = $client->getAccountDeposits($account);

获取存款信息

$deposit = $client->getAccountDeposit($account, $depositId);

存入资金

$deposit = Coinbase::deposit([
    'amount' => Coinbase::money(10, Coinbase::currencyCode()::USD)
]);

$client->createAccountDeposit($account, $deposit);

提交存款

只有在您在创建存款时传递 commit=false 时才需要这样做。

$client->createAccountDeposit($account, $deposit, [Coinbase::param()::COMMIT => false]);
$client->commitDeposit($deposit);

列出提款

$withdrawals = $client->getAccountWithdrawals($account);

获取提款信息

$withdrawal = $client->getAccountWithdrawal($account, $withdrawalId);

提取资金

$withdrawal = Coinbase::withdrawal([
    'amount' => new Money(10, CurrencyCode::USD)
]);

$client->createAccountWithdrawal($account, $withdrawal);

提交提款

只有在调用提款方法时传递 commit=true 才需要这样做。

$client->createAccountWithdrawal($account, $withdrawal, [Coinbase::param()::COMMIT => false]);
$client->commitWithdrawal($withdrawal);

列出支付方式

$paymentMethods = $client->getPaymentMethods();

获取支付方式

$paymentMethod = $client->getPaymentMethod($paymentMethodId);

获取商家

$merchant = $client->getMerchant($merchantId);

列出订单

$orders = $client->getOrders();

获取订单

$order = $client->getOrder($orderId);

创建订单

$order = Coinbase::order([
    'name' => 'Order #1234',
    'amount' => Coinbase::moneyBTC(1)
]);

$client->createOrder($order);

退款订单

$client->refundOrder($order, Coinbase::currencyCode()::BTC);

结账

列出结账

$checkouts = $client->getCheckouts();

创建结账

$params = array(
    'name'               => 'My Order',
    'amount'             => Coinbase::money(100, 'USD'),
    'metadata'           => array( 'order_id' => $custom_order_id )
);

$checkout = new Checkout($params);
$client->createCheckout($checkout);
$code = $checkout->getEmbedCode();
$redirect_url = "https://www.coinbase.com/checkouts/$code";

获取结账信息

$checkout = $client->getCheckout($checkoutId);

获取结账订单

$orders = $client->getCheckoutOrders($checkout);

为结账创建订单

$order = $client->createNewCheckoutOrder($checkout);