Created
January 28, 2015 12:51
-
-
Save m8rge/5ed074d7e973092151ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class OCSClient extends CComponent | |
{ | |
public $login; | |
public $token; | |
public $url = 'https://b2btestservice.ocs.ru/b2b.asmx?WSDL'; | |
/** @var SoapClient */ | |
protected $client; | |
public function init() | |
{ | |
$this->client = new SoapClient($this->url); | |
} | |
protected function soapCall($function, $params) | |
{ | |
try { | |
$errs = error_reporting(); | |
error_reporting($errs ^ E_WARNING); | |
$answer = $this->client->{$function}($params); | |
error_reporting($errs); | |
$answer = $this->processResponse($answer); | |
return $answer; | |
} catch (SoapFault $e) { | |
/** @var RSentryComponent $sentry */ | |
$sentry = Yii::app()->getComponent('RSentryException'); | |
if ($sentry) { | |
$sentry->getClient()->captureException($e, ['level' => Raven_Client::WARNING]); | |
} | |
} | |
return null; | |
} | |
/** | |
* @param stdClass $answer | |
* @return mixed result | |
* @throws OCSException | |
*/ | |
protected function processResponse($answer) | |
{ | |
$vars = get_object_vars($answer); | |
if (count($vars) != 1) { | |
throw new OCSException("answer contains more than 1 root elements: " . print_r($answer, true)); | |
} | |
$result = reset($vars); | |
if (!empty($result->OperationStatus)) { | |
if (!empty($result->ErrorText)) { | |
throw new OCSException($result->ErrorText); | |
} else { | |
throw new OCSException("error code {$result->OperationStatus}"); | |
} | |
} | |
return $result; | |
} | |
/** | |
* @param bool $onlyOnlineOrder | |
* @param string $shipmentCity | |
* @throws OCSException | |
* @return mixed | |
*/ | |
public function getLocations($onlyOnlineOrder = true, $shipmentCity = 'Екатеринбург') | |
{ | |
if (!empty($shipmentCity) && !$onlyOnlineOrder) { | |
throw new OCSException('wrong parameters'); | |
} | |
$answer = $this->soapCall(__FUNCTION__, array( | |
'login' => $this->login, | |
'token' => $this->token, | |
'availability' => $onlyOnlineOrder, | |
) | |
); | |
if (empty($answer->LocationList->LocationInfo)) { | |
throw new OCSException('missing required field'); | |
} | |
return $answer->LocationList->LocationInfo; | |
} | |
/** | |
* @return stdClass[] | |
* @throws OCSException | |
*/ | |
public function getCatalog() | |
{ | |
$answer = $this->soapCall(__FUNCTION__, array( | |
'login' => $this->login, | |
'token' => $this->token, | |
) | |
); | |
if (empty($answer->Categories->Category)) { | |
throw new OCSException('missing required field'); | |
} | |
return $answer->Categories->Category; | |
} | |
/** | |
* @param string[]|int[] $categoryIDList | |
* @param bool $onlyOnlineOrder | |
* @param string $shipmentCity | |
* @param bool $displayMissing | |
* @return mixed | |
* @throws OCSException | |
*/ | |
public function getProductAvailability($categoryIDList, $onlyOnlineOrder = true, $shipmentCity = 'Екатеринбург', $displayMissing = true) | |
{ | |
$answer = $this->soapCall( | |
__FUNCTION__, | |
array( | |
'login' => $this->login, | |
'token' => $this->token, | |
'availability' => $onlyOnlineOrder, | |
'displayMissing' => $displayMissing, | |
'shipmentCity' => $shipmentCity, | |
'categoryIDList' => array_values($categoryIDList), | |
) | |
); | |
if (!isset($answer->Products)) { | |
throw new OCSException('missing required field'); | |
} | |
return !empty($answer->Products->Product) ? $answer->Products->Product : array(); | |
} | |
/** | |
* @param mixed $orderId | |
* @param array $orderItems [ocsItemId => count] | |
* @param $contactPersonCode | |
* @param string $shipmentCity | |
* @throws OCSException | |
* @return array [string, bool] ocsOrderId, success | |
*/ | |
public function createOrderOnline($orderId, $orderItems, $contactPersonCode, $shipmentCity = 'Екатеринбург') | |
{ | |
$items = []; | |
foreach ($orderItems as $id => $count) { | |
$items[] = [ | |
'ItemID' => (string)$id, | |
'OperationType' => 0, // add | |
'Quantity' => $count, | |
'PriceCorrectionNeeded' => false, | |
]; | |
} | |
$answer = $this->soapCall( | |
__FUNCTION__, | |
array( | |
'login' => $this->login, | |
'token' => $this->token, | |
'order' => [ | |
'OrderID' => (string)$orderId, | |
'OperationID' => md5(microtime()), | |
'ReservePlace' => 4, | |
'ContactPersonCode' => $contactPersonCode, | |
'ShipmentCity' => $shipmentCity, | |
'OrderLines' => $items, | |
], | |
) | |
); | |
if (!isset($answer->OrderLines)) { | |
throw new OCSException('missing required field'); | |
} | |
$OCSOrderId = $answer->OCSOrderID; | |
$reserved = true; | |
if (is_array($answer->OrderLines->OrderLineResult)) { | |
foreach ($answer->OrderLines->OrderLineResult as $orderLine) { | |
if ($orderLine->LineOperationStatus != 0) { | |
$reserved = false; | |
break; | |
} | |
} | |
} elseif ($answer->OrderLines->OrderLineResult->LineOperationStatus != 0) { | |
$reserved = false; | |
} | |
return [$OCSOrderId, $reserved]; | |
} | |
/** | |
* @return mixed | |
* @throws OCSException | |
*/ | |
public function getContactPersons() | |
{ | |
$answer = $this->soapCall( | |
__FUNCTION__, | |
array( | |
'login' => $this->login, | |
'token' => $this->token, | |
) | |
); | |
if (!isset($answer->ContactPersonList)) { | |
throw new OCSException('missing required field'); | |
} | |
return $answer->ContactPersonList->ContactPersonInfo; | |
} | |
/** | |
* @return mixed | |
* @throws OCSException | |
*/ | |
public function getReservePlaces() | |
{ | |
$answer = $this->soapCall( | |
__FUNCTION__, | |
array( | |
'login' => $this->login, | |
'token' => $this->token, | |
) | |
); | |
if (!isset($answer->ReservePlaceList)) { | |
throw new OCSException('missing required field'); | |
} | |
return $answer->ReservePlaceList->ReservePlaceInfo; | |
} | |
public function cancelOrderOnline($orderId) | |
{ | |
throw new Exception('not implemented'); | |
// $answer = $this->soapCall( | |
// __FUNCTION__, | |
// array( | |
// 'login' => $this->login, | |
// 'token' => $this->token, | |
// 'orderID' => (string)$orderId, | |
// 'operationID' => md5(microtime()), | |
// ) | |
// ); | |
// if (!isset($answer->ReservePlaceList)) { | |
// throw new OCSException('missing required field'); | |
// } | |
// | |
// return $answer->ReservePlaceList->ReservePlaceInfo; | |
} | |
public function getOrderStatus($orderId) | |
{ | |
throw new Exception('not implemented'); | |
// $answer = $this->soapCall( | |
// __FUNCTION__, | |
// array( | |
// 'login' => $this->login, | |
// 'token' => $this->token, | |
// 'orderID' => (string)$orderId, | |
// ) | |
// ); | |
// if (!isset($answer->ReservePlaceList)) { | |
// throw new OCSException('missing required field'); | |
// } | |
// | |
// return $answer->ReservePlaceList->ReservePlaceInfo; | |
} | |
} | |
class OCSException extends CException{}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment