PHP code example of justcommunication-ru / paykeeper-sdk

1. Go to this page and download the library: Download justcommunication-ru/paykeeper-sdk 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/ */

    

justcommunication-ru / paykeeper-sdk example snippets


use JustCommunication\PaykeeperSDK\PaykeeperAPIClient;

$client = new PaykeeperAPIClient($url, $username, $password, $tokenHandler);

use JustCommunication\PaykeeperSDK\TokenHandler\FileTokenHandler;

$tokenHandler = new FileTokenHandler('/path/to/token.txt');

use JustCommunication\PaykeeperSDK\TokenHandler\CallbackTokenHandler;

$tokenHandler = new CallbackTokenHandler(function () use ($db) {
    return $db->fetchColumn('SELECT token FROM tokens WHERE name = "paykeeper"');
}, function ($new_token) {
    $db->update('UPDATE tokens SET token = :token WHERE name = "paykeeper"', [
        'token' => $new_token
    ]);
});

use JustCommunication\PaykeeperSDK\API\Invoice\InvoicePreviewRequest;

$invoicePreviewRequest = new InvoicePreviewRequest();
$invoicePreviewRequest
    ->setOrderId(123)
    ->setServiceName('Test service')
    ->setAmount(100)
;

try {
    $response = $client->sendInvoicePreviewRequest($invoicePreviewRequest);

    header('Location: ' . $response->getInvoiceUrl()); // перенаправляем пользователя на страницу оплаты
    exit;
} catch (PaykeeperAPIException $e) {
    // обработка ошибки
}

$invoice = $client->getInvoice($invoice_id);

$invoice->getId();
$invoice->getStatus();
$invoice->getCreatedAt();
$invoice->getPaidAt();
$invoice->getPayAmount();

use JustCommunication\PaykeeperSDK\API\Invoice\InvoiceListRequest;

$invoiceListRequest = new InvoiceListRequest();
$invoiceListRequest->setStatuses([
    $invoiceListRequest::STATUS_SENT,
    $invoiceListRequest::STATUS_PAID,
    $invoiceListRequest::STATUS_EXPIRED
])
    ->setStartDate(new \DateTime('-10 days'))
    ->setEndDate(new \DateTime('+10 days'))
;

$response = $client->sendInvoiceListRequest($invoiceListRequest);

foreach ($response->getInvoices() as $invoice) {
    $invoice->getId();
    $invoice->getStatus();
    $invoice->getCreatedAt();
    $invoice->getPaidAt();
    $invoice->getPayAmount();
}

use JustCommunication\PaykeeperSDK\PaykeeperAPIClient;

$client = new PaykeeperAPIClient($url, $username, $password, $tokenHandler, [
    'proxy' => 'tcp://localhost:8125',
    'timeout' => 6,
    'connect_timeout' => 4
]);

// Http клиент с логгированием всех запросов

$stack = HandlerStack::create();
$stack->push(Middleware::log($logger, new MessageFormatter(MessageFormatter::DEBUG)));

$httpClient = new \GuzzleHttp\Client([
    'handler' => $stack,
    'timeout' => 6
]);

use JustCommunication\PaykeeperSDK\PaykeeperAPIClient;

$client = new PaykeeperAPIClient($url, $username, $password, $tokenHandler, $httpClient);

use JustCommunication\PaykeeperSDK\PaykeeperAPIClient;

$client = new PaykeeperAPIClient($url, $username, $password, $tokenHandler);
$client->setHttpClient($httpClient);

$client->setLogger($someLogger);