Paybetr PHP Library

The Paybetr PHP library was created to serve as an example implementation of the Paybetr API. This library is currently in beta testing and may not be suitable for production environments.

Installation

Dependencies

The Paybetr PHP library utilizes HTTP client extraction through HTTPlug and will therefor require an implementation of the HTTPlug interfaces to make HTTP requests to the API. HTTPlug provides some thorough documentation on getting your environment set up to make HTTPlug requests. 

TL;DR

First, install a suitable HTTPlug library:

composer require php-http/curl-client guzzlehttp/psr7 php-http/message

Second, install the Paybetr PHP library:

composer require paybetr/paybetr-php

Usage

The Client Object

The Client object handles the majority of the API requests.

Instantiating the Client object

The client object can be instantiated directly or through the static factory constructor. You can pass in specific Http\Client\HttpClient and Http\Message\MessageFactory objects or allow the class to auto discover these objects. The static constructor takes the api token as the first argumen with the HttpClient and MessageFactory objects as optional 2nd and 3rd arguments.

$client = new Paybetr\PaybetrPHP\Client();
$client->setApiToken($apiToken);
// or
$client = Paybetr\PaybetrPHP\Client::factory($apiToken);

Client Object Methods

The following are the methods available in the client object:

MethodArgumentsDescription
__construct

object $client (optional) - An instance of Http\Client\HttpClient

object $messageFactory (optional) - An instance of Http\Message\MessageFactory

Class constructor
factory

string $apiToken - The api token needed to access the Paybetr API

object $client (optional) - An instance of Http\Client\HttpClient

object $messageFactory (optional) - An instance of Http\Message\MessageFactory

Static method to construct the class with an API token
getApiToken
Returns the API token
setApiTokenstring $apiToken - The api token needed to access the Paybetr APISets the API token
getCollectionClass
Returns the class used for collections
setCollectionClassstring $class - The class you want to use to handle collectionsSets the class to be used for collections
getResourceClass
Returns the current resource class
setClass

string $className - The name of the resource class you want to override

string $class - The class you want to override $className with

Overrides the default classes for resources
getBaseUrl
Returns the base URL for the API
setBaseUrlstring $url - The base URL for the Paybetr APISets the base URL for the API
getUri
Returns the current endpoint
setUristring $uri - The endpoint for the requestSets the current endpoint
getUrl
Returns the full URL for the request
setUrlstring $url - The full URL to useSets the full URL for the request
getQuery
Returns the current query parameters
setQueryarray $query - The query parameters to use for the requestOverrides the current query parameters with new ones
addQuery

string $query - The query parameter

string $value - The value of the query parameter

Adds a query parameter to the existing ones
clearQuery
Clears the query parameters
filter

string $column - The column to filter the request on

string $value - The value of the filter

Adds a column filter to the request
sortstring $sort - The value of the sort queryAdds a sort query to the request
limitint $limit - The number of records to returnAdds a limit query to the request
reset
Resets the class. Clears the queries and the URI
address
Sets the address class as the current resource class
availableTriggers
Sets the availableTriggers class as the current resource class
balance
Sets the balance class as the current resource class
currency
Sets the currency class as the current resource class
exchange
Sets the exchange class as the current resource class
transaction
Sets the transaction class as the current resource class
trigger
Sets the trigger class as the current resource class
webhook
Sets the webhook class as the current resource class
withdrawal
Sets the withdrawal class as the current resource class
getarray $arguments - Array of arguments to pass to the requestPerforms a request to get all records
firstarray $arguments - Array of arguments to pass to the requestPerforms a request to get the first record
lastarray $arguments - Array of arguments to pass to the requestPerforms a request to get the last record
findstring $identifier - The identifier for the record you want to findPerforms a request to find a single record
createarray $attributes - Array of attributes to use in the create requestPerforms a request to create a resource (not implemented on all resources)
updatearray $attributes - Array of attributes to use in the update requestPerforms a request to update a resource (not implemented on all resources)
delete
Performs a request to delete a resource (not implemented on all resources)
request

string $method - The HTTP method for the request

array $body - Array of data to send with the request

Sends a generic request to the API

Resource Objects

Each API resource such as addresses or transactions is represented by a resource object that implements the Paybetr\PaybetrPHP\Interfaces\ResourceInterface contract. The Paybetr Client object has a method for each resource that activates the proper resource object for the query.

Resource Object Methods

MethodArgumentsDescription
__construct

object $client - Instance of Paybetr\PaybetrPHP\Client

object $resource (optional) - A resource object returned from the API

Class constructor
resource

object $client - Instance of Paybetr\PaybetrPHP\Client

object $resource - A resource object returned from the API

Static class constructor
getId
Returns the ID for the resource
getAttributes
Returns the attributes for the resource
toArray
Converts the resource to an array
getIdentifier
Returns the identifier field for the resource
findstring $identifier - The identifier field value for the recordFinds a specific record
getarray $arguments - Array of arguments used for the requestReturns a collection of records
createarray $arguments - Array of arguments used to create the recordCreates a record
updatearray $arguments - Array of arguments used to update the recordUpdates a record
delete
Deletes a record

The Collection Object

In the instance that multiple resources are returned from a query, a collection object is returned. The collection object has methods for navigating the paging of results returned by the api. The Collection object stores each resource object in a data array.

Collection Object Methods

MethodArgumentsDescription
__construct

object $client - Instance of Paybetr\PaybetrPHP\Client

object $collection - A collection object returned from the API

Class constructor
collection

object $client - Instance of Paybetr\PaybetrPHP\Client

object $collection - A collection object returned from the API

Static class constructor
toArray
Converts the collection to an array
data
Returns the data in the collection
first
Returns the first page of a paginated result set
last

Returns the last page of a paginated result set

previous
Returns the previous page of a paginated result set
next
Returns the next page of a paginated result set

Examples

Instantiating the Client class

$apiToken = 'aaaabbbbccccdddd';
$client = Paybetr\PaybetrPHP\Client::factory($apiToken);

Retrieve all addresses

$addressCollection = $client->address()->get();

Retrieve all BTC addresses

$addressCollection = $client->address()->get(['symbol' => 'BTC']);

Retrieve a specific address

$address = 'aaaabbbbccccdddd';
$addressResource = $client->address()->find($address);

Create a new address

$addressResource = $client->address()->create(['symbol' => 'BTC']);

Retrieve all addresses sorted by created_at descending

$addressCollection = $client->address()->sort('-created_at')->get();

Retrieve balances for all currencies

$balanceCollection = $client->balance()->get();

Retrieve balance for a specific currency

$balanceResource = $client->balance()->find('BTC');

Retrieve a list of all currencies

$currencyCollection = $client->currency()->get();

Retrieve a specific currency

$currencyResource = $client->currency()->find('BTC');

Retrieve all exchanges

$exchangeCollection = $client->exchange()->get();

Retrieve a specific exchange

$exchangeId = '111122223333';
$exchangeResource = $client->exchange()->find($exchangeId);

Retrieve all transactions

$transactionCollection = $client->transaction()->get();

Retrieve a specific transaction

$txid = '111122223333';
$transactionResource = $client->transaction()->find($txid);

Retrieve all transactions with the 'receive' category sorted by amount ascending

$transactionCollection = $client->transaction()->filter('category', 'receive')->sort('amount')->get();

Retrieve a list of all withdrawals and then retrieving the next page of results

$withdrawalCollection = $client->withdrawal()->get();
$withdrawalCollection->next();

Request a withdrawal with an optional external_id set

$toAddress = '1111222233334444';
$externalId = 999;
$withdrawalResource = $client->withdrawal()->create(['symbol' => 'BTC', 'to' => $toAddress, 'amount' => 1.05, 'external_id' => $externalId]);

Confirm a withdrawal

$withdrawalId = '1111222233334444';
$withdrawalResource = $client->withdrawal()->find($withdrawalId);
$withdrawalResource->update(['action' => 'confirm']);

Cancel a withdrawal

$withdrawalId = '1111222233334444';
$withdrawalResource = $client->withdrawal()->find($withdrawalId);
$withdrawalResource->delete();

Retrieve all webhooks

$webhookCollection = $client->webhook()->get();

Create a webhook

$webhookResource = $client->webhook()->create(['url' => 'https://example.com/handlewebhook?api_token=12345']);

Retrieve available webhook triggers

$availableTriggers = $client->availableTriggers()->first();

Add a trigger to a webhook

$webhookId = '1111222233334444';
$trigger = 'transaction.confirmed';
$triggerResource = $client->trigger()->create(['webhook_id' => $webhookId, 'trigger' => $trigger]);