Skip to content

Use Separate Config for Each Instance of ShopifySDK #329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ $config = array(
'Password' => '***YOUR-PRIVATE-API-PASSWORD***',
);

PHPShopify\ShopifySDK::config($config);
$shopifySdk = new PHPShopify\ShopifySDK($config);
```

For Third party apps, use the permanent access token.
Expand All @@ -54,7 +54,7 @@ $config = array(
'AccessToken' => '***ACCESS-TOKEN-FOR-THIRD-PARTY-APP***',
);

PHPShopify\ShopifySDK::config($config);
$shopifySdk = new PHPShopify\ShopifySDK($config);
```
You can use specific Shopify API Version by adding in the config array

Expand All @@ -79,7 +79,7 @@ $config = array(
'SharedSecret' => '***YOUR-SHARED-SECRET***',
);

PHPShopify\ShopifySDK::config($config);
$shopifySdk = new PHPShopify\ShopifySDK($config);
```

2) Create the authentication request
Expand All @@ -93,30 +93,30 @@ $scopes = 'read_products,write_products,read_script_tags,write_script_tags';
//$scopes = array('read_products','write_products','read_script_tags', 'write_script_tags');
$redirectUrl = 'https://yourappurl.com/your_redirect_url.php';

\PHPShopify\AuthHelper::createAuthRequest($scopes, $redirectUrl);
\PHPShopify\AuthHelper::createAuthRequest($shopifySdk, $scopes, $redirectUrl);
```

> If you want the function to return the authentication url instead of auto-redirecting, you can set the argument `$return` (5th argument) to `true`.

```php
\PHPShopify\AuthHelper::createAuthRequest($scopes, $redirectUrl, null, null, true);
\PHPShopify\AuthHelper::createAuthRequest($shopifySdk, $scopes, $redirectUrl, null, null, true);
```

3) Get the access token when redirected back to the `$redirectUrl` after app authorization.

```php
//your_redirect_url.php
PHPShopify\ShopifySDK::config($config);
$accessToken = \PHPShopify\AuthHelper::getAccessToken();
$shopifySdk = PHPShopify\ShopifySDK($config);
$accessToken = \PHPShopify\AuthHelper::getAccessToken($shopifySdk);
//Now store it in database or somewhere else
```

> You can use the same page for creating the request and getting the access token (redirect url). In that case just skip the 2nd parameter `$redirectUrl` while calling `createAuthRequest()` method. The AuthHelper class will do the rest for you.

```php
//your_authorize_and_redirect_url.php
PHPShopify\ShopifySDK::config($config);
$accessToken = \PHPShopify\AuthHelper::createAuthRequest($scopes);
$shopifySdk = new PHPShopify\ShopifySDK($config);
$accessToken = \PHPShopify\AuthHelper::createAuthRequest($shopifySdk, $scopes);
//Now store it in database or somewhere else
```

Expand Down
38 changes: 22 additions & 16 deletions lib/AuthHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,36 @@ public static function getCurrentUrl()
*
* @param array $data Data array
*
* @return array
* @return string
*/
public static function buildQueryString($data)
{
$paramStrings = [];
foreach ($data as $key => $value) {
$paramStrings[] = "$key=$value";
}
return join('&', $paramStrings);
return implode('&', $paramStrings);
}

