Input:
*** Begin Patch
*** Update File: /Users/fabianwesner/Herd/shop/database/seeders/AppSeeder.php
@@
 namespace Database\Seeders;

-use Illuminate\Database\Console\Seeds\WithoutModelEvents;
+use App\Enums\AppInstallationStatus;
+use App\Enums\AppStatus;
+use App\Enums\WebhookEventType;
+use App\Enums\WebhookSubscriptionStatus;
+use App\Models\App as AppModel;
+use App\Models\AppInstallation;
+use App\Models\OauthClient;
+use App\Models\OauthToken;
+use App\Models\Store;
+use App\Models\WebhookSubscription;
 use Illuminate\Database\Seeder;
+use Illuminate\Support\Str;
@@
     public function run(): void
     {
-        //
+        Store::query()->get()->each(function (Store $store): void {
+            $this->seedInstalledApps($store);
+        });
+    }
+
+    private function seedInstalledApps(Store $store): void
+    {
+        $apps = [
+            [
+                'name' => 'Inventory Sync',
+                'scopes' => ['read-products', 'write-products', 'read-orders'],
+                'events' => [WebhookEventType::OrderCreated, WebhookEventType::ProductUpdated],
+            ],
+            [
+                'name' => 'Fulfillment Bridge',
+                'scopes' => ['read-orders', 'write-orders'],
+                'events' => [WebhookEventType::OrderCreated, WebhookEventType::FulfillmentCreated],
+            ],
+        ];
+
+        foreach ($apps as $index => $appData) {
+            $app = AppModel::query()->updateOrCreate(
+                ['name' => $appData['name']],
+                [
+                    'status' => AppStatus::Active,
+                    'created_at' => now()->subMonths(3 - $index),
+                ],
+            );
+
+            $installation = AppInstallation::withoutGlobalScopes()->updateOrCreate(
+                [
+                    'store_id' => $store->getKey(),
+                    'app_id' => $app->getKey(),
+                ],
+                [
+                    'scopes_json' => $appData['scopes'],
+                    'status' => AppInstallationStatus::Active,
+                    'installed_at' => now()->subWeeks(8 - ($index * 3)),
+                ],
+            );
+
+            OauthClient::query()->updateOrCreate(
+                ['client_id' => 'app_client_'.$store->handle.'_'.Str::slug($app->name)],
+                [
+                    'app_id' => $app->getKey(),
+                    'client_secret_encrypted' => 'secret_'.Str::random(40),
+                    'redirect_uris_json' => ["https://{$store->handle}.integrations.example/oauth/callback"],
+                ],
+            );
+
+            OauthToken::query()->updateOrCreate(
+                [
+                    'installation_id' => $installation->getKey(),
+                    'name' => $app->name.' token',
+                ],
+                [
+                    'access_token_hash' => hash('sha256', 'shop_seed_'.$store->handle.'_'.Str::slug($app->name)),
+                    'refresh_token_hash' => hash('sha256', 'refresh_seed_'.$store->handle.'_'.Str::slug($app->name)),
+                    'abilities_json' => $appData['scopes'],
+                    'expires_at' => now()->addYear(),
+                    'last_used_at' => $index === 0 ? now()->subHours(2) : null,
+                    'created_at' => $installation->installed_at,
+                ],
+            );
+
+            foreach ($appData['events'] as $eventType) {
+                WebhookSubscription::withoutGlobalScopes()->updateOrCreate(
+                    [
+                        'store_id' => $store->getKey(),
+                        'app_installation_id' => $installation->getKey(),
+                        'event_type' => $eventType->value,
+                    ],
+                    [
+                        'target_url' => "https://{$store->handle}.integrations.example/webhooks/{$eventType->value}",
+                        'signing_secret_encrypted' => 'whsec_'.Str::random(40),
+                        'status' => WebhookSubscriptionStatus::Active,
+                    ],
+                );
+            }
+        }
     }
 }
*** Update File: /Users/fabianwesner/Herd/shop/database/seeders/DatabaseSeeder.php
@@
             OrganizationSeeder::class,
             StoreSeeder::class,
             StoreDomainSeeder::class,
+            AppSeeder::class,
             UserSeeder::class,
             StoreUserSeeder::class,
*** End Patch


Success. Updated the following files:
M /Users/fabianwesner/Herd/shop/database/seeders/AppSeeder.php
M /Users/fabianwesner/Herd/shop/database/seeders/DatabaseSeeder.php

@@ -4,4 +4,14 @@

-use Illuminate\Database\Console\Seeds\WithoutModelEvents;
+use App\Enums\AppInstallationStatus;
+use App\Enums\AppStatus;
+use App\Enums\WebhookEventType;
+use App\Enums\WebhookSubscriptionStatus;
+use App\Models\App as AppModel;
+use App\Models\AppInstallation;
+use App\Models\OauthClient;
+use App\Models\OauthToken;
+use App\Models\Store;
+use App\Models\WebhookSubscription;
 use Illuminate\Database\Seeder;
+use Illuminate\Support\Str;

@@ -14,3 +24,82 @@
     {
-        //
+        Store::query()->get()->each(function (Store $store): void {
+            $this->seedInstalledApps($store);
+        });
+    }
+
+    private function seedInstalledApps(Store $store): void
+    {
+        $apps = [
+            [
+                'name' => 'Inventory Sync',
+                'scopes' => ['read-products', 'write-products', 'read-orders'],
+                'events' => [WebhookEventType::OrderCreated, WebhookEventType::ProductUpdated],
+            ],
+            [
+                'name' => 'Fulfillment Bridge',
+                'scopes' => ['read-orders', 'write-orders'],
+                'events' => [WebhookEventType::OrderCreated, WebhookEventType::FulfillmentCreated],
+            ],
+        ];
+
+        foreach ($apps as $index => $appData) {
+            $app = AppModel::query()->updateOrCreate(
+                ['name' => $appData['name']],
+                [
+                    'status' => AppStatus::Active,
+                    'created_at' => now()->subMonths(3 - $index),
+                ],
+            );
+
+            $installation = AppInstallation::withoutGlobalScopes()->updateOrCreate(
+                [
+                    'store_id' => $store->getKey(),
+                    'app_id' => $app->getKey(),
+                ],
+                [
+                    'scopes_json' => $appData['scopes'],
+                    'status' => AppInstallationStatus::Active,
+                    'installed_at' => now()->subWeeks(8 - ($index * 3)),
+                ],
+            );
+
+            OauthClient::query()->updateOrCreate(
+                ['client_id' => 'app_client_'.$store->handle.'_'.Str::slug($app->name)],
+                [
+                    'app_id' => $app->getKey(),
+                    'client_secret_encrypted' => 'secret_'.Str::random(40),
+                    'redirect_uris_json' => ["https://{$store->handle}.integrations.example/oauth/callback"],
+                ],
+            );
+
+            OauthToken::query()->updateOrCreate(
+                [
+                    'installation_id' => $installation->getKey(),
+                    'name' => $app->name.' token',
+                ],
+                [
+                    'access_token_hash' => hash('sha256', 'shop_seed_'.$store->handle.'_'.Str::slug($app->name)),
+                    'refresh_token_hash' => hash('sha256', 'refresh_seed_'.$store->handle.'_'.Str::slug($app->name)),
+                    'abilities_json' => $appData['scopes'],
+                    'expires_at' => now()->addYear(),
+                    'last_used_at' => $index === 0 ? now()->subHours(2) : null,
+                    'created_at' => $installation->installed_at,
+                ],
+            );
+
+            foreach ($appData['events'] as $eventType) {
+                WebhookSubscription::withoutGlobalScopes()->updateOrCreate(
+                    [
+                        'store_id' => $store->getKey(),
+                        'app_installation_id' => $installation->getKey(),
+                        'event_type' => $eventType->value,
+                    ],
+                    [
+                        'target_url' => "https://{$store->handle}.integrations.example/webhooks/{$eventType->value}",
+                        'signing_secret_encrypted' => 'whsec_'.Str::random(40),
+                        'status' => WebhookSubscriptionStatus::Active,
+                    ],
+                );
+            }
+        }
     }

@@ -17,2 +17,3 @@
             StoreDomainSeeder::class,
+            AppSeeder::class,
             UserSeeder::class,


Output: {"output":"Success. Updated the following files:\nM /Users/fabianwesner/Herd/shop/database/seeders/AppSeeder.php\nM /Users/fabianwesner/Herd/shop/database/seeders/DatabaseSeeder.php\n","metadata":{"exit_code":0,"duration_seconds":0.0}}


Next I’m wiring the webhook runtime: subscriptions create delivery records, jobs sign the exact JSON body with timestamped HMAC, failures update delivery logs, and five consecutive failed deliveries pause the subscription.


Input:
*** Begin Patch
*** Update File: /Users/fabianwesner/Herd/shop/app/Services/WebhookService.php
@@
 <?php

 namespace App\Services;

