Files
ladill-woo-manager/app/Http/Controllers/Api/StoreActivationController.php
T
isaaccladandCursor cfdc8c7c09
Deploy Ladill Woo Manager / deploy (push) Successful in 2m11s
Add Woo Manager email notification milestones with shared mail templates.
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>
2026-07-07 02:59:45 +00:00

93 lines
2.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Jobs\StoreBootstrapSync;
use App\Models\WooStore;
use App\Services\Woo\InstallTokenService;
use App\Services\Woo\WooNotificationService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use RuntimeException;
class StoreActivationController extends Controller
{
public function __construct(
private InstallTokenService $tokens,
private WooNotificationService $notifications,
) {}
public function activate(Request $request): JsonResponse
{
$validated = $request->validate([
'install_token' => ['required', 'string'],
'site_url' => ['required', 'url', 'max:500'],
]);
try {
$store = $this->tokens->consume($validated['install_token']);
} catch (RuntimeException $e) {
return response()->json(['message' => $e->getMessage()], 422);
}
$parsed = parse_url($validated['site_url']);
$scheme = $parsed['scheme'] ?? 'https';
$host = strtolower((string) ($parsed['host'] ?? ''));
$siteUrl = rtrim($scheme.'://'.$host, '/');
if ($store->site_url !== $siteUrl) {
return response()->json(['message' => 'Site URL does not match the connection request.'], 422);
}
$wasPending = $store->status === WooStore::STATUS_PENDING;
$pluginToken = $this->tokens->issuePluginToken($store);
$store = $store->fresh();
if ($wasPending) {
$this->notifications->storeConnected($store);
}
StoreBootstrapSync::dispatch($store->id)->afterResponse();
return response()->json([
'store_id' => $store->public_id,
'webhook_url' => $store->webhookUrl(),
'webhook_secret' => $store->webhook_secret,
'plugin_token' => $pluginToken,
'webhook_topics' => config('woo.webhook_topics'),
]);
}
public function show(Request $request): JsonResponse
{
$store = $this->storeFromRequest($request);
if (! $store) {
return response()->json(['message' => 'Unauthorized.'], 401);
}
return response()->json([
'store_id' => $store->public_id,
'site_url' => $store->site_url,
'site_name' => $store->site_name,
'status' => $store->status,
'webhook_url' => $store->webhookUrl(),
]);
}
private function storeFromRequest(Request $request): ?WooStore
{
$storeId = (string) $request->header('X-Ladill-Store', '');
$token = (string) $request->bearerToken();
if ($storeId === '' || $token === '') {
return null;
}
$store = WooStore::query()->where('public_id', $storeId)->first();
if (! $store || ! $this->tokens->verifyPluginToken($store, $token)) {
return null;
}
return $store;
}
}