• This repository has been archived on 20/Jan/2020
  • Stars
    star
    293
  • Rank 136,289 (Top 3 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created almost 11 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

DEPRECATED β€” PHP wrapper for the Coinbase API

Coinbase Wallet PHP Library

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

This is the official client library for the Coinbase Wallet API v2. We provide an intuitive, stable interface to integrate Coinbase Wallet into your PHP project.

Important: As this library is targeted for newer API v2, it requires v2 permissions (i.e. wallet:accounts:read). If you're still using v1, please use the older version of this library.

Installation

Install the library using Composer. Please read the Composer Documentation if you are unfamiliar with Composer or dependency managers in general.

"require": {
    "coinbase/coinbase": "~2.0"
}

Authentication

API Key

Use an API key and secret to access your own Coinbase account.

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;

$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);

OAuth2

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.

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;

// with a refresh token
$configuration = Configuration::oauth($accessToken, $refreshToken);

// without a refresh token
$configuration = Configuration::oauth($accessToken);

$client = Client::create($configuration);

Two factor authentication

The send money endpoint requires a 2FA token in certain situations (read more here). A specific exception is thrown when this is required.

use Coinbase\Wallet\Enum\Param;
use Coinbase\Wallet\Exception\TwoFactorRequiredException;
use Coinbase\Wallet\Resource\Transaction;

$transaction = Transaction::send([
    'toEmail' => '[email protected]',
    'bitcoinAmount' => 1
]);

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

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

Pagination

Several endpoints are paginated. By default, the library will only fetch the first page of data for a given request. You can easily load more than just the first page of results.

$transactions = $client->getAccountTransactions($account);
while ($transactions->hasNextPage()) {
    $client->loadNextTransactions($transactions);
}

You can also use the fetch_all parameter to have the library issue all the necessary requests to load the complete collection.

use Coinbase\Wallet\Enum\Param;

$transactions = $client->getAccountTransactions($account, [
    Param::FETCH_ALL => true,
]);

Warnings

It's prudent to be conscious of warnings. The library will log all warnings to a standard PSR-3 logger if one is configured.

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;

$configuration = Configuration::apiKey($apiKey, $apiSecret);
$configuration->setLogger($logger);
$client = Client::create($configuration);

Resource references

In some cases the API will return resource references in place of expanded resource objects. These references can be expanded by refreshing them.

$deposit = $this->client->getAccountDeposit($account, $depositId);
$transaction = $deposit->getTransaction();
if (!$transaction->isExpanded()) {
    $this->client->refreshTransaction($transaction);
}

You can also request that the API return an expanded resource in the initial request by using the expand parameter.

use Coinbase\Wallet\Enum\Param;

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

Resource references can be used when creating new resources, avoiding the overhead of requesting a resource from the API.

use Coinbase\Wallet\Resource\Deposit;
use Coinbase\Wallet\Resource\PaymentMethod;

$deposit = new Deposit([
    'paymentMethod' => PaymentMethod::reference($paymentMethodId)
]);

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

Responses

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.

$data = $deposit->getRawData();

Raw data from the last HTTP response is also available on the client object.

$data = $client->decodeLastResponse();

Active record methods

The library includes support for active record methods on resource objects. You must enable this functionality when bootstrapping your application.

$client->enableActiveRecord();

Once enabled, you can call active record methods on resource objects.

use Coinbase\Wallet\Enum\Param;

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

Usage

This is not intended to provide complete documentation of the API. For more detail, please refer to the official documentation.

List supported native currencies

$currencies = $client->getCurrencies();

List exchange rates

$rates = $client->getExchangeRates();

Buy price

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

Sell price

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

Spot price

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

Current server time

$time = $client->getTime();

Get authorization info

$auth = $client->getCurrentAuthorization();

Lookup user info

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

Get current user

$user = $client->getCurrentUser();

Update current user

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

List all accounts

$accounts = $client->getAccounts();

List account details

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

List primary account details

$account = $client->getPrimaryAccount();

Set account as primary

$client->setPrimaryAccount($account);

Create a new bitcoin account

use Coinbase\Wallet\Resource\Account;

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

Update an account

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

Delete an account

$client->deleteAccount($account);

List receive addresses for account

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

Get receive address info

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

List transactions for address

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

Create a new receive address

use Coinbase\Wallet\Resource\Address;

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

List transactions

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

Get transaction info

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

Send funds

use Coinbase\Wallet\Enum\CurrencyCode;
use Coinbase\Wallet\Resource\Transaction;
use Coinbase\Wallet\Value\Money;

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

