gateway = 'twofactor'; // Using 2Factor as primary gateway // 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' // Your actual 2Factor 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 - FIXED VERSION * Using the correct API endpoint format for sending OTP */ private function sendViaTwoFactor($mobileNumber, $otpCode) { $config = $this->config['twofactor']; // 2Factor OTP API endpoint // Format: https://2factor.in/API/V1/{api_key}/SMS/{mobile_number}/{otp_code} $url = "https://2factor.in/API/V1/{$config['api_key']}/SMS/{$mobileNumber}/{$otpCode}"; $result = $this->makeAPICall($url, [], 'GET'); // 2Factor API response format: {"Status":"Success","Details":"Session ID"} // Check if response is successful if ($result['success']) { $response = $result['response']; // Check if Status exists and is Success if (isset($response['Status']) && $response['Status'] == 'Success') { return [ 'success' => true, 'message' => 'OTP sent successfully via 2Factor', 'session_id' => $response['Details'] ?? null, 'response' => $response ]; } } // If failed, return error return [ 'success' => false, 'message' => isset($result['response']['Details']) ? $result['response']['Details'] : 'Failed to send OTP via 2Factor', 'response' => $result['response'] ?? [] ]; } /** * 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'); case 'twofactor': $config = $this->config['twofactor']; $url = "https://2factor.in/API/V1/{$config['api_key']}/ADDON_SERVICES/BAL/SMS"; 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 2FACTOR SMS GATEWAY * =========================================== * * Your 2Factor API Key: 79d4feb6-d168-11ea-9fa5-0200cd936042 * * 1. API is already configured in this file * 2. Current balance: 7,038 SMS credits * 3. Phone verified: Yes * 4. International OTP: Enabled * * HOW IT WORKS: * - When user requests OTP, system calls 2Factor API * - 2Factor generates and sends OTP automatically * - User receives SMS with 6-digit OTP * - System validates the OTP against database * * TESTING: * - Send test SMS to verify working: Test mobile number * - Check errors.log for detailed 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']; * } * */ ?>