Skip to content

IceQ1337/coinbase-commerce-php

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coinbase Commerce

Fork of the official PHP library for the Coinbase Commerce API.
Note: The official repository is not actively maintained.

I strongly recommend to access the few endpoints that are left directly via a client of your choice and advise against using this library! Also, the old API could be discontinued at any time.

If the official repository gets updated, this fork will be deleted or updated accordingly. You'll notice when updating packages using composer results in an error. In this case, the official repository has probably been updated and this fork has been removed. However, this will probably never happen 🙂

The information in this documentation may no longer be correct! In addition, this library provides functions that no longer exist, but I couldn't be bothered to remove them yet.

Table of contents

PHP versions

PHP version 8.1 and above are supported.

Installation

If you already have a composer.json file in your project, you can skip Step 3 by pre-editing the composer.json file (Step 2) and then finish with Step 1.

Step 1: Install the official package with composer

composer require coinbase/coinbase-commerce

Step 2: Add this fork to the version control system (VCS)

The composer.json file should require the package like this:

"require": {
    "coinbase/coinbase-commerce": "^1.0"
}

Edit composer.json to require the package like this instead:

"require": {
    "coinbase/coinbase-commerce": "dev-master"
},
"repositories": [
    {
        "type": "vcs",
        "url": "git@github.com:IceQ1337/coinbase-commerce-php"
    }
]

Step 3: Update the source files with composer

composer update coinbase/coinbase-commerce

Usage

For more details visit the Coinbase Commerce API documentation.

To start using this library register an account on Coinbase Commerce.
To find your API_KEY, navigate to the User Settings section.

Next initialize a Client for interacting with the API. The only required parameter to initialize a client is apiKey, however, you can also pass in baseUrl, apiVersion and timeout. You can also set your own HTTP Client, e.g. to use proxy settings.

Parameters can be also be set post-initialization:

use CoinbaseCommerce\ApiClient;

// Make sure you don't store your API Key in your source code!
$apiClientObj = ApiClient::init(<API_KEY>);
$apiClientObj->setHttpClient(HttpFactory::makeClient());
$apiClientObj->setTimeout(3);

Disable SSL Check

$apiClientObj->verifySsl(false);

The API resource class provides the following static methods: list, all, create, retrieve, updateById, deleteById. Additionally, the API resource class also provides the following instance methods: save, delete, insert, update.

Each API method returns an ApiResource which represents the JSON response from the API. When the response data is parsed into objects, the appropriate ApiResource subclass will automatically be used.

Client supports the handling of common API errors and warnings. All errors that occur during any interaction with the API will be raised as exceptions.

Error Status Code
APIException *
InvalidRequestException 400
ParamRequiredException 400
ValidationException 400
AuthenticationException 401
ResourceNotFoundException 404
RateLimitExceededException 429
InternalServerException 500
ServiceUnavailableException 503

Checkouts

Checkouts API docs

More examples on how to use checkouts can be found in the examples/Resources/CheckoutExample.php file.

Load checkout resource class

use CoinbaseCommerce\Resources\Checkout;

Retrieve

$checkoutObj = Checkout::retrieve(<checkout_id>);

Create

$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();

List

List method returns ApiResourceList object.

$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);
    }
}

Get all checkouts

$params = [
    'order' => 'desc'  
];

$allCheckouts = Checkout::getAll($params);

Charges

Charges API docs

More examples on how to use charges can be found in the examples/Resources/ChargeExample.php file.

Load charge resource class

use CoinbaseCommerce\Resources\Charge;

Retrieve

$chargeObj = Charge::retrieve(<charge_id>);

Create

$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

$list = Charge::getList();

foreach($list as $charge) {
    var_dump($list);
}

$pagination = $list->getPagination();

Get all charges

$allCharges = Charge::getAll();

Cancel a charge

Cancels a charge that has been previously created.

Note: Only new charges can be successfully canceled. Once payment is detected, charge can no longer be canceled.

$chargeObj = Charge::retrieve(<charge_id>);

if ($chargeObj) {
    $chargeObj->cancel();
}

Events

Events API docs

More examples on how to use events can be found in the examples/Resources/EventExample.php file.

Load event resource class

use CoinbaseCommerce\Resources\Event;

Retrieve

$eventObj = Event::retrieve(<event_id>);

List

$listEvent = Event::getList();

foreach($listEvent as $event) {
    var_dump($event);
}

$pagination = $listEvent->getPagination();

Get all events

$allEvents = Event::getAll();

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 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 signs the webhook events it sends to your endpoint, allowing you to validate and verify that they weren't sent by someone else. You can find a simple example of how to use this in the examples/Webhook folder.

Verify Signature header

use CoinbaseCommerce\Webhook;

try {
    Webhook::verifySignature($body, $signature, $sharedSecret);
    echo 'Successfully verified';
} catch (\Exception $exception) {
    echo $exception->getMessage();
    echo 'Failed';
}

Testing and Contributing

Any and all contributions are welcome! The process is simple: fork this repo, make your changes, run the test suite, and submit a pull request. To run the tests, clone the repository and run the following commands:

composer install
composer test

License

Apache-2.0

About

Coinbase Commerce PHP

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%