-class WebhookService
+use App\Enums\AppInstallationStatus;
+use App\Enums\WebhookDeliveryStatus;
+use App\Enums\WebhookEventType;
+use App\Enums\WebhookSubscriptionStatus;
+use App\Jobs\DeliverWebhook;
+use App\Models\App as AppModel;
+use App\Models\AppInstallation;
+use App\Models\OauthToken;
+use App\Models\Store;
+use App\Models\WebhookDelivery;
+use App\Models\WebhookSubscription;
+use Illuminate\Support\Str;
+use InvalidArgumentException;
+
+class WebhookService
 {
-    //
+    /**
+     * @param  array<string, mixed>  $payload
+     */
+    public function dispatch(Store $store, string $eventType, array $payload): void
+    {
+        $event = WebhookEventType::tryFrom($eventType);
+
+        if (! $event instanceof WebhookEventType) {
+            throw new InvalidArgumentException("Unsupported webhook event type [{$eventType}].");
+        }
+
+        WebhookSubscription::withoutGlobalScopes()
+            ->where('store_id', $store->getKey())
+            ->where('event_type', $event->value)
+            ->where('status', WebhookSubscriptionStatus::Active->value)
+            ->get()
+            ->each(function (WebhookSubscription $subscription) use ($store, $event, $payload): void {
+                $delivery = WebhookDelivery::query()->create([
+                    'subscription_id' => $subscription->getKey(),
+                    'event_id' => (string) Str::uuid(),
+                    'attempt_count' => 1,
+                    'status' => WebhookDeliveryStatus::Pending,
+                ]);
+
+                DeliverWebhook::dispatch(
+                    $delivery->getKey(),
+                    $event->value,
+                    $this->envelope($store, $event, $delivery->event_id, $payload),
+                );
+            });
+    }
+
+    public function sign(string $payload, string $secret): string
+    {
+        return hash_hmac('sha256', $payload, $secret);
+    }
+
+    public function verify(string $payload, string $signature, string $secret): bool
+    {
+        return hash_equals($this->sign($payload, $secret), $signature);
+    }
+
+    /**
+     * @param  list<string>  $abilities
+     * @return array{token: OauthToken, plain_text: string}
+     */
+    public function createApiToken(Store $store, string $name, array $abilities): array
+    {
+        $app = AppModel::query()->firstOrCreate(
+            ['name' => 'Admin API'],
+            [
+                'status' => 'active',
+                'created_at' => now(),
+            ],
+        );
+
+        $installation = AppInstallation::withoutGlobalScopes()->firstOrCreate(
+            [
+                'store_id' => $store->getKey(),
+                'app_id' => $app->getKey(),
+            ],
+            [
+                'scopes_json' => $abilities,
+                'status' => AppInstallationStatus::Active,
+                'installed_at' => now(),
+            ],
+        );
+
+        $plainText = 'shop_'.Str::random(48);
+        $token = OauthToken::query()->create([
+            'installation_id' => $installation->getKey(),
+            'name' => $name,
+            'access_token_hash' => hash('sha256', $plainText),
+            'refresh_token_hash' => null,
+            'abilities_json' => $abilities,
+            'expires_at' => now()->addYear(),
+            'created_at' => now(),
+        ]);
+
+        return [
+            'token' => $token,
+            'plain_text' => $plainText,
+        ];
+    }
+
+    public function createSigningSecret(): string
+    {
+        return 'whsec_'.Str::random(40);
+    }
+
+    public function recordSuccess(WebhookDelivery $delivery, int $attemptCount, int $responseCode, string $responseBody): void
+    {
+        $delivery->forceFill([
+            'attempt_count' => $attemptCount,
+            'status' => WebhookDeliveryStatus::Success,
+            'last_attempt_at' => now(),
+            'response_code' => $responseCode,
+            'response_body_snippet' => Str::limit($responseBody, 1000, ''),
+        ])->save();
+    }
+
+    public function recordFailure(WebhookDelivery $delivery, int $attemptCount, ?int $responseCode, ?string $responseBody): void
+    {
+        $delivery->forceFill([
+            'attempt_count' => $attemptCount,
+            'status' => WebhookDeliveryStatus::Failed,
+            'last_attempt_at' => now(),
+            'response_code' => $responseCode,
+            'response_body_snippet' => Str::limit((string) $responseBody, 1000, ''),
+        ])->save();
+
+        $subscription = $delivery->subscription()->first();
+
+        if ($subscription instanceof WebhookSubscription && $this->hasFiveConsecutiveFailures($subscription)) {
+            $subscription->forceFill([
+                'status' => WebhookSubscriptionStatus::Paused,
+            ])->save();
+        }
+    }
+
+    public function hasFiveConsecutiveFailures(WebhookSubscription $subscription): bool
+    {
+        $recentStatuses = $subscription->deliveries()
+            ->latest('id')
+            ->limit(5)
+            ->pluck('status');
+
+        return $recentStatuses->count() === 5
+            && $recentStatuses->every(fn (string $status): bool => $status === WebhookDeliveryStatus::Failed->value);
+    }
+
+    /**
+     * @param  array<string, mixed>  $payload
+     * @return array<string, mixed>
+     */
+    private function envelope(Store $store, WebhookEventType $event, string $eventId, array $payload): array
+    {
+        return [
+            'id' => $eventId,
+            'api_version' => '2026-05',
+            'event_type' => $event->value,
+            'store_id' => $store->getKey(),
+            'occurred_at' => now()->toISOString(),
+            'data' => $payload,
+        ];
+    }
 }
*** Update File: /Users/fabianwesner/Herd/shop/app/Jobs/DeliverWebhook.php
@@
 namespace App\Jobs;

+use App\Enums\WebhookSubscriptionStatus;
+use App\Models\WebhookDelivery;
+use App\Models\WebhookSubscription;
+use App\Services\WebhookService;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Queue\Queueable;
+use Illuminate\Support\Facades\Http;
+use RuntimeException;
+use Throwable;

 class DeliverWebhook implements ShouldQueue
 {
     use Queueable;

-    /**
-     * Create a new job instance.
-     */
-    public function __construct()
-    {
-        //
-    }
+    public int $tries = 6;
+
+    public int $timeout = 15;

-    /**
-     * Execute the job.
+    /**
+     * @param  array<string, mixed>  $payload
      */
-    public function handle(): void
+    public function __construct(
+        public int $deliveryId,
+        public string $eventType,
+        public array $payload,
+    ) {}
+
+    /**
+     * @return list<int>
+     */
+    public function backoff(): array
+    {
+        return [60, 300, 1800, 7200, 43200];
+    }
+
+    public function handle(WebhookService $webhooks): void
     {
-        //
+        $delivery = WebhookDelivery::query()
+            ->with('subscription')
+            ->findOrFail($this->deliveryId);
+        $subscription = $delivery->subscription;
+
+        if (! $subscription instanceof WebhookSubscription || $subscription->status !== WebhookSubscriptionStatus::Active) {
+            return;
+        }
+
+        $attemptCount = max(1, $this->attempts());
+        $body = json_encode($this->payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
+        $timestamp = (string) now()->timestamp;
+        $signature = $webhooks->sign($timestamp.'.'.$body, (string) $subscription->signing_secret_encrypted);
+        $recordedFailure = false;
+
+        try {
+            $response = Http::timeout(5)
+                ->connectTimeout(2)
+                ->withHeaders([
+                    'X-Platform-Signature' => $signature,
+                    'X-Platform-Event' => $this->eventType,
+                    'X-Platform-Delivery-Id' => $delivery->event_id,
+                    'X-Platform-Timestamp' => $timestamp,
+                    'Content-Type' => 'application/json',
+                ])
+                ->withBody($body, 'application/json')
+                ->post($subscription->target_url);
+
+            if ($response->successful()) {
+                $webhooks->recordSuccess($delivery, $attemptCount, $response->status(), $response->body());
+
+                return;
+            }
+
+            $webhooks->recordFailure($delivery, $attemptCount, $response->status(), $response->body());
+            $recordedFailure = true;
+
+            throw new RuntimeException("Webhook delivery failed with HTTP {$response->status()}.");
+        } catch (Throwable $throwable) {
+            if (! $recordedFailure) {
+                $webhooks->recordFailure($delivery, $attemptCount, null, $throwable->getMessage());
+            }
+
+            throw $throwable;
+        }
     }
 }
*** End Patch


Output:
apply_patch verification failed: Failed to find expected lines in /Users/fabianwesner/Herd/shop/app/Services/WebhookService.php:
<?php

namespace App\Services;

class WebhookService
{
    //
}


sed -n '1,80p' app/Services/WebhookService.php


sed -n '1,120p' app/Jobs/DeliverWebhook.php


sed -n '1,80p' app/Services/WebhookService.php

<?php

namespace App\Services;

class WebhookService
{
    /**
     * Create a new class instance.
     */
    public function __construct()
    {
        //
    }
}


sed -n '1,120p' app/Jobs/DeliverWebhook.php

<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class DeliverWebhook implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        //
    }
}


Output:
Chunk ID: e1ff74
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 42
Output:
<?php

namespace App\Services;

class WebhookService
{
    /**
     * Create a new class instance.
     */
    public function __construct()
    {
        //
    }
}


Output:
Chunk ID: 14739f
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 98
Output:
<?php

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class DeliverWebhook implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new job instance.
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        //
    }
}


Input:
*** Begin Patch
*** Update File: /Users/fabianwesner/Herd/shop/app/Services/WebhookService.php
@@
 namespace App\Services;

+use App\Enums\AppInstallationStatus;
+use App\Enums\WebhookDeliveryStatus;
+use App\Enums\WebhookEventType;
+use App\Enums\WebhookSubscriptionStatus;
+use App\Jobs\DeliverWebhook;
+use App\Models\App as AppModel;
+use App\Models\AppInstallation;
+use App\Models\OauthToken;
+use App\Models\Store;
+use App\Models\WebhookDelivery;
+use App\Models\WebhookSubscription;
+use Illuminate\Support\Str;
+use InvalidArgumentException;
+
 class WebhookService
 {
     /**
-     * Create a new class instance.
+     * @param  array<string, mixed>  $payload
      */
-    public function __construct()
+    public function dispatch(Store $store, string $eventType, array $payload): void
     {
-        //
+        $event = WebhookEventType::tryFrom($eventType);
+
+        if (! $event instanceof WebhookEventType) {
+            throw new InvalidArgumentException("Unsupported webhook event type [{$eventType}].");
+        }
+
+        WebhookSubscription::withoutGlobalScopes()
+            ->where('store_id', $store->getKey())
+            ->where('event_type', $event->value)
+            ->where('status', WebhookSubscriptionStatus::Active->value)
+            ->get()
+            ->each(function (WebhookSubscription $subscription) use ($store, $event, $payload): void {
+                $delivery = WebhookDelivery::query()->create([
+                    'subscription_id' => $subscription->getKey(),
+                    'event_id' => (string) Str::uuid(),
+                    'attempt_count' => 1,
+                    'status' => WebhookDeliveryStatus::Pending,
+                ]);
+
+                DeliverWebhook::dispatch(
+                    $delivery->getKey(),
+                    $event->value,
+                    $this->envelope($store, $event, $delivery->event_id, $payload),
+                );
+            });
+    }
+
+    public function sign(string $payload, string $secret): string
+    {
+        return hash_hmac('sha256', $payload, $secret);
+    }
+
+    public function verify(string $payload, string $signature, string $secret): bool
+    {
+        return hash_equals($this->sign($payload, $secret), $signature);
+    }
+
+    /**
+     * @param  list<string>  $abilities
+     * @return array{token: OauthToken, plain_text: string}
+     */
+    public function createApiToken(Store $store, string $name, array $abilities): array
+    {
+        $app = AppModel::query()->firstOrCreate(
+            ['name' => 'Admin API'],
+            [
+                'status' => 'active',
+                'created_at' => now(),
+            ],
+        );
+
+        $installation = AppInstallation::withoutGlobalScopes()->firstOrCreate(
+            [
+                'store_id' => $store->getKey(),
+                'app_id' => $app->getKey(),
+            ],
+            [
+                'scopes_json' => $abilities,
+                'status' => AppInstallationStatus::Active,
+                'installed_at' => now(),
+            ],
+        );
+
+        $plainText = 'shop_'.Str::random(48);
+        $token = OauthToken::query()->create([
+            'installation_id' => $installation->getKey(),
+            'name' => $name,
+            'access_token_hash' => hash('sha256', $plainText),
+            'refresh_token_hash' => null,
+            'abilities_json' => $abilities,
+            'expires_at' => now()->addYear(),
+            'created_at' => now(),
+        ]);
+
+        return [
+            'token' => $token,
+            'plain_text' => $plainText,
+        ];
+    }
+
+    public function createSigningSecret(): string
+    {
+        return 'whsec_'.Str::random(40);
+    }
+
+    public function recordSuccess(WebhookDelivery $delivery, int $attemptCount, int $responseCode, string $responseBody): void
+    {
+        $delivery->forceFill([
+            'attempt_count' => $attemptCount,
+            'status' => WebhookDeliveryStatus::Success,
+            'last_attempt_at' => now(),
+            'response_code' => $responseCode,
+            'response_body_snippet' => Str::limit($responseBody, 1000, ''),
+        ])->save();
+    }
+
+    public function recordFailure(WebhookDelivery $delivery, int $attemptCount, ?int $responseCode, ?string $responseBody): void
+    {
+        $delivery->forceFill([
+            'attempt_count' => $attemptCount,
+            'status' => WebhookDeliveryStatus::Failed,
+            'last_attempt_at' => now(),
+            'response_code' => $responseCode,
+            'response_body_snippet' => Str::limit((string) $responseBody, 1000, ''),
+        ])->save();
+
+        $subscription = $delivery->subscription()->first();
+
+        if ($subscription instanceof WebhookSubscription && $this->hasFiveConsecutiveFailures($subscription)) {
+            $subscription->forceFill([
+                'status' => WebhookSubscriptionStatus::Paused,
+            ])->save();
+        }
+    }
+
+    public function hasFiveConsecutiveFailures(WebhookSubscription $subscription): bool
+    {
+        $recentStatuses = $subscription->deliveries()
+            ->latest('id')
+            ->limit(5)
+            ->pluck('status');
+
+        return $recentStatuses->count() === 5
+            && $recentStatuses->every(fn (string $status): bool => $status === WebhookDeliveryStatus::Failed->value);
+    }
+
+    /**
+     * @param  array<string, mixed>  $payload
+     * @return array<string, mixed>
+     */
+    private function envelope(Store $store, WebhookEventType $event, string $eventId, array $payload): array
+    {
+        return [
+            'id' => $eventId,
+            'api_version' => '2026-05',
+            'event_type' => $event->value,
+            'store_id' => $store->getKey(),
+            'occurred_at' => now()->toISOString(),
+            'data' => $payload,
+        ];
     }
 }
