option('url') ?: $this->defaultWebhookUrl()); if ($destination === '') { $this->error('Webhook destination URL is required. Use --url=...'); return self::FAILURE; } $events = collect((array) $this->option('events')) ->filter() ->map(fn ($event) => trim((string) $event)) ->filter() ->values() ->all(); if ($events === []) { $events = config('lemonsqueezy.webhook_events', []); } if ($events === [] || ! is_array($events)) { $this->error('No webhook events configured. Set config(lemonsqueezy.webhook_events) or pass --events.'); return self::FAILURE; } $secret = (string) ($this->option('secret') ?: config('lemonsqueezy.webhook_secret')); if ($secret === '') { $this->error('Webhook signing secret is required. Set LEMONSQUEEZY_WEBHOOK_SECRET or pass --secret.'); return self::FAILURE; } $storeId = (string) config('lemonsqueezy.store_id'); if ($storeId === '') { $this->error('Lemon Squeezy store id is required. Set LEMONSQUEEZY_STORE_ID.'); return self::FAILURE; } $testMode = (bool) $this->option('test-mode') || (bool) config('lemonsqueezy.test_mode', false); $attributes = array_filter([ 'url' => $destination, 'events' => $events, 'secret' => $secret, 'test_mode' => $testMode ? true : null, ], static fn ($value) => $value !== null && $value !== ''); $payload = [ 'data' => [ 'type' => 'webhooks', 'attributes' => $attributes, 'relationships' => [ 'store' => [ 'data' => [ 'type' => 'stores', 'id' => $storeId, ], ], ], ], ]; if ((bool) $this->option('dry-run')) { $this->line(json_encode($payload, JSON_PRETTY_PRINT)); return self::SUCCESS; } $response = $client->post('/webhooks', $payload); $data = Arr::get($response, 'data', $response); $id = Arr::get($data, 'id'); Log::channel('lemonsqueezy-sync')->info('Lemon Squeezy webhook registered', [ 'webhook_id' => $id, 'destination' => $destination, 'test_mode' => $testMode, ]); $this->info('Lemon Squeezy webhook registered.'); if ($id) { $this->line('ID: '.$id); } return self::SUCCESS; } protected function defaultWebhookUrl(): string { $base = rtrim((string) config('app.url'), '/'); return $base !== '' ? $base.'/lemonsqueezy/webhook' : ''; } }