HEX
Server: LiteSpeed
System: Linux php-prod-3.spaceapp.ru 5.15.0-151-generic #161-Ubuntu SMP Tue Jul 22 14:25:40 UTC 2025 x86_64
User: xnsbl7462 (1008)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //home/marketing.cfbon.ru/public_html/app/Services/IicoCardService.php
<?php

namespace App\Services;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\RequestException;
use Illuminate\Support\Facades\Http;

class IicoCardService
{

    private string $iicoCardHost;
    private string $iicoLogin;
    private string $iicoPassword;
    private string $iicoOrganizationId;

    private string $key = '';
    private string $programName = 'кофе_бон_бонусная';

    private $proxyToken = '7b84459332c6dbcd6cacce71c309eebcb45206c3b15eb9e053c33c97958b8851';
    private $proxyUrl = 'https://api.coffeebon.ru/appApi/v2.0/simple-proxy.php';

    function __construct(string $iicoCardHost, string $iicoLogin, string $iicoPassword, string $iicoOrganizationId)
    {
        $this->iicoCardHost = $iicoCardHost;
        $this->iicoLogin = $iicoLogin;
        $this->iicoPassword = $iicoPassword;
        $this->iicoOrganizationId = $iicoOrganizationId;
    }

    public function proxy(string $url, string $method, string $action, array $data = null): false|string
    {
        try {
            $response = Http::withToken($this->proxyToken)
                ->withHeaders(['Content-Type' => 'application/json'])
                ->withoutVerifying()
                ->send($method, $this->proxyUrl, [
                    'json' => [
                        'url' => $url,
                        'action' => $action,
                        'data' => $data,
                    ]
                ]);
            return $response->successful() ? $response->body() : false;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }
    public function connect(): void
    {
        $url = "{$this->iicoCardHost}/auth/access_token?user_id={$this->iicoLogin}&user_secret={$this->iicoPassword}";

        try {
            $response = $this->proxy($url, 'get', 'access');
            $this->key = trim($response, '"');

            if (empty($this->key)) {
                throw new \Exception('Empty access token received');
            }

        } catch (RequestException $e) {
            throw new \Exception("Connection to IIKO CARD failed: " . $e->getMessage());
        } catch (ConnectionException $e) {
        }
    }
    public function clientInfoById($cardId) : ?object
    {
        $query = array(
            'access_token' => $this->key,
            'organization' => $this->iicoOrganizationId,
            'id' => $cardId
        );

        $url = $this->iicoCardHost . '/customers/get_customer_by_id?' . http_build_query($query);
        $reply = $this->proxy($url, 'GET', 'get_customer_by_id');

        if (!empty($reply)) return json_decode($reply);

        return null;
    }

    public function getWalletId(array $walletData): ?string
    {
        if (empty($walletData)) return null;

        foreach ($walletData as $wallet) {
            if ($wallet->wallet->name == $this->programName) return $wallet->wallet->id;
        }
        return null;
    }

    public function refillBonus(string $cardId, int $sum, string $message): void
    {
        $data = $this->clientInfoById($cardId);
        $walletId = $this->getWalletId($data->walletBalances);

        $url = $this->iicoCardHost . '/customers/refill_balance?access_token=' . $this->key;
        $data = [
            'customerId' => $cardId,
            'organizationId' => $this->iicoOrganizationId,
            'walletId' => $walletId,
            'sum' => $sum,
            'comment' => $message,
        ];

        $reply = $this->proxy($url, 'POST', 'refill_bonus', $data);
        if (!empty($reply)) throw new \Exception($reply);
    }

    public function withdrawBonus(string $cardId, int $sum, string $message): void
    {
        $data = $this->clientInfoById($cardId);
        $walletId = $this->getWalletId($data->walletBalances);

        $url = $this->iicoCardHost . '/customers/withdraw_balance?access_token=' . $this->key;
        $data = [
            'customerId' => $cardId,
            'organizationId' => $this->iicoOrganizationId,
            'walletId' => $walletId,
            'sum' => $sum,
            'comment' => $message,
        ];

        $reply = $this->proxy($url, 'POST', 'withdraw_bonus', $data);
        if (!empty($reply)) throw new \Exception($reply);
    }
}