Skip to content

Commit

Permalink
Search sales, create, update items
Browse files Browse the repository at this point in the history
  • Loading branch information
ursuleacv committed Apr 8, 2016
1 parent 7bd1644 commit 39f270b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 25 deletions.
2 changes: 1 addition & 1 deletion 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": "2.0.1",
"version": "2.0.2",
"authors": [
{
"name": "Valentin Ursuleac",
Expand Down
3 changes: 1 addition & 2 deletions src/Provider/Lightspeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ class Lightspeed extends AbstractProvider
/**
* @var mixed
*/
private $oauthToken;
private $accountId;
protected $accountId;

/**
* @param array $options
Expand Down
119 changes: 97 additions & 22 deletions src/Provider/MerchantOS.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ class MerchantOS extends Lightspeed
* @var mixed
*/
private $oauthToken;

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

protected $userAgent = 'MerchantOS';

/**
* @var array
Expand All @@ -34,13 +37,69 @@ public function __construct(AccessToken $token, $accountId)
$this->accountId = $accountId;
}

public function setUserAgent($agent)
{
$this->userAgent = $agent;
}

/**
* @param $itemId
* @param $extra
* @return mixed
*/
public function getItem($itemId, $extra = [])
{
$params = [];
$response = $this->makeAPICall('Account.Item', 'GET', $itemId, $params, null);

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

return [];
}

/**
* @param $itemId
* @param $data
* @return mixed
*/
public function createItem($data)
{
$params = [];
$response = $this->makeAPICall('Account.Item', 'POST', null, $params, $data);

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

return [];
}

/**
* @param $itemId
* @param $data
* @return mixed
*/
public function updateItem($itemId, $data)
{
$params = [];
$response = $this->makeAPICall('Account.Item', 'PUT', $itemId, $params, $data);

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

return [];
}

/**
* @param $saleId
* @return mixed
*/
public function getSale($saleId)
{
$params = ['oauth_token' => $this->oauthToken, 'load_relations' => 'all', 'orderby' => 'saleLineID', 'orderby_desc' => 1];
$params = ['load_relations' => 'all', 'orderby' => 'saleLineID', 'orderby_desc' => 1];
$response = $this->makeAPICall('Account.Sale', 'GET', $saleId, $params, null);

if (isset($response['Sale']) && $this->itemsCount($response) > 0) {
Expand All @@ -57,7 +116,7 @@ public function getSale($saleId)
*/
public function updateSale($saleId, $saleData)
{
$params = ['oauth_token' => $this->oauthToken];
$params = [];
$response = $this->makeAPICall('Account.Sale', 'PUT', $saleId, $params, $saleData);

if (isset($response['Sale']) && $this->itemsCount($response) > 0) {
Expand All @@ -67,21 +126,38 @@ public function updateSale($saleId, $saleData)
return [];
}

/**
* @param array $params
* @return mixed
*/
public function getSales($params = [])
{
$response = $this->makeAPICall('Account.Sale', 'GET', null, $params, null);

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

return [];
}

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

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

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

Expand All @@ -94,7 +170,7 @@ public function getSaleLine($saleId, $extra = [])
*/
public function updateSaleLine($saleId, $saleLineId, $data)
{
$params = ['oauth_token' => $this->oauthToken];
$params = [];

$control = 'Account.Sale' . '/' . $saleId . '/SaleLine' . '/' . $saleLineId;
$response = $this->makeAPICall($control, 'PUT', null, $params, $data);
Expand All @@ -113,7 +189,7 @@ public function updateSaleLine($saleId, $saleLineId, $data)
*/
public function createSaleLine($saleId, $data)
{
$params = ['oauth_token' => $this->oauthToken];
$params = [];

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

Expand All @@ -129,7 +205,7 @@ public function createSaleLine($saleId, $data)
*/
public function getShops()
{
$params = ['oauth_token' => $this->oauthToken];
$params = [];
$response = $this->makeAPICall('Account.Shop', 'GET', null, $params, null);

//validate the response
Expand All @@ -149,7 +225,6 @@ public function getShops()
public function getCustomer($customerId)
{
$params = array(
'oauth_token' => $this->oauthToken,
'archived' => 0,
'limit' => '1',
'load_relations' => 'all',
Expand All @@ -175,10 +250,7 @@ public function getCustomer($customerId)
*/
public function updateCustomer($customerId, $data)
{
$params = array(
'oauth_token' => $this->oauthToken,
);

$params = [];
$response = $this->makeAPICall('Account.Customer', 'PUT', $customerId, $params, $data);

//validate the response
Expand All @@ -197,7 +269,6 @@ public function updateCustomer($customerId, $data)
public function getCustomField($customFieldId)
{
$params = array(
'oauth_token' => $this->oauthToken,
'archived' => 0,
'limit' => '1',
'load_relations' => 'all',
Expand All @@ -222,10 +293,7 @@ public function getCustomField($customFieldId)
*/
public function createCustomField($data)
{
$params = array(
'oauth_token' => $this->oauthToken,
);

$params = [];
$response = $this->makeAPICall('Account.Customer/CustomField', 'POST', null, $params, $data);

//validate the response
Expand All @@ -245,7 +313,6 @@ public function createCustomField($data)
public function getEmployee($employeeId)
{
$params = array(
'oauth_token' => $this->oauthToken,
'archived' => 0,
'limit' => '1',
'load_relations' => 'all',
Expand All @@ -268,7 +335,7 @@ public function getEmployee($employeeId)
*/
public function getDiscount($discountId = null)
{
$params = ['oauth_token' => $this->oauthToken];
$params = [];
$response = $this->makeAPICall('Account.Discount', 'GET', $discountId, $params, null);

if (isset($response['Discount']) && $this->itemsCount($response) > 0) {
Expand All @@ -284,7 +351,7 @@ public function getDiscount($discountId = null)
*/
public function createDiscount($data)
{
$params = ['oauth_token' => $this->oauthToken];
$params = [];

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

Expand Down Expand Up @@ -313,8 +380,16 @@ public function makeAPICall($controlUrl, $action, $uniqueId, $params, $data)

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

$headers = [
'User-Agent' => $this->userAgent,
'Accept' => 'application/vnd.merchantos-v2+json',
'Authorization' => 'OAuth ' . $this->oauthToken,
];

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

// print_r($response->getHeader('x-ls-api-bucket-level')); die;

$body = (string) $response->getBody();
$r = json_decode($body, true);
Expand Down

0 comments on commit 39f270b

Please sign in to comment.