*** Update File: /Users/fabianwesner/Herd/shop/app/Jobs/DeliverWebhook.php
@@
 namespace App\Jobs;

+use App\Enums\WebhookSubscriptionStatus;
+use App\Models\WebhookDelivery;
+use App\Models\WebhookSubscription;
+use App\Services\WebhookService;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Queue\Queueable;
+use Illuminate\Support\Facades\Http;
+use RuntimeException;
+use Throwable;

 class DeliverWebhook implements ShouldQueue
 {
     use Queueable;

+    public int $tries = 6;
+
+    public int $timeout = 15;
+
     /**
-     * Create a new job instance.
+     * @param  array<string, mixed>  $payload
      */
-    public function __construct()
-    {
-        //
-    }
+    public function __construct(
+        public int $deliveryId,
+        public string $eventType,
+        public array $payload,
+    ) {}
+
+    /**
+     * @return list<int>
+     */
+    public function backoff(): array
+    {
+        return [60, 300, 1800, 7200, 43200];
+    }

     /**
      * Execute the job.
      */
-    public function handle(): void
+    public function handle(WebhookService $webhooks): void
     {
-        //
+        $delivery = WebhookDelivery::query()
+            ->with('subscription')
+            ->findOrFail($this->deliveryId);
+        $subscription = $delivery->subscription;
+
+        if (! $subscription instanceof WebhookSubscription || $subscription->status !== WebhookSubscriptionStatus::Active) {
+            return;
+        }
+
+        $attemptCount = max(1, $this->attempts());
+        $body = json_encode($this->payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
+        $timestamp = (string) now()->timestamp;
+        $signature = $webhooks->sign($timestamp.'.'.$body, (string) $subscription->signing_secret_encrypted);
+        $recordedFailure = false;
+
+        try {
+            $response = Http::timeout(5)
+                ->connectTimeout(2)
+                ->withHeaders([
+                    'X-Platform-Signature' => $signature,
+                    'X-Platform-Event' => $this->eventType,
+                    'X-Platform-Delivery-Id' => $delivery->event_id,
+                    'X-Platform-Timestamp' => $timestamp,
+                    'Content-Type' => 'application/json',
+                ])
+                ->withBody($body, 'application/json')
+                ->post($subscription->target_url);
+
+            if ($response->successful()) {
+                $webhooks->recordSuccess($delivery, $attemptCount, $response->status(), $response->body());
+
+                return;
+            }
+
+            $webhooks->recordFailure($delivery, $attemptCount, $response->status(), $response->body());
+            $recordedFailure = true;
+
+            throw new RuntimeException("Webhook delivery failed with HTTP {$response->status()}.");
+        } catch (Throwable $throwable) {
+            if (! $recordedFailure) {
+                $webhooks->recordFailure($delivery, $attemptCount, null, $throwable->getMessage());
+            }
+
+            throw $throwable;
+        }
     }
 }
*** End Patch


Success. Updated the following files:
M /Users/fabianwesner/Herd/shop/app/Services/WebhookService.php
M /Users/fabianwesner/Herd/shop/app/Jobs/DeliverWebhook.php

@@ -4,4 +4,11 @@

+use App\Enums\WebhookSubscriptionStatus;
+use App\Models\WebhookDelivery;
+use App\Models\WebhookSubscription;
+use App\Services\WebhookService;
 use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Foundation\Queue\Queueable;
+use Illuminate\Support\Facades\Http;
+use RuntimeException;
+use Throwable;

@@ -11,8 +18,21 @@

+    public int $tries = 6;
+
+    public int $timeout = 15;
+
     /**
-     * Create a new job instance.
+     * @param  array<string, mixed>  $payload
      */
-    public function __construct()
+    public function __construct(
+        public int $deliveryId,
+        public string $eventType,
+        public array $payload,
+    ) {}
+
+    /**
+     * @return list<int>
+     */
+    public function backoff(): array
     {
-        //
+        return [60, 300, 1800, 7200, 43200];
     }
@@ -22,5 +42,49 @@
      */
-    public function handle(): void
+    public function handle(WebhookService $webhooks): void
     {
-        //
+        $delivery = WebhookDelivery::query()
+            ->with('subscription')
+            ->findOrFail($this->deliveryId);
+        $subscription = $delivery->subscription;
+
+        if (! $subscription instanceof WebhookSubscription || $subscription->status !== WebhookSubscriptionStatus::Active) {
+            return;
+        }
+
+        $attemptCount = max(1, $this->attempts());
+        $body = json_encode($this->payload, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES);
+        $timestamp = (string) now()->timestamp;
+        $signature = $webhooks->sign($timestamp.'.'.$body, (string) $subscription->signing_secret_encrypted);
+        $recordedFailure = false;
+
+        try {
+            $response = Http::timeout(5)
+                ->connectTimeout(2)
+                ->withHeaders([
+                    'X-Platform-Signature' => $signature,
+                    'X-Platform-Event' => $this->eventType,
+                    'X-Platform-Delivery-Id' => $delivery->event_id,
+                    'X-Platform-Timestamp' => $timestamp,
+                    'Content-Type' => 'application/json',
+                ])
+                ->withBody($body, 'application/json')
+                ->post($subscription->target_url);
+
+            if ($response->successful()) {
+                $webhooks->recordSuccess($delivery, $attemptCount, $response->status(), $response->body());
+
+                return;
+            }
+
+            $webhooks->recordFailure($delivery, $attemptCount, $response->status(), $response->body());
+            $recordedFailure = true;
+
+            throw new RuntimeException("Webhook delivery failed with HTTP {$response->status()}.");
+        } catch (Throwable $throwable) {
+            if (! $recordedFailure) {
+                $webhooks->recordFailure($delivery, $attemptCount, null, $throwable->getMessage());
+            }
+
+            throw $throwable;
+        }
     }

@@ -4,2 +4,16 @@

+use App\Enums\AppInstallationStatus;
+use App\Enums\WebhookDeliveryStatus;
+use App\Enums\WebhookEventType;
+use App\Enums\WebhookSubscriptionStatus;
+use App\Jobs\DeliverWebhook;
+use App\Models\App as AppModel;
+use App\Models\AppInstallation;
+use App\Models\OauthToken;
+use App\Models\Store;
+use App\Models\WebhookDelivery;
+use App\Models\WebhookSubscription;
+use Illuminate\Support\Str;
+use InvalidArgumentException;
+
 class WebhookService
@@ -7,7 +21,146 @@
     /**
-     * Create a new class instance.
+     * @param  array<string, mixed>  $payload
+     */
+    public function dispatch(Store $store, string $eventType, array $payload): void
+    {
+        $event = WebhookEventType::tryFrom($eventType);
+
+        if (! $event instanceof WebhookEventType) {
+            throw new InvalidArgumentException("Unsupported webhook event type [{$eventType}].");
+        }
+
+        WebhookSubscription::withoutGlobalScopes()
+            ->where('store_id', $store->getKey())
+            ->where('event_type', $event->value)
+            ->where('status', WebhookSubscriptionStatus::Active->value)
+            ->get()
+            ->each(function (WebhookSubscription $subscription) use ($store, $event, $payload): void {
+                $delivery = WebhookDelivery::query()->create([
+                    'subscription_id' => $subscription->getKey(),
+                    'event_id' => (string) Str::uuid(),
+                    'attempt_count' => 1,
+                    'status' => WebhookDeliveryStatus::Pending,
+                ]);
+
+                DeliverWebhook::dispatch(
+                    $delivery->getKey(),
+                    $event->value,
+                    $this->envelope($store, $event, $delivery->event_id, $payload),
+                );
+            });
+    }
+
+    public function sign(string $payload, string $secret): string
+    {
+        return hash_hmac('sha256', $payload, $secret);
+    }
+
+    public function verify(string $payload, string $signature, string $secret): bool
+    {
+        return hash_equals($this->sign($payload, $secret), $signature);
+    }
+
+    /**
+     * @param  list<string>  $abilities
+     * @return array{token: OauthToken, plain_text: string}
      */
-    public function __construct()
+    public function createApiToken(Store $store, string $name, array $abilities): array
     {
-        //
+        $app = AppModel::query()->firstOrCreate(
+            ['name' => 'Admin API'],
+            [
+                'status' => 'active',
+                'created_at' => now(),
+            ],
+        );
+
+        $installation = AppInstallation::withoutGlobalScopes()->firstOrCreate(
+            [
+                'store_id' => $store->getKey(),
+                'app_id' => $app->getKey(),
+            ],
+            [
+                'scopes_json' => $abilities,
+                'status' => AppInstallationStatus::Active,
+                'installed_at' => now(),
+            ],
+        );
+
+        $plainText = 'shop_'.Str::random(48);
+        $token = OauthToken::query()->create([
+            'installation_id' => $installation->getKey(),
+            'name' => $name,
+            'access_token_hash' => hash('sha256', $plainText),
+            'refresh_token_hash' => null,
+            'abilities_json' => $abilities,
+            'expires_at' => now()->addYear(),
+            'created_at' => now(),
+        ]);
+
+        return [
+            'token' => $token,
+            'plain_text' => $plainText,
+        ];
+    }
+
+    public function createSigningSecret(): string
+    {
+        return 'whsec_'.Str::random(40);
+    }
+
+    public function recordSuccess(WebhookDelivery $delivery, int $attemptCount, int $responseCode, string $responseBody): void
+    {
+        $delivery->forceFill([
+            'attempt_count' => $attemptCount,
+            'status' => WebhookDeliveryStatus::Success,
+            'last_attempt_at' => now(),
+            'response_code' => $responseCode,
+            'response_body_snippet' => Str::limit($responseBody, 1000, ''),
+        ])->save();
+    }
+
+    public function recordFailure(WebhookDelivery $delivery, int $attemptCount, ?int $responseCode, ?string $responseBody): void
+    {
+        $delivery->forceFill([
+            'attempt_count' => $attemptCount,
+            'status' => WebhookDeliveryStatus::Failed,
+            'last_attempt_at' => now(),
+            'response_code' => $responseCode,
+            'response_body_snippet' => Str::limit((string) $responseBody, 1000, ''),
+        ])->save();
+
+        $subscription = $delivery->subscription()->first();
+
+        if ($subscription instanceof WebhookSubscription && $this->hasFiveConsecutiveFailures($subscription)) {
+            $subscription->forceFill([
+                'status' => WebhookSubscriptionStatus::Paused,
+            ])->save();
+        }
+    }
+
+    public function hasFiveConsecutiveFailures(WebhookSubscription $subscription): bool
+    {
+        $recentStatuses = $subscription->deliveries()
+            ->latest('id')
+            ->limit(5)
+            ->pluck('status');
+
+        return $recentStatuses->count() === 5
+            && $recentStatuses->every(fn (string $status): bool => $status === WebhookDeliveryStatus::Failed->value);
+    }
+
+    /**
+     * @param  array<string, mixed>  $payload
+     * @return array<string, mixed>
+     */
+    private function envelope(Store $store, WebhookEventType $event, string $eventId, array $payload): array
+    {
+        return [
+            'id' => $eventId,
+            'api_version' => '2026-05',
+            'event_type' => $event->value,
+            'store_id' => $store->getKey(),
+            'occurred_at' => now()->toISOString(),
+            'data' => $payload,
+        ];
     }


Output: {"output":"Success. Updated the following files:\nM /Users/fabianwesner/Herd/shop/app/Services/WebhookService.php\nM /Users/fabianwesner/Herd/shop/app/Jobs/DeliverWebhook.php\n","metadata":{"exit_code":0,"duration_seconds":0.0}}


sed -n '1,100p' app/Listeners/DispatchWebhooks.php


sed -n '1,100p' app/Listeners/DispatchWebhooks.php

<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class DispatchWebhooks
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(object $event): void
    {
        //
    }
}


