PHP code example of chekonline / chekonline-sdk-php

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

    

chekonline / chekonline-sdk-php example snippets


use Chekonline\Cashbox\DocumentType;
use Chekonline\Cashbox\PayAttribute;
use Chekonline\Cashbox\Line;
use Chekonline\Cashbox\Commands\ComplexCommand;
use Chekonline\Cashbox\Api;

try {
    $host = 'https://fce.chekonline.ru:4443';
    $api  = new Api($host);
    $api->setClientName('custom');
    $api->setClientVer('1.0');
    $api->setCertificate(
        'cert',
        'cert_key',
        'cert_password'
    );

    $requestId = 'orderId';
    $total     = 100 * 10;
    $command   = new ComplexCommand($requestId);
    $command->setPhoneOrEmail('[email protected]')
        ->setClientId('clientId')
        ->setNonCash([0, $total, 0])
        ->setTaxMode(pow(2, 0))
        ->setMaxDocumentsInTurn(20)
        ->setDocumentType(DocumentType::DEBIT)
        ->setPassword(1)
        ->setFullResponse(false)
        ->setDevice('auto');

//Product
    $subtotal   = 90 * 10;
    $isShipping = false;
    $line       = new Line($isShipping);
    $line->setDescription('Товар')
        ->setPayAttribute(PayAttribute::FULL_PAYMENT)
        ->setPrice($subtotal)
        ->setQty(1000)
        ->setTaxId(2);
    $line->validate();

    $command->addLine($line);

//Shipping
    $subtotal   = 10 * 10;
    $isShipping = true;
    $line       = new Line($isShipping);
    $line->setDescription('Доставка')
        ->setPayAttribute(PayAttribute::FULL_PAYMENT)
        ->setPrice($subtotal)
        ->setQty(1000)
        ->setTaxId(2);
    $line->validate();

    $command->addLine($line);

    $command->normalizeBySubTotal($total);
    $command->validate();

    $api->validate();
    $response = $api->executeCommand($command);

    $request = $response->getRequest();

    if (isset($log) == true && empty($log) == false) {
        Helper::writeLog('request', $request);
        Helper::writeLog('response', $response);
    }

    if ($response->getHttpStatusCode() != 200 &&
        $response->getApiErrorCode() != 0 &&
        $response->getHttpError() != '') {
        writeLog($request);
        writeLog($response);
    }

} catch (ChekonlineLineException $exception) {
    $logMessage = "chekonline line receipt exception:\n\tMessage: {$exception->getMessage()}";
    $logMessage .= "\n\tLine:" . var_export($exception->getReceiptLine()->getParam(), true);
    $logMessage .= "\n\tAt: {$exception->getFile()}\n";
    $logMessage .= "Trace:\n{$exception->getTraceAsString()}\n";
    writeLog($logMessage);
    writeLog($request);
    writeLog($response);
} catch (ChekonlineCommandException $exception) {
    $logMessage = "chekonline command exception: \n\tMessage: {$exception->getMessage()}";
    $logMessage .= "\n\tCommand:" . var_export($exception->getCommand()->getParams(), true);
    $logMessage .= "Trace:\n{$exception->getTraceAsString()}\n";
    writeLog($logMessage);
    writeLog($request);
    writeLog($response);
} catch (ChekonlineSDKException $exception) {
    $logMessage = "chekonline exception: \n\tMessage: {$exception->getMessage()}";
    $logMessage .= "\n\tAt: {$exception->getFile()}\n";
    $logMessage .= "Trace:\n{$exception->getTraceAsString()}\n";
    writeLog($logMessage);
    writeLog($request);
    writeLog($response);
}