where('public_id', $storePublicId) ->where('status', WooStore::STATUS_ACTIVE) ->first(); if (! $store) { return response()->json(['message' => 'Store not found.'], 404); } if (! $this->verifier->verify($request, $store)) { return response()->json(['message' => 'Invalid signature.'], 401); } $topic = (string) $request->header('X-WC-Webhook-Topic', ''); if (! in_array($topic, (array) config('woo.webhook_topics', []), true)) { return response()->json(['ignored' => true]); } /** @var array $payload */ $payload = $request->json()->all(); if ($payload === []) { return response()->json(['ignored' => true]); } return match (true) { str_starts_with($topic, 'order.') => $this->handleOrder($store, $topic, $payload), str_starts_with($topic, 'product_category.') => $this->handleCategory($store, $topic, $payload), str_starts_with($topic, 'product.') => $this->handleProduct($store, $topic, $payload), default => response()->json(['ignored' => true]), }; } /** @param array $payload */ private function handleOrder(WooStore $store, string $topic, array $payload): JsonResponse { $order = $this->orders->ingest($store, $payload); if ($topic === 'order.created' && $order->wasRecentlyCreated) { $this->notifications->orderReceived($order, $store); } return response()->json([ 'ok' => true, 'order_id' => $order->id, ]); } /** @param array $payload */ private function handleProduct(WooStore $store, string $topic, array $payload): JsonResponse { if ($topic === 'product.deleted') { $this->products->delete($store, (int) ($payload['id'] ?? 0)); return response()->json(['ok' => true, 'deleted' => true]); } $product = $this->products->ingest($store, $payload); return response()->json([ 'ok' => true, 'product_id' => $product->id, ]); } /** @param array $payload */ private function handleCategory(WooStore $store, string $topic, array $payload): JsonResponse { if ($topic === 'product_category.deleted') { $this->categories->delete($store, (int) ($payload['id'] ?? 0)); return response()->json(['ok' => true, 'deleted' => true]); } $category = $this->categories->ingest($store, $payload); return response()->json([ 'ok' => true, 'category_id' => $category->id, ]); } }