From 52d489493d7ad91ccefbd5dfe25ff79d6c97beb1 Mon Sep 17 00:00:00 2001 From: Valentin Ursuleac Date: Thu, 31 Mar 2016 16:42:23 -0400 Subject: [PATCH] Creating MerchantOS class for api requests and methods --- README.md | 7 +- composer.json | 6 +- src/Provider/Lightspeed.php | 323 +---------------------------------- src/Provider/MerchantOS.php | 328 ++++++++++++++++++++++++++++++++++++ 4 files changed, 346 insertions(+), 318 deletions(-) create mode 100644 src/Provider/MerchantOS.php diff --git a/README.md b/README.md index 92ca1e1..e3a1e87 100644 --- a/README.md +++ b/README.md @@ -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 '
';
     print_r($client); echo '
'; diff --git a/composer.json b/composer.json index 56d52a0..f764338 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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", diff --git a/src/Provider/Lightspeed.php b/src/Provider/Lightspeed.php index ed0d342..19a043f 100644 --- a/src/Provider/Lightspeed.php +++ b/src/Provider/Lightspeed.php @@ -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 @@ -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); } /** @@ -454,7 +149,7 @@ protected function checkResponse(ResponseInterface $response, $data) * * @return string */ - private function getBaseLightspeedApiUrl() + protected function getBaseLightspeedApiUrl() { return static::LIGHTSPEED_API_URL; } diff --git a/src/Provider/MerchantOS.php b/src/Provider/MerchantOS.php new file mode 100644 index 0000000..06a19a0 --- /dev/null +++ b/src/Provider/MerchantOS.php @@ -0,0 +1,328 @@ + false, 'apiCall' => '']; + + /** + * Creates new resource owner. + * + * @param array $response + */ + public function __construct(AccessToken $token, $accountId) + { + $this->oauthToken = $token->getToken(); + $this->accountId = $accountId; + } + + /** + * @param AccessToken $token + * @param $saleId + * @return mixed + */ + public function getSale($saleId) + { + $params = ['oauth_token' => $this->oauthToken]; + $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($saleId, $saleData) + { + $params = ['oauth_token' => $this->oauthToken]; + $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($saleId) + { + $params = ['oauth_token' => $this->oauthToken, '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($saleId, $saleLineId, $data) + { + $params = ['oauth_token' => $this->oauthToken]; + + //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($data) + { + $params = ['oauth_token' => $this->oauthToken]; + + $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() + { + $params = ['oauth_token' => $this->oauthToken]; + $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($customerId) + { + $params = array( + 'oauth_token' => $this->oauthToken, + '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($employeeId) + { + $params = array( + 'oauth_token' => $this->oauthToken, + '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($discountId = null) + { + $params = ['oauth_token' => $this->oauthToken]; + $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($data) + { + $params = ['oauth_token' => $this->oauthToken]; + + $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 = []; + } + + $url = $this->prepareApiUrl($controlUrl, $this->accountId, $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) + { + $attributes = '@attributes'; + + if (isset($response[$attributes])) { + return $response[$attributes]['count']; + } + + return 0; + } +}