try { $client->createAccountTransaction($account, $transaction); }
catch(Exception $e) {
     echo $e->getMessage(); 
}

Transfer funds to a new account

use Coinbase\Wallet\Resource\Transaction;
use Coinbase\Wallet\Resource\Account;

$fromAccount = Account::reference($accountId);

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

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

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

Request funds

use Coinbase\Wallet\Enum\CurrencyCode;
use Coinbase\Wallet\Resource\Transaction;
use Coinbase\Wallet\Value\Money;

$transaction = Transaction::request([
    'amount'      => new Money(8, CurrencyCode::USD),
    'description' => 'Burrito'
]);

$client->createAccountTransaction($transaction);

Resend request

$account->resendTransaction($transaction);

Cancel request

$account->cancelTransaction($transaction);

Fulfill request

$account->completeTransaction($transaction);

List buys

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

Get buy info

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

Buy bitcoins

use Coinbase\Wallet\Resource\Buy;

$buy = new Buy([
    'bitcoinAmount' => 1
]);

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

Commit a buy

You only need to do this if you pass commit=false when you create the buy.

use Coinbase\Wallet\Enum\Param;

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

List sells

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

Get sell info

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

Sell bitcoins

use Coinbase\Wallet\Resource\Sell;

$sell = new Sell([
    'bitcoinAmount' => 1
]);

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

Commit a sell

You only need to do this if you pass commit=false when you create the sell.

use Coinbase\Wallet\Enum\Param;

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

List deposits

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

Get deposit info

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

Deposit funds

use Coinbase\Wallet\Enum\CurrencyCode;
use Coinbase\Wallet\Resource\Deposit;
use Coinbase\Wallet\Value\Money;

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

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

Commit a deposit

You only need to do this if you pass commit=false when you create the deposit.

use Coinbase\Wallet\Enum\Param;

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

List withdrawals

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

Get withdrawal

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

Withdraw funds

use Coinbase\Wallet\Enum\CurrencyCode;
use Coinbase\Wallet\Resource\Withdrawal;
use Coinbase\Wallet\Value\Money;

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

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

Commit a withdrawal

You only need to do this if you pass commit=true when you call the withdrawal method.

use Coinbase\Wallet\Enum\Param;

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

List payment methods

$paymentMethods = $client->getPaymentMethods();

Get payment method

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

Get merchant

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

List orders

$orders = $client->getOrders();

Get order

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

Create order

use Coinbase\Wallet\Resource\Order;
use Coinbase\Wallet\Value\Money;

$order = new Order([
    'name' => 'Order #1234',
    'amount' => Money::btc(1)
]);

$client->createOrder($order);

Refund order

use Coinbase\Wallet\Enum\CurrencyCode;

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

Checkouts

List checkouts

$checkouts = $client->getCheckouts();

Create checkout

use Coinbase\Wallet\Resource\Checkout;

