IMMREX7
<?php
namespace App\Http\Controllers\API;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\QueryException;
use \App\Http\SendNotificationApi;
use App\Http\SendSmsApi;
use Illuminate\Support\Str;
use Carbon\Carbon;
use Twilio\Rest\Client;
class OTPPasswordController extends Controller {
public function sendOtp(Request $request) {
if(!isset($request->studentRole)){
$teacher = \App\Employee::where('mobile', '=', $request->mobile)->first();
if (isset($teacher)) {
$request->studentRole = "N";
}else $request->studentRole = "Y";
}
if($request->studentRole == "Y"){
$otp = mt_rand(100000, 999999);
$parent = \App\Parents::where('mobile', '=', $request->mobile)->first();
$school = \App\School::where('idSchool', '=', $parent->idSchool)->first();
$parent->otp = $otp;
$parent->otpSendAt = \Carbon\Carbon::now();
$parent->update();
$message = 'Your OTP for verifying mobile no is ' . $otp . ' Kindly Share with Receptionist. Regards ' .$school->sms_regard_text .'.';
$phone_number = 9497882610;//$request->mobile;
$template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'mobile_verify_otp')->first();
if (!empty($template)) {
$tempid = $template->template_id;
\App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
}
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}else{
$teacher = \App\Employee::where('mobile', '=', $request->mobile)->first();
if($teacher){
$otp = mt_rand(100000, 999999);
$school = \App\School::where('idSchool', '=', $teacher->idSchool)->first();
$teacher->otp = $otp;
$teacher->otpSendAt = \Carbon\Carbon::now();
$teacher->update();
$message = 'Your OTP for verifying mobile no is ' . $otp . ' Kindly Share with Receptionist. Regards ' .$school->sms_regard_text .'.';
$phone_number = $request->mobile;
$template = \App\SchoolSmsTemplate::where('idSchool', '=', $school->idSchool)->where('template_name', '=', 'mobile_verify_otp')->first();
if (!empty($template)) {
$tempid = $template->template_id;
\App\Http\SendSmsApi::getUserNumber($phone_number, $message, $school, $tempid);
}
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}else{
return response()->json(['success' => "failed",'message'=>'This Mobile no is not registered with us.'], 422, ['app-status' => 'failed']);
}
}
}
public function sendWhatsAppOtp(Request $request) {
if(!isset($request->studentRole)){
$teacher = \App\Employee::where('mobile', '=', $request->mobile)->first();
if (isset($teacher)) {
$request->studentRole = "N";
}else $request->studentRole = "Y";
}
if($request->studentRole == "Y"){
$otp = mt_rand(100000, 999999);
$parent = \App\Parents::where('mobile', '=', $request->mobile)->first();
$school = \App\School::where('idSchool', '=', $parent->idSchool)->first();
$parent->otp = $otp;
$parent->otpSendAt = \Carbon\Carbon::now();
$parent->update();
$message = $otp .' is your OTP for reseting password . Please dont share it with anyone.!';
$phone_number = $request->mobile;
return $this->sendWhatsappNotification('+91'.$phone_number, $message);
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}else{
$teacher = \App\Employee::where('mobile', '=', $request->mobile)->first();
if($teacher){
$otp = mt_rand(100000, 999999);
$school = \App\School::where('idSchool', '=', $teacher->idSchool)->first();
$teacher->otp = $otp;
$teacher->otpSendAt = \Carbon\Carbon::now();
$teacher->update();
$message = $otp .' is your OTP for reseting password . Please dont share it with anyone.!';
$phone_number = $request->mobile;
$this->sendWhatsappNotification('+91'.$phone_number, $message);
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}else{
return response()->json(['success' => "failed",'message'=>'This Mobile no is not registered with us.'], 422, ['app-status' => 'failed']);
}
}
}
private function sendWhatsappNotification(string $recipient,string $message)
{
$twilio_whatsapp_number = "+14155238886" ;
$account_sid = "AC88bc7924a2a078634a701530a9c5e72a";
$auth_token = "22da3c12a967f59e1af8ab43e9b71029";
$client = new Client($account_sid, $auth_token);
return $client->messages->create("whatsapp:$recipient", array('from' => "whatsapp:$twilio_whatsapp_number", 'body' => $message));
}
public function verifyOtp(Request $request) {
if(!isset($request->studentRole)){
$teacher = \App\Employee::where('mobile', '=', $request->mobile)->first();
if (isset($teacher)) {
$request->studentRole = "N";
}else $request->studentRole = "Y";
}
if ($request->studentRole == "Y") {
$parent = \App\Parents::where('mobile', '=', $request->mobile)->where('otp', '=', $request->otp)->first();
$to = \Carbon\Carbon::now();
$otpvalid = $parent->otpsendAt;
$diff_in_minutes = $to->diffInMinutes($otpvalid);
if ($diff_in_minutes <= 5) {
$token= Str::random(32);
$parent->password_token=$token;
$parent->passwordSendAt = \Carbon\Carbon::now();
$parent->update();
return response()->json(['success' => "SUCCESS",'message'=>$token], 200, ['app-status' => 'success']);
} else {
return response()->json(['success' => "failed",'message'=>'Otp is invalid. Re-enter/Re-generate the OTP'], 422, ['app-status' => 'failed']);
}
} else {
$teacher = \App\Employee::where('mobile', '=', $request->mobile)->first();
if($teacher){
$to = \Carbon\Carbon::now();
$otpvalid = $teacher->otpsendAt;
$diff_in_minutes = $to->diffInMinutes($otpvalid);
if ($diff_in_minutes <= 5) {
$token= Str::random(32);
$teacher->password_token=$token;
$teacher->passwordSendAt = \Carbon\Carbon::now();
$teacher->update();
return response()->json(['success' => "SUCCESS",'message'=>$token], 200, ['app-status' => 'success']);
} else {
return response()->json(['success' => "failed",'message'=>'Otp is invalid. Re-enter/Re-generate the OTP'], 422, ['app-status' => 'failed']);
}
}else{
return response()->json(['success' => "failed",'message'=>'Otp is invalid. Re-enter/Re-generate the OTP'], 422, ['app-status' => 'failed']);
}
}
}
public function resetPassword(Request $request){
if(!isset($request->studentRole)){
$teacher = \App\Employee::where('mobile', '=', $request->mobile)->first();
if (isset($teacher)) {
$request->studentRole = "N";
}else $request->studentRole = "Y";
}
if ($request->studentRole == "Y") {
$to = \Carbon\Carbon::now();
$parent = \App\Parents::where('mobile', '=', $request->mobile)->first();
$otpvalid = $parent->passwordSendAt;
$diff_in_minutes = $to->diffInMinutes($otpvalid);
if ($diff_in_minutes <= 10 && $request->token==$parent->password_token) {
$parent->password = bcrypt($request->pwd);
if($parent->save())
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
else
return response()->json(['success' => "Failed"], 200, ['app-status' => 'success']);
}else{
return response()->json(['success' => "failed",'message'=>'Token has expired please try again'], 200, ['app-status' => 'failed']);
}
}else{
$teacher = \App\Employee::where('mobile', '=', $request->mobile)->first();
if($teacher){
$to = \Carbon\Carbon::now();
$otpvalid = $teacher->passwordSendAt;
$diff_in_minutes = $to->diffInMinutes($otpvalid);
if ($diff_in_minutes <= 10 && $request->token==$teacher->password_token) {
$teacher->password = bcrypt($request->pwd);
if($teacher->save())
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
else
return response()->json(['success' => "Failed"], 200, ['app-status' => 'success']);
}else{
return response()->json(['success' => "failed",'message'=>'Token has expired please try again'], 200, ['app-status' => 'failed']);
}
}else{
return response()->json(['success' => "failed",'message'=>'Token has expired please try again'], 200, ['app-status' => 'failed']);
}}
}
}
Copyright © 2021 -