PHP code example of paysera / lib-rest-client-common

1. Go to this page and download the library: Download paysera/lib-rest-client-common library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

paysera / lib-rest-client-common example snippets


class TestClientFactory extends ClientFactoryAbstract
{
    const DEFAULT_BASE_URL = 'http://example.com/test/rest/v1/{locale}/';
    
    private $apiClient;

    public function __construct(array $options)
    {
        $this->apiClient = $this->createApiClient($options);
    }

    public function getTestClient()
    {
        return new TestClient($this->apiClient);
    }
}

class TestClient
{
    private $apiClient;

    public function __construct(ApiClient $apiClient)
    {
        $this->apiClient = $apiClient;
    }
    
    public function withOptions(array $options)
    {
        return new TestClient($this->apiClient->withOptions($options));
    }

    /**
     * @return array
     */
    public function getSomething()
    {
        $request = $this->apiClient->createRequest(
            RequestMethodInterface::METHOD_GET,
            sprintf('/something'),
            null
        );
        return $this->apiClient->makeRequest($request);
    }
}


use Paysera\Client\CategoryClient\ClientFactory;

$clientFactory = new ClientFactory([
    'base_url' => 'custom base url',
    'auth_base_url' => 'custom auth base url',
    'basic' => [
        'username' => 'user',
        'password' => 'pass'
    ],
    'oauth' => [
        'token' => [
            'access_token' => 'your oauth access token',
            'refresh_token' => 'your oauth refresh token',
        ],
    ],
    'mac' => [
        'mac_id' => 'mac id',
        'mac_secret' => 'mac secret',
        'parameters' => [
            // list of needed parameters
        ]
    ],
    'url_parameters' => [
        'locale' => 'en',
        // list of base_url placeholder parameter values
    ],
    'headers' => [
        'Accept-Language' => 'en',
    ],
    // other configuration options
]);

$testClient = $clientFactory->getTestClient();
$data = $testClient->getSomething();

$factory = new TestClientFactory([
    MacAuthentication::TYPE => [
        'mac_id' => $macId,
        'mac_secret' => $macSecret,
        'parameters' => [
            'user_id' => 100,
        ]
    ]
]);

$client = $factory->getTestClient();

$client2 = $factory->getTestClient()->withOptions([
    MacAuthentication::TYPE => [
        'parameters' => ['user_id' => 999],
    ]
]);