Deploy Ladill Mini / deploy (push) Successful in 23s
Staff-facing counter register at pos.ladill.com with catalog cart, cash and MoMo/card checkout via Ladill Pay, CRM timeline/import, invoice prefill, and Merchant catalog import. Co-authored-by: Cursor <cursoragent@cursor.com>
71 lines
1.8 KiB
PHP
71 lines
1.8 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;
|
|
|
|
class CrmClient
|
|
{
|
|
public function __construct(private readonly string $owner) {}
|
|
|
|
public static function for(string $owner): self
|
|
{
|
|
return new self($owner);
|
|
}
|
|
|
|
public function customers(array $filters = []): array
|
|
{
|
|
return $this->get('customers', $filters);
|
|
}
|
|
|
|
public function products(array $filters = []): array
|
|
{
|
|
return $this->get('products', $filters);
|
|
}
|
|
|
|
public function pushTimeline(array $data): array
|
|
{
|
|
return $this->post('timeline', $data);
|
|
}
|
|
|
|
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, [...$query, 'owner' => $this->owner]));
|
|
}
|
|
|
|
private function post(string $path, array $data): array
|
|
{
|
|
return $this->handle(fn () => $this->client()->post($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();
|
|
}
|
|
}
|