Output:
Chunk ID: ddb86c
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 92
Output:
<?php

namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class DispatchWebhooks
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(object $event): void
    {
        //
    }
}


sed -n '1,80p' app/Enums/ProductStatus.php


sed -n '1,80p' app/Enums/ProductStatus.php

<?php

namespace App\Enums;

enum ProductStatus: string
{
    case Draft = 'draft';
    case Active = 'active';
    case Archived = 'archived';
}


Output:
Chunk ID: 1e6824
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 37
Output:
<?php

namespace App\Enums;

enum ProductStatus: string
{
    case Draft = 'draft';
    case Active = 'active';
    case Archived = 'archived';
}


Input:
*** Begin Patch
*** Update File: /Users/fabianwesner/Herd/shop/app/Listeners/DispatchWebhooks.php
@@
 namespace App\Listeners;

-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Queue\InteractsWithQueue;
+use App\Enums\ProductStatus;
+use App\Enums\WebhookEventType;
+use App\Events\FulfillmentCreated;
+use App\Events\FulfillmentDelivered;
+use App\Events\FulfillmentShipped;
+use App\Events\OrderCancelled;
+use App\Events\OrderCreated;
+use App\Events\OrderPaid;
+use App\Events\OrderRefunded;
+use App\Events\ProductStatusChanged;
+use App\Models\Fulfillment;
+use App\Models\Order;
+use App\Models\Product;
+use App\Models\Store;
+use App\Services\WebhookService;

 class DispatchWebhooks
 {
-    /**
-     * Create the event listener.
-     */
-    public function __construct()
-    {
-        //
-    }
+    public function __construct(private WebhookService $webhooks) {}

     /**
      * Handle the event.
      */
     public function handle(object $event): void
     {
-        //
+        match (true) {
+            $event instanceof OrderCreated => $this->dispatchOrder($event->order, WebhookEventType::OrderCreated),
+            $event instanceof OrderPaid => $this->dispatchOrder($event->order, WebhookEventType::OrderPaid),
+            $event instanceof OrderCancelled => $this->dispatchOrder($event->order, WebhookEventType::OrderCancelled),
+            $event instanceof OrderRefunded => $this->dispatchRefund($event),
+            $event instanceof FulfillmentCreated => $this->dispatchFulfillment($event->fulfillment, WebhookEventType::FulfillmentCreated),
+            $event instanceof FulfillmentShipped,
+            $event instanceof FulfillmentDelivered => $this->dispatchFulfillment($event->fulfillment, WebhookEventType::OrderFulfilled),
+            $event instanceof ProductStatusChanged => $this->dispatchProductStatusChange($event),
+            default => null,
+        };
+    }
+
+    private function dispatchRefund(OrderRefunded $event): void
+    {
+        $payload = $this->orderPayload($event->order);
+        $payload['refund'] = [
+            'id' => $event->refund->getKey(),
+            'amount' => $event->refund->amount,
+            'reason' => $event->refund->reason,
+            'status' => $event->refund->status?->value,
+        ];
+
+        $store = $this->store($event->order->store_id);
+
+        $this->webhooks->dispatch($store, WebhookEventType::OrderRefunded->value, $payload);
+        $this->webhooks->dispatch($store, WebhookEventType::RefundCreated->value, $payload);
+    }
+
+    private function dispatchOrder(Order $order, WebhookEventType $eventType): void
+    {
+        $this->webhooks->dispatch($this->store($order->store_id), $eventType->value, $this->orderPayload($order));
+    }
+
+    private function dispatchFulfillment(Fulfillment $fulfillment, WebhookEventType $eventType): void
+    {
+        $order = $fulfillment->order()->withoutGlobalScopes()->firstOrFail();
+
+        $payload = $this->orderPayload($order);
+        $payload['fulfillment'] = [
+            'id' => $fulfillment->getKey(),
+            'status' => $fulfillment->status?->value,
+            'tracking_company' => $fulfillment->tracking_company,
+            'tracking_number' => $fulfillment->tracking_number,
+            'tracking_url' => $fulfillment->tracking_url,
+        ];
+
+        $this->webhooks->dispatch($this->store($order->store_id), $eventType->value, $payload);
+    }
+
+    private function dispatchProductStatusChange(ProductStatusChanged $event): void
+    {
+        $eventType = $event->to === ProductStatus::Archived
+            ? WebhookEventType::ProductDeleted
+            : WebhookEventType::ProductUpdated;
+
+        $this->webhooks->dispatch($this->store($event->product->store_id), $eventType->value, $this->productPayload($event->product));
+    }
+
+    /**
+     * @return array<string, mixed>
+     */
+    private function orderPayload(Order $order): array
+    {
+        return [
+            'order' => [
+                'id' => $order->getKey(),
+                'order_number' => $order->order_number,
+                'status' => $order->status?->value,
+                'financial_status' => $order->financial_status?->value,
+                'fulfillment_status' => $order->fulfillment_status?->value,
+                'currency' => $order->currency,
+                'total_amount' => $order->total_amount,
+                'placed_at' => $order->placed_at?->toISOString(),
+            ],
+        ];
+    }
+
+    /**
+     * @return array<string, mixed>
+     */
+    private function productPayload(Product $product): array
+    {
+        return [
+            'product' => [
+                'id' => $product->getKey(),
+                'title' => $product->title,
+                'handle' => $product->handle,
+                'status' => $product->status?->value,
+                'updated_at' => $product->updated_at?->toISOString(),
+            ],
+        ];
+    }
+
+    private function store(int $storeId): Store
+    {
+        return Store::query()->findOrFail($storeId);
     }
 }
*** End Patch


Success. Updated the following files:
M /Users/fabianwesner/Herd/shop/app/Listeners/DispatchWebhooks.php

@@ -4,4 +4,17 @@

-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Queue\InteractsWithQueue;
+use App\Enums\ProductStatus;
+use App\Enums\WebhookEventType;
+use App\Events\FulfillmentCreated;
+use App\Events\FulfillmentDelivered;
+use App\Events\FulfillmentShipped;
+use App\Events\OrderCancelled;
+use App\Events\OrderCreated;
+use App\Events\OrderPaid;
+use App\Events\OrderRefunded;
+use App\Events\ProductStatusChanged;
+use App\Models\Fulfillment;
+use App\Models\Order;
+use App\Models\Product;
+use App\Models\Store;
+use App\Services\WebhookService;

