GWM : sms api integration

This commit is contained in:
Gowtham M 2025-10-07 17:58:13 +05:30
parent 2dbb0ad464
commit 74938ec1e5
4 changed files with 92 additions and 25 deletions

View File

@ -90,5 +90,5 @@ class Autoload extends AutoloadConfig
*
* @var list<string>
*/
public $helpers = ['JwtHelper','common_helper','mail_helper','url_helper','status_helper'];
public $helpers = ['JwtHelper','common_helper','mail_helper','url_helper','status_helper','sms_helper'];
}

View File

@ -31,8 +31,26 @@ class AgentAuthController extends ResourceController
if (isset($agentData['id']))
{
$result = ['agent_verification' => true ,'message' => "Verified Successfully"];
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
$otp = random_int(100000, 999999);
$update = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->set(['email_otp' => $otp])->update();
if ($update) {
$SMSResult = sendOtpSms($mobile,$otp);
if ($SMSResult['status'] == 'success') {
$result = ['agent_verification' => true, 'message' => "Verified Successfully"];
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
$result = ['agent_verification' => false, 'message' => "Mail sending failed , try again"];
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
}
} else {
$result = ['agent_verification' => false, 'message' => "Verification failed , try again"];
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
}
} else {
@ -89,28 +107,24 @@ class AgentAuthController extends ResourceController
public function getVerifiedAgentData()
{
try {
$otp_verification = isset($this->request->getJSON()->otp_verification) ? $this->request->getJSON()->otp_verification : null;
$mobile = isset($this->request->getJSON()->mobile) ? $this->request->getJSON()->mobile : null;
$otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null;
$mobile = isset($this->request->getJSON()->mobile) ? $this->request->getJSON()->mobile : null;
$email = isset($this->request->getJSON()->email) ? $this->request->getJSON()->email : null;
$otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null;
if (isset($mobile))
{
$agentData = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->first();
$agentData = $this->AgentModel->where('mobile', $mobile)->where('is_active', 1)->where('email_otp', $otp)->first();
} else {
$agentData = $this->AgentModel->where('email', $email)->where('email_otp', $otp)->where('is_active', 1)->first();
}
if ($agentData && $otp_verification == true || $agentData && isset($this->request->getJSON()->otp) ) {
if ($agentData && isset($this->request->getJSON()->otp) ) {
if(isset($this->request->getJSON()->otp)){
$this->AgentModel->where('id', $agentData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update();
}
$this->AgentModel->where('id', $agentData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update();
$result = generateJWT($agentData);

View File

@ -31,8 +31,25 @@ class StaffAuthController extends ResourceController
if (isset($StaffData['id']))
{
$result = ['Staff_verification' => true ,'message' => "Verified Successfully"];
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);
$otp = random_int(100000, 999999);
$update = $this->StaffModel->where('mobile', $mobile)->where('is_active', 1)->set(['email_otp' => $otp])->update();
if ($update) {
$SMSResult = sendOtpSms($mobile,$otp);
if ($SMSResult['status'] == 'success') {
$result = ['Staff_verification' => true, 'message' => "Verified Successfully"];
return $this->respond(['status' => 'success', 'code' => 200, 'data' => $result], 200);
} else {
$result = ['Staff_verification' => false, 'message' => "Mail sending failed , try again"];
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
}
} else {
$result = ['Staff_verification' => false, 'message' => "Verification failed , try again"];
return $this->respond(['status' => 'failed', 'code' => 404, 'data' => $result], 200);
}
} else {
@ -89,29 +106,25 @@ class StaffAuthController extends ResourceController
public function getVerifiedStaffData()
{
try {
$otp_verification = isset($this->request->getJSON()->otp_verification) ? $this->request->getJSON()->otp_verification : null;
$mobile = isset($this->request->getJSON()->mobile) ? $this->request->getJSON()->mobile : null;
$otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null;
$email = isset($this->request->getJSON()->email) ? $this->request->getJSON()->email : null;
$otp = isset($this->request->getJSON()->otp) ? $this->request->getJSON()->otp : null;
if (isset($mobile))
{
$StaffData = $this->StaffModel->where('mobile', $mobile)->where('is_active', 1)->first();
$StaffData = $this->StaffModel->where('mobile', $mobile)->where('is_active', 1)->where('email_otp', $otp)->first();
} else {
$StaffData = $this->StaffModel->where('email', $email)->where('email_otp', $otp)->where('is_active', 1)->first();
}
if ($StaffData && $otp_verification == true || $StaffData && isset($this->request->getJSON()->otp) ) {
if(isset($this->request->getJSON()->otp)){
$this->StaffModel->where('id', $StaffData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update();
}
if ($StaffData && isset($this->request->getJSON()->otp) ) {
$this->StaffModel->where('id', $StaffData['id'])->where('email_otp', $otp)->where('is_active', 1)->set(['email_otp'=>null])->update();
$result = generateJWT($StaffData);
return $this->respond(['status' => 'success','code' => 200,'data' => $result],200);

View File

@ -0,0 +1,40 @@
<?php
use CodeIgniter\Database\BaseConnection;
if (!function_exists('sendOtpSms')) {
function sendOtpSms(string $mobile, string $otp)
{
$apiKey = env('SMS_API_KEY');
$senderId = env('SMS_SENDER_ID');
$templateId = env('SMS_TEMPLATE_ID');
$serviceName = env('SMS_SERVICE_NAME');
$appName = env('SMS_APP_NAME');
// Replace variables {#var#}
$message = "Hi, Your One Time Code for logging into Nhance {$appName} App is {$otp}. Valid for 3 minutes. Please do not share this with anyone. -NHANCE";
// Build the API URL
$url = "https://smsapi.24x7sms.com/api_2.0/SendSMS.aspx?" . http_build_query([
'APIKEY' => $apiKey,
'MobileNo' => $mobile,
'SenderID' => $senderId,
'Message' => $message,
'ServiceName' => $serviceName,
'DLTTemplateID' => $templateId,
]);
// Send request
$response = @file_get_contents($url);
// If response failed
if ($response === FALSE) {
return ['status' => 'failed', 'message' => 'Failed to send SMS.'];
}
return ['status' => 'success', 'message' => 'OTP sent successfully', 'api_response' => $response];
}
}