gateway = 'msg91'; // Options: msg91, textlocal, twofactor, fast2sms // SMS Gateway configurations $this->config = [ 'msg91' => [ 'auth_key' => 'YOUR_MSG91_AUTH_KEY', // Replace with actual key 'template_id' => 'YOUR_TEMPLATE_ID', // Replace with actual template ID 'route' => '4', // Transactional route 'country' => '91' ], 'textlocal' => [ 'api_key' => 'YOUR_TEXTLOCAL_API_KEY', 'username' => 'YOUR_TEXTLOCAL_USERNAME', 'hash' => 'YOUR_TEXTLOCAL_HASH', 'sender' => 'TXTLCL' // 6 characters or less ], 'twofactor' => [ 'api_key' => '79d4feb6-d168-11ea-9fa5-0200cd936042' // Replace with your actual API key ], 'fast2sms' => [ 'api_key' => 'YOUR_FAST2SMS_API_KEY', 'route' => 'dlt', // DLT route for OTP 'sender_id' => 'FSTSMS' ] ]; } /** * Send OTP SMS */ public function sendOTP($mobileNumber, $otpCode, $templateMessage = null) { // Clean mobile number $mobileNumber = preg_replace('/[^0-9]/', '', $mobileNumber); // Remove leading zero if present if (substr($mobileNumber, 0, 1) === '0') { $mobileNumber = substr($mobileNumber, 1); } // Default OTP message if (!$templateMessage) { $templateMessage = "Your Relevant Reflex verification code is: {$otpCode}. Valid for 10 minutes. Do not share this with anyone."; } switch ($this->gateway) { case 'msg91': return $this->sendViaMSG91($mobileNumber, $otpCode, $templateMessage); case 'textlocal': return $this->sendViaTextLocal($mobileNumber, $templateMessage); case 'twofactor': return $this->sendViaTwoFactor($mobileNumber, $otpCode); case 'fast2sms': return $this->sendViaFast2SMS($mobileNumber, $templateMessage); default: return $this->mockSend($mobileNumber, $otpCode); // For testing } } /** * MSG91 SMS Gateway */ private function sendViaMSG91($mobileNumber, $otpCode, $message) { $config = $this->config['msg91']; $url = "https://api.msg91.com/api/v5/otp"; $data = [ 'template_id' => $config['template_id'], 'mobile' => $config['country'] . $mobileNumber, 'authkey' => $config['auth_key'], 'otp' => $otpCode, 'extra_param' => json_encode(['otp' => $otpCode]) ]; return $this->makeAPICall($url, $data, 'POST'); } /** * TextLocal SMS Gateway */ private function sendViaTextLocal($mobileNumber, $message) { $config = $this->config['textlocal']; $url = "https://api.textlocal.in/send/"; $data = [ 'apikey' => $config['api_key'], 'numbers' => '91' . $mobileNumber, 'message' => $message, 'sender' => $config['sender'] ]; return $this->makeAPICall($url, $data, 'POST'); } /** * 2Factor SMS Gateway */ private function sendViaTwoFactor($mobileNumber, $otpCode) { $config = $this->config['twofactor']; // Use AUTOGEN3 for pre-approved template (works immediately) $url = "https://2factor.in/API/V1/{$config['api_key']}/SMS/91{$mobileNumber}/{$otpCode}/AUTOGEN3"; return $this->makeAPICall($url, [], 'GET'); } /** * Fast2SMS Gateway */ private function sendViaFast2SMS($mobileNumber, $message) { $config = $this->config['fast2sms']; $url = "https://www.fast2sms.com/dev/bulkV2"; $data = [ 'authorization' => $config['api_key'], 'sender_id' => $config['sender_id'], 'message' => $message, 'route' => $config['route'], 'numbers' => $mobileNumber ]; $headers = [ 'authorization: ' . $config['api_key'], 'Content-Type: application/json' ]; return $this->makeAPICall($url, $data, 'POST', $headers); } /** * Mock SMS sending for testing */ private function mockSend($mobileNumber, $otpCode) { logError('Mock SMS sent', [ 'mobile' => $mobileNumber, 'otp' => $otpCode, 'message' => 'OTP for testing: ' . $otpCode ]); return [ 'success' => true, 'message' => 'SMS sent successfully (Mock)', 'reference_id' => 'MOCK_' . time() ]; } /** * Make API call to SMS gateway */ private function makeAPICall($url, $data, $method = 'POST', $headers = []) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); if ($method === 'POST') { curl_setopt($ch, CURLOPT_POST, true); if (!empty($data)) { if (empty($headers) || !in_array('Content-Type: application/json', $headers)) { curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); } else { curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } } } if (!empty($headers)) { curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); curl_close($ch); if ($error) { logError('SMS API Error', ['error' => $error, 'url' => $url]); return [ 'success' => false, 'message' => 'Network error: ' . $error ]; } $decodedResponse = json_decode($response, true); logError('SMS API Response', [ 'url' => $url, 'http_code' => $httpCode, 'response' => $decodedResponse ?: $response ]); // Parse response based on gateway return $this->parseResponse($decodedResponse ?: $response, $httpCode); } /** * Parse SMS gateway response */ private function parseResponse($response, $httpCode) { if ($httpCode >= 200 && $httpCode < 300) { return [ 'success' => true, 'message' => 'SMS sent successfully', 'response' => $response ]; } else { return [ 'success' => false, 'message' => 'SMS sending failed', 'response' => $response ]; } } /** * Get available SMS balance (if supported by gateway) */ public function getBalance() { switch ($this->gateway) { case 'msg91': $config = $this->config['msg91']; $url = "https://api.msg91.com/api/balance.php?authkey={$config['auth_key']}&type=4"; return $this->makeAPICall($url, [], 'GET'); default: return ['success' => false, 'message' => 'Balance check not supported for this gateway']; } } } // Global SMS helper function function sendOTPSMS($mobileNumber, $otpCode) { $smsManager = new SMSManager(); return $smsManager->sendOTP($mobileNumber, $otpCode); } /* * SETUP INSTRUCTIONS FOR SMS GATEWAYS * =================================== * * 1. MSG91 Setup: * - Sign up at https://msg91.com/ * - Get Auth Key from dashboard * - Create OTP template and get Template ID * - Update auth_key and template_id in config above * * 2. TextLocal Setup: * - Sign up at https://www.textlocal.in/ * - Get API Key from settings * - Update api_key in config above * * 3. 2Factor Setup: * - Sign up at https://2factor.in/ * - Get API Key from dashboard * - Update api_key in config above * * 4. Fast2SMS Setup: * - Sign up at https://www.fast2sms.com/ * - Get API Key from dashboard * - Update api_key in config above * * TESTING: * - Set gateway to 'mock' for testing without sending real SMS * - Check errors.log for SMS logs * * USAGE IN YOUR CODE: * * require_once 'sms-config.php'; * * $result = sendOTPSMS('9876543210', '123456'); * if ($result['success']) { * echo "SMS sent successfully"; * } else { * echo "Failed to send SMS: " . $result['message']; * } * */ // Example DLT (Distributed Ledger Technology) approved template for India: /* * Template: "Your verification code for Relevant Reflex is {#var#}. Valid for 10 minutes. Do not share this OTP with anyone." * * Replace {#var#} with actual OTP in your template * Make sure to register this template with your SMS provider for DLT compliance */ ?>