Files
2026-08-01 11:42:42 +07:00

41 lines
1.7 KiB
PHP

<?php
namespace App\Services\Notification\Adapters;
use App\Models\NotificationDelivery;
use App\Services\Notification\Contracts\NotificationChannelAdapter;
use App\Services\Notification\WhapiIdService;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;
class WhatsAppUnofficialAdapter implements NotificationChannelAdapter
{
public function send(NotificationDelivery $delivery): array
{
$config = $delivery->channelConfig;
if ($config?->provider === 'whapi_id') {
$response = app(WhapiIdService::class)->send($config, $delivery->destination, $delivery->rendered_body);
return ['provider_message_id' => data_get($response, 'results.id_message'), 'response' => $response];
}
$credentials = $config?->credentials ?? [];
$settings = $config?->settings ?? [];
$url = $settings['send_url'] ?? null;
if (! $url) {
throw ValidationException::withMessages(['channel' => 'URL pengiriman gateway unofficial belum diatur.']);
}
$headers = [];
if ($apiKey = $credentials['api_key'] ?? null) {
$headers[$settings['api_key_header'] ?? 'X-API-Key'] = $apiKey;
}
$payload = [
$settings['destination_field'] ?? 'number' => $delivery->destination,
$settings['message_field'] ?? 'message' => $delivery->rendered_body,
'instance_id' => $credentials['instance_id'] ?? null,
];
$response = Http::withHeaders($headers)->timeout(30)->post($url, $payload)->throw()->json();
return ['provider_message_id' => data_get($response, $settings['message_id_path'] ?? 'id'), 'response' => $response];
}
}