PHP code example of amadulhaque / shopify-bridge

1. Go to this page and download the library: Download amadulhaque/shopify-bridge 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/ */

    

amadulhaque / shopify-bridge example snippets


return Shopify::oauth()->redirect($shop, route('shopify.callback'), [
    'id' => 1234,
]);

// After exchange:
$result->state['id'];

// app/Providers/AppServiceProvider.php

use App\Shopify\MongoOAuthStateRepository;
use App\Shopify\SubdomainShopResolver;
use App\Shopify\TenantTokenRepository;
use App\Shopify\TenantWebhookRepository;
use Amadulhaque\Shopify\Contracts\OAuthStateRepository;
use Amadulhaque\Shopify\Contracts\ShopResolver;
use Amadulhaque\Shopify\Contracts\TokenRepository;
use Amadulhaque\Shopify\Contracts\WebhookRepository;

public function register(): void
{
    $this->app->bind(OAuthStateRepository::class, MongoOAuthStateRepository::class);
    $this->app->bind(TokenRepository::class, TenantTokenRepository::class);
    $this->app->bind(ShopResolver::class, SubdomainShopResolver::class);
    $this->app->bind(WebhookRepository::class, TenantWebhookRepository::class);
}

use App\Shopify\CurrentUserShopAuthorization;
use Amadulhaque\Shopify\Contracts\AuthorizationResolver;

$this->app->bind(AuthorizationResolver::class, CurrentUserShopAuthorization::class);

use App\Http\Controllers\ShopifyInstallController;
use Illuminate\Support\Facades\Route;

Route::get('/shopify/install', [ShopifyInstallController::class, 'install']);
Route::get('/shopify/callback', [ShopifyInstallController::class, 'callback'])
    ->name('shopify.callback');

namespace App\Http\Controllers;

use App\Shopify\ShopifyInstallationStore;
use Amadulhaque\Shopify\Facades\Shopify;
use Illuminate\Http\Request;

class ShopifyInstallController
{
    public function install(Request $request)
    {
        return Shopify::oauth()->redirect($request->string('shop')->toString());
    }

    public function callback(Request $request, ShopifyInstallationStore $installations)
    {
        $result = Shopify::oauth()->exchange($request);

        // Your application decides where and how this is persisted/encrypted.
        $installations->save($result->shop, $result->token);

        return to_route('dashboard');
    }
}

$oauth = Shopify::oauth('amad123s.myshopify.com', 'shpat_existing_token');

if (! $oauth->accessTokenIsValid()) {
    // Reinstall or ask the merchant to reconnect the app.
}

// The configured shop makes the domain optional here.
$url = Shopify::oauth('amad123s.myshopify.com')->authorizationUrl();

use Amadulhaque\Shopify\Facades\Shopify;

$response = Shopify::graph()
    ->shop('demo.myshopify.com')
    ->query('query ($id: ID!) { product(id: $id) { id title } }', [
        'id' => 'gid://shopify/Product/1',
    ]);

if ($response->hasErrors()) {
    return response()->json(['errors' => $response->errors()], 422);
}

return response()->json($response->data());

Shopify::graph()
    ->shop($shop)
    ->mutation('mutation ($input: ProductInput!) { productCreate(input: $input) { product { id } userErrors { message } } }', [
        'input' => ['title' => 'New product'],
    ]);

Shopify::graph('amad123s.myshopify.com', 'shpat_existing_token')
    ->query('query { shop { name } }');

Route::get('/shop', ShopController::class)->middleware('shopify.shop');
Route::get('/shop/data', DataController::class)->middleware('shopify.auth');
Route::post('/webhooks/shopify', ShopifyWebhookController::class)->middleware('shopify.webhook');

Route::get('/settings', SettingsController::class)
    ->middleware(['auth', 'shopify.auth']);

use Amadulhaque\Shopify\Facades\Shopify;
use Amadulhaque\Shopify\Webhooks\WebhookSubscription;

Shopify::webhooks()->register($shop, new WebhookSubscription(
    'orders/create',
    'https://app.example.com/webhooks/orders',
));

Shopify::webhooks()->delete($shop, $subscriptionId);
Shopify::webhooks()->sync($shop);       // Registers missing desired subscriptions.
Shopify::webhooks()->sync($shop, true); // Also removes stale remote subscriptions.

$shop = 'amad123s.myshopify.com';
$token = 'shpat_existing_token';

Shopify::webhooks()->register($shop, new WebhookSubscription(
    'orders/create',
    'https://app.example.com/webhooks/orders',
), $token);

$firstPage = Shopify::webhooks()->list($shop, null, $token);
$subscriptions = Shopify::webhooks()->all($shop, $token);
Shopify::webhooks()->sync($shop, true, $token);
Shopify::webhooks()->delete($shop, $subscriptionId, $token);

public function __invoke(Request $request)
{
    $webhook = $request->attributes->get('shopify.webhook');

    ProcessShopifyWebhook::dispatch($webhook);

    return response()->noContent();
}

use Amadulhaque\Shopify\Events\ShopInstalled;
use Illuminate\Support\Facades\Event;

Event::listen(ShopInstalled::class, function (ShopInstalled $event) {
    // Persist the result, initialize a tenant, or enqueue onboarding work.
});

use Illuminate\Support\Facades\Http;

Http::fake([
    'https://demo.myshopify.com/admin/api/*/graphql.json' => Http::response([
        'data' => ['shop' => ['name' => 'Demo']],
    ]),
]);

Shopify::oauth()->redirect($shop);
Shopify::oauth()->exchange($request);
Shopify::oauth($shop, $accessToken)->accessTokenIsValid();
Shopify::graph()->shop($shop)->query($query, $variables);
Shopify::graph($shop, $accessToken)->query($query, $variables);
Shopify::graph()->shop($shop)->mutation($mutation, $variables);
Shopify::webhooks()->register($shop, $subscription);
Shopify::webhooks()->list($shop);
Shopify::webhooks()->all($shop);
Shopify::webhooks()->delete($shop, $subscriptionId);
Shopify::webhooks()->sync($shop);
Shopify::client();
bash
php artisan vendor:publish --tag=shopify-config
bash
php artisan test