@@ -9,16 +22,106 @@
 {
+    public function __construct(private WebhookService $webhooks) {}
+
     /**
-     * Create the event listener.
+     * Handle the event.
      */
-    public function __construct()
+    public function handle(object $event): void
+    {
+        match (true) {
+            $event instanceof OrderCreated => $this->dispatchOrder($event->order, WebhookEventType::OrderCreated),
+            $event instanceof OrderPaid => $this->dispatchOrder($event->order, WebhookEventType::OrderPaid),
+            $event instanceof OrderCancelled => $this->dispatchOrder($event->order, WebhookEventType::OrderCancelled),
+            $event instanceof OrderRefunded => $this->dispatchRefund($event),
+            $event instanceof FulfillmentCreated => $this->dispatchFulfillment($event->fulfillment, WebhookEventType::FulfillmentCreated),
+            $event instanceof FulfillmentShipped,
+            $event instanceof FulfillmentDelivered => $this->dispatchFulfillment($event->fulfillment, WebhookEventType::OrderFulfilled),
+            $event instanceof ProductStatusChanged => $this->dispatchProductStatusChange($event),
+            default => null,
+        };
+    }
+
+    private function dispatchRefund(OrderRefunded $event): void
+    {
+        $payload = $this->orderPayload($event->order);
+        $payload['refund'] = [
+            'id' => $event->refund->getKey(),
+            'amount' => $event->refund->amount,
+            'reason' => $event->refund->reason,
+            'status' => $event->refund->status?->value,
+        ];
+
+        $store = $this->store($event->order->store_id);
+
+        $this->webhooks->dispatch($store, WebhookEventType::OrderRefunded->value, $payload);
+        $this->webhooks->dispatch($store, WebhookEventType::RefundCreated->value, $payload);
+    }
+
+    private function dispatchOrder(Order $order, WebhookEventType $eventType): void
     {
-        //
+        $this->webhooks->dispatch($this->store($order->store_id), $eventType->value, $this->orderPayload($order));
     }

+    private function dispatchFulfillment(Fulfillment $fulfillment, WebhookEventType $eventType): void
+    {
+        $order = $fulfillment->order()->withoutGlobalScopes()->firstOrFail();
+
+        $payload = $this->orderPayload($order);
+        $payload['fulfillment'] = [
+            'id' => $fulfillment->getKey(),
+            'status' => $fulfillment->status?->value,
+            'tracking_company' => $fulfillment->tracking_company,
+            'tracking_number' => $fulfillment->tracking_number,
+            'tracking_url' => $fulfillment->tracking_url,
+        ];
+
+        $this->webhooks->dispatch($this->store($order->store_id), $eventType->value, $payload);
+    }
+
+    private function dispatchProductStatusChange(ProductStatusChanged $event): void
+    {
+        $eventType = $event->to === ProductStatus::Archived
+            ? WebhookEventType::ProductDeleted
+            : WebhookEventType::ProductUpdated;
+
+        $this->webhooks->dispatch($this->store($event->product->store_id), $eventType->value, $this->productPayload($event->product));
+    }
+
     /**
-     * Handle the event.
+     * @return array<string, mixed>
      */
-    public function handle(object $event): void
+    private function orderPayload(Order $order): array
     {
-        //
+        return [
+            'order' => [
+                'id' => $order->getKey(),
+                'order_number' => $order->order_number,
+                'status' => $order->status?->value,
+                'financial_status' => $order->financial_status?->value,
+                'fulfillment_status' => $order->fulfillment_status?->value,
+                'currency' => $order->currency,
+                'total_amount' => $order->total_amount,
+                'placed_at' => $order->placed_at?->toISOString(),
+            ],
+        ];
+    }
+
+    /**
+     * @return array<string, mixed>
+     */
+    private function productPayload(Product $product): array
+    {
+        return [
+            'product' => [
+                'id' => $product->getKey(),
+                'title' => $product->title,
+                'handle' => $product->handle,
+                'status' => $product->status?->value,
+                'updated_at' => $product->updated_at?->toISOString(),
+            ],
+        ];
+    }
+
+    private function store(int $storeId): Store
+    {
+        return Store::query()->findOrFail($storeId);
     }


Output: {"output":"Success. Updated the following files:\nM /Users/fabianwesner/Herd/shop/app/Listeners/DispatchWebhooks.php\n","metadata":{"exit_code":0,"duration_seconds":0.0}}


Input:
*** Begin Patch
*** Update File: /Users/fabianwesner/Herd/shop/app/Providers/AppServiceProvider.php
@@
 use App\Auth\CustomerUserProvider;
 use App\Contracts\PaymentProvider;
+use App\Events\FulfillmentCreated;
+use App\Events\FulfillmentDelivered;
+use App\Events\FulfillmentShipped;
+use App\Events\OrderCancelled;
+use App\Events\OrderCreated;
+use App\Events\OrderPaid;
+use App\Events\OrderRefunded;
+use App\Events\ProductStatusChanged;
 use App\Http\Middleware\CheckStoreRole;
 use App\Http\Middleware\EnsureUserEmailIsVerified;
 use App\Http\Middleware\ResolveStore;
+use App\Listeners\DispatchWebhooks;
 use App\Models\Product;
 use App\Models\Store;
 use App\Observers\ProductObserver;
 use App\Services\AnalyticsService;
 use App\Services\NavigationService;
@@
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Date;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Event;
 use Illuminate\Support\Facades\RateLimiter;
 use Illuminate\Support\Facades\View;
 use Illuminate\Support\ServiceProvider;
@@
 use App\Services\SearchService;
 use App\Services\ThemeSettingsService;
+use App\Services\WebhookService;
@@
         $this->app->singleton(NavigationService::class);
         $this->app->singleton(SearchService::class);
         $this->app->singleton(AnalyticsService::class);
+        $this->app->singleton(WebhookService::class);
@@
         $this->configureDefaults();
+        $this->configureEventListeners();
         $this->configureLivewireMiddleware();
         $this->configureStorefrontViewData();
@@
         RateLimiter::for('analytics', function (Request $request): Limit {
             return Limit::perMinute(60)->by($request->ip());
         });
+
+        RateLimiter::for('webhooks', function (Request $request): Limit {
+            return Limit::perMinute(100)->by($request->ip());
+        });
@@
     protected function configureStorefrontViewData(): void
@@
     }
+
+    protected function configureEventListeners(): void
+    {
+        foreach ([
+            OrderCreated::class,
+            OrderPaid::class,
+            OrderCancelled::class,
+            OrderRefunded::class,
+            FulfillmentCreated::class,
+            FulfillmentShipped::class,
+            FulfillmentDelivered::class,
+            ProductStatusChanged::class,
+        ] as $event) {
+            Event::listen($event, DispatchWebhooks::class);
+        }
+    }

     protected function configureLivewireMiddleware(): void
*** End Patch


Output:
apply_patch verification failed: Failed to find expected lines in /Users/fabianwesner/Herd/shop/app/Providers/AppServiceProvider.php:
use App\Services\SearchService;
use App\Services\ThemeSettingsService;


sed -n '1,70p' app/Providers/AppServiceProvider.php


sed -n '1,70p' app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use App\Auth\CustomerUserProvider;
use App\Contracts\PaymentProvider;
use App\Http\Middleware\CheckStoreRole;
use App\Http\Middleware\EnsureUserEmailIsVerified;
use App\Http\Middleware\ResolveStore;
use App\Models\Product;
use App\Models\Store;
use App\Observers\ProductObserver;
use App\Services\AnalyticsService;
use App\Services\NavigationService;
use App\Services\Payments\MockPaymentProvider;
use App\Services\SearchService;
use App\Services\ThemeSettingsService;
use Carbon\CarbonImmutable;
use Illuminate\Auth\Middleware\Authenticate;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rules\Password;
use Illuminate\View\View as ViewInstance;
use Livewire\Livewire;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->bind(PaymentProvider::class, MockPaymentProvider::class);

        Auth::provider('store_scoped_eloquent', function ($app, array $config): CustomerUserProvider {
            return new CustomerUserProvider($app['hash'], $config['model']);
        });

        $this->app->singleton(ThemeSettingsService::class);
        $this->app->singleton(NavigationService::class);
        $this->app->singleton(SearchService::class);
        $this->app->singleton(AnalyticsService::class);
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        $this->configureDefaults();
        $this->configureLivewireMiddleware();
        $this->configureStorefrontViewData();
    }

    /**
     * Configure default behaviors for production-ready applications.
     */
    protected function configureDefaults(): void
    {
        Date::use(CarbonImmutable::class);

        DB::prohibitDestructiveCommands(
            app()->isProduction(),
        );


Output:
Chunk ID: 88cd2d
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 537
Output:
<?php

namespace App\Providers;

use App\Auth\CustomerUserProvider;
use App\Contracts\PaymentProvider;
use App\Http\Middleware\CheckStoreRole;
use App\Http\Middleware\EnsureUserEmailIsVerified;
use App\Http\Middleware\ResolveStore;
use App\Models\Product;
use App\Models\Store;
use App\Observers\ProductObserver;
use App\Services\AnalyticsService;
use App\Services\NavigationService;
use App\Services\Payments\MockPaymentProvider;
use App\Services\SearchService;
use App\Services\ThemeSettingsService;
use Carbon\CarbonImmutable;
use Illuminate\Auth\Middleware\Authenticate;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rules\Password;
use Illuminate\View\View as ViewInstance;
use Livewire\Livewire;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->app->bind(PaymentProvider::class, MockPaymentProvider::class);

        Auth::provider('store_scoped_eloquent', function ($app, array $config): CustomerUserProvider {
            return new CustomerUserProvider($app['hash'], $config['model']);
        });

        $this->app->singleton(ThemeSettingsService::class);
        $this->app->singleton(NavigationService::class);
        $this->app->singleton(SearchService::class);
        $this->app->singleton(AnalyticsService::class);
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        $this->configureDefaults();
        $this->configureLivewireMiddleware();
        $this->configureStorefrontViewData();
    }

    /**
     * Configure default behaviors for production-ready applications.
     */
    protected function configureDefaults(): void
    {
        Date::use(CarbonImmutable::class);

        DB::prohibitDestructiveCommands(
            app()->isProduction(),
        );


sed -n '70,180p' app/Providers/AppServiceProvider.php


sed -n '70,180p' app/Providers/AppServiceProvider.php

        );

        Password::defaults(fn (): ?Password => app()->isProduction()
            ? Password::min(12)
                ->mixedCase()
                ->letters()
                ->numbers()
                ->symbols()
                ->uncompromised()
            : null
        );

        RateLimiter::for('api.storefront', function (Request $request): Limit {
            return Limit::perMinute(120)->by($request->ip());
        });

        RateLimiter::for('checkout', function (Request $request): Limit {
            $sessionId = $request->hasSession() ? $request->session()->getId() : null;

            return Limit::perMinute(10)->by($sessionId ?: $request->ip());
        });

        RateLimiter::for('search', function (Request $request): Limit {
            return Limit::perMinute(30)->by($request->ip());
        });

        RateLimiter::for('analytics', function (Request $request): Limit {
            return Limit::perMinute(60)->by($request->ip());
        });

        Product::observe(ProductObserver::class);

        Authenticate::redirectUsing(function (Request $request): string {
            if ($request->is('admin*')) {
                return route('admin.login');
            }

            if ($request->is('account*')) {
                return route('account.login');
            }

            return route('login');
        });
    }

    protected function configureStorefrontViewData(): void
    {
        View::composer('layouts.storefront', function (ViewInstance $view): void {
            $store = app()->bound('current_store') ? app('current_store') : null;

            if (! $store instanceof Store) {
                $view->with([
                    'themeSettings' => [],
                    'mainNavigation' => [],
                    'footerNavigation' => [],
                ]);

                return;
            }

            $themeSettings = app(ThemeSettingsService::class)->forStore($store);
            $navigation = app(NavigationService::class);

            $view->with([
                'themeSettings' => $themeSettings,
                'mainNavigation' => $navigation->forHandle($store, data_get($themeSettings, 'header.main_menu', 'main-menu')),
                'footerNavigation' => $navigation->forHandle($store, data_get($themeSettings, 'footer.menu', 'footer-menu')),
            ]);
        });
    }

    protected function configureLivewireMiddleware(): void
    {
        Livewire::addPersistentMiddleware([
            EnsureUserEmailIsVerified::class,
            ResolveStore::class,
            CheckStoreRole::class,
        ]);
    }
}


