Skip to content

Commit

Permalink
Merge pull request #29 from wienkit/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
avido authored Dec 13, 2018
2 parents 7de31f1 + 942c0b3 commit 21eabf4
Show file tree
Hide file tree
Showing 7 changed files with 328 additions and 103 deletions.
183 changes: 112 additions & 71 deletions src/BolPlazaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Wienkit\BolPlazaClient\Entities\BolPlazaInboundProductlabelsRequest;
use Wienkit\BolPlazaClient\Exceptions\BolPlazaClientException;
use Wienkit\BolPlazaClient\Exceptions\BolPlazaClientRateLimitException;
use Wienkit\BolPlazaClient\Requests\CurlHttpRequest;

class BolPlazaClient
{
Expand All @@ -32,17 +33,20 @@ class BolPlazaClient
const API_VERSION = 'v2';
const OFFER_API_VERSION = 'v2';

const HTTP_TIMEOUT = 60;
const HTTP_USER_AGENT = "'Wienkit BolPlaza PHP Client (wienkit.com)'";

private $testMode = false;
private $skipSslVerification = false;
// parse response headers
private $parseHeaders = false;
private $parseHeaders = false;

private $publicKey;
private $privateKey;

// response headers placeholder
private $responseHeaders = [];

/**
* BolPlazaClient constructor.
* @param $publicKey
Expand Down Expand Up @@ -76,25 +80,57 @@ public function setSkipSslVerification($mode = true)
* Get list of orders
* @return array
*/
public function getOrders()
public function getOrders($page = 1, $fulfilmentMethod = 'FBR')
{
$parameters = [
'page' => $page,
'fulfilment-method' => $fulfilmentMethod
];

$url = '/services/rest/orders/' . self::API_VERSION;

$apiResult = $this->makeRequest('GET', $url);
$apiResult = $this->makeRequest('GET', $url, $parameters, ['Accept: application/vnd.orders-v2.1+xml']);
$orders = BolPlazaDataParser::createCollectionFromResponse('BolPlazaOrder', $apiResult);

return $orders;
}

/**
* Get single order
* @param string $orderId
* @return Mixed BolPlazaOrder|null
*/
public function getOrder($orderId)
{
$url = sprintf(
"/services/rest/orders/%s/%s",
self::API_VERSION,
$orderId
);
$apiResult = $this->makeRequest('GET', $url, null, ['Accept: application/vnd.orders-v2.1+xml']);
$order = BolPlazaDataParser::createCollectionFromResponse('BolPlazaOrder', $apiResult);
return isset($order[0]) ? $order[0] : null;
}

/**
* Get list of shipments
* @param int $page The page of the set of shipments
* @param string $fulfilmentMethod
* @param string|null $orderId
* @return array
*/
public function getShipments($page = 1)
public function getShipments($page = 1, $fulfilmentMethod = 'FBR', $orderId = null)
{
$parameters = [
'page' => $page,
'fulfilment-method' => $fulfilmentMethod,
];

if (!is_null($orderId)) {
$parameters['order-id'] = $orderId;
}

$url = '/services/rest/shipments/' . self::API_VERSION;
$apiResult = $this->makeRequest('GET', $url, array("page" => $page));
$apiResult = $this->makeRequest('GET', $url, $parameters, ['Accept: application/vnd.shipments-v2.1+xml']);
$shipments = BolPlazaDataParser::createCollectionFromResponse('BolPlazaShipment', $apiResult);
return $shipments;
}
Expand Down Expand Up @@ -311,15 +347,15 @@ public function getCommission($ean, $condition = false, $price = false)
*/
public function getOwnOffers($filter = '')
{
$url = '/offers/' . self::OFFER_API_VERSION . '/export';
$data = [];
if(!empty($filter)) {
$data['filter'] = $filter;
}
$apiResult = $this->makeRequest('GET', $url, $data);
/** @var BolPlazaOfferFile $result */
$result = BolPlazaDataParser::createEntityFromResponse('BolPlazaOfferFile', $apiResult);
return $result;
$url = '/offers/' . self::OFFER_API_VERSION . '/export';
$data = [];
if(!empty($filter)) {
$data['filter'] = $filter;
}
$apiResult = $this->makeRequest('GET', $url, $data);
/** @var BolPlazaOfferFile $result */
$result = BolPlazaDataParser::createEntityFromResponse('BolPlazaOfferFile', $apiResult);
return $result;
}

/**
Expand All @@ -338,7 +374,7 @@ public function getOwnOffersResult($path = '')
/**
* Get Latest Reductions filename
* @see https://developers.bol.com/reductions-list/
*
*
* @access public
* @return string
*/
Expand All @@ -348,12 +384,12 @@ public function getLatestReductionsFilename()
$apiResult = $this->makeRequest('GET', $url);
return $apiResult;
}

