feat(messaging): suite-channel email/SMS and entitlement sync
Deploy Ladill Events / deploy (push) Successful in 48s
Deploy Ladill Events / deploy (push) Successful in 48s
Prefer platform suite messaging (channel=suite) for attendee email, fall back to Bird integrations; sync suite plan entitlements on subscribe/renew.
This commit is contained in:
@@ -4,9 +4,21 @@ namespace App\Services\Billing;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Platform suite SMS (zero-config): POST /api/sms/messages/send with channel=suite.
|
||||
* No customer API key or sms_service_id required when platform messaging is enabled.
|
||||
*/
|
||||
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'), '/');
|
||||
@@ -17,96 +29,60 @@ class PlatformSmsClient
|
||||
return (string) config('sms.platform_api_key', '');
|
||||
}
|
||||
|
||||
/** @return list<array<string, mixed>> */
|
||||
public function services(string $ownerPublicId): array
|
||||
public function isConfigured(): bool
|
||||
{
|
||||
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 [];
|
||||
}
|
||||
return $this->token() !== '';
|
||||
}
|
||||
|
||||
public function ensureServiceId(string $ownerPublicId): ?int
|
||||
{
|
||||
$services = $this->services($ownerPublicId);
|
||||
if ($services !== []) {
|
||||
return (int) ($services[0]['id'] ?? 0) ?: null;
|
||||
}
|
||||
public function send(
|
||||
string $ownerPublicId,
|
||||
string $to,
|
||||
string $message,
|
||||
?string $senderId = null,
|
||||
?string $idempotencyKey = null,
|
||||
): bool {
|
||||
$this->lastError = null;
|
||||
|
||||
if ($this->token() === '') {
|
||||
return null;
|
||||
}
|
||||
if (! $this->isConfigured()) {
|
||||
$this->lastError = 'SMS is not configured on this Events instance.';
|
||||
|
||||
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;
|
||||
}
|
||||
$key = $idempotencyKey ?: ('events-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) {
|
||||
Log::warning('Platform SMS send: insufficient balance', ['user' => $ownerPublicId]);
|
||||
$this->lastError = (string) ($res->json('error') ?: 'SMS allowance exceeded. Upgrade your plan or top up your wallet.');
|
||||
Log::warning('Events suite SMS: allowance/balance', ['user' => $ownerPublicId]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($res->failed()) {
|
||||
Log::warning('Platform SMS send failed', ['status' => $res->status(), 'body' => $res->body()]);
|
||||
$this->lastError = (string) ($res->json('error') ?: 'The SMS could not be sent.');
|
||||
Log::warning('Events suite SMS 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()]);
|
||||
$this->lastError = 'Could not reach the SMS service.';
|
||||
Log::warning('Events suite SMS error', ['error' => $e->getMessage()]);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user