PHP code example of 131 / php-sdk

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

    

131 / php-sdk example snippets


use Bank131\SDK\Client;
use Bank131\SDK\Config;

$config = new Config(
    'https://demo.bank131.ru',
    'test_project',
    file_get_contents('/path/to/your/private_key.pem'),
    file_get_contents('/path/to/bank131/public_key.pem')
);
$client = new Client($config);

use Bank131\SDK\API\Request\Builder\RequestBuilderFactory;

$request = RequestBuilderFactory::create()
    ->issuePublicTokenBuilder()
    ->setTokenizeWidget()                   // публичный токен с доступом к виджету токенизации
    ->setSelfEmployedWidget('111111111111') // публичный токен с доступом к виджету для работы с самозанятыми
    ->setAcquiringWidget('session_id')      // публичный токен с доступом к виджету эквайринга
    ->build();

$response = $this->client->widget()->issuePublicToken($request);

use Bank131\SDK\API\Request\Builder\RequestBuilderFactory;
use Bank131\SDK\Client;
use Bank131\SDK\DTO\Customer;
use Bank131\SDK\DTO\Card\BankCard;

$request = RequestBuilderFactory::create()
    ->createPaymentSession()
    ->build();

/** @var Client $client */
$createSessionResponse = $client->session()->create($request);

$request = RequestBuilderFactory::create()
    ->startPaymentSession($createSessionResponse->getSession()->getId())
    ->setCard(new BankCard('4242424242424242', '12', '22', '123', 'CARDHOLDER NAME'))
    ->setAmount(10000, 'rub')
    ->setCustomer(new Customer('reference'))
    ->setMetadata('your metadata here')
    ->build();

$sessionStartResponse = $this->client->session()->startPayment($request);


use Bank131\SDK\API\Request\Builder\RequestBuilderFactory;
use Bank131\SDK\DTO\Card\EncryptedCard;
use Bank131\SDK\DTO\Customer;
use Bank131\SDK\DTO\Participant;
use Bank131\SDK\Client;

$request = RequestBuilderFactory::create()
    ->createPayoutSession()
    ->build();

/** @var Client $client */
$createSessionResponse = $client->session()->create($request);

$recipient = new Participant();
$recipient->setFullName('John Doe');

$request = RequestBuilderFactory::create()
    ->startPayoutSession($createSessionResponse->getSession()->getId())
    ->setCard(new EncryptedCard('number_hash_here'))
    ->setRecipient($recipient)
    ->setAmount(10000, 'rub')
    ->setCustomer(new Customer('reference'))
    ->setMetadata('your metadata here')
    ->build();

$sessionStartResponse = $this->client->session()->startPayout($request);

$request = RequestBuilderFactory::create()
    ->createPayoutSession()
    ->setBankAccount(
    new BankAccountFPS(
        '0070009210197',
        '100000000197',
        'Перевод средств по договору'
        )
    )
    ->build();

RequestBuilderFactory::create()
    ->createPaymentSession()
    ->makeFasterPaymentSystem()
    ->setAmount('3000', \Bank131\SDK\DTO\Enum\CurrencyEnum::RUB)
    ->build();

use Bank131\SDK\Client;

/** @var Client $client */
$response = $this->client->session()->status('session_id');

use Bank131\SDK\API\Request\Builder\RequestBuilderFactory;

$request = RequestBuilderFactory::create()
    ->refundSession('session_id')
    ->setAmount(1000, 'rub')
    ->setMetadata('your metadata here')
    ->build();

$response = $this->client->session()->refund($request);

use Bank131\SDK\Client;

/** @var Client $client */
$response = $client->wallet()->balance();

use Bank131\SDK\Client;
use Bank131\SDK\Services\WebHook\Hook\WebHookTypeEnum;

/** @var Client $client */
$hook = $client->handleWebHook('sign from headers', 'request body');

switch ($hook->getType()) {
    case WebHookTypeEnum::READY_TO_CONFIRM:
        $client->session()->confirm($hook->getSession()->getId());
        break;
    case WebHookTypeEnum::READY_TO_CAPTURE:
        $client->session()->capture($hook->getSession()->getId());
        break;
    case WebHookTypeEnum::ACTION_REQUIRED:
        //do some logic
        break;
    case WebHookTypeEnum::PAYMENT_FINISHED:
        //do some logic
        break;
    case WebHookTypeEnum::PAYMENT_REFUNDED:
        //do some logic
        break;
}