Files
ladill-woo-manager/app/Http/Controllers/WooWebhookController.php
T
isaaccladandCursor e4b6c2c1ba
Deploy Ladill Woo Manager / deploy (push) Successful in 55s
Add WooCommerce product and category management.
Sync catalog via plugin webhooks and REST proxy, with list/create/edit UI and updated logo from monolith assets.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-06 23:40:14 +00:00

101 lines
3.2 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\WooStore;
use App\Services\Woo\CategoryIngestService;
use App\Services\Woo\OrderIngestService;
use App\Services\Woo\ProductIngestService;
use App\Services\Woo\WooWebhookVerifier;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class WooWebhookController extends Controller
{
public function __construct(
private WooWebhookVerifier $verifier,
private OrderIngestService $orders,
private ProductIngestService $products,
private CategoryIngestService $categories,
) {}
public function __invoke(Request $request, string $storePublicId): JsonResponse
{
$store = WooStore::query()
->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<string, mixed> $payload */
$payload = $request->json()->all();
if ($payload === []) {
return response()->json(['ignored' => true]);
}
return match (true) {
str_starts_with($topic, 'order.') => $this->handleOrder($store, $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<string, mixed> $payload */
private function handleOrder(WooStore $store, array $payload): JsonResponse
{
$order = $this->orders->ingest($store, $payload);
return response()->json([
'ok' => true,
'order_id' => $order->id,
]);
}
/** @param array<string, mixed> $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<string, mixed> $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,
]);
}
}