PHP code example of esign / laravel-shopify

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

    

esign / laravel-shopify example snippets




namespace App\GraphQL\Queries;

use Esign\LaravelShopify\GraphQL\Contracts\Query;

class GetProductQuery implements Query
{
    public function __construct(private string $productId) {}
    
    public function getQuery(): string
    {
        return <<<'GQL'
            query getProduct($id: ID!) {
                product(id: $id) {
                    id
                    title
                    description
                    variants(first: 10) {
                        edges {
                            node {
                                id
                                price
                                sku
                            }
                        }
                    }
                }
            }
        GQL;
    }
    
    public function getVariables(): array
    {
        return ['id' => $this->productId];
    }
    
    public function mapFromResponse(array $response): mixed
    {
        return $response['data']['product'];
    }
}

use Esign\LaravelShopify\Facades\Shopify;
use App\GraphQL\Queries\GetProductQuery;

// In a controller or job
$product = Shopify::query(new GetProductQuery('gid://shopify/Product/123'));



namespace App\GraphQL\Mutations;

use Esign\LaravelShopify\GraphQL\Contracts\Mutation;

class CreateProductMutation implements Mutation
{
    public function __construct(
        private string $title,
        private string $description
    ) {}
    
    public function getQuery(): string
    {
        return <<<'GQL'
            mutation createProduct($input: ProductInput!) {
                productCreate(input: $input) {
                    product {
                        id
                        title
                    }
                    userErrors {
                        field
                        message
                    }
                }
            }
        GQL;
    }
    
    public function getVariables(): array
    {
        return [
            'input' => [
                'title' => $this->title,
                'description' => $this->description,
            ],
        ];
    }
    
    public function mapFromResponse(array $response): mixed
    {
        return $response['data']['productCreate']['product'];
    }
}



namespace App\GraphQL\Queries;

use Esign\LaravelShopify\GraphQL\Contracts\PaginatedQuery;

class GetAllProductsQuery implements PaginatedQuery
{
    public function getQuery(): string
    {
        return <<<'GQL'
            query getAllProducts($cursor: String) {
                products(first: 50, after: $cursor) {
                    edges {
                        node {
                            id
                            title
                        }
                        cursor
                    }
                    pageInfo {
                        hasNextPage
                        endCursor
                    }
                }
            }
        GQL;
    }
    
    public function getVariables(): array
    {
        return [];
    }
    
    public function mapFromResponse(array $response): array
    {
        return $response['data']['products']['edges'];
    }
    
    public function hasNextPage(array $response): bool
    {
        return $response['data']['products']['pageInfo']['hasNextPage'];
    }
    
    public function getNextCursor(array $response): ?string
    {
        return $response['data']['products']['pageInfo']['endCursor'];
    }
}

// Execute paginated query (automatically fetches all pages)
$allProducts = Shopify::queryPaginated(new GetAllProductsQuery());



namespace App\Shopify\DTOs;

use Esign\LaravelShopify\DTOs\OrderDto;

class CustomOrderDto extends OrderDto
{
    public function __construct(
        // Base OrderDto properties
        ?string $id = null,
        ?string $name = null,
        // ... other base properties
        
        // Your custom properties
        public ?string $customField = null,
        public ?array $customMetadata = null,
    ) {
        parent::__construct(
            id: $id,
            name: $name,
            // ... pass other base properties
        );
    }
    
    // Override methods if needed
    public function toArray(): array
    {
        $data = parent::toArray();
        $data['custom_field'] = $this->customField;
        return $data;
    }
}



use Esign\LaravelShopify\Inputs\CustomerInput;
use Esign\LaravelShopify\Inputs\MailingAddressInput;

$customerInput = new CustomerInput(
    email: '[email protected]',
    firstName: 'John',
    lastName: 'Doe',
    addresses: [
        new MailingAddressInput(
            address1: '123 Main St',
            city: 'Toronto',
            countryCode: 'CA',
            provinceCode: 'ON',
            zip: 'M5H 2N2',
        ),
    ],
);

// Use in your mutation
$variables = [
    'input' => $customerInput->toArray(),
];

'webhooks' => [
    'routes' => [
        // Built-in handlers (already configured)
        // 'app/uninstalled' => [...]
        // 'customers/data_request' => [...]
        // 'customers/redact' => [...]
        // 'shop/redact' => [...]
        
        // Add your custom handlers:
        'orders/create' => [
            'job' => \App\Jobs\Shopify\OrdersCreateJob::class,
            'queue' => 'webhooks',
        ],
        'products/update' => [
            'job' => \App\Jobs\Shopify\ProductsUpdateJob::class,
            'queue' => 'webhooks',
        ],
    ],
],



namespace App\Jobs\Shopify;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class OrdersCreateJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(
        public string $shopDomain,
        public array $webhookData,
    ) {}

    public function handle(): void
    {
        Log::info('Order created', [
            'shop' => $this->shopDomain,
            'order_id' => $this->webhookData['id'],
        ]);

        // Process order data
        // YourOrder::create([...]);
    }
}

// In EventServiceProvider or via Event::listen()
use Esign\LaravelShopify\Events\AppInstalledEvent;
use Esign\LaravelShopify\Events\AppUninstalledEvent;

Event::listen(AppInstalledEvent::class, function (AppInstalledEvent $event) {
    // $event->shop contains the Shop model
    Log::info('New shop installed', ['domain' => $event->shop->domain]);
    
    // Dispatch a job if heavy processing is needed
    dispatch(new SetupNewShopJob($event->shop));
});

Event::listen(AppUninstalledEvent::class, function (AppUninstalledEvent $event) {
    // Clean up external resources, notify team, etc.
});

// In app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
    // Delete shops soft-deleted 90+ days ago
    $schedule->command('shopify:cleanup-uninstalled-shops --days=90 --force')
        ->daily();
}

use Esign\LaravelShopify\Models\Shop;

// Get authenticated shop
$shop = Auth::user(); // Returns Shop model

// Check installation status
if ($shop->isInstalled()) {
    // Shop is currently installed
}

// Mark as uninstalled (soft delete)
$shop->markAsUninstalled();

// Mark as reinstalled (restore + update token)
$newAccessToken = '...'; // Get new token via token exchange
$shop->markAsReinstalled($newAccessToken);

// Access token (encrypted in database)
$token = $shop->access_token;

'logging' => [
    'enabled' => true,
    'channel' => 'stack',
    'log_queries' => true,      // Log all GraphQL queries
    'log_mutations' => true,    // Log all GraphQL mutations
    'log_webhooks' => true,     // Log webhook dispatch
],
bash
php artisan vendor:publish --provider="Esign\LaravelShopify\ShopifyServiceProvider"
php artisan migrate
bash
php artisan shopify:make-webhook OrdersCreateJob --topic=orders/create
bash
php artisan shopify:cleanup-uninstalled-shops --days=90