/
var
/
www
/
html
/
plugin-techloyce
/
Modules
/
Webstores
/
Services
/
Upload File
HOME
<?php namespace Modules\Webstores\Services; use PHPShopify\AuthHelper; use PHPShopify\ShopifySDK; use Illuminate\Http\Request; use Modules\Webstores\Contracts\ShopifyContract; use Modules\Webstores\Entities\Webstore; use Modules\Webstores\Repositories\ShopifyRepository; class ShopifyService implements ShopifyContract { public $request; public $response; private $repository; public function __construct() { $this->response = ['status'=>false]; $this->repository = new ShopifyRepository; } /** * Global config for shopify requests */ private function shopifyConfig($returnConfig=false) { $credentials = config('webstores.service:Shopify'); // Shopify global configuration $config = array( 'ShopUrl' => $credentials['shop_url'], 'ApiKey' => $credentials['api_key'], 'SharedSecret' => $credentials['api_secret'], ); if($returnConfig) return $config; ShopifySDK::config($config); // Passing configuration to Shopify SDK return $credentials; } /** * Requesting authorization url */ public function authorize(Request $request) { $this->request = $request; if($request->has('destinationAdditionalData') && isset($request->destinationAdditionalData['shopify_store_url'])) { config(['webstores.service:Shopify.shop_url'=>$request->destinationAdditionalData['shopify_store_url']]); } $credentials = $this->shopifyConfig(); $authUrl = AuthHelper::createAuthRequest($credentials['scopes'], $credentials['redirect_url'], null, null, true); if(!empty($authUrl)) { // AuthURL contains URL to client's store requesting access to our app. $this->authorized = true; $this->response['status'] = true; $this->response['data'] = [ 'service' => 'Shopify', 'response_type' => 'auth_url', 'response_trigger' => 'redirect_away', 'response_data' => $authUrl ]; } else { $this->response['status'] = false; $this->response['data'] = [ 'service' => 'Shopify', 'response_type' => 'WS_SHOPIFY_ERROR_UNAUTHENTICATED', 'response_trigger' => 'json', 'response_data' => 'Error occured while authenticating. Please check your credentials' ]; } return $this->response; } /** * Generating access-token from shopify */ public function authorizeCallback(Request $request) { if($request->has('shop')) { config(['webstores.service:Shopify.shop_url'=>$request->shop]); } $credentials = $this->shopifyConfig(); // Validating if callback request comes from Shopify or someone trying to access maliciously if(!$this->validateAuthorizeCallback($request,$credentials['api_secret'])) { $this->response['status'] = false; $this->response['data'] = [ 'service' => 'Shopify', 'response_type' => 'WS_SHOPIFY_ERROR_UNAUTHORIZED', 'response_trigger' => 'json', 'response_data' => 'Error occured while authorizing. Please check your credentials' ]; return $this->response; } $accessToken = AuthHelper::getAccessToken(); if(!empty($accessToken)) { $this->response['status'] = true; $this->response['data'] = [ 'service' => 'Shopify', 'response_type' => 'access_token', 'response_trigger' => 'json', 'response_data' => $accessToken, 'key' => $request->shop ]; } else { $this->response['status'] = false; $this->response['data'] = [ 'service' => 'Shopify', 'response_type' => 'WS_SHOPIFY_ERROR_UNAUTHORIZED', 'response_trigger' => 'json', 'response_data' => 'Error occured while authorizing. Please check your credentials' ]; } return $this->response; } /** * Get requested resources */ public function getResource(Request $request,Webstore $identifier) { $requiredResource = $request->requiredResource; $filters = []; $repositoryResponse = null; if($request->has('since_id')) { $filters['since_id'] = $request->since_id; } if(!empty($identifier->destination_extra_information)) { $dei = unserialize($identifier->destination_extra_information); if(is_array($dei) && isset($dei['shopify_store_url'])) { config(['webstores.service:Shopify.shop_url'=>$dei['shopify_store_url']]); } } if(in_array($requiredResource,ShopifyRepository::$availableResources)) { $method = 'get'.$requiredResource; $this->repository->setIdentifier($identifier); $this->repository->setShopifyObject($this->shopifyConfig(true)); switch($requiredResource) { case 'Customers': $repositoryResponse = $this->repository->getCustomers($filters); break; case 'Products': $repositoryResponse = $this->repository->getProducts($filters); break; case 'Orders': $repositoryResponse = $this->repository->getOrders($filters); break; default: break; } if(!empty($repositoryResponse)) { $this->response['status'] = true; $this->response['data'] = [ 'service' => 'Shopify', 'response_type' => $requiredResource, 'response_trigger' => 'json', 'response_data' => $repositoryResponse ]; } else { $this->response['status'] = false; $this->response['data'] = [ 'service' => 'Shopify', 'response_type' => 'WS_SHOPIFY_ERROR_RESOURCE', 'response_trigger' => 'json', 'response_data' => 'Error occured while loading required resource' ]; } } else { $this->response['status'] = false; $this->response['data'] = [ 'service' => 'Shopify', 'response_type' => 'WS_SHOPIFY_ERROR_RESOURCE', 'response_trigger' => 'json', 'response_data' => 'Resource not found.' ]; } return $this->response; } /** * Validating authorize-callback request */ private function validateAuthorizeCallback(Request $request,$shared_secret) { // By computing the HMAC SHA256 digest, // we can compare this against the incoming hmac parameter // and determine if the request is legitimate. if($request->has('hmac')) { $params = $request->query(); // Retrieve all request parameters $hmac = $request->hmac; // Retrieve HMAC request parameter $params = array_diff_key($params, array('hmac' => '')); // Remove hmac from params ksort($params); // Sort params lexographically // Compute SHA256 digest $computed_hmac = hash_hmac('sha256', http_build_query($params), $shared_secret); // Use hmac data to check that the response is from Shopify or not if (hash_equals($hmac, $computed_hmac)) { return true; } } return false; } }