In this section, we will guide you through the process of integrating the Trustist Ecommerce Payments API into your PHP application. We will demonstrate how to set up authentication and general usage, as well as how to use the API endpoints through standard HTTP requests.
We’ll be using the Guzzle HTTP client, which is widely used and recommended for PHP applications. If you prefer a different HTTP client, please let us know.
1. Installation #
To get started, ensure you have Guzzle installed in your PHP project. You can install it using Composer:
composer require guzzlehttp/guzzle
You will also need to install a library to work with the Hawk authentication, in this example we have used hawk-auth-php:
composer require shawm11/hawk-auth
2. Configuration #
Create a new Guzzle client and configure it with your API credentials and the desired environment (sandbox or production). The base URI and Hawk authentication will be set up for all requests made using this client. Here’s an example:
use GuzzleHttp\Client;
use Shawm11\Hawk\Client\Client as HawkClient;
use Shawm11\Hawk\Client\ClientException as HawkClientException;
$base_uri = 'https://api-sandbox.trustistecommerce.com';
$client = new Client([
'base_uri' => $base_uri,
]);
function createHawkClient()
{
return new HawkClient();
}
function getCredentials()
{
return [
'id' => 'your-client-id',
'key' => 'your-client-key',
'algorithm' => 'sha256',
];
}
function sendRequest($method, $url, $payload = null)
{
global $client, $base_uri;
$hawkClient = createHawkClient();
$options = [
'credentials' => getCredentials(),
'ext' => null,
];
if ($payload !== null) {
$options['contentType'] = 'application/json';
$options['payload'] = json_encode($payload);
}
$result = $hawkClient->header($base_uri . $url, $method, $options);
$header = $result['header'];
$response = $client->request($method, $url, [
'headers' => [
'Authorization' => $header,
'Content-Type' => 'application/json'
],
'body' => $payload !== null ? json_encode($payload) : null
]);
return json_decode($response->getBody(), true);
}
3. Creating a Payment #
To create a new payment, make a POST request to the `/payments` endpoint, passing an array containing the necessary payment details.
Example:
function createPayment($amount, $reference, $returnUrl)
{
$data = [
'amount' => $amount,
'reference' => $reference,
'returnUrl' => $returnUrl
];
$url = '/v1/payments';
return sendRequest('POST', $url, $data);
}
4. Retrieving a Payment #
To retrieve the details of an existing payment, make a GET request to the `/payments/{paymentId}` endpoint, passing the payment ID as a parameter.
Example:
function getPayment($paymentId)
{
$url = "/v1/payments/{$paymentId}";
return sendRequest('GET', $url);
}
By following these steps, you should now be able to integrate the Trustist Ecommerce Payments API into your PHP application using Guzzle. For further information or assistance, please consult our comprehensive API documentation or contact our support team.