/**
* Verify if the request is made from shopify using hmac hash value
*
* @param ShopifySDK $shopifySdk
*
* @throws SdkException if SharedSecret is not provided or hmac is not found in the url parameters
*
* @return bool
*/
public static function verifyShopifyRequest()
public static function verifyShopifyRequest($shopifySdk)
{
$data = $_GET;
$config = $shopifySdk->getConfig();

if(!isset(ShopifySDK::$config['SharedSecret'])) {
if(!isset($config['SharedSecret'])) {
throw new SdkException("Please provide SharedSecret while configuring the SDK client.");
}

$sharedSecret = ShopifySDK::$config['SharedSecret'];
$sharedSecret = $config['SharedSecret'];

//Get the hmac and remove it from array
if (isset($data['hmac'])) {
Expand All @@ -88,16 +91,17 @@ public static function verifyShopifyRequest()
//hash the values before comparing (to prevent time attack)
if(md5($realHmac) === md5($hmac)) {
return true;
} else {
return false;
}

return false;
}

/**
* Redirect the user to the authorization page to allow the app access to the shop
*
* @see https://help.shopify.com/api/guides/authentication/oauth#scopes For allowed scopes
*
* @param ShopifySDK $shopifySdk
* @param string|string[] $scopes Scopes required by app
* @param string $redirectUrl
* @param string $state
Expand All @@ -107,9 +111,9 @@ public static function verifyShopifyRequest()
*
* @return void|string
*/
public static function createAuthRequest($scopes, $redirectUrl = null, $state = null, $options = null, $return = false)
public static function createAuthRequest($shopifySdk, $scopes, $redirectUrl = null, $state = null, $options = null, $return = false)
{
$config = ShopifySDK::$config;
$config = $shopifySdk->getConfig();

if(!isset($config['ShopUrl']) || !isset($config['ApiKey'])) {
throw new SdkException("ShopUrl and ApiKey are required for authentication request. Please check SDK configuration!");
Expand All @@ -122,10 +126,10 @@ public static function createAuthRequest($scopes, $redirectUrl = null, $state =

//If redirect url is the same as this url, then need to check for access token when redirected back from shopify
if(isset($_GET['code'])) {
return self::getAccessToken($config);
} else {
$redirectUrl = self::getCurrentUrl();
return self::getAccessToken($shopifySdk);
}

$redirectUrl = self::getCurrentUrl();
}

if (is_array($scopes)) {
Expand All @@ -152,13 +156,15 @@ public static function createAuthRequest($scopes, $redirectUrl = null, $state =
* Get Access token for the API
* Call this when being redirected from shopify page ( to the $redirectUrl) after authentication
*
* @param ShopifySDK $shopifySdk
*
* @throws SdkException if SharedSecret or ApiKey is missing in SDK configuration or request is not valid
*
* @return string
*/
public static function getAccessToken()
public static function getAccessToken($shopifySdk)
{
$config = ShopifySDK::$config;
$config = $shopifySdk->getConfig();

if(!isset($config['SharedSecret']) || !isset($config['ApiKey'])) {
throw new SdkException("SharedSecret and ApiKey are required for getting access token. Please check SDK configuration!");
Expand All @@ -178,8 +184,8 @@ public static function getAccessToken()
}

return isset($response['access_token']) ? $response['access_token'] : null;
} else {
throw new SdkException("This request is not initiated from a valid shopify shop!");
}

throw new SdkException("This request is not initiated from a valid shopify shop!");
}
}
2 changes: 1 addition & 1 deletion lib/HttpRequestJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static function processRequest($method, $url) {
* @return bool
*/
public static function shouldRetry($response, $error, $retry) {
$config = ShopifySDK::$config;
$config = [];

if (isset($config['RequestRetryCallback'])) {
return $config['RequestRetryCallback']($response, $error, $retry);
Expand Down
11 changes: 7 additions & 4 deletions lib/ShopifyResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@ abstract class ShopifyResource
* @var string $prevLink
*/
private $prevLink = null;
private $config;

public function __construct($id = null, $parentResourceUrl = '')
public function __construct($config, $id = null, $parentResourceUrl = null)
{
$this->id = $id;
$this->config = $config;

$parentResourceUrl = $parentResourceUrl ?: '';

$config = ShopifySDK::$config;
$this->id = $id;

$this->resourceUrl = ($parentResourceUrl ? $parentResourceUrl . '/' : $config['ApiUrl']) . $this->getResourcePath() . ($this->id ? '/' . $this->id : '');

Expand Down Expand Up @@ -210,7 +213,7 @@ public function __call($name, $arguments)
$resourceID = !empty($arguments) ? $arguments[0] : null;


$api = new $childClass($resourceID, $this->resourceUrl);
$api = new $childClass($this->config, $resourceID, $this->resourceUrl);

return $api;
} else {
Expand Down
Loading