/**
* Get Reductions
*
*
* @see https://developers.bol.com/reductions-list/
*
*
* @access public
* @return BolPlazaReductionList
*/
Expand All @@ -373,19 +409,19 @@ public function getReductions()
}
/**
* Get inventory
*
* The inventory endpoint is a specific LVB/FBB endpoint. Meaning this only provides information
* about your Fulfillment by bol.com Inventory. This endpoint does not provide information on your
*
* The inventory endpoint is a specific LVB/FBB endpoint. Meaning this only provides information
* about your Fulfillment by bol.com Inventory. This endpoint does not provide information on your
* own stock.
*
*
* @see https://developers.bol.com/get-inventory/
* @access public
* @param int $page
* @param string/int $quantity
* @param string $stock - valid options: sufficient, insufficient
* @param string $state - valid options: saleable, unsaleable
* @param string $query
*
* @param string $query
*
* @return BolPlazaInventory
*/
public function getInventory($page = 1, $quantity=null, $stock=null, $state=null, $query=null)
Expand Down Expand Up @@ -427,9 +463,9 @@ public function createInbound(BolPlazaInboundRequest $inboundRequest)
$apiResult = $this->makeRequest('POST', $url, $xmlData);
$result = BolPlazaDataParser::createEntityFromResponse('BolPlazaProcessStatus', $apiResult);
return $result;

}

/**
* Get delivery windows for specific date (LvB)
*
Expand All @@ -447,12 +483,12 @@ public function getDeliveryWindows(\DateTime $deliveryDate, $qty)
$timeSlots = BolPlazaDataParser::createCollectionFromResponse('BolPlazaDeliveryWindowTimeSlot', $apiResult);
return $timeSlots;
}

/**
* Get inbound details
* Get inbound details
*
* @see https://developers.bol.com/single-inbound/
*
*
* @access public
* @param int $id
* @return BolPlazaInbound
Expand All @@ -462,7 +498,7 @@ public function getSingleInbound($id)
$apiResult = $this->makeRequest('GET', '/services/rest/inbounds/' . (int)$id);
return BolPlazaDataParser::createEntityFromResponse('BolPlazaInbound', $apiResult);
}

/**
* Get (Inbound) Product labels
*
Expand All @@ -478,7 +514,7 @@ public function getProductLabels(BolPlazaInboundProductlabelsRequest $request, $
$xmlData = BolPlazaDataParser::createXmlFromEntity($request, '1');
return $this->makeRequest('POST', "/services/rest/inbounds/productlabels?format={$format}", $xmlData);
}

/**
* Get (Inbound) Packinglist
*
Expand All @@ -492,10 +528,10 @@ public function getPackinglist($id)
{
return $this->makeRequest('GET', "/services/rest/inbounds/" . (int)$id . "/packinglistdetails");
}

/**
* Get Inbound list
*
*
* @see https://developers.bol.com/inbound-list/
*
* @access public
Expand All @@ -519,9 +555,9 @@ public function getInboundList($page = 1)
// copy actual inbounds
$result->Inbound = $inbounds;
return $result;

}

/**
* Makes the request to the server and processes errors
*
Expand All @@ -533,62 +569,58 @@ public function getInboundList($page = 1)
* @throws BolPlazaClientException
* @throws BolPlazaClientRateLimitException
*/
protected function makeRequest($method = 'GET', $endpoint, $data = null, $headers=[])
protected function makeRequest($method = 'GET', $endpoint, $data = null, $headers = [])
{
$date = gmdate('D, d M Y H:i:s T');
$contentType = 'application/xml';
$url = $this->getUrlFromEndpoint($endpoint);

// set new endpoint (endpoint without arguments, signature is based on endpoint without arguments)
$parsedUrl = parse_url($url);
$endpoint = isset($parsedUrl['path']) ? $parsedUrl['path'] : $endpoint;

$headers = (isset($headers) && is_array($headers)) ? $headers : [];


$signature = $this->getSignature($method, $contentType, $date, $endpoint);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Wienkit BolPlaza PHP Client (wienkit.com)');
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge([

$headers = array_merge($headers, [
'Content-type: ' . $contentType,
'X-BOL-Date: ' . $date,
'X-BOL-Authorization: ' . $signature
], $headers));
]);

