Deploy Ladill Merchant / deploy (push) Successful in 30s
- New Products page (merchant.products.*) with full CRUD proxied to the Ladill CRM products API via a new CrmClient + config/crm.php (owner-scoped, type=product). - Sidebar gains a Products entry. - The new storefront form loads the merchant's catalog: each shop/menu section gets an "Add from products…" picker that drops a CRM product in as an item (name, price, description). Catalog fetch is resilient — empty if CRM is down. Wires CRM_API_URL + CRM_API_KEY_MERCHANT on the merchant env (matches CRM). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
86 lines
2.4 KiB
PHP
86 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Crm;
|
|
|
|
use Illuminate\Http\Client\ConnectionException;
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/**
|
|
* Thin client for the Ladill CRM internal API (crm.ladill.com/api). Authenticates
|
|
* with the per-service key (CRM_API_KEY_MERCHANT) and scopes every call to one
|
|
* platform account via the `owner` parameter (the user's public_id / OIDC sub).
|
|
*/
|
|
class CrmClient
|
|
{
|
|
public function __construct(private readonly string $owner) {}
|
|
|
|
public static function for(string $owner): self
|
|
{
|
|
return new self($owner);
|
|
}
|
|
|
|
public function products(array $filters = []): array
|
|
{
|
|
return $this->get('products', $filters);
|
|
}
|
|
|
|
public function product(int|string $id): array
|
|
{
|
|
return $this->get("products/{$id}");
|
|
}
|
|
|
|
public function createProduct(array $data): array
|
|
{
|
|
return $this->send('post', 'products', $data);
|
|
}
|
|
|
|
public function updateProduct(int|string $id, array $data): array
|
|
{
|
|
return $this->send('patch', "products/{$id}", $data);
|
|
}
|
|
|
|
public function deleteProduct(int|string $id): array
|
|
{
|
|
return $this->send('delete', "products/{$id}", []);
|
|
}
|
|
|
|
private function client(): PendingRequest
|
|
{
|
|
return Http::baseUrl((string) config('crm.url'))
|
|
->withToken((string) config('crm.key'))
|
|
->acceptJson()
|
|
->asJson()
|
|
->connectTimeout(10)
|
|
->timeout(20);
|
|
}
|
|
|
|
private function get(string $path, array $query = []): array
|
|
{
|
|
return $this->handle(fn () => $this->client()->get($path, [...array_filter($query, fn ($v) => $v !== null), 'owner' => $this->owner]));
|
|
}
|
|
|
|
private function send(string $method, string $path, array $data): array
|
|
{
|
|
return $this->handle(fn () => $this->client()->{$method}($path, [...$data, 'owner' => $this->owner]));
|
|
}
|
|
|
|
private function handle(callable $request): array
|
|
{
|
|
try {
|
|
$response = $request();
|
|
} catch (ConnectionException) {
|
|
throw ValidationException::withMessages([
|
|
'crm' => ['Could not reach the CRM service. Please try again in a moment.'],
|
|
]);
|
|
}
|
|
|
|
if ($response->failed()) {
|
|
abort($response->status() === 404 ? 404 : 502, 'CRM service error.');
|
|
}
|
|
|
|
return (array) $response->json();
|
|
}
|
|
}
|