Files
ladill-care/app/Services/Billing/PlatformSmsClient.php
T
isaaccladandCursor 2568b8a2f0
Deploy Ladill Care / deploy (push) Successful in 43s
Add patient SMS/email messaging and fix Meet video visit errors.
Wire Care to Ladill SMS and Bird management APIs on the patient page, and surface Meet auth/config failures as friendly validation errors instead of a 502 page.

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

139 lines
4.3 KiB
PHP

<?php
namespace App\Services\Billing;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class PlatformSmsClient
{
private ?string $lastError = null;
public function lastError(): ?string
{
return $this->lastError;
}
private function base(): string
{
return rtrim((string) config('sms.platform_api_url', 'https://ladill.com/api'), '/');
}
private function token(): string
{
return (string) config('sms.platform_api_key', '');
}
public function isConfigured(): bool
{
return $this->token() !== '';
}
/** @return list<array<string, mixed>> */
public function services(string $ownerPublicId): array
{
if (! $this->isConfigured()) {
return [];
}
try {
$res = Http::withToken($this->token())->acceptJson()->timeout(15)
->get($this->base().'/sms/services', ['user' => $ownerPublicId]);
if ($res->failed()) {
return [];
}
return (array) ($res->json('data') ?? []);
} catch (\Throwable $e) {
Log::warning('Care platform SMS services lookup failed', ['error' => $e->getMessage()]);
return [];
}
}
public function ensureServiceId(string $ownerPublicId): ?int
{
$services = $this->services($ownerPublicId);
if ($services !== []) {
return (int) ($services[0]['id'] ?? 0) ?: null;
}
if (! $this->isConfigured()) {
return null;
}
try {
$res = Http::withToken($this->token())->acceptJson()->timeout(15)
->post($this->base().'/sms/services', [
'user' => $ownerPublicId,
'name' => 'Ladill Care',
'brand_name' => 'Care',
]);
if ($res->failed()) {
$this->lastError = (string) ($res->json('error') ?: 'Could not create an SMS service for this account.');
return null;
}
return (int) ($res->json('data.id') ?? 0) ?: null;
} catch (\Throwable $e) {
Log::warning('Care platform SMS service create failed', ['error' => $e->getMessage()]);
$this->lastError = 'Could not reach the SMS service.';
return null;
}
}
public function send(string $ownerPublicId, string $to, string $message, ?string $senderId = null): bool
{
$this->lastError = null;
if (! $this->isConfigured()) {
$this->lastError = 'SMS is not configured on this Care instance. Set SMS_API_KEY_CARE.';
return false;
}
$serviceId = $this->ensureServiceId($ownerPublicId);
if (! $serviceId) {
$this->lastError ??= 'No SMS service is available for this account. Open sms.ladill.com once, then retry.';
return false;
}
try {
$res = Http::withToken($this->token())->acceptJson()->timeout(30)
->post($this->base().'/sms/messages/send', array_filter([
'user' => $ownerPublicId,
'sms_service_id' => $serviceId,
'to' => $to,
'text' => $message,
'sender_id' => $senderId ?? config('sms.default_sender_id'),
]));
if ($res->status() === 402) {
$this->lastError = (string) ($res->json('error') ?: 'Insufficient SMS credit. Top up your Ladill SMS wallet and try again.');
Log::warning('Care platform SMS send: insufficient balance', ['user' => $ownerPublicId]);
return false;
}
if ($res->failed()) {
$this->lastError = (string) ($res->json('error') ?: $res->json('message') ?: 'The SMS could not be sent.');
Log::warning('Care platform SMS send failed', ['status' => $res->status(), 'body' => $res->body()]);
return false;
}
return (bool) ($res->json('success') ?? true);
} catch (\Throwable $e) {
$this->lastError = 'Could not reach the SMS service. Please try again.';
Log::warning('Care platform SMS send error', ['error' => $e->getMessage()]);
return false;
}
}
}