PHP code example of clarity-tech / laravel-shopify

1. Go to this page and download the library: Download clarity-tech/laravel-shopify library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clarity-tech / laravel-shopify example snippets

5


'providers' => [
    ...
    ClarityTech\Shopify\ShopifyServiceProvider::class,
],
5


'aliases' => [
    ...
    'Shopify' => ClarityTech\Shopify\Facades\Shopify::class,
],
5

'guards' => [
    ...
    'shopify' => [
        'driver' => 'shopify-auth',
        'provider' => 'shops',
    ],
],

'providers' => [
    ...
    'shops' => [
        'driver' => 'eloquent',
        'model' => App\Models\Shop::class,
    ]
],

use ClarityTech\Shopify\Facades\Shopify;

Route::get("shop/install", function(\Illuminate\Http\Request $request)
{
    //$redirectUrl = route('shop.authorize');
    $redirectUrl = "http://mydomain.com/shop/authorize";
    $myShopifyDomain = $request->shop;
    $scope = ["write_products","read_orders"];
    $authorizeUrl = Shopify::getAuthorizeUrl($myShopifyDomain, $scopes, $redirectUrl);
    
    return redirect()->to($authorizeUrl);
});
5
Route::get("process_oauth_result",function(\Illuminate\Http\Request $request)
{
    $shopifyApi = resolve('shopify');
    $myShopifyDomain = $request->shop;
    $code = $request->code;

    $token = $shopifyApi
        ->setShopDomain($myShopifyDomain)
        ->getAccessToken($code);
        //this gets access token from shopify and set it to the current  instance which will be passed in further api calls


    dd($accessToken);
    //store the access token for future api calls on behalf of the shop    
    
    // redirect to success page or billing etc.
});
5
SubscribeAppUninstalledWebhookJob::dispatch($shop);
5
use ClarityTech\Shopify\Facades\Shopify;

public function verifyRequest(Request $request)
{
    $params = $request->all();

    if (Shopify::verifyRequest($params)){
        logger("verification passed");
    }else{
        logger("verification failed");
    }
}

5
use ClarityTech\Shopify\Facades\Shopify;

public function verifyWebhook(Request $request)
{
    $data = $request->getContent();
    $hmacHeader = $request->header('x-shopify-hmac-sha256');

    if (Shopify::verifyWebHook($data, $hmacHeader)) {
        logger("verification passed");
    } else {
        logger("verification failed");
    }
}

5
$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 } } } }', [])
5
Shopify::get("resource uri", ["query string params"]);
Shopify::post("resource uri", ["post body"]);
Shopify::put("resource uri", ["put body"]);
Shopify::delete("resource uri");
5
use ClarityTech\Shopify\Facades\Shopify;

$shopUrl = "example.myshopify.com";
$accessToken = "xxxxxxxxxxxxxxxxxxxxx"; //retrieve from your storage(db)
$products = Shopify::setShop($myShopifyDomain, $accessToken)->get("admin/products.json");
5
// returns Collection
Shopify::setShop($myShopifyDomain, $accessToken);
$products = Shopify::get('admin/products.json', ["limit"=>20, "page" => 1]);
5
use Illuminate\Http\Request;
use ClarityTech\Shopify\Shopify;

class Foo
{
    protected $shopify;

    public function __construct(Shopify $shopify)
    {
        $this->shopify = $shopify;
    }

    /*
    * returns products
    */
    public function getProducts(Request $request)
    {
        $accessToken = 'xxxxxxxxxxxxxxxxxxxxx';//retrieve from your storage(db)
        $products = $this->shopify->setShop($request->shop, $accessToken)
            ->get('admin/products.json');

        dump($products);
    }
}
5
Shopify::getHeaders();
5
Shopify::getHeader("Content-Type");
5
if(Shopify::hasHeader("Content-Type")){
    echo "Yes header exist";
}
5
Shopify::getStatusCode(); // 200
Shopify::getReasonPhrase(); // ok
5
SubscribeAppUninstalledWebhookJob::dispatch($shop);

'key' => env("SHOPIFY_APIKEY", null),
'secret' => env("SHOPIFY_SECRET", null)