diff --git a/.env.example b/.env.example index d37fc33..4f78e30 100644 --- a/.env.example +++ b/.env.example @@ -79,4 +79,7 @@ GOOGLE_CLIENT_SECRET=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 GOOGLE_REDIRECT_URI=http://localhost:8000/login/google/callback -XENDIT_PRIVATE_KEY= \ No newline at end of file +XENDIT_PRIVATE_KEY= + + +WAHITS_TOKEN= \ No newline at end of file diff --git a/app/Http/Controllers/Auth/LoginEmailController.php b/app/Http/Controllers/Auth/LoginEmailController.php index 43629d0..e7bac65 100644 --- a/app/Http/Controllers/Auth/LoginEmailController.php +++ b/app/Http/Controllers/Auth/LoginEmailController.php @@ -9,6 +9,8 @@ use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; +use App\Notifications\Auth\RequestOtp; + class LoginEmailController extends Controller { protected $memberAuthRepository; @@ -50,8 +52,7 @@ class LoginEmailController extends Controller // Use MemberAuthRepository to generate OTP $otp = $this->memberAuthRepository->emailOtp(['email' => $email]); - // TODO: Integrate with WhatsApp API to send OTP - // For now, we'll just log it (remove in production) + $user->notify(new RequestOtp($otp->otp, "mail")); Log::info("OTP for {$email}: {$otp->otp}"); return response()->json([ diff --git a/app/Http/Controllers/Auth/LoginWaController.php b/app/Http/Controllers/Auth/LoginWaController.php index cf28252..150c9c9 100644 --- a/app/Http/Controllers/Auth/LoginWaController.php +++ b/app/Http/Controllers/Auth/LoginWaController.php @@ -9,6 +9,8 @@ use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Validator; +use App\Notifications\Auth\RequestOtp; + class LoginWaController extends Controller { protected $memberAuthRepository; @@ -55,8 +57,9 @@ class LoginWaController extends Controller // Use MemberAuthRepository to generate OTP $otp = $this->memberAuthRepository->waOtp(['phone' => $identity]); - // TODO: Integrate with WhatsApp API to send OTP - // For now, we'll just log it (remove in production) + $user->notify(new RequestOtp($otp->otp)); + + Log::info("OTP for {$identity}: {$otp->otp}"); return response()->json([ diff --git a/app/Notifications/Auth/RequestOtp.php b/app/Notifications/Auth/RequestOtp.php new file mode 100644 index 0000000..52dc6dc --- /dev/null +++ b/app/Notifications/Auth/RequestOtp.php @@ -0,0 +1,75 @@ +otp = $otp; + $this->prefer = $prefer; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return $this->prefer == "wa" ? [WahitsChannel::class] : ["mail"]; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toMail($notifiable) + { + return (new MailMessage) + ->subject('Your One-Time Password (OTP)') + ->greeting('Hi '.$notifiable->name) + ->line('Your One-Time Password (OTP) is:') + ->line("**{$this->otp}**") + ->line('This code is valid for 10 minutes.') + ->line('If you did not request this, please ignore this email.') + ->salutation('Regards, Asia Golf'); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toWa($notifiable) + { + $otp = $this->otp; + return [ + "text" => "$otp ini adalah kode OTP untuk anda login. +JANGAN berikan kode OTP ke siapapun!" + ]; + } +} diff --git a/app/Notifications/WaNotification.php b/app/Notifications/WaNotification.php new file mode 100644 index 0000000..3174a55 --- /dev/null +++ b/app/Notifications/WaNotification.php @@ -0,0 +1,12 @@ +routeNotificationFor('wa') + : $notifiable->phone; + + $phones = explode("/", $phone); + + foreach($phones as $phone){ + + $phone = trim($phone); + $phone = substr($phone,0,1) == "0" ? "62".substr($phone,1,strlen($phone)):$phone; + $phone = preg_replace("/[^0-9]/", "",$phone); + + $payload = $notification->toWa($notifiable); + $message = $payload["text"]; + $image = @$payload["image"]; + $link = @$payload["link"]; + + $token = config("wahits.token"); + + $curl = curl_init(); + curl_setopt_array($curl, array( + CURLOPT_URL => 'https://wa.wahits.com/api/send_message', + CURLOPT_RETURNTRANSFER => true, + CURLOPT_ENCODING => '', + CURLOPT_MAXREDIRS => 10, + CURLOPT_TIMEOUT => 0, + CURLOPT_FOLLOWLOCATION => true, + CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => "token=$token&number=$phone&message=$message&link=$link", + CURLOPT_HTTPHEADER => array( + 'Content-Type: application/x-www-form-urlencoded' + ), + )); + $response = curl_exec($curl); +\Log::info($response); + curl_close($curl); + } + } +} diff --git a/app/Repositories/Member/Auth/MemberAuthRepository.php b/app/Repositories/Member/Auth/MemberAuthRepository.php index 03b7f3b..9efa49e 100644 --- a/app/Repositories/Member/Auth/MemberAuthRepository.php +++ b/app/Repositories/Member/Auth/MemberAuthRepository.php @@ -40,9 +40,9 @@ class MemberAuthRepository function generateUniqueOtp() { - if (config('app.env') == 'local'){ - return 123456; - } + // if (config('app.env') == 'local'){ + // return 123456; + // } do { $otp = rand(100000, 999999); } while (UserOtp::where('otp', $otp)->where("expired_at",">", Carbon::now())->exists()); diff --git a/config/wahits.php b/config/wahits.php new file mode 100644 index 0000000..85de394 --- /dev/null +++ b/config/wahits.php @@ -0,0 +1,5 @@ + env("WAHITS_TOKEN") +]; \ No newline at end of file