feat(messaging): suite-channel patient email/SMS and entitlement sync
Deploy Ladill Care / deploy (push) Successful in 40s

Prefer platform suite messaging for patient email/SMS, fall back to product
keys; write suite entitlements on Pro wallet, prepaid, and renewal.
This commit is contained in:
isaacclad
2026-07-16 11:24:21 +00:00
parent 7a0a7fcb88
commit c9f5df6ed7
9 changed files with 409 additions and 128 deletions
+23 -72
View File
@@ -4,7 +4,11 @@ namespace App\Services\Billing;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
/**
* Platform suite SMS (zero-config): channel=suite, no customer SMS API key.
*/
class PlatformSmsClient
{
private ?string $lastError = null;
@@ -29,65 +33,13 @@ class PlatformSmsClient
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
{
public function send(
string $ownerPublicId,
string $to,
string $message,
?string $senderId = null,
?string $idempotencyKey = null,
): bool {
$this->lastError = null;
if (! $this->isConfigured()) {
@@ -96,33 +48,32 @@ class PlatformSmsClient
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;
}
$key = $idempotencyKey ?: ('care-sms-'.Str::uuid());
try {
$res = Http::withToken($this->token())->acceptJson()->timeout(30)
$res = Http::withToken($this->token())
->withHeaders(['Idempotency-Key' => $key])
->acceptJson()
->timeout(30)
->post($this->base().'/sms/messages/send', array_filter([
'user' => $ownerPublicId,
'sms_service_id' => $serviceId,
'channel' => 'suite',
'to' => $to,
'text' => $message,
'sender_id' => $senderId ?? config('sms.default_sender_id'),
]));
'reference' => $key,
], static fn ($v) => $v !== null && $v !== ''));
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]);
$this->lastError = (string) ($res->json('error') ?: 'SMS allowance exceeded. Upgrade Care Pro or top up your wallet.');
Log::warning('Care suite SMS: allowance/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()]);
Log::warning('Care suite SMS failed', ['status' => $res->status(), 'body' => $res->body()]);
return false;
}
@@ -130,7 +81,7 @@ class PlatformSmsClient
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()]);
Log::warning('Care suite SMS error', ['error' => $e->getMessage()]);
return false;
}