$httpRequest = $this->createHttpRequest($url);
$httpRequest->setOption(CURLOPT_CUSTOMREQUEST, $method);
$httpRequest->setOption(CURLOPT_RETURNTRANSFER, true);
$httpRequest->setOption(CURLOPT_TIMEOUT, self::HTTP_TIMEOUT);
$httpRequest->setOption(CURLOPT_HEADER, false);
$httpRequest->setOption(CURLOPT_USERAGENT, self::HTTP_USER_AGENT);
$httpRequest->setOption(CURLOPT_HTTPHEADER, $headers);

if (in_array($method, ['POST', 'PUT', 'DELETE']) && ! is_null($data)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$httpRequest->setOption(CURLOPT_POSTFIELDS, $data);
} elseif ($method == 'GET' && !empty($data)) {
$separator = "?";
$suffix = "";
foreach ($data as $key => $value) {
$suffix .= $separator . $key . '=' . $value;
$separator = "&";
}
curl_setopt($ch, CURLOPT_URL, $url . $suffix);
$httpRequest->setOption(CURLOPT_URL, $url . '?' . http_build_query($data));
}

if ($this->skipSslVerification) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$httpRequest->setOption(CURLOPT_SSL_VERIFYPEER, false);
$httpRequest->setOption(CURLOPT_SSL_VERIFYHOST, false);
}
if ($this->parseHeaders) {
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($curl, $line) {
$httpRequest->setOption(CURLOPT_HEADERFUNCTION, function($curl, $line) {
if (stristr($line, ":")) {
list($key,$value) = explode(":", $line);
$this->responseHeaders[$key] = $value;
}
return strlen($line);
});
}
$result = curl_exec($ch);
$headerInfo = curl_getinfo($ch);
$this->checkForErrors($ch, $headerInfo, $result);
$result = $httpRequest->execute();
$headerInfo = $httpRequest->getInfo();

// check for errors in results
$this->checkForErrors($httpRequest, $headerInfo, $result);

curl_close($ch);
$httpRequest->close();

return $result;
}
Expand Down Expand Up @@ -635,16 +667,16 @@ protected function getSignature($method, $contentType, $date, $endpoint)
*
* @see https://plazaapi.bol.com/services/xsd/serviceerror-1.5.xsd
* @see https://developers.bol.com/documentatie/plaza-api/developer-guide-plaza-api/error-codes-messages/
* @param resource $ch The CURL resource of the request
* @param resource $httpRequest The CURL resource of the request
* @param array $headerInfo
* @param string $result
* @throws BolPlazaClientException
* @throws BolPlazaClientRateLimitException
*/
protected function checkForErrors($ch, $headerInfo, $result)
protected function checkForErrors($httpRequest, $headerInfo, $result)
{
if (curl_errno($ch)) {
throw new BolPlazaClientException(curl_errno($ch));
if ($httpRequest->getErrorNumber()) {
throw new BolPlazaClientException($httpRequest->getErrorNumber());
}

if (intval($headerInfo['http_code']) < 200 || intval($headerInfo['http_code']) > 226) {
Expand Down Expand Up @@ -678,4 +710,13 @@ protected function checkForErrors($ch, $headerInfo, $result)
}
}
}

/**
* @param string $url
* @return CurlHttpRequest
*/
protected function createHttpRequest($url)
{
return new CurlHttpRequest($url);
}
}
2 changes: 2 additions & 0 deletions src/Entities/BolPlazaOrderItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class BolPlazaOrderItem extends BaseModel {
'Quantity',
'OfferPrice',
'TransactionFee',
'LatestDeliveryDate',
'FulfilmentMethod',
'PromisedDeliveryDate',
'OfferCondition',
'CancelRequest'
Expand Down
Loading

0 comments on commit 21eabf4

Please sign in to comment.