HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
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: /var/www/ipsremont-demo/app/Services/Shipment/SdekService.php
<?php

namespace App\Services\Shipment;

use App\Models\Service\Service;
use App\Models\Shipment;
use App\Models\ShipmentServerResponse;
use App\Services\Status\StatusService;
use Carbon\Carbon;

class SdekService
{

    protected $token;

    public function __construct()
    {
        $this->authorization();
    }

    protected function request($url, $type, $data = null, $shipment_id = 0)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($type == 'POST') {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
            curl_setopt($ch, CURLOPT_POST, true);
        } else {
            curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer ' . $this->token,]);
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);

        $error_code = curl_errno($ch);
        if ($error_code) {
            $err = new ShipmentServerResponse();
            $err->_save($shipment_id, $output, $error_code, curl_error($ch), $url, Service::TYPE_CDEK);
        }

        curl_close($ch);

        return $output;
    }

    public function authorization()
    {
        if (!env('SDEK_GET_ORDER_URL')) {
            $err = new ShipmentServerResponse();
            $err->_save(0, '', '', 'empty url SDEK_AUTH_URL ', '', Service::TYPE_CDEK);
            $this->token = '';
        } elseif (!env('GRANT_TYPE')) {
            $err = new ShipmentServerResponse();
            $err->_save(0, '', '', 'empty GRANT_TYPE ', '', Service::TYPE_CDEK);
            $this->token = '';
        } elseif (!env('CLIENT_ID')) {
            $err = new ShipmentServerResponse();
            $err->_save(0, '', '', 'empty CLIENT_ID ', '', Service::TYPE_CDEK);
            $this->token = '';
        } elseif (!env('CLIENT_SECRET')) {
            $err = new ShipmentServerResponse();
            $err->_save(0, '', '', 'empty CLIENT_SECRET ', '', Service::TYPE_CDEK);
            $this->token = '';
        } else {
            $postData = [
                'grant_type' => env('GRANT_TYPE'),
                'client_id' => env('CLIENT_ID'),
                'client_secret' => env('CLIENT_SECRET'),
            ];

            $output = $this->request(env('SDEK_AUTH_URL'), 'POST', $postData);

            if (isset(json_decode($output)->access_token)) {
                $this->token = json_decode($output)->access_token;
            } else {
                $this->token = '';
            }
        }
    }

    public function refresh_token()
    {
        $this->authorization();

        return $this->token;
    }

    /**
     * @param Shipment $shipment
     *
     * @return string|void
     */
    public function getStatuses($shipment)
    {
        if ($this->token) {
            if (!env('SDEK_GET_ORDER_URL')) {
                $err = new ShipmentServerResponse();
                $err->_save($shipment->id, '', '', 'empty url SDEK_GET_ORDER_URL ', '', Service::TYPE_CDEK);
            } else {
                $url = env('SDEK_GET_ORDER_URL') . $shipment->getTrackNumber();
                $order = json_decode($this->request($url, 'GET', '', $shipment->id));

                $shipment->last_status_date = Carbon::now();
                $shipment->update();

                if (isset($order->entity)) {
                    if ($shipment->error == 1) {
                        $shipment->error = 0;
                        $shipment->update();
                    }
                    if (empty($shipment->uuid)) {
                        $shipment->uuid = $order->entity->uuid;
                        $shipment->update();
                    }

                    $externalStatuses = $order->entity->statuses;
                    usort($externalStatuses, fn($a, $b) => $a->date_time <=> $b->date_time);

                    $i = 0;
                    foreach ($externalStatuses as $status) {
                        StatusService::shipmentSdekStatusChanged($shipment, $status, $i);
                        $i++;
                    }
                } else {
                    if ($shipment->error == 0) {
                        $shipment->error = 1;
                        $shipment->update();
                    }
                }

                if (!empty($order->requests)) {
                    if (empty($shipment->uuid) && !empty($order->requests[0]->request_uuid)) {
                        $shipment->uuid = $order->requests[0]->request_uuid;
                        $shipment->update();
                    }

                    $err = new ShipmentServerResponse();

                    if (isset($order->requests[0]->errors[0])) {
                        $err->_save($shipment->id, json_encode($order), $order->requests[0]->errors[0]->code, $order->requests[0]->errors[0]->message, $url, Service::TYPE_CDEK);

                        return $order->requests[0]->errors[0]->message;
                    }

                    $err->_save($shipment->id, json_encode($order), '', '', $url, Service::TYPE_CDEK);
                }
            }
        }
    }
}