ibercheck/ibercheck-api-sdk

v1.0.0 2020-10-31 11:34 UTC

This package is auto-updated.

Last update: 2024-11-27 23:45:23 UTC


README

Ibercheck page | API Documentation page | API Playground page

This SDK provides a friendly interface for performing requests and decode responses.

Installation

You can use Composer:

composer require ibercheck/ibercheck-api-sdk

Usage

Create the sale hash

$hash = Ibercheck\Api\Sale::createSaleHash(
    's3cr3t', // Affiliate Secret
    'ABCD_123456', // Order number
    'autocheck' // Product mnemonic
);

Authenticate webhook request

if(!Ibercheck\Api\Webhook::isAuthentic(
        's3cr3t', // Affiliate Secret
        '596f6838cb5b....ae812fb62f91c6', // Value of `X-Ibercheck-Signature` header
        '{....}' // Webhook payload
    )
) {
    throw new Exception('Fraudulent webhook received');
}

API Calls

This SDK assist you for craft a well formatted API request and decode the response back to PHP.

// Exchange affiliate credentials with a private access token.
$oAuthRequest = new Ibercheck\Api\ApiRequest('POST', 'http://api_dev.ibercheck.net/oauth');
$oAuthRequest = $oAuthRequest->withJsonSerializableData(
    [
        'grant_type' => 'client_credentials',
        'client_id' => 'ACME_SL', // Affiliate Name
        'client_secret' => 's3cr3t', // Affiliate Secret
    ]
);
$oAuthResponsePayload = sendRequestToApi($oAuthRequest);

// Use the new private access token for authenticate your API requests.
$meRequest = new Ibercheck\Api\ApiRequest('GET', 'http://api_dev.ibercheck.net/me');
$meRequest = $meRequest->withAuthentication($oAuthResponsePayload['access_token']);
$meResponsePayload = sendRequestToApi($meRequest);
print_r($meResponsePayload);

function sendRequestToApi(Psr\Http\Message\RequestInterface $request) {
    $psr18HttpClient = new Psr\Http\Client\ClientInterface();
    $ibercheckApiClient = new Ibercheck\Api\Client($psr18HttpClient);

    try {
        $response = $ibercheckApiClient->sendRequest($request);
        $payload = $ibercheckApiClient->decodeResponseBody((string) $response->getBody());
    } catch (Ibercheck\Api\ApiCommunicationException $apiCommunicationException) {
        // A network error has occurred while sending the request or receiving the response.

        // Retry
    } catch (Ibercheck\Api\DeserializeException $deserializationException) {
        // Nobody knows when this happen, may an HTTP Proxy on our side or on your side started to return HTML responses with errors.

        // Retry
    } catch (Ibercheck\Api\ApiServerException $apiServerException) {
        // Our server has crashed. We promise to fix it ASAP.

        echo 'Error code', $apiClientException->getStatus(), PHP_EOL;
        echo 'Error type', $apiClientException->getType(), PHP_EOL;
        echo 'Error message', $apiClientException->getMessage(), PHP_EOL;
        echo 'Error detail', var_export($apiClientException->getDetail(), true), PHP_EOL;
    } catch (Ibercheck\Api\ApiClientException $apiClientException) {
        // Your client has sent an invalid request. Please check your code.

        echo 'Error code', $apiClientException->getStatus(), PHP_EOL;
        echo 'Error type', $apiClientException->getType(), PHP_EOL;
        echo 'Error message', $apiClientException->getMessage(), PHP_EOL;
        echo 'Error detail', var_export($apiClientException->getDetail(), true), PHP_EOL;
    }

    return $payload;
}

License

Distributed under the MIT license