Output:
Chunk ID: 5ce765
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 657
Output:
        );

        Password::defaults(fn (): ?Password => app()->isProduction()
            ? Password::min(12)
                ->mixedCase()
                ->letters()
                ->numbers()
                ->symbols()
                ->uncompromised()
            : null
        );

        RateLimiter::for('api.storefront', function (Request $request): Limit {
            return Limit::perMinute(120)->by($request->ip());
        });

        RateLimiter::for('checkout', function (Request $request): Limit {
            $sessionId = $request->hasSession() ? $request->session()->getId() : null;

            return Limit::perMinute(10)->by($sessionId ?: $request->ip());
        });

        RateLimiter::for('search', function (Request $request): Limit {
            return Limit::perMinute(30)->by($request->ip());
        });

        RateLimiter::for('analytics', function (Request $request): Limit {
            return Limit::perMinute(60)->by($request->ip());
        });

        Product::observe(ProductObserver::class);

        Authenticate::redirectUsing(function (Request $request): string {
            if ($request->is('admin*')) {
                return route('admin.login');
            }

            if ($request->is('account*')) {
                return route('account.login');
            }

            return route('login');
        });
    }

    protected function configureStorefrontViewData(): void
    {
        View::composer('layouts.storefront', function (ViewInstance $view): void {
            $store = app()->bound('current_store') ? app('current_store') : null;

            if (! $store instanceof Store) {
                $view->with([
                    'themeSettings' => [],
                    'mainNavigation' => [],
                    'footerNavigation' => [],
                ]);

                return;
            }

            $themeSettings = app(ThemeSettingsService::class)->forStore($store);
            $navigation = app(NavigationService::class);

            $view->with([
                'themeSettings' => $themeSettings,
                'mainNavigation' => $navigation->forHandle($store, data_get($themeSettings, 'header.main_menu', 'main-menu')),
                'footerNavigation' => $navigation->forHandle($store, data_get($themeSettings, 'footer.menu', 'footer-menu')),
            ]);
        });
    }

    protected function configureLivewireMiddleware(): void
    {
        Livewire::addPersistentMiddleware([
            EnsureUserEmailIsVerified::class,
            ResolveStore::class,
            CheckStoreRole::class,
        ]);
    }
}


Input:
*** Begin Patch
*** Update File: /Users/fabianwesner/Herd/shop/app/Providers/AppServiceProvider.php
@@
 use App\Auth\CustomerUserProvider;
 use App\Contracts\PaymentProvider;
+use App\Events\FulfillmentCreated;
+use App\Events\FulfillmentDelivered;
+use App\Events\FulfillmentShipped;
+use App\Events\OrderCancelled;
+use App\Events\OrderCreated;
+use App\Events\OrderPaid;
+use App\Events\OrderRefunded;
+use App\Events\ProductStatusChanged;
 use App\Http\Middleware\CheckStoreRole;
 use App\Http\Middleware\EnsureUserEmailIsVerified;
 use App\Http\Middleware\ResolveStore;
+use App\Listeners\DispatchWebhooks;
 use App\Models\Product;
@@
 use App\Services\Payments\MockPaymentProvider;
 use App\Services\SearchService;
 use App\Services\ThemeSettingsService;
+use App\Services\WebhookService;
@@
 use Illuminate\Support\Facades\Auth;
 use Illuminate\Support\Facades\Date;
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Event;
 use Illuminate\Support\Facades\RateLimiter;
@@
         $this->app->singleton(NavigationService::class);
         $this->app->singleton(SearchService::class);
         $this->app->singleton(AnalyticsService::class);
+        $this->app->singleton(WebhookService::class);
@@
     public function boot(): void
     {
         $this->configureDefaults();
+        $this->configureEventListeners();
         $this->configureLivewireMiddleware();
         $this->configureStorefrontViewData();
@@
         RateLimiter::for('analytics', function (Request $request): Limit {
             return Limit::perMinute(60)->by($request->ip());
         });
+
+        RateLimiter::for('webhooks', function (Request $request): Limit {
+            return Limit::perMinute(100)->by($request->ip());
+        });
@@
     protected function configureStorefrontViewData(): void
@@
         });
     }
+
+    protected function configureEventListeners(): void
+    {
+        foreach ([
+            OrderCreated::class,
+            OrderPaid::class,
+            OrderCancelled::class,
+            OrderRefunded::class,
+            FulfillmentCreated::class,
+            FulfillmentShipped::class,
+            FulfillmentDelivered::class,
+            ProductStatusChanged::class,
+        ] as $event) {
+            Event::listen($event, DispatchWebhooks::class);
+        }
+    }

     protected function configureLivewireMiddleware(): void
*** End Patch


Success. Updated the following files:
M /Users/fabianwesner/Herd/shop/app/Providers/AppServiceProvider.php

@@ -6,2 +6,10 @@
 use App\Contracts\PaymentProvider;
+use App\Events\FulfillmentCreated;
+use App\Events\FulfillmentDelivered;
+use App\Events\FulfillmentShipped;
+use App\Events\OrderCancelled;
+use App\Events\OrderCreated;
+use App\Events\OrderPaid;
+use App\Events\OrderRefunded;
+use App\Events\ProductStatusChanged;
 use App\Http\Middleware\CheckStoreRole;
@@ -9,2 +17,3 @@
 use App\Http\Middleware\ResolveStore;
+use App\Listeners\DispatchWebhooks;
 use App\Models\Product;
@@ -17,2 +26,3 @@
 use App\Services\ThemeSettingsService;
+use App\Services\WebhookService;
 use Carbon\CarbonImmutable;
@@ -24,2 +34,3 @@
 use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Event;
 use Illuminate\Support\Facades\RateLimiter;
@@ -48,2 +59,3 @@
         $this->app->singleton(AnalyticsService::class);
+        $this->app->singleton(WebhookService::class);
     }
@@ -56,2 +68,3 @@
         $this->configureDefaults();
+        $this->configureEventListeners();
         $this->configureLivewireMiddleware();
@@ -99,2 +112,6 @@

+        RateLimiter::for('webhooks', function (Request $request): Limit {
+            return Limit::perMinute(100)->by($request->ip());
+        });
+
         Product::observe(ProductObserver::class);
@@ -140,2 +157,18 @@

+    protected function configureEventListeners(): void
+    {
+        foreach ([
+            OrderCreated::class,
+            OrderPaid::class,
+            OrderCancelled::class,
+            OrderRefunded::class,
+            FulfillmentCreated::class,
+            FulfillmentShipped::class,
+            FulfillmentDelivered::class,
+            ProductStatusChanged::class,
+        ] as $event) {
+            Event::listen($event, DispatchWebhooks::class);
+        }
+    }
+
     protected function configureLivewireMiddleware(): void


Output: {"output":"Success. Updated the following files:\nM /Users/fabianwesner/Herd/shop/app/Providers/AppServiceProvider.php\n","metadata":{"exit_code":0,"duration_seconds":0.0}}


I’m moving up to the admin layer now: app installation list/detail and the developer page for one-time token generation plus webhook CRUD. Both are scoped to the current store and owner/admin roles.


Input:
*** Begin Patch
*** Update File: /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Apps/Index.php
@@
 namespace App\Livewire\Admin\Apps;

+use App\Enums\AppInstallationStatus;
+use App\Models\AppInstallation;
+use App\Models\Store;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
+use Illuminate\Support\Collection;
+use Livewire\Attributes\Locked;
 use Livewire\Component;

 class Index extends Component
 {
+    use AuthorizesRequests;
+
+    #[Locked]
+    public int $storeId;
+
+    public function mount(): void
+    {
+        $store = $this->store();
+
+        $this->authorize('update', $store);
+        $this->storeId = $store->getKey();
+    }
+
+    public function uninstallApp(int $appId): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        AppInstallation::withoutGlobalScopes()
+            ->where('store_id', $this->storeId)
+            ->where('app_id', $appId)
+            ->update(['status' => AppInstallationStatus::Uninstalled]);
+
+        session()->flash('status', __('App uninstalled'));
+        $this->dispatch('toast', type: 'success', message: __('App uninstalled'));
+    }
+
+    /**
+     * @return Collection<int, AppInstallation>
+     */
+    public function installedApps(): Collection
+    {
+        return AppInstallation::withoutGlobalScopes()
+            ->with(['app', 'webhookSubscriptions'])
+            ->where('store_id', $this->storeId)
+            ->whereNot('status', AppInstallationStatus::Uninstalled->value)
+            ->latest('installed_at')
+            ->get();
+    }
+
     public function render()
     {
-        return view('livewire.admin.apps.index');
+        return view('livewire.admin.apps.index', [
+            'installedApps' => $this->installedApps(),
+        ])->layout('layouts.app', [
+            'title' => __('Apps'),
+        ]);
+    }
+
+    private function store(): Store
+    {
+        $store = app('current_store');
+
+        abort_unless($store instanceof Store, 404);
+
+        return $store;
+    }
+
+    private function scopedStore(): Store
+    {
+        return Store::query()->findOrFail($this->storeId);
     }
 }
*** Update File: /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Apps/Show.php
@@
 namespace App\Livewire\Admin\Apps;

+use App\Enums\AppInstallationStatus;
+use App\Models\AppInstallation;
+use App\Models\Store;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
+use Livewire\Attributes\Locked;
 use Livewire\Component;

 class Show extends Component
 {
+    use AuthorizesRequests;
+
+    #[Locked]
+    public int $storeId;
+
+    #[Locked]
+    public int $installationId;
+
+    public function mount(AppInstallation $installation): void
+    {
+        $store = $this->store();
+
+        $this->authorize('update', $store);
+
+        $installation = AppInstallation::withoutGlobalScopes()
+            ->where('store_id', $store->getKey())
+            ->whereKey($installation->getKey())
+            ->first();
+
+        abort_unless($installation instanceof AppInstallation, 404);
+
+        $this->storeId = $store->getKey();
+        $this->installationId = $installation->getKey();
+    }
+
+    public function uninstallApp(): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->installation()->forceFill([
+            'status' => AppInstallationStatus::Uninstalled,
+        ])->save();
+
+        session()->flash('status', __('App uninstalled'));
+        $this->dispatch('toast', type: 'success', message: __('App uninstalled'));
+    }
+
     public function render()
     {
-        return view('livewire.admin.apps.show');
+        return view('livewire.admin.apps.show', [
+            'installation' => $this->installation(),
+        ])->layout('layouts.app', [
+            'title' => __('App detail'),
+        ]);
+    }
+
+    private function installation(): AppInstallation
+    {
+        return AppInstallation::withoutGlobalScopes()
+            ->with(['app', 'oauthTokens', 'webhookSubscriptions.deliveries'])
+            ->where('store_id', $this->storeId)
+            ->findOrFail($this->installationId);
+    }
+
+    private function store(): Store
+    {
+        $store = app('current_store');
+
+        abort_unless($store instanceof Store, 404);
+
+        return $store;
+    }
+
+    private function scopedStore(): Store
+    {
+        return Store::query()->findOrFail($this->storeId);
     }
 }
