From ec730646f41498a9cd6bb1578433a1f8829a394b Mon Sep 17 00:00:00 2001 From: rupamjbordoloi Date: Thu, 19 Aug 2021 15:05:36 +0530 Subject: [PATCH] added osiset/basic-shopify-api as external dependency (#3) Co-authored-by: rupam --- README.md | 24 ++++++++++++++++++++---- composer.json | 3 ++- src/Shopify/Shopify.php | 27 +++++++++++++++++++++++++++ src/config/shopify.php | 3 +++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d9b7350..36f38e8 100644 --- a/README.md +++ b/README.md @@ -62,10 +62,12 @@ Update config/app.php with below code and in routes add `auth:shopify` as middle ## Set credendials -in your `.env` file set these values from your app -`SHOPIFY_APIKEY=your-api-key` -`SHOPIFY_SECRET=your-secret-key` -`SHOPIFY_VERSION=admin-api-version` +in your `.env` file set these values from your app \ +`SHOPIFY_APIKEY=your-api-key` \ +`SHOPIFY_SECRET=your-secret-key` \ +`SHOPIFY_VERSION=admin-api-version` \ +only if app is private \ +`API_PASSWORD=private-app-password` ## Optional Configuration (publishing) @@ -174,6 +176,20 @@ public function verifyWebhook(Request $request) } ``` +To access Admin API use + +```php5 +$myshopify_domain = "example.myshopify.com"; +$access_token = "xxxxxxxxxxxxxxxxxxxxx"; +$api = Shopify::setShop($myshopify_domain, $access_token)->basicApi(); + +// For sync rest api +$res = $api->rest('GET', '/admin/shop.json') + +// For sync graphql api +$res = $api->graph('{ products(first: 1) { edges { node { handle, id } } } }', []) +``` + To access API resource use diff --git a/composer.json b/composer.json index a5db1d7..ef2093d 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "require": { "php": ">=7.3.0|8.0", "laravel/framework": "^7.0|^8.0", - "psr/http-message": "^1.0" + "psr/http-message": "^1.0", + "osiset/basic-shopify-api": "^10.0" }, "autoload": { "psr-4": { diff --git a/src/Shopify/Shopify.php b/src/Shopify/Shopify.php index 6ee034b..602a247 100644 --- a/src/Shopify/Shopify.php +++ b/src/Shopify/Shopify.php @@ -6,12 +6,16 @@ use Illuminate\Support\Facades\Http; use ClarityTech\Shopify\Exceptions\ShopifyApiException; use ClarityTech\Shopify\Exceptions\ShopifyApiResourceNotFoundException; +use Osiset\BasicShopifyAPI\BasicShopifyAPI; +use Osiset\BasicShopifyAPI\Options; +use Osiset\BasicShopifyAPI\Session; use Psr\Http\Message\ResponseInterface; class Shopify { protected static ?string $key = null; protected static ?string $secret = null; + protected static ?string $api_password = null; protected static ?string $version = null; protected static ?string $shopDomain = null; protected static ?string $accessToken = null; @@ -37,6 +41,7 @@ public function __construct() { self::$key = Config::get('shopify.key'); self::$secret = Config::get('shopify.secret'); + self::$api_password = Config::get('shopify.api_password'); self::$version = Config::get('shopify.version'); } @@ -213,6 +218,28 @@ public function __call($method, $args) return $response; } + /** + * @param bool $is_private true for private apps + * + * @return \ClarityTech\Shopify\BasicApi\BasicShopifyAPI $api + */ + public function basicApi(bool $is_private = false) + { + $options = new Options(); + $options->setVersion($this::$version); + if($is_private) { + $options->setType(true); + $options->setApiKey(env($this::$key)); + $options->setApiPassword($this::$api_password); + $session = new Session($this::$shopDomain); + }else { + $session = new Session($this::$shopDomain, $this::$accessToken); + } + $api = new BasicShopifyAPI($options); + $api->setSession($session); + return $api; + } + public function getHeadersForSend() : array { $headers = []; diff --git a/src/config/shopify.php b/src/config/shopify.php index 38d911b..77cee91 100644 --- a/src/config/shopify.php +++ b/src/config/shopify.php @@ -16,6 +16,9 @@ 'key' => env("SHOPIFY_APIKEY", null), 'secret' => env("SHOPIFY_SECRET", null), + // required only for private apps + 'api_password' => env("API_PASSWORD", null), + 'version' => env("SHOPIFY_VERSION", '2020-01'), // the prefix of the webhook url for uninstalled job