Deploy Ladill Care / deploy (push) Successful in 43s
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>
77 lines
2.4 KiB
PHP
77 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Billing;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class PlatformEmailClient
|
|
{
|
|
private ?string $lastError = null;
|
|
|
|
public function lastError(): ?string
|
|
{
|
|
return $this->lastError;
|
|
}
|
|
|
|
private function base(): string
|
|
{
|
|
return rtrim((string) config('smtp.platform_api_url', 'https://ladill.com/api/smtp'), '/');
|
|
}
|
|
|
|
private function token(): string
|
|
{
|
|
return (string) config('smtp.platform_api_key', '');
|
|
}
|
|
|
|
public function isConfigured(): bool
|
|
{
|
|
return $this->token() !== '';
|
|
}
|
|
|
|
public function send(string $ownerPublicId, string $to, string $subject, string $html, ?string $text = null): bool
|
|
{
|
|
$this->lastError = null;
|
|
|
|
if (! $this->isConfigured()) {
|
|
$this->lastError = 'Outbound email is not configured on this Care instance. Set SMTP_API_KEY_CARE.';
|
|
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$res = Http::withToken($this->token())->acceptJson()->timeout(30)
|
|
->post($this->base().'/messages/send', array_filter([
|
|
'user' => $ownerPublicId,
|
|
'from' => config('smtp.from'),
|
|
'from_name' => config('smtp.from_name'),
|
|
'to' => $to,
|
|
'subject' => $subject,
|
|
'html' => $html,
|
|
'text' => $text,
|
|
], fn ($value) => $value !== null && $value !== ''));
|
|
|
|
if ($res->status() === 402) {
|
|
$this->lastError = (string) ($res->json('error') ?: 'Insufficient Bird email credits. Top up Bird and try again.');
|
|
Log::warning('Care platform email send: insufficient balance', ['user' => $ownerPublicId]);
|
|
|
|
return false;
|
|
}
|
|
|
|
if ($res->failed()) {
|
|
$this->lastError = (string) ($res->json('error') ?: $res->json('message') ?: 'The email could not be sent.');
|
|
Log::warning('Care platform email 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 email service. Please try again.';
|
|
Log::warning('Care platform email send error', ['error' => $e->getMessage()]);
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|