dev-master
9999999-devLaravel wrapper for the Coinbase Wallet API v2
MIT
The Requires
- php >=5.6.0
- coinbase/coinbase ~2.0
by Ahmed Galal
Laravel wrapper for the Coinbase Wallet API v2
Install the library using Composer. Please read the Composer Documentation if you are unfamiliar with Composer or dependency managers in general., (*1)
composer require galal/coinbase
Provider => Galal\Coinbase\CoinbaseServiceProvider::class,
Aliase => 'Coinbase'=>Galal\Coinbase\Facades\Coinbase::class,
To get started, you'll need to publish all vendor assets:, (*2)
$ php artisan vendor:publish
Add an API key and secret of your own Coinbase account to config/coinbase.php ., (*3)
use Galal\Coinbase\Facades\Coinbase; $client = Coinbase::client();
Use OAuth2 authentication to access a user's account other than your own. This library does not handle the handshake process, and assumes you have an access token when it's initialized. You can handle the handshake process using an [OAuth2 client] such as [league/oauth2-client]., (*4)
use Galal\Coinbase\Facades\Coinbase; // with a refresh token $client = Coinbase::clientUsingOauth($accessToken, $refreshToken); // without a refresh token $client = Coinbase::clientUsingOauth($accessToken);
The send money endpoint requires a 2FA token in certain situations . A specific exception is thrown when this is required., (*5)
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', ]); }
It's prudent to be conscious of warnings. The library will log all warnings to a standard PSR-3 logger if one is configured., (*6)
use Galal\Coinbase\Facades\Coinbase; $client = Coinbase::clientWithLogging($logger);
You can also request that the API return an expanded resource in the initial
request by using the expand
parameter., (*7)
$deposit = $this->client->getAccountDeposit($account, $depositId, [ Coinbase::param()::EXPAND = ['transaction'], ]);
Resource references can be used when creating new resources, avoiding the overhead of requesting a resource from the API., (*8)
$deposit = Coinbase::deposit([ 'paymentMethod' => Coinbase::paymentMethod()->reference($paymentMethodId) ]); // or use the convenience method $deposit = Coinbase::deposit([ 'paymentMethodId' => $paymentMethodId ]);
There are multiple ways to access raw response data. First, each resource
object has a getRawData()
method which you can use to access any field that
are not mapped to the object properties., (*9)
$data = $deposit->getRawData();
Raw data from the last HTTP response is also available on the client object., (*10)
$data = $client->decodeLastResponse();
The library includes support for active record methods on resource objects. You must enable this functionality when bootstrapping your application., (*11)
$client->enableActiveRecord();
Once enabled, you can call active record methods on resource objects., (*12)
$transactions = $account->getTransactions([ Coinbase::param()::FETCH_ALL => true, ]);
List supported native currencies, (*13)
$currencies = $client->getCurrencies();
List exchange rates, (*14)
$rates = $client->getExchangeRates();
Buy price, (*15)
$buyPrice = $client->getBuyPrice('BTC-USD');
Sell price, (*16)
$sellPrice = $client->getSellPrice('BTC-USD');
Spot price, (*17)
$spotPrice = $client->getSpotPrice('BTC-USD');
Current server time, (*18)
$time = $client->getTime();
Get authorization info, (*19)
$auth = $client->getCurrentAuthorization();
Lookup user info, (*20)
$user = $client->getUser($userId);
Get current user, (*21)
$user = $client->getCurrentUser();
Update current user, (*22)
$user->setName('New Name'); $client->updateCurrentUser($user);
List all accounts, (*23)
$accounts = $client->getAccounts();
List account details, (*24)
$account = $client->getAccount($accountId);
List primary account details, (*25)
$account = $client->getPrimaryAccount();
Set account as primary, (*26)
$client->setPrimaryAccount($account);
Create a new bitcoin account, (*27)
$account = Coinbase::account([ 'name' => 'New Account' ]); $client->createAccount($account);
Update an account, (*28)
$account->setName('New Account Name'); $client->updateAccount($account):
Delete an account, (*29)
$client->deleteAccount($account);
List receive addresses for account, (*30)
$addresses = $client->getAccountAddresses($account);
Get receive address info, (*31)
$address = $client->getAccountAddress($account, $addressId);
List transactions for address, (*32)
$transactions = $client->getAddressTransactions($address);
Create a new receive address, (*33)
$address = Coinbase::address([ 'name' => 'New Address' ]); $client->createAccountAddress($account, $address);
List transactions, (*34)
$transactions = $client->getAccountTransactions($account);
Get transaction info, (*35)
$transaction = $client->getAccountTransaction($account, $transactionId);
Send funds, (*36)
$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);
Transfer funds to a new account, (*37)
$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);
Request funds, (*38)
$transaction = Coinbase::transaction()->request([ 'amount' => Coinbase::money(8, CurrencyCode::USD), 'description' => 'Burrito' ]); $client->createAccountTransaction($transaction);
Resend request, (*39)
$account->resendTransaction($transaction);
Cancel request, (*40)
$account->cancelTransaction($transaction);
Fulfill request, (*41)
$account->completeTransaction($transaction);
List buys, (*42)
$buys = $client->getAccountBuys($account);
Get buy info, (*43)
$buy = $client->getAccountBuy($account, $buyId);
Buy bitcoins, (*44)
$buy = Coinbase::buy([ 'bitcoinAmount' => 1 ]); $client->createAccountBuy($account, $buy);
Commit a buy, (*45)
You only need to do this if you pass commit=false
when you create the buy., (*46)
$client->createAccountBuy($account, $buy, [Coinbase::param()::COMMIT => false]); $client->commitBuy($buy);
List sells, (*47)
$sells = $client->getAccountSells($account);
Get sell info, (*48)
$sell = $client->getAccountSell($account, $sellId);
Sell bitcoins, (*49)
$sell = Coinbase::sell([ 'bitcoinAmount' => 1 ]); $client->createAccountSell($account, $sell);
Commit a sell, (*50)
You only need to do this if you pass commit=false
when you create the sell., (*51)
$client->createAccountSell($account, $sell, [Coinbase::param()::COMMIT => false]); $client->commitSell($sell);
List deposits, (*52)
$deposits = $client->getAccountDeposits($account);
Get deposit info, (*53)
$deposit = $client->getAccountDeposit($account, $depositId);
Deposit funds, (*54)
$deposit = Coinbase::deposit([ 'amount' => Coinbase::money(10, Coinbase::currencyCode()::USD) ]); $client->createAccountDeposit($account, $deposit);
Commit a deposit, (*55)
You only need to do this if you pass commit=false
when you create the deposit., (*56)
$client->createAccountDeposit($account, $deposit, [Coinbase::param()::COMMIT => false]); $client->commitDeposit($deposit);
List withdrawals, (*57)
$withdrawals = $client->getAccountWithdrawals($account);
Get withdrawal, (*58)
$withdrawal = $client->getAccountWithdrawal($account, $withdrawalId);
Withdraw funds, (*59)
$withdrawal = Coinbase::withdrawal([ 'amount' => new Money(10, CurrencyCode::USD) ]); $client->createAccountWithdrawal($account, $withdrawal);
Commit a withdrawal, (*60)
You only need to do this if you pass commit=true
when you call the withdrawal method., (*61)
$client->createAccountWithdrawal($account, $withdrawal, [Coinbase::param()::COMMIT => false]); $client->commitWithdrawal($withdrawal);
List payment methods, (*62)
$paymentMethods = $client->getPaymentMethods();
Get payment method, (*63)
$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);
Laravel wrapper for the Coinbase Wallet API v2
MIT