*** Update File: /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Developers/Index.php
@@
 namespace App\Livewire\Admin\Developers;

+use App\Enums\WebhookEventType;
+use App\Enums\WebhookSubscriptionStatus;
+use App\Models\OauthToken;
+use App\Models\Store;
+use App\Models\WebhookSubscription;
+use App\Services\WebhookService;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
+use Illuminate\Support\Collection;
+use Illuminate\Validation\Rule;
+use Livewire\Attributes\Locked;
 use Livewire\Component;

 class Index extends Component
 {
+    use AuthorizesRequests;
+
+    #[Locked]
+    public int $storeId;
+
+    public string $newTokenName = '';
+
+    public ?string $generatedToken = null;
+
+    public string $webhookEventType = 'order.created';
+
+    public string $webhookUrl = '';
+
+    public ?int $editingWebhookId = null;
+
+    /**
+     * @var list<string>
+     */
+    public array $tokenAbilities = [
+        'read-products',
+        'write-products',
+        'read-orders',
+        'write-orders',
+        'read-customers',
+        'write-customers',
+        'read-analytics',
+    ];
+
+    public function mount(): void
+    {
+        $store = $this->store();
+
+        $this->authorize('update', $store);
+        $this->storeId = $store->getKey();
+    }
+
+    public function generateToken(WebhookService $webhooks): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->validate([
+            'newTokenName' => ['required', 'string', 'max:255'],
+        ], [], [
+            'newTokenName' => 'token name',
+        ]);
+
+        $result = $webhooks->createApiToken($this->scopedStore(), $this->newTokenName, $this->tokenAbilities);
+
+        $this->generatedToken = $result['plain_text'];
+        $this->newTokenName = '';
+        $this->modal('generate-token')->close();
+        $this->dispatch('toast', type: 'success', message: __('API token generated'));
+    }
+
+    public function revokeToken(int $tokenId): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->token($tokenId)->delete();
+
+        session()->flash('status', __('API token revoked'));
+        $this->dispatch('toast', type: 'success', message: __('API token revoked'));
+    }
+
+    public function openWebhookModal(?int $webhookId = null): void
+    {
+        $this->editingWebhookId = $webhookId;
+
+        if ($webhookId !== null) {
+            $webhook = $this->webhook($webhookId);
+            $this->webhookEventType = $webhook->event_type->value;
+            $this->webhookUrl = $webhook->target_url;
+        } else {
+            $this->webhookEventType = WebhookEventType::OrderCreated->value;
+            $this->webhookUrl = '';
+        }
+
+        $this->modal('webhook-form')->show();
+    }
+
+    public function saveWebhook(WebhookService $webhooks): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->validate([
+            'webhookEventType' => ['required', Rule::enum(WebhookEventType::class)],
+            'webhookUrl' => ['required', 'url', 'max:2048'],
+        ], [], [
+            'webhookEventType' => 'event type',
+            'webhookUrl' => 'endpoint URL',
+        ]);
+
+        $webhook = $this->editingWebhookId !== null
+            ? $this->webhook($this->editingWebhookId)
+            : new WebhookSubscription([
+                'store_id' => $this->storeId,
+                'signing_secret_encrypted' => $webhooks->createSigningSecret(),
+            ]);
+
+        $webhook->forceFill([
+            'event_type' => $this->webhookEventType,
+            'target_url' => $this->webhookUrl,
+            'status' => WebhookSubscriptionStatus::Active,
+        ])->save();
+
+        $this->editingWebhookId = null;
+        $this->webhookUrl = '';
+        $this->modal('webhook-form')->close();
+        $this->dispatch('toast', type: 'success', message: __('Webhook saved'));
+    }
+
+    public function deleteWebhook(int $webhookId): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->webhook($webhookId)->delete();
+        $this->dispatch('toast', type: 'success', message: __('Webhook deleted'));
+    }
+
+    /**
+     * @return Collection<int, OauthToken>
+     */
+    public function tokens(): Collection
+    {
+        return OauthToken::query()
+            ->with('installation.app')
+            ->whereHas('installation', function ($query): void {
+                $query->withoutGlobalScopes()->where('store_id', $this->storeId);
+            })
+            ->latest('created_at')
+            ->get();
+    }
+
+    /**
+     * @return Collection<int, WebhookSubscription>
+     */
+    public function webhooks(): Collection
+    {
+        return WebhookSubscription::withoutGlobalScopes()
+            ->withCount([
+                'deliveries',
+                'deliveries as failed_deliveries_count' => fn ($query) => $query->where('status', 'failed'),
+            ])
+            ->where('store_id', $this->storeId)
+            ->orderBy('event_type')
+            ->get();
+    }
+
     public function render()
     {
-        return view('livewire.admin.developers.index');
+        return view('livewire.admin.developers.index', [
+            'tokens' => $this->tokens(),
+            'webhooks' => $this->webhooks(),
+            'eventTypes' => WebhookEventType::selectable(),
+        ])->layout('layouts.app', [
+            'title' => __('Developers'),
+        ]);
+    }
+
+    private function token(int $tokenId): OauthToken
+    {
+        return OauthToken::query()
+            ->whereHas('installation', function ($query): void {
+                $query->withoutGlobalScopes()->where('store_id', $this->storeId);
+            })
+            ->findOrFail($tokenId);
+    }
+
+    private function webhook(int $webhookId): WebhookSubscription
+    {
+        return WebhookSubscription::withoutGlobalScopes()
+            ->where('store_id', $this->storeId)
+            ->findOrFail($webhookId);
+    }
+
+    private function store(): Store
+    {
+        $store = app('current_store');
+
+        abort_unless($store instanceof Store, 404);
+
+        return $store;
+    }
+
+    private function scopedStore(): Store
+    {
+        return Store::query()->findOrFail($this->storeId);
     }
 }
*** End Patch


Success. Updated the following files:
M /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Apps/Index.php
M /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Apps/Show.php
M /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Developers/Index.php

@@ -4,2 +4,7 @@

+use App\Enums\AppInstallationStatus;
+use App\Models\AppInstallation;
+use App\Models\Store;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
+use Livewire\Attributes\Locked;
 use Livewire\Component;
@@ -8,5 +13,68 @@
 {
+    use AuthorizesRequests;
+
+    #[Locked]
+    public int $storeId;
+
+    #[Locked]
+    public int $installationId;
+
+    public function mount(AppInstallation $installation): void
+    {
+        $store = $this->store();
+
+        $this->authorize('update', $store);
+
+        $installation = AppInstallation::withoutGlobalScopes()
+            ->where('store_id', $store->getKey())
+            ->whereKey($installation->getKey())
+            ->first();
+
+        abort_unless($installation instanceof AppInstallation, 404);
+
+        $this->storeId = $store->getKey();
+        $this->installationId = $installation->getKey();
+    }
+
+    public function uninstallApp(): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->installation()->forceFill([
+            'status' => AppInstallationStatus::Uninstalled,
+        ])->save();
+
+        session()->flash('status', __('App uninstalled'));
+        $this->dispatch('toast', type: 'success', message: __('App uninstalled'));
+    }
+
     public function render()
     {
-        return view('livewire.admin.apps.show');
+        return view('livewire.admin.apps.show', [
+            'installation' => $this->installation(),
+        ])->layout('layouts.app', [
+            'title' => __('App detail'),
+        ]);
+    }
+
+    private function installation(): AppInstallation
+    {
+        return AppInstallation::withoutGlobalScopes()
+            ->with(['app', 'oauthTokens', 'webhookSubscriptions.deliveries'])
+            ->where('store_id', $this->storeId)
+            ->findOrFail($this->installationId);
+    }
+
+    private function store(): Store
+    {
+        $store = app('current_store');
+
+        abort_unless($store instanceof Store, 404);
+
+        return $store;
+    }
+
+    private function scopedStore(): Store
+    {
+        return Store::query()->findOrFail($this->storeId);
     }

@@ -4,2 +4,12 @@

+use App\Enums\WebhookEventType;
+use App\Enums\WebhookSubscriptionStatus;
+use App\Models\OauthToken;
+use App\Models\Store;
+use App\Models\WebhookSubscription;
+use App\Services\WebhookService;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
+use Illuminate\Support\Collection;
+use Illuminate\Validation\Rule;
+use Livewire\Attributes\Locked;
 use Livewire\Component;
