Files
ladill-events/app/Services/Billing/PlatformSmsClient.php
T
isaaccladandCursor 06fedcfc55
Deploy Ladill Events / deploy (push) Successful in 41s
Add virtual event sessions with Meet sync and platform-billed messaging.
Events can define hybrid/virtual sessions synced to Meet rooms; SMS and email use wallet-billed platform APIs instead of Termii and Laravel mail.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-01 22:08:09 +00:00

115 lines
3.3 KiB
PHP

<?php
namespace App\Services\Billing;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class PlatformSmsClient
{
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', '');
}
/** @return list<array<string, mixed>> */
public function services(string $ownerPublicId): array
{
if ($this->token() === '') {
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('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->token() === '') {
return null;
}
try {
$res = Http::withToken($this->token())->acceptJson()->timeout(15)
->post($this->base().'/sms/services', [
'user' => $ownerPublicId,
'name' => 'Ladill Events',
'brand_name' => 'Events',
]);
if ($res->failed()) {
return null;
}
return (int) ($res->json('data.id') ?? 0) ?: null;
} catch (\Throwable $e) {
Log::warning('Platform SMS service create failed', ['error' => $e->getMessage()]);
return null;
}
}
public function send(string $ownerPublicId, string $to, string $message, ?string $senderId = null): bool
{
if ($this->token() === '') {
return false;
}
$serviceId = $this->ensureServiceId($ownerPublicId);
if (! $serviceId) {
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) {
Log::warning('Platform SMS send: insufficient balance', ['user' => $ownerPublicId]);
return false;
}
if ($res->failed()) {
Log::warning('Platform SMS send failed', ['status' => $res->status(), 'body' => $res->body()]);
return false;
}
return (bool) ($res->json('success') ?? true);
} catch (\Throwable $e) {
Log::warning('Platform SMS send error', ['error' => $e->getMessage()]);
return false;
}
}
}