<?php
 namespace App\Http\Controllers\Webhook; use App\Core\Sms\SmsManager; use App\Http\Controllers\Controller; use App\Models\SmsMessage; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; class SmsWebhookController extends Controller { public function handle(Request $request, string $provider): JsonResponse { $manager = app(SmsManager::class); if (! $manager->hasProvider($provider)) { abort(404); } $instance = $manager->getProviderInstance($provider); $sigValid = $instance->verifyWebhookSignature($request); if ($sigValid === false) { Log::warning('SMS webhook: signature verification failed, rejected', [ 'provider' => $provider, ]); return response()->json(['ok' => true]); } try { $normalized = $instance->handleDeliveryReport($request->all()); } catch (\Throwable $e) { Log::warning('SMS webhook: provider DLR handling failed', [ 'provider' => $provider, 'error' => $e->getMessage(), ]); return response()->json(['ok' => true]); } if (! $normalized) { return response()->json(['ok' => true]); } $message = SmsMessage::where('provider_message_id', $normalized['provider_message_id']) ->latest('id') ->first(); if (! $message) { Log::info('SMS webhook: unknown provider_message_id, ignored', [ 'provider' => $provider, 'provider_message_id' => $normalized['provider_message_id'], ]); return response()->json(['ok' => true]); } $message->update([ 'status' => $normalized['status'], 'status_detail' => $normalized['detail'], 'delivered_at' => $normalized['status'] === 'delivered' ? now() : null, ]); return response()->json(['ok' => true]); } } 