File Manager Lite
Dir:
/home/codewavebd/public_html/app/Services
Upload
[..]
WhatsAppService.php (2 KB)
Edit
Rename
Del
Edit: WhatsAppService.php
<?php namespace App\Services; use App\Models\Setting; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class WhatsAppService { public function sendOTP($number, $code) { $enabled = Setting::get('whatsapp_2fa_enabled', 'false') === 'true'; if (!$enabled) { return false; } $instanceId = Setting::get('whatsapp_2fa_instance_id'); $token = Setting::get('whatsapp_2fa_token'); $message = "Your CodeWave BD Admin OTP code is: *{$code}*. It is valid for 5 minutes. Please do not share this code."; // Format number: ensure it has country code prefix $number = preg_replace('/[^0-9]/', '', $number); if (strlen($number) === 11 && str_starts_with($number, '01')) { $number = '880' . substr($number, 1); } // Fallback/Simulated Log delivery if credentials are not filled if (empty($instanceId) || empty($token)) { Log::info("SIMULATED WHATSAPP OTP SEND to {$number}: {$message}"); // Storing the last sent OTP in session so we can display it on the verification page for testing convenience session()->flash('simulated_otp_alert', "Development Mode: Sent OTP to WhatsApp number {$number}. Check laravel.log or use code: {$code}"); return true; } try { $response = Http::post("https://api.ultramsg.com/{$instanceId}/messages/chat", [ 'token' => $token, 'to' => $number, 'body' => $message, 'priority' => 10 ]); if ($response->successful()) { Log::info("WhatsApp OTP sent successfully via UltraMsg to {$number}."); return true; } Log::error("Failed to send WhatsApp OTP: " . $response->body()); return false; } catch (\Exception $e) { Log::error("WhatsApp API Error: " . $e->getMessage()); return false; } } }
Simpan