Skip to content

Commit

Permalink
Creating MerchantOS class for api requests and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ursuleacv committed Mar 31, 2016
1 parent d4cd862 commit 52d4894
Show file tree
Hide file tree
Showing 4 changed files with 346 additions and 318 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ $token = $provider->getAccessToken('authorization_code', [
try {

// We got an access token, let's now get the Account ID and sale details
$client = $ls->getAccountId($token);
$sale = $ls->getSale($token, 1);
$client = $provider->getResourceOwner($token);
$merchantos = $provider->merchantosApi($token, $client->getId());

$clientId = $client->getId();
$sale = $merchantos->getSale(1);

echo '<pre>';
print_r($client); echo '<br>';
Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "ursuleacv/oauth2-lightspeed",
"description": "LightSpeed OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
"license": "MIT",
"version": "1.0.4",
"version": "2.0.0",
"authors": [
{
"name": "Valentin Ursuleac",
Expand All @@ -21,7 +21,9 @@
],
"require": {
"php": ">=5.5.0",
"league/oauth2-client": "~1.0"
"ext-curl": "*",
"league/oauth2-client": "~1.0",
"guzzlehttp/guzzle": "~6.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
323 changes: 9 additions & 314 deletions src/Provider/Lightspeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@ class Lightspeed extends AbstractProvider
const LIGHTSPEED_REGISTRATION_ENDPOINT = 'https://cloud.merchantos.com/oauth/register.php';
const LIGHTSPEED_AUTHORIZATION_ENDPOINT = 'https://cloud.merchantos.com/oauth/authorize.php';
const LIGHTSPEED_TOKEN_ENDPOINT = 'https://cloud.merchantos.com/oauth/access_token.php';
const LS_FORMAT = '.json';

/**
* @var array
*/
private $context = ['error' => false, 'apiCall' => ''];

/**
* @var mixed
*/
private $oauthToken;
private $accountId;

/**
* @param array $options
Expand Down Expand Up @@ -99,329 +94,29 @@ public function getAccountId(AccessToken $token)

/**
* @param AccessToken $token
* @param $saleId
* @return mixed
*/
public function getSale(AccessToken $token, $saleId)
{
$this->oauthToken = $token;
$params = ['oauth_token' => $token->getToken()];
$response = $this->makeAPICall('Account.Sale', 'GET', $saleId, $params, null);

if (isset($response['Sale']) && $this->itemsCount($response) > 0) {
return $response['Sale'];
}

return [];
}

/**
* @param AccessToken $token
* @param int $saleId
* @param array $saleData
* @return mixed
*/
public function updateSale(AccessToken $token, $saleId, $saleData)
{
$this->oauthToken = $token;
$params = ['oauth_token' => $token->getToken()];
$response = $this->makeAPICall('Account.Sale', 'PUT', $saleId, $params, $saleData);

if (isset($response['Sale']) && $this->itemsCount($response) > 0) {
return $response['Sale'];
}

return [];
}

/**
* @param AccessToken $token
* @param $saleId
* @return mixed
*/
public function getSaleLine(AccessToken $token, $saleId)
{
$this->oauthToken = $token;
$params = ['oauth_token' => $token->getToken(), 'limit' => 1];

//return $this->prepareApiUrl('Account.Sale'.'/'.$saleId.'/SaleLine', '125620', null, $params);
$response = $this->makeAPICall('Account.Sale' . '/' . $saleId . '/SaleLine', 'GET', null, $params, null);

if (isset($response['SaleLine']) && $this->itemsCount($response) > 0) {
return $response['SaleLine'];
}

return [];
}

/**
* @param AccessToken $token
* @param $saleId
* @return mixed
*/
public function updateSaleLine(AccessToken $token, $saleId, $saleLineId, $data)
{
$this->oauthToken = $token;
$params = ['oauth_token' => $token->getToken()];

//return $this->prepareApiUrl('Account.Sale'.'/'.$saleId.'/SaleLine', '125620', null, $params);
$control = 'Account.Sale' . '/' . $saleId . '/SaleLine' . '/' . $saleLineId;
$response = $this->makeAPICall($control, 'PUT', null, $params, $data);

if (isset($response['SaleLine']) && $this->itemsCount($response) > 0) {
return $response['SaleLine'];
}

return [];
}

/**
* @param AccessToken $token
* @param $data
* @return mixed
*/
public function createSaleLine(AccessToken $token, $data)
{
$this->oauthToken = $token;
$params = ['oauth_token' => $token->getToken()];

$response = $this->makeAPICall('Account.Sale/49/SaleLine', 'POST', null, $params, $data);

if (isset($response['SaleLine']) && $this->itemsCount($response) > 0) {
return $response['SaleLine'];
}

return [];
}

/**
* @param AccessToken $token
* @return mixed
*/
public function getShops(AccessToken $token)
{
$this->oauthToken = $token;
$params = ['oauth_token' => $token->getToken()];
$response = $this->makeAPICall('Account.Shop', 'GET', null, $params, null);

//validate the response
if (isset($response['Shop']) && $this->itemsCount($response) == 1) {
return [$response['Shop']];
} elseif (isset($response['Shop']) && $this->itemsCount($response) > 1) {
return $response['Shop'];
}

return [];
}

/**
* @param AccessToken $token
* @param int $customerId
* @return mixed
*/
public function getCustomer(AccessToken $token, $customerId)
{
$this->oauthToken = $token;
$params = array(
'oauth_token' => $token->getToken(),
'archived' => 0,
'limit' => '1',
'load_relations' => 'all',
'customerID' => $customerId,
);

$response = $this->makeAPICall('Account.Customer', 'GET', null, $params, null);

//validate the response
if (isset($response['Customer']) && $this->itemsCount($response) == 1) {
return $response['Customer'];
} elseif (isset($response['Customer']) && $this->itemsCount($response) > 1) {
return $response['Customer'];
}

return [];
}

/**
* @param AccessToken $token
* @param int $employeeId
* @return mixed
*/
public function getEmployee(AccessToken $token, $employeeId)
{
$this->oauthToken = $token;
$params = array(
'oauth_token' => $token->getToken(),
'archived' => 0,
'limit' => '1',
'load_relations' => 'all',
'employeeID' => $employeeId,
);

$response = $this->makeAPICall('Account.Employee', 'GET', $employeeId, $params, null);

//validate the response
if (isset($response['Employee']) && $this->itemsCount($response) > 0) {
return $response['Employee'];
}

return [];
}

/**
* @param AccessToken $token
* @param $discountId
* @return mixed
*/
public function getDiscount(AccessToken $token, $discountId = null)
{
$this->oauthToken = $token;
$params = ['oauth_token' => $token->getToken()];
$response = $this->makeAPICall('Account.Discount', 'GET', $discountId, $params, null);

if (isset($response['Discount']) && $this->itemsCount($response) > 0) {
return $response['Discount'];
}

return [];
}

/**
* @param AccessToken $token
* @param $data
* @return mixed
*/
public function createDiscount(AccessToken $token, $data)
{
$this->oauthToken = $token;
$params = ['oauth_token' => $token->getToken()];

$response = $this->makeAPICall('Account.Discount', 'POST', null, $params, $data);

if (isset($response['Discount']) && $this->itemsCount($response) > 0) {
return $response['Discount'];
}

return [];
}

/**
* @param $controlUrl
* @param $action
* @param $uniqueId
* @param $params
* @param $data
* @return mixed
*/
public function makeAPICall($controlUrl, $action, $uniqueId, $params, $data)
{
$this->context['apiCall'] = $controlUrl;

if (is_null($data) || $data == '') {
$data = [];
}

$account = $this->getResourceOwner($this->oauthToken);

$url = $this->prepareApiUrl($controlUrl, $account->getId(), $uniqueId, $params);

$client = new \GuzzleHttp\Client();
$response = $client->request($action, $url, ['json' => $data]);

$body = (string) $response->getBody()->read(3024);
$r = json_decode($body, true);

$this->checkApiResponse($r);
return $r;
}

/**
* @param $controlName
* @param $accountId
* @param $uniqueId
* @param $queryStr
* @return string
*/
private function prepareApiUrl($controlName, $accountId, $uniqueId = null, $queryStr = null)
{
$controlUrl = $this->getBaseLightspeedApiUrl();
$controlUrl .= str_replace('.', '/', str_replace('Account.', 'Account.' . $accountId . '.', $controlName));

if ($uniqueId) {
$controlUrl .= '/' . $uniqueId;
}
if ($queryStr && is_array($queryStr)) {
$_queryStr = $this->buildQueryString($queryStr);

$controlUrl .= self::LS_FORMAT . '?' . $_queryStr;
} else {
$controlUrl .= self::LS_FORMAT;
}

return $controlUrl;
}

/**
* @param array $data
* @return string
*/
private function buildQueryString($data)
{
if (function_exists('http_build_query')) {
return http_build_query($data);
} else {
$qs = '';
foreach ($data as $key => $value) {
$append = urlencode($key) . '=' . urlencode($value);
$qs .= $qs ? '&' . $append : $append;
}
return $qs;
}
}

/**
* @param $response
*/
private function checkApiResponse($response)
{
// must be an error
if (isset($response['httpCode']) && $response['httpCode'] != '200') {
$message = $response['httpMessage'] . ': ' . $response['message'] . ' (' . $response['errorClass'] . ')';
throw new IdentityProviderException($message, $response['httpCode'], $response);
}
}

/**
* @param $response
* @return int
*/
private function itemsCount($response)
public function getResourceOwnerDetailsUrl(AccessToken $token)
{
$attributes = '@attributes';

if (isset($response[$attributes])) {
return $response[$attributes]['count'];
}

return 0;
return $this->getBaseLightspeedApiUrl() . 'Account/.json?oauth_token=' . $token;
}

/**
* @param array $response
* @param AccessToken $token
* @return mixed
*/
public function getResourceOwnerDetailsUrl(AccessToken $token)
protected function createResourceOwner(array $response, AccessToken $token)
{
return $this->getBaseLightspeedApiUrl() . 'Account/.json?oauth_token=' . $token;
return new LightspeedResourceOwner($response);
}

/**
* @param array $response
* @param AccessToken $token
*/
protected function createResourceOwner(array $response, AccessToken $token)
public function merchantosApi(AccessToken $token, $accountId)
{
return new LightspeedResourceOwner($response);
return new MerchantOS($token, $accountId);
}

/**
Expand Down Expand Up @@ -454,7 +149,7 @@ protected function checkResponse(ResponseInterface $response, $data)
*
* @return string
*/
private function getBaseLightspeedApiUrl()
protected function getBaseLightspeedApiUrl()
{
return static::LIGHTSPEED_API_URL;
}
Expand Down
Loading

0 comments on commit 52d4894

Please sign in to comment.