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:
Method | Arguments | Description |
---|---|---|
__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 | |
setApiToken | string $apiToken - The api token needed to access the Paybetr API | Sets the API token |
getCollectionClass | Returns the class used for collections | |
setCollectionClass | string $class - The class you want to use to handle collections | Sets 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 | |
setBaseUrl | string $url - The base URL for the Paybetr API | Sets the base URL for the API |
getUri | Returns the current endpoint | |
setUri | string $uri - The endpoint for the request | Sets the current endpoint |
getUrl | Returns the full URL for the request | |
setUrl | string $url - The full URL to use | Sets the full URL for the request |
getQuery | Returns the current query parameters | |
setQuery | array $query - The query parameters to use for the request | Overrides 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 |
sort | string $sort - The value of the sort query | Adds a sort query to the request |
limit | int $limit - The number of records to return | Adds 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 | |
get | array $arguments - Array of arguments to pass to the request | Performs a request to get all records |
first | array $arguments - Array of arguments to pass to the request | Performs a request to get the first record |
last | array $arguments - Array of arguments to pass to the request | Performs a request to get the last record |
find | string $identifier - The identifier for the record you want to find | Performs a request to find a single record |
create | array $attributes - Array of attributes to use in the create request | Performs a request to create a resource (not implemented on all resources) |
update | array $attributes - Array of attributes to use in the update request | Performs 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
Method | Arguments | Description |
---|---|---|
__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 | |
find | string $identifier - The identifier field value for the record | Finds a specific record |
get | array $arguments - Array of arguments used for the request | Returns a collection of records |
create | array $arguments - Array of arguments used to create the record | Creates a record |
update | array $arguments - Array of arguments used to update the record | Updates 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
Method | Arguments | Description |
---|---|---|
__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]);