396 lines
9.3 KiB
PHP
396 lines
9.3 KiB
PHP
<?php
|
|
|
|
|
|
namespace Empik\Marketplace\API;
|
|
|
|
use Empik\Marketplace\Factory\HttpClientsFactory;
|
|
use Empik\Marketplace\Factory\HttpClientFactoryInterface;
|
|
use Empik\Marketplace\Exception\InvalidArgumentException;
|
|
|
|
class EmpikClient
|
|
{
|
|
const IMPORT_MODE_NORMAL = 'NORMAL';
|
|
const IMPORT_MODE_PARTIAL_UPDATE = 'PARTIAL_UPDATE';
|
|
const IMPORT_MODE_REPLACE = 'REPLACE';
|
|
|
|
/** @var string */
|
|
private $apiUrl;
|
|
|
|
/** @var string */
|
|
private $apiKey;
|
|
|
|
/** @var HttpClientInterface */
|
|
private $client;
|
|
|
|
/**
|
|
* @param $apiUrl
|
|
* @param $apiKey
|
|
* @param HttpClientFactoryInterface|null $httpClientFactory
|
|
* @throws \Exception
|
|
*/
|
|
public function __construct($apiUrl, $apiKey, HttpClientFactoryInterface $httpClientFactory = null)
|
|
{
|
|
if (!$httpClientFactory) {
|
|
$httpClientFactory = new HttpClientsFactory();
|
|
}
|
|
|
|
$this->apiUrl = $apiUrl;
|
|
$this->apiKey = $apiKey;
|
|
$this->client = $httpClientFactory->createHttpClient($apiUrl);
|
|
}
|
|
|
|
/**
|
|
* @param int|string $id
|
|
* @param array $headers
|
|
* @param array $body
|
|
*
|
|
* @return false|array
|
|
*/
|
|
private function post($id, array $headers = [], array $body = [])
|
|
{
|
|
return $this->sendRequest($id, $headers, $body, 'POST');
|
|
}
|
|
|
|
/**
|
|
* @param int|string $id
|
|
* @param array $headers
|
|
* @param array $body
|
|
*
|
|
* @return false|array
|
|
*/
|
|
private function put($id, array $headers = [], array $body = [])
|
|
{
|
|
return $this->sendRequest($id, $headers, $body, 'PUT');
|
|
}
|
|
|
|
/**
|
|
* @param int|string $id
|
|
* @param array $headers
|
|
* @param array $body
|
|
*
|
|
* @return false|array
|
|
*/
|
|
private function get($id, array $headers = [], array $body = [])
|
|
{
|
|
return $this->sendRequest($id, $headers, $body, 'GET');
|
|
}
|
|
|
|
/**
|
|
* @param int|string $id
|
|
* @param array $headers
|
|
* @param array $body
|
|
* @param string $method
|
|
*
|
|
* @return false|array|string
|
|
*/
|
|
private function sendRequest($id, array $headers, array $body, $method)
|
|
{
|
|
$headers['Authorization'] = $this->apiKey;
|
|
|
|
switch ($method)
|
|
{
|
|
case 'POST':
|
|
$response = $this->client->post(
|
|
$this->apiUrl . $id,
|
|
$headers,
|
|
$body
|
|
);
|
|
break;
|
|
case 'PUT':
|
|
$response = $this->client->put(
|
|
$this->apiUrl . $id,
|
|
$headers,
|
|
$body
|
|
);
|
|
break;
|
|
case 'GET':
|
|
$response = $this->client->get(
|
|
$this->apiUrl . $id,
|
|
$headers,
|
|
$body
|
|
);
|
|
break;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @return array|bool|false
|
|
*/
|
|
public function getAccount()
|
|
{
|
|
return $this->get(
|
|
'/api/account'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* P41 - Import products to the operator information
|
|
*
|
|
* @param string $csvPath
|
|
*
|
|
* @return array|false
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
public function postProducts($csvPath)
|
|
{
|
|
if (!file_exists($csvPath)) {
|
|
throw new InvalidArgumentException(sprintf('CSV product file does not exists: %s', $csvPath));
|
|
}
|
|
|
|
return $this->post(
|
|
'/api/products/imports',
|
|
[],
|
|
[
|
|
'file' => [
|
|
'filename' => basename($csvPath),
|
|
'filepath' => $csvPath,
|
|
]
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* OF01 - Import a file to add offers
|
|
*
|
|
* @param string $csvPath
|
|
* @param string $importMode
|
|
*
|
|
* @return array|false
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
public function postOffersImports($csvPath, $importMode = self::IMPORT_MODE_NORMAL)
|
|
{
|
|
if (!file_exists($csvPath)) {
|
|
throw new InvalidArgumentException(sprintf('CSV offer file does not exists: %s', $csvPath));
|
|
}
|
|
|
|
if (!in_array($importMode, [self::IMPORT_MODE_NORMAL, self::IMPORT_MODE_PARTIAL_UPDATE, self::IMPORT_MODE_REPLACE])) {
|
|
throw new InvalidArgumentException(sprintf('Unsupported import mode: %s', $importMode));
|
|
}
|
|
|
|
return $this->post(
|
|
'/api/offers/imports',
|
|
[],
|
|
[
|
|
'import_mode' => $importMode,
|
|
'file' => [
|
|
'filename' => basename($csvPath),
|
|
'filepath' => $csvPath,
|
|
]
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* OF24 - Create, update, or delete offers
|
|
*
|
|
* @param array $offers
|
|
*
|
|
* @return array|false
|
|
*/
|
|
public function postOffers(array $offers)
|
|
{
|
|
return $this->post(
|
|
'/api/offers',
|
|
[
|
|
'Content-Type' => 'application/json'
|
|
],
|
|
[
|
|
'offers' => $offers,
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET P42 - Get the import status for a product import
|
|
*
|
|
* @param int $importId
|
|
* @return array|bool|false
|
|
*/
|
|
public function getProductsImport($importId)
|
|
{
|
|
return $this->get(
|
|
'/api/products/imports/' . (int)$importId
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET OF02 - Get information and statistics about an offer import
|
|
*
|
|
* @param int $importId
|
|
* @return array|bool|false
|
|
*/
|
|
public function getOffersImport($importId)
|
|
{
|
|
return $this->get(
|
|
'/api/offers/imports/' . (int)$importId
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET SH21 - List all carriers
|
|
*
|
|
* @return array|bool|false
|
|
*/
|
|
public function getCarriers()
|
|
{
|
|
return $this->get(
|
|
'/api/shipping/carriers'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET SH12 - List all active shipping methods
|
|
*
|
|
* @return array|bool|false
|
|
*/
|
|
public function getShippingTypes()
|
|
{
|
|
return $this->get(
|
|
'/api/shipping/types'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET OR11 - List orders
|
|
*
|
|
* @param array $filterParams
|
|
* @return array|bool|false
|
|
*/
|
|
public function getOrders($filterParams = [])
|
|
{
|
|
$filterParams = array_merge([
|
|
'sort' => 'dateCreated',
|
|
'order' => 'desc',
|
|
'max' => '20',
|
|
], $filterParams);
|
|
|
|
$query = http_build_query($filterParams);
|
|
|
|
return $this->get(
|
|
'/api/orders' . '?' . $query
|
|
);
|
|
}
|
|
|
|
/**
|
|
* PUT OR21 - Accept or refuse order lines
|
|
*
|
|
* @param int|string $orderReference
|
|
* @param array $orderLines
|
|
* @return array|bool|false
|
|
*/
|
|
public function putOrderAccept($orderReference, array $orderLines)
|
|
{
|
|
$bodyParams['order_lines'] = $orderLines;
|
|
|
|
return $this->put(
|
|
'/api/orders/' . $orderReference . '/accept',
|
|
[
|
|
'Content-Type' => 'application/json'
|
|
],
|
|
$bodyParams
|
|
);
|
|
}
|
|
|
|
/**
|
|
* PUT OR24 - Validate the shipment of an order
|
|
*
|
|
* @param int|string $orderReference
|
|
* @return array|bool|false
|
|
*/
|
|
public function putOrderShip($orderReference)
|
|
{
|
|
return $this->put(
|
|
'/api/orders/' . $orderReference . '/ship',
|
|
[
|
|
'Content-Type' => 'application/json'
|
|
]
|
|
);
|
|
}
|
|
|
|
/**
|
|
* PUT OR23 - Update carrier tracking information for a specific order
|
|
*
|
|
* @param int|string $orderReference
|
|
* @param string $carrierCode
|
|
* @param string $carrierName
|
|
* @param string $carrierUrl
|
|
* @param string $trackingNumber
|
|
* @return array|false
|
|
*/
|
|
public function putOrderTracking(
|
|
$orderReference,
|
|
$carrierCode = null,
|
|
$carrierName = null,
|
|
$carrierUrl = null,
|
|
$trackingNumber = null
|
|
)
|
|
{
|
|
$bodyParams = [];
|
|
|
|
if ($carrierCode) {
|
|
$bodyParams['carrier_code'] = $carrierCode;
|
|
}
|
|
|
|
if ($carrierName) {
|
|
$bodyParams['carrier_name'] = $carrierName;
|
|
}
|
|
|
|
if ($carrierUrl) {
|
|
$bodyParams['carrier_url'] = $carrierUrl;
|
|
}
|
|
|
|
if ($trackingNumber) {
|
|
$bodyParams['tracking_number'] = $trackingNumber;
|
|
}
|
|
|
|
return $this->put(
|
|
'/api/orders/' . $orderReference . '/tracking',
|
|
[
|
|
'Content-Type' => 'application/json'
|
|
],
|
|
$bodyParams
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET P44 - Get the error report file for a product import
|
|
*
|
|
* @param string $importId
|
|
* @return array|bool|false
|
|
*/
|
|
public function getProductImportErrorReport($importId)
|
|
{
|
|
return $this->get(
|
|
'/api/products/imports/' . $importId . '/new_product_report'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET P44 - Get the error report file for a product import
|
|
*
|
|
* @param string $importId
|
|
* @return array|bool|false
|
|
*/
|
|
public function getOfferImportErrorReport($importId)
|
|
{
|
|
return $this->get(
|
|
'/api/offers/imports/' . $importId . '/error_report'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* GET SH31 - List all logistic classes
|
|
*
|
|
* @return array|bool|false
|
|
*/
|
|
public function getLogisticClasses()
|
|
{
|
|
return $this->get(
|
|
'/api/shipping/logistic_classes'
|
|
);
|
|
}
|
|
}
|