src/Services/Insales/Client.php line 120

Open in your IDE?
  1. <?php
  2.     namespace App\Services\Insales;
  3.     use \InSales\API;
  4.     use App\Entity;
  5.     use Doctrine\ORM\EntityManagerInterface;
  6.     use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  7.     use Symfony\Component\HttpFoundation\Response;
  8.     /**
  9.      * Class Client
  10.      * @package App\Services\Insales
  11.      */
  12.     class Client
  13.     {
  14.         const INSALES_FINANCIAL_STATUS_PAID 'paid';
  15.         const INSALES_FINANCIAL_STATUS_PENDING 'pending';
  16.         /** @var EntityManagerInterface $entityManager' */
  17.         protected $entityManager;
  18.         /** @var string $appId */
  19.         protected $appId;
  20.         /** @var Entity\Shop|null $shop */
  21.         protected $shop null;
  22.         /** @var API\ApiClient|null $client */
  23.         protected $client null;
  24.         /** @var ParameterBagInterface  */
  25.         protected $parameterBag;
  26.         /**
  27.          * Orders constructor.
  28.          * @param EntityManagerInterface $entityManager
  29.          * @param ParameterBagInterface $parameterBag
  30.          */
  31.         public function __construct(EntityManagerInterface $entityManagerParameterBagInterface $parameterBag)
  32.         {
  33.             $this->parameterBag $parameterBag;
  34.             $this->appId $this->parameterBag->get('app.insales.id');
  35.             $this->entityManager $entityManager;
  36.         }
  37.         /**
  38.          * @param string $password
  39.          * @param string $shopDomain
  40.          * @return self
  41.          */
  42.         public function createClientByShopData(string $passwordstring $shopDomain): self
  43.         {
  44.             $this->client = new API\ApiClient(
  45.                 $this->appId,
  46.                 $password,
  47.                 $shopDomain
  48.             );
  49.             return $this;
  50.         }
  51.         /**
  52.          * @return Entity\Shop|null
  53.          */
  54.         public function getShop(): ?Entity\Shop
  55.         {
  56.             return $this->shop;
  57.         }
  58.         /**
  59.          * @param Entity\Shop|null $shop
  60.          * @return self
  61.          */
  62.         public function setShop(?Entity\Shop $shop): self
  63.         {
  64.             $this->shop $shop;
  65.             /** @var API\ApiClient $client */
  66.             $this->client = new API\ApiClient(
  67.                 $this->appId,
  68.                 $this->shop->getPassword(),
  69.                 $this->shop->getShop()
  70.             );
  71.             return $this;
  72.         }
  73.         /**
  74.          * @param array $fields
  75.          * @param Entity\Shop $shop
  76.          * @return bool
  77.          */
  78.         public function downloadOrder(array $fieldsbool $setManageToken false) : bool
  79.         {
  80.             /** @var Api\ApiResponse $response */
  81.             $response $this->client->getOrderById($fields['id']);
  82.             if($response->getHttpCode() !== Response::HTTP_OK)
  83.                 return false;
  84.             /** @var Entity\Order $order */
  85.             $order $this->entityManager->getRepository(Entity\Order::class)
  86.                 ->getOrderForUpdate($this->shop$fields['id']);
  87.             $order->setKey($response->getData()['key'])
  88.                 ->setNumber($response->getData()['number'])
  89.                 ->setPayed($response->getData()['financial_status'] === 'paid')
  90.                 ->setDeliveryVariantId($response->getData()['delivery_variant_id'])
  91.                 ->setDeliveryInformation($response->getData()['delivery_info'])
  92.                 ->setFieldValues($response->getData()['fields_values'])
  93.                 ->setProducts($response->getData()['order_lines'])
  94.                 ->setShippingAddress($response->getData()['shipping_address'])
  95.                 ->setClient($response->getData()['client'])
  96.                 ->setPrice($order::calcOrderSum($order->getProducts()))
  97.                 ->setComment($response->getData()['comment'])
  98.                 ->setManagerComment($response->getData()['manager_comment'])
  99.                 ->setPaymentGatewayId(intval($response->getData()['payment_gateway_id']))
  100.             ;
  101.             if ($setManageToken)
  102.                 $order->setManageToken();
  103.             $this->entityManager->persist($order);
  104.             $this->entityManager->flush();
  105.             return true;
  106.         }
  107.         /**
  108.          * Скачивание и получение объекта заказа
  109.          * @param $orderId
  110.          * @return Entity\Order|object|null
  111.          */
  112.         public function getByOrderId($orderIdbool $setManageToken false)
  113.         {
  114.             $this->downloadOrder(['id' => $orderId],$setManageToken);
  115.             return $this->entityManager->getRepository(Entity\Order::class)
  116.                 ->findOneBy(
  117.                     [
  118.                         'shop' => $this->shop,
  119.                         'orderId' => $orderId
  120.                     ]
  121.                 );
  122.         }
  123.         /**
  124.          * Получение статусов insales
  125.          * @return array[]
  126.          */
  127.         public function getStatuses()
  128.         {
  129.             return $this->client->getCustomStatuses()->getData();
  130.         }
  131.         /**
  132.          *  Получение заказа
  133.          * @param string|integer $id Идентификатор заказа
  134.          * @return \InSales\API\ApiResponse
  135.          */
  136.         public function getOrderById($id)
  137.         {
  138.             return $this->client->getOrderById($id);
  139.         }
  140.         /**
  141.          * Обновление статуса заказа
  142.          * @param $orderId
  143.          * @param $statusCode
  144.          * @param false $customStatus
  145.          */
  146.         public function updateOrderStatus($orderId$statusCode$customStatus false)
  147.         {
  148.             if($customStatus)
  149.             {
  150.                 $this->client->updateOrder(
  151.                     $orderId,
  152.                     [
  153.                         'custom_status_permalink' => $statusCode,
  154.                     ]
  155.                 );
  156.             }
  157.             else
  158.             {
  159.                 $this->client->updateOrder(
  160.                     $orderId,
  161.                     [
  162.                         'fulfillment_status' => $statusCode
  163.                     ]
  164.                 );
  165.             }
  166.         }
  167.         /**
  168.          * Обновление свойств заказа
  169.          * @param int $orderId id заказа
  170.          * @param string $handle символьный код свойства
  171.          * @param mixed $value значение свойства
  172.          *
  173.          * @return API\ApiResponse
  174.          */
  175.         public function updateOrderField(int $orderIdstring $handle$value)
  176.         {
  177.             $response $this->client->updateOrder(
  178.                 $orderId,
  179.                 [
  180.                     'fields_values_attributes' => [
  181.                         [
  182.                             "handle" => $handle,
  183.                             "value" => $value
  184.                         ]
  185.                     ]
  186.                 ]
  187.             );
  188.             return $response;
  189.         }
  190.         /**
  191.          * Обновление свойств заказа
  192.          * @param int $orderId id заказа
  193.          * @param string $financial_status pending or paid
  194.          * @return API\ApiResponse
  195.          */
  196.         public function updateFinalcialStatus(int $orderIdstring $financial_status)
  197.         {
  198.             $response $this->client->updateOrder(
  199.                 $orderId,
  200.                 [
  201.                     'financial_status' => $financial_status
  202.                 ]
  203.             );
  204.             return $response;
  205.         }
  206.         /**
  207.          * Получения списка оплат
  208.          * @return API\ApiResponse
  209.          */
  210.         public function getPaymentGateways() {
  211.             return $this->client->getPaymentGateways();
  212.         }
  213.         public function getFields() {
  214.             return $this->client->getFields();
  215.         }
  216.         public function updateTrackNumber(int $orderId$trackNumber){
  217.             return $this->updateOrderField(
  218.                 $orderId,
  219.                 $this->parameterBag->get('app.insales.property.track.number.handle'),
  220.                 $trackNumber
  221.             );
  222.         }
  223.         public function updateOrder(int $orderId,array $data)
  224.         {
  225.             return $this->client->updateOrder($orderId,$data);
  226.         }
  227.         public function updateInsalesDelivery(int $deliveryIdstring $script1Urlstring $script2Urlstring $insalesServiceName '')
  228.         {
  229.             $data = [
  230.                 'javascript' => sprintf(
  231.                     '<script type="text/javascript" src="%s"></script><script type="module" src="%s"></script>',
  232.                     $script1Url,
  233.                     $script2Url
  234.                 )
  235.             ];
  236.             if (!empty($insalesServiceName)) {
  237.                 $data['title'] = $insalesServiceName;
  238.             }
  239.             return $this->client->updateDeliveryVariant($deliveryId$data);
  240.         }
  241.     }