@@ -8,5 +18,189 @@
 {
+    use AuthorizesRequests;
+
+    #[Locked]
+    public int $storeId;
+
+    public string $newTokenName = '';
+
+    public ?string $generatedToken = null;
+
+    public string $webhookEventType = 'order.created';
+
+    public string $webhookUrl = '';
+
+    public ?int $editingWebhookId = null;
+
+    /**
+     * @var list<string>
+     */
+    public array $tokenAbilities = [
+        'read-products',
+        'write-products',
+        'read-orders',
+        'write-orders',
+        'read-customers',
+        'write-customers',
+        'read-analytics',
+    ];
+
+    public function mount(): void
+    {
+        $store = $this->store();
+
+        $this->authorize('update', $store);
+        $this->storeId = $store->getKey();
+    }
+
+    public function generateToken(WebhookService $webhooks): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->validate([
+            'newTokenName' => ['required', 'string', 'max:255'],
+        ], [], [
+            'newTokenName' => 'token name',
+        ]);
+
+        $result = $webhooks->createApiToken($this->scopedStore(), $this->newTokenName, $this->tokenAbilities);
+
+        $this->generatedToken = $result['plain_text'];
+        $this->newTokenName = '';
+        $this->modal('generate-token')->close();
+        $this->dispatch('toast', type: 'success', message: __('API token generated'));
+    }
+
+    public function revokeToken(int $tokenId): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->token($tokenId)->delete();
+
+        session()->flash('status', __('API token revoked'));
+        $this->dispatch('toast', type: 'success', message: __('API token revoked'));
+    }
+
+    public function openWebhookModal(?int $webhookId = null): void
+    {
+        $this->editingWebhookId = $webhookId;
+
+        if ($webhookId !== null) {
+            $webhook = $this->webhook($webhookId);
+            $this->webhookEventType = $webhook->event_type->value;
+            $this->webhookUrl = $webhook->target_url;
+        } else {
+            $this->webhookEventType = WebhookEventType::OrderCreated->value;
+            $this->webhookUrl = '';
+        }
+
+        $this->modal('webhook-form')->show();
+    }
+
+    public function saveWebhook(WebhookService $webhooks): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->validate([
+            'webhookEventType' => ['required', Rule::enum(WebhookEventType::class)],
+            'webhookUrl' => ['required', 'url', 'max:2048'],
+        ], [], [
+            'webhookEventType' => 'event type',
+            'webhookUrl' => 'endpoint URL',
+        ]);
+
+        $webhook = $this->editingWebhookId !== null
+            ? $this->webhook($this->editingWebhookId)
+            : new WebhookSubscription([
+                'store_id' => $this->storeId,
+                'signing_secret_encrypted' => $webhooks->createSigningSecret(),
+            ]);
+
+        $webhook->forceFill([
+            'event_type' => $this->webhookEventType,
+            'target_url' => $this->webhookUrl,
+            'status' => WebhookSubscriptionStatus::Active,
+        ])->save();
+
+        $this->editingWebhookId = null;
+        $this->webhookUrl = '';
+        $this->modal('webhook-form')->close();
+        $this->dispatch('toast', type: 'success', message: __('Webhook saved'));
+    }
+
+    public function deleteWebhook(int $webhookId): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        $this->webhook($webhookId)->delete();
+        $this->dispatch('toast', type: 'success', message: __('Webhook deleted'));
+    }
+
+    /**
+     * @return Collection<int, OauthToken>
+     */
+    public function tokens(): Collection
+    {
+        return OauthToken::query()
+            ->with('installation.app')
+            ->whereHas('installation', function ($query): void {
+                $query->withoutGlobalScopes()->where('store_id', $this->storeId);
+            })
+            ->latest('created_at')
+            ->get();
+    }
+
+    /**
+     * @return Collection<int, WebhookSubscription>
+     */
+    public function webhooks(): Collection
+    {
+        return WebhookSubscription::withoutGlobalScopes()
+            ->withCount([
+                'deliveries',
+                'deliveries as failed_deliveries_count' => fn ($query) => $query->where('status', 'failed'),
+            ])
+            ->where('store_id', $this->storeId)
+            ->orderBy('event_type')
+            ->get();
+    }
+
     public function render()
     {
-        return view('livewire.admin.developers.index');
+        return view('livewire.admin.developers.index', [
+            'tokens' => $this->tokens(),
+            'webhooks' => $this->webhooks(),
+            'eventTypes' => WebhookEventType::selectable(),
+        ])->layout('layouts.app', [
+            'title' => __('Developers'),
+        ]);
+    }
+
+    private function token(int $tokenId): OauthToken
+    {
+        return OauthToken::query()
+            ->whereHas('installation', function ($query): void {
+                $query->withoutGlobalScopes()->where('store_id', $this->storeId);
+            })
+            ->findOrFail($tokenId);
+    }
+
+    private function webhook(int $webhookId): WebhookSubscription
+    {
+        return WebhookSubscription::withoutGlobalScopes()
+            ->where('store_id', $this->storeId)
+            ->findOrFail($webhookId);
+    }
+
+    private function store(): Store
+    {
+        $store = app('current_store');
+
+        abort_unless($store instanceof Store, 404);
+
+        return $store;
+    }
+
+    private function scopedStore(): Store
+    {
+        return Store::query()->findOrFail($this->storeId);
     }

@@ -4,2 +4,8 @@

+use App\Enums\AppInstallationStatus;
+use App\Models\AppInstallation;
+use App\Models\Store;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
+use Illuminate\Support\Collection;
+use Livewire\Attributes\Locked;
 use Livewire\Component;
@@ -8,5 +14,62 @@
 {
+    use AuthorizesRequests;
+
+    #[Locked]
+    public int $storeId;
+
+    public function mount(): void
+    {
+        $store = $this->store();
+
+        $this->authorize('update', $store);
+        $this->storeId = $store->getKey();
+    }
+
+    public function uninstallApp(int $appId): void
+    {
+        $this->authorize('update', $this->scopedStore());
+
+        AppInstallation::withoutGlobalScopes()
+            ->where('store_id', $this->storeId)
+            ->where('app_id', $appId)
+            ->update(['status' => AppInstallationStatus::Uninstalled]);
+
+        session()->flash('status', __('App uninstalled'));
+        $this->dispatch('toast', type: 'success', message: __('App uninstalled'));
+    }
+
+    /**
+     * @return Collection<int, AppInstallation>
+     */
+    public function installedApps(): Collection
+    {
+        return AppInstallation::withoutGlobalScopes()
+            ->with(['app', 'webhookSubscriptions'])
+            ->where('store_id', $this->storeId)
+            ->whereNot('status', AppInstallationStatus::Uninstalled->value)
+            ->latest('installed_at')
+            ->get();
+    }
+
     public function render()
     {
-        return view('livewire.admin.apps.index');
+        return view('livewire.admin.apps.index', [
+            'installedApps' => $this->installedApps(),
+        ])->layout('layouts.app', [
+            'title' => __('Apps'),
+        ]);
+    }
+
+    private function store(): Store
+    {
+        $store = app('current_store');
+
+        abort_unless($store instanceof Store, 404);
+
+        return $store;
+    }
+
+    private function scopedStore(): Store
+    {
+        return Store::query()->findOrFail($this->storeId);
     }


Output: {"output":"Success. Updated the following files:\nM /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Apps/Index.php\nM /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Apps/Show.php\nM /Users/fabianwesner/Herd/shop/app/Livewire/Admin/Developers/Index.php\n","metadata":{"exit_code":0,"duration_seconds":0.0}}


sed -n '1,60p' resources/views/livewire/admin/apps/index.blade.php


sed -n '1,60p' resources/views/livewire/admin/apps/show.blade.php


sed -n '1,60p' resources/views/livewire/admin/developers/index.blade.php


sed -n '1,60p' resources/views/livewire/admin/apps/show.blade.php

{{-- Life is available only in the present moment. - Thich Nhat Hanh --}}


sed -n '1,60p' resources/views/livewire/admin/apps/index.blade.php

{{-- Nothing worth having comes easy. - Theodore Roosevelt --}}


sed -n '1,60p' resources/views/livewire/admin/developers/index.blade.php

{{-- Simplicity is the essence of happiness. - Cedric Bledsoe --}}


Output:
Chunk ID: e796f1
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 21
Output:
<div>
    {{-- Nothing worth having comes easy. - Theodore Roosevelt --}}
</div>


Output:
Chunk ID: 543ecd
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 23
Output:
<div>
    {{-- Life is available only in the present moment. - Thich Nhat Hanh --}}
</div>


Output:
Chunk ID: f537f9
Wall time: 0.0000 seconds
Process exited with code 0
Original token count: 21
Output:
<div>
    {{-- Simplicity is the essence of happiness. - Cedric Bledsoe --}}
</div>


sed -n '1,220p' resources/views/livewire/admin/analytics/index.blade.php


sed -n '1,220p' resources/views/livewire/admin/analytics/index.blade.php

            <flux:heading size="xl">Analytics</flux:heading>
            <flux:text class="mt-1">Sales, traffic, and conversion signals for the selected store.</flux:text>

            <flux:select wire:model.live="dateRange" aria-label="Date range">
                <flux:select.option value="today">Today</flux:select.option>
                <flux:select.option value="last_7_days">Last 7 days</flux:select.option>
                <flux:select.option value="last_30_days">Last 30 days</flux:select.option>
                <flux:select.option value="custom">Custom</flux:select.option>
            </flux:select>

            <flux:input type="date" wire:model.live="customStartDate" :disabled="$dateRange !== 'custom'" aria-label="Custom range start" />
            <flux:input type="date" wire:model.live="customEndDate" :disabled="$dateRange !== 'custom'" aria-label="Custom range end" />

            <flux:select wire:model.live="channelFilter" aria-label="Channel">
                <flux:select.option value="all">All channels</flux:select.option>
                <flux:select.option value="storefront">Storefront</flux:select.option>
                <flux:select.option value="api">API</flux:select.option>
            </flux:select>

            <flux:select wire:model.live="deviceFilter" aria-label="Device">
                <flux:select.option value="all">All devices</flux:select.option>
                <flux:select.option value="desktop">Desktop</flux:select.option>
                <flux:select.option value="mobile">Mobile</flux:select.option>
                <flux:select.option value="tablet">Tablet</flux:select.option>
            </flux:select>

        <flux:button wire:click="exportCsv" icon="arrow-down-tray" variant="ghost" wire:loading.attr="disabled">
            Export CSV
            Exporting...
        </flux:button>

        @if ($exportUrl)
            <flux:button :href="$exportUrl" download="analytics.csv" icon="document-arrow-down" variant="filled">
                Download
            </flux:button>
        @endif

        @foreach ([
            ['label' => 'Total sales', 'value' => $this->formattedTotalSales, 'icon' => 'banknotes'],
            ['label' => 'Orders', 'value' => number_format($ordersCount), 'icon' => 'shopping-bag'],
            ['label' => 'Average order', 'value' => $this->formattedAov, 'icon' => 'receipt-percent'],
            ['label' => 'Conversion', 'value' => number_format($conversionRate, 2).'%', 'icon' => 'chart-bar'],
        ] as $metric)

{{ $metric['label'] }}

{{ $metric['value'] }}

                        <flux:icon :name="$metric['icon']" class="size-5" />

        @endforeach

                <flux:heading size="lg">Sales over time</flux:heading>
                <flux:text class="mt-1">Daily revenue and order volume</flux:text>

            @foreach ($salesChartData as $point)
                @php
                    $height = $point['revenue'] === 0 ? 2 : max(8, (int) round(($point['revenue'] / $maxSalesChartAmount) * 100));
                @endphp

                    @if ($loop->first || $loop->last || $loop->iteration % 7 === 0)

{{ $point['label'] }}

                    @endif

            @endforeach

            <flux:heading size="lg">Top products</flux:heading>

                            Rank
                            Product
                            Units
                            Revenue
                            Share

                        @forelse ($topProducts as $product)

                                {{ $loop->iteration }}
                                {{ $product['title'] }}
                                {{ number_format($product['units_sold']) }}
                                {{ \App\Support\Money::format($product['revenue'], $storeCurrency) }}
                                {{ number_format($product['percentage'], 1) }}%

                        @empty

                                No product sales in this range.

                        @endforelse

            <flux:heading size="lg">Top referrers</flux:heading>

                @forelse ($topReferrers as $referrer)

{{ $referrer['source'] }}

                            <flux:badge>{{ number_format($referrer['conversion_rate'], 2) }}%</flux:badge>

{{ number_format($referrer['sessions']) }} sessions

{{ number_format($referrer['orders']) }} orders

                @empty

                        <flux:icon name="cursor-arrow-rays" class="mx-auto size-8 text-zinc-400" />
                        <flux:text class="mt-3">No referrer activity in this range.</flux:text>

                @endforelse