Deploy Ladill Woo Manager / deploy (push) Successful in 2m11s
New order, store connected, Pro expiry, and past-due alerts use the Ladill notification layout with woo branding; expiry reminders dedupe per threshold like hosting. Co-authored-by: Cursor <cursoragent@cursor.com>
107 lines
3.5 KiB
PHP
107 lines
3.5 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\WooNotificationService;
|
|
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,
|
|
private WooNotificationService $notifications,
|
|
) {}
|
|
|
|
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, $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<string, mixed> $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<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,
|
|
]);
|
|
}
|
|
}
|