$params = array(
    'name'               => 'My Order',
    'amount'             => new 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";

Get checkout

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

Get checkout's orders

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

Create order for checkout

$order = $client->createNewCheckoutOrder($checkout);
$raw_body = file_get_contents('php://input');
$signature = $_SERVER['HTTP_CB_SIGNATURE'];
$authenticity = $client->verifyCallback($raw_body, $signature); // boolean

Contributing and testing

The test suite is built using PHPUnit. Run the suite of unit tests by running the phpunit command.

phpunit

There is also a collection of integration tests that issues real requests to the API and inspects the resulting objects. To run these tests, you must copy phpunit.xml.dist to phpunit.xml, provide values for the CB_API_KEY and CB_API_SECRET variables, and specify the integration group when running the test suite.

phpunit --group integration

More Repositories

1

terraform-landscape

Improve Terraform's plan output to be easier to read and understand
Ruby
1,546
star
2

coinbase-wallet-sdk

An open protocol that lets users connect their mobile wallets to your DApp
TypeScript
1,276
star
3

coinbase-pro-trading-toolkit

DEPRECATED β€” The Coinbase Pro trading toolkit
TypeScript
856
star
4

kryptology

Go
838
star
5

coinbase-pro-node

DEPRECATED β€” The official Node.js library for Coinbase Pro
JavaScript
828
star
6

build-onchain-apps

Accelerate your web3 creativity with the Build Onchain Apps Toolkit. ⛡️
TypeScript
570
star
7

odin

Archived: Odin deployer to AWS for 12 Factor applications.
Go
540
star
8

coinbase-python

DEPRECATED β€” Coinbase Python API
Python
511
star
9

assume-role

DEPRECATED β€” assume-role: a CLI tool making it easy to assume IAM roles through an AWS Bastion account
Shell
424
star
10

geoengineer

DEPRECATED β€” Infrastructure As Code
Ruby
403
star
11

coinbase-node

DEPRECATED β€” The official Node.js library for the Coinbase API.
JavaScript
361
star
12

mesh-specifications

Specification files for the Rosetta Blockchain Standard
Shell
313
star
13

onchainkit

React components and TypeScript utilities for top-tier onchain apps.
TypeScript
287
star
14

cbpay-js

Coinbase Pay SDK
TypeScript
270
star
15

coinbase-ruby

DEPRECATED β€” Ruby wrapper for the Coinbase API
Ruby
239
star
16

waas-sdk-react-native

Coinbase Wallet as a Service (WaaS) SDK for React Native. Enables MPC Operations for iOS and Android Devices.
TypeScript
222
star
17

step

step is a framework for building, testing and deploying AWS Step Functions and Lambda
Go
207
star
18

wallet-mobile-sdk

An open protocol for mobile web3 apps to interact with wallets
Kotlin
203
star
19

temporal-ruby

Ruby SDK for Temporal
Ruby
194
star
20

mesh-sdk-go

Rosetta Client Go SDK
Go
182
star
21

coinbase-ios-sdk

Integrate bitcoin into your iOS application with Coinbase
Swift
172
star
22

nft-dapp-starter-kit

Starter kit for developers who want to build an NFT minting site
TypeScript
153
star
23

coinbase-java

Coinbase API v1 library for Java
Java
146
star
24

coinbase-commerce-node

Coinbase Commerce Node
JavaScript
143
star
25

mesh-cli

CLI for the Rosetta API
Go
142
star
26

waas-client-library-go

Coinbase Wallet as a Service (WaaS) Client Library in Go.
Go
138
star
27

traffic_jam

DEPRECATED β€” Ruby library for time-based rate limiting
Ruby
129
star
28

coinbase-commerce-php

Coinbase Commerce PHP
PHP
127
star
29

coinbase-exchange-ruby

DEPRECATED β€” Official Ruby library for the GDAX API
Ruby
122
star
30

dexter

Forensics acquisition framework designed to be extensible and secure
Go
118
star
31

multisig-tool

DEPRECATED β€” Multisig Vault recovery tool
JavaScript
110
star
32

mesh-bitcoin

Bitcoin Rosetta API Implementation
Go
104
star
33

smart-wallet

Solidity
103
star
34

mesh-ethereum

Ethereum Rosetta API Implementation
Go
98
star
35

coinbase-android-sdk

DEPRECATED β€” Android SDK for Coinbase
Java
95
star
36

mongobetween

Go
93
star
37

fenrir

Archived: AWS SAM deployer to manage serverless projects.
Go
91
star
38

react-coinbase-commerce

Coinbase Commerce React
JavaScript
91
star
39

pwnbot

You call PwnBot in Slack on someone else's unlocked computer
JavaScript
89
star
40

digital-asset-policy-proposal

Digital Asset Policy Proposal: Safeguarding America’s Financial Leadership
85
star
41

coinbase-commerce-python

Coinbase Commerce Python
Python
77
star
42

CBTabViewExample

TypeScript
69
star
43

coinbase-bitmonet-sdk

DEPRECATED β€” Library to accept bitcoin payments in your Android App
Java
62
star
44

chainstorage

The File System For a Multi-Blockchain World
Go
61
star
45

self-service-iam

DEPRECATED β€” Self Service AWS IAM Policies for dev at scale
JavaScript
58
star
46

coinbase-wordpress

DEPRECATED β€” Coinbase plugin/widget for Wordpress
57
star
47

coinbase-commerce-woocommerce

Accept Bitcoin on your WooCommerce-powered website.
PHP
55
star
48

barbar

DEPRECATED β€” OSX crypto-currency price ticker
Swift
53
star
49

demeter

DEPRECATED β€” Security Group Management For AWS
Ruby
52
star
50

verifications

πŸ“œ "Coinbase Verifications" is a set of Coinbase-verified onchain attestations that enable access to apps and other onchain benefits.
Solidity
50
star
51

coinbase-exchange-node

DEPRECATED β€” Use gdax-node
JavaScript
46
star
52

cadence-ruby

Ruby SDK for Cadence
Ruby
44
star
53

commerce-onchain-payment-protocol

Solidity
41
star
54

protoc-gen-rbi

Protobuf compiler plugin that generates Sorbet .rbi "Ruby Interface" files.
Go
38
star
55

coinbase-woocommerce

DEPRECATED β€” Accept Bitcoin on your WooCommerce-powered website.
38
star
56

coinbase-advanced-py

The Advanced API Python SDK is a Python package that makes it easy to interact with the Coinbase Advanced API. The SDK handles authentication, HTTP connections, and provides helpful methods for interacting with the API.
Python
37
star
57

mesh-ecosystem

Repository of all open source Rosetta implementations and SDKs
33
star
58

master_lock

Inter-process locking library using Redis.
Ruby
31
star
59

coinbase-commerce-ruby

Coinbase Commerce Ruby Gem
Ruby
30
star
60

watchdog

DEPRECATED -- Github Bot for Datadog codification
Go
28
star
61

bittip

DEPRECATED β€” Reddit tip bot
JavaScript
27
star
62

maxfuzz

DEPRECATED β€” Containerized Cloud Fuzzing
C
26
star
63

cash-addr

Utility to convert between base58 and CashAddr BCH addresses.
Ruby
25
star
64

rules_ruby

Bazel Ruby Rules
Starlark
24
star
65

mesh-geth-sdk

go-ethereum based sdk for Rosetta API
Go
23
star
66

gtt-ui

DEPRECATED
JavaScript
22
star
67

btcexport

Export process for Bitcoin blockchain data to CSV
Go
22
star
68

bchd-explorer

Vue
21
star
69

redisbetween

Go
20
star
70

baseca

Go
18
star
71

coinbase-magento

DEPRECATED β€” Accept Bitcoin on your Magento-powered website.
17
star
72

coinbase-commerce-whmcs

Coinbase Commerce module for WHMCS
PHP
16
star
73

coinbase-android-sdk-example

DEPRECATED β€” Example android app leveraging the coinbase android sdk
Java
15
star
74

coinbase-nft-floor-price

Coinbase NFT floor price estimate model
Python
15
star
75

coinbase-spree

DEPRECATED β€” Accept bitcoin payments on your Spree store with Coinbase.
15
star
76

service_variables

Service level variables backed by Redis - useful for service wide configuration.
Ruby
12
star
77

solidity-workshop

JavaScript
12
star
78

omniauth-coinbase

DEPRECATED β€” Coinbase OAuth 2 Strategy for Omniauth
Ruby
12
star
79

coinbase-javascript-sdk

DEPRECATED
JavaScript
11
star
80

coinbase-commerce-prestashop

DEPRECATED β€” Official Coinbase Commerce Prestashop Payment Module
PHP
11
star
81

wrapped-tokens-os

TypeScript
11
star
82

coinbase-cloud-sdk-js

TypeScript
11
star
83

step-asg-deployer

Deprecated, renamed and maintained at https://github.com/coinbase/odin
Go
10
star
84

eip-token-upgrade

Solidity
10
star
85

mkr-vote-proxy

Cold storage-friendly voting for MKR tokens
Solidity
10
star
86

salus

We would like to request that all contributors please clone a *fresh copy* of this repository since the September 21st maintenance.
HTML
9
star
87

chainsformer

Go
9
star
88

coinbase-magento2

DEPRECATED: Accept Bitcoin on your Magento2-powered website.
8
star
89

code-of-conduct

Code of conduct for open source projects managed by Coinbase
8
star
90

coinbase-commerce-opencart

DEPRECATED β€” Coinbase Commerce Integration For Opencart
PHP
8
star
91

magic-spend

Solidity
8
star
92

chainnode

Go
7
star
93

waas-proxy-server

Go
7
star
94

client-analytics

TypeScript
7
star
95

node-process-lock

DEPRECATED β€” Simple process locking using Redis.
JavaScript
7
star
96

coinbase-commerce-magento

DEPRECATED β€” Coinbase Commerce Payment Gateway For Magento 2
PHP
7
star
97

coinbase-commerce-gravity-forms

DEPRECATED β€” Official Coinbase Commerce Payment Gateway For Gravity Forms
PHP
7
star
98

paymaster-bundler-examples

7
star
99

coinbase-zencart

DEPRECATED β€” Accept Bitcoin on your Zen Cart-powered website.
6
star
100

demeter-example

DEPRECATED β€” Demeter
6
star