PHP code example of compayer / compayer-lib-php

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

    

compayer / compayer-lib-php example snippets




use Compayer\SDK\Client;
use Compayer\SDK\Config;
use Compayer\SDK\Event;
use Compayer\SDK\Exceptions\SdkException;

const CLIENT_ID = 'client_id';
const SECRET_KEY = 'secret_key';

// Create and configure a configuration object (including debug mode).
$config = new Config(CLIENT_ID, SECRET_KEY);
$config->setDebugMode(true);

// Create an SDK client for sending events.
$client = new Client($config);

// Create an instance of the Event class and set the maximum possible properties about a user and payment.
// All fields are optional, but it's important to fill out one of the fields: "userEmails", "userPhones" or "userAccounts" 
// to identify the user made the payment.
$event = (new Event())
    ->setMerchantTransactionId('12345')
    ->setPaymentAmount(250.50)
    ->setPaymentCurrency('RUB')
    ->setUserLang('RUS')
    ->setUserEmails(['[email protected]'])
    ->setUserAccounts(['54321'])
    ->setExtra(['my_property' => 'value']);

// You can also create an object with an event from an array, 
// where names of the keys of the array match names of the Event properties.
$event = Event::fromArray([
    'merchantTransactionId' => '12345',
    'paymentAmount' => 250.50,
    'paymentCurrency' => 'RUB',
    'userLang' => 'RUS',
    'userEmails' => ['[email protected]'],
    'userAccounts' => ['54321'],
    'extra' => ['my_property' => 'value'],
]);

try {
    // Send the generated event and get the response message with a transaction identifier and log.
    $response = $client->pushStartEvent($event);
} catch (SdkException $e) {
    print_r($e->getMessage());
}

// Use it to send "success", "fail" or "refund" events and to chain events.
// The transaction identifier is UUID string like 3677eb06-1a9a-4b6c-9d6a-1799cae1b6bb.
$transactionId = $response->getTransactionId();

// Show logs with the debug mode configuration.
print_r($response->getLog());

use Compayer\SDK\Client;
use Compayer\SDK\Config;
use Compayer\SDK\Event;
use Compayer\SDK\Exceptions\SdkException;

const CLIENT_ID = 'client_id';
const SECRET_KEY = 'secret_key';

// Create and configure a configuration object (e.g. with debug mode).
$config = new Config(CLIENT_ID, SECRET_KEY);
$config->setDebugMode(true);

// Create SDK client for sending events.
$client = new Client($config);

// Transaction ID received on start event
$transactionId = '3677eb06-1a9a-4b6c-9d6a-1799cae1b6bb';

// Create an instance of the Event class and set the maximum possible properties about the user and payment
// All fields are optional, but you must fill out one of the fields: "userEmails", "userPhones" or "userAccounts" 
// to identify the user who made the payment. If you have a transaction ID for the start event, specify it.
$event = (new Event())
    ->setTransactionId($transactionId)
    ->setMerchantTransactionId('12345')
    ->setPaymentAmount(250.50)
    ->setPaymentCurrency('RUB')
    ->setPayoutAmount(3.87)
    ->setPayoutCurrency('USD')
    ->setUserEmails(['[email protected]'])
    ->setUserAccounts(['54321'])
    ->setExtra(['my_property' => 'value'])
    ->setPaymentSystemResponse('Payment system response as a string');

try {
    // Send the generated event
    // Or use $client->pushFailEvent($event) in case of payment failure
    // Or use $client->pushRefundEvent($event) in case of payment refund
    $client->pushSuccessEvent($event);
} catch (SdkException $e) {
    print_r($e->getMessage());
}