Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Shopify Laravel Package

ohmybrew/laravel-shopify

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require kyon147/laravel-shopify
    

    Publish the config and migrations:

    php artisan vendor:publish --provider="Kyon147\Shopify\ShopifyServiceProvider"
    php artisan migrate
    
  2. Configuration: Add your Shopify app credentials to .env:

    SHOPIFY_API_KEY=your_api_key
    SHOPIFY_API_SECRET=your_api_secret
    SHOPIFY_REDIRECT_URI=http://your-app.test/api/auth/callback
    SHOPIFY_SCOPES=write_products,write_orders,read_themes
    
  3. First Use Case: Set up OAuth routes in routes/web.php:

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

    Use the ShopifyController to handle OAuth flow and store shop data.


First Integration

Create a controller to handle Shopify webhooks:

use Kyon147\Shopify\Facades\Shopify;

class ShopifyWebhookController extends Controller
{
    public function handleWebhook(Request $request)
    {
        $topic = $request->header('X-Shopify-Topic');
        $hmac = $request->header('X-Shopify-Hmac-Sha256');

        return Shopify::webhook($topic, $hmac, $request->getContent());
    }
}

Implementation Patterns

OAuth Flow

  1. Installation Route: Redirect users to Shopify for OAuth:

    public function install()
    {
        return Shopify::install();
    }
    
  2. Callback Handling: Process the OAuth response and store the shop data:

    public function callback(Request $request)
    {
        $shop = Shopify::callback($request, 'your_store_callback_url');
        // Store $shop->id in your database
    }
    

API Integration

  1. Shop Context: Use the Shopify facade with a shop ID:

    $shop = Shopify::findShopById($shopId);
    $products = $shop->callApi('GET', '/admin/api/2023-01/products.json');
    
  2. Resource Management: Fetch and update resources (e.g., products, orders):

    $product = $shop->callApi('GET', '/admin/api/2023-01/products/123456789.json');
    $product->title = 'Updated Title';
    $shop->callApi('PUT', '/admin/api/2023-01/products/123456789.json', $product);
    

Webhooks

  1. Register Webhooks: Subscribe to Shopify events during OAuth callback:

    $shop->subscribeWebhook('orders/create', 'your_webhook_url');
    
  2. Handle Incoming Webhooks: Verify and process webhook payloads:

    public function handleWebhook(Request $request)
    {
        $topic = $request->header('X-Shopify-Topic');
        $data = Shopify::verifyWebhook($topic, $request->getContent());
    
        // Process $data (e.g., create an order in your DB)
    }
    

Billable Metrics

  1. Track Usage: Use the Billable trait to log API calls for billing:

    use Kyon147\Shopify\Traits\Billable;
    
    class MyShopifyService
    {
        use Billable;
    
        public function fetchProducts()
        {
            $this->increment('products_fetched');
            return Shopify::callApi(...);
        }
    }
    
  2. Retrieve Metrics: Fetch usage data for a shop:

    $metrics = Shopify::getBillableMetrics($shopId);
    

Gotchas and Tips

Common Pitfalls

  1. API Versioning:

    • Ensure your API calls use the correct version (e.g., /admin/api/2023-01/products.json). Mismatches cause 404 Not Found.
    • Check Shopify's API changelog for breaking changes.
  2. Webhook Verification:

    • Always verify webhook HMAC signatures. Use Shopify::verifyWebhook() to avoid spoofing:
      $data = Shopify::verifyWebhook($topic, $rawBody, $hmac);
      
    • Store the shared secret securely (e.g., in the database per-shop).
  3. Rate Limiting:

    • Shopify enforces rate limits. Handle 429 Too Many Requests gracefully:
      try {
          $response = $shop->callApi('GET', '/admin/api/2023-01/products.json');
      } catch (RateLimitExceededException $e) {
          // Retry with exponential backoff
      }
      
  4. Session Management:

    • Shopify sessions expire after 5 minutes of inactivity. Refresh tokens proactively:
      $shop->refreshAccessToken();
      

Debugging Tips

  1. Enable Logging: Configure logging in .env:

    SHOPIFY_LOG_LEVEL=debug
    

    Logs appear in storage/logs/shopify.log.

  2. Inspect Responses: Use dd() to debug API responses:

    $response = $shop->callApi('GET', '/admin/api/2023-01/products.json');
    dd($response->body);
    
  3. Test Locally: Use ngrok to expose local endpoints for webhook testing:

    ngrok http 8000
    

    Configure Shopify to send webhooks to your ngrok URL.


Extension Points

  1. Custom API Clients: Extend the Shopify facade for reusable logic:

    class CustomShopify extends \Kyon147\Shopify\Facades\Shopify
    {
        public function getCustomProducts()
        {
            return $this->callApi('GET', '/admin/api/2023-01/custom_products.json');
        }
    }
    
  2. Middleware for Shop Context: Attach a middleware to inject the current shop into requests:

    Shopify::middleware(function ($request, $next) {
        $shop = Shopify::findShopByRequest($request);
        $request->merge(['shop' => $shop]);
        return $next($request);
    });
    
  3. Event Listeners: Listen for Shopify events (e.g., after webhook processing):

    Shopify::listen('webhook.processed', function ($topic, $data) {
        // Custom logic (e.g., send Slack notification)
    });
    

Configuration Quirks

  1. Scopes:

    • Ensure your app’s SHOPIFY_SCOPES in .env match the scopes requested during OAuth. Missing scopes cause 403 Forbidden.
  2. Redirect URI:

    • The SHOPIFY_REDIRECT_URI must exactly match the URI configured in your Shopify Partners dashboard. Use absolute URLs (e.g., https://your-app.com/auth/callback).
  3. Environment-Specific Settings: Use Laravel’s .env files to manage different environments:

    # .env.production
    SHOPIFY_API_KEY=prod_key
    SHOPIFY_API_SECRET=prod_secret
    
    # .env.local
    SHOPIFY_API_KEY=local_key
    SHOPIFY_API_SECRET=local_secret
    

Performance Tips

  1. Batch API Calls: Use pagination for large datasets:

    $limit = 250; // Max allowed by Shopify
    $products = $shop->callApi('GET', "/admin/api/2023-01/products.json?limit=$limit");
    
  2. Cache Responses: Cache frequent API calls (e.g., shop themes):

    $theme = Cache::remember("shop_{$shopId}_theme", now()->addHours(1), function () use ($shop) {
        return $shop->callApi('GET', '/admin/api/2023-01/themes.json');
    });
    
  3. Async Webhook Processing: Offload webhook processing to a queue (e.g., Laravel Queues):

    Shopify::listen('webhook.processed', function ($topic, $data) {
        dispatch(new ProcessWebhook($topic, $data));
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle