GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/console.fixgini.com/app/Services/PhoneOtpService.php
<?php

namespace App\Services;

use App\Models\User;
use App\Mail\OtpMail;
use App\Models\OtpVerification;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Mail;

class PhoneOtpService
{
    private function termiiUrl()
    {
        return config('services.termii.baseUrl');
    }

    private function header()
    {
        $api = config('services.termii.api');

        $headers = [
            "Authorization" => "Bearer $api",
            "Accept" => "application/json",
            "Content-Type" => "application/json",
        ];
        return $headers;
    }
    public function sendOtpToPhone(string $user): void
    {
        try {
            $phone = $user['phone'];
            $email = $user['email'];
            $name = $user['name'];

            if ($phone) {
                $otp = random_int(000000, 999999);
                OtpVerification::updateOrCreate([
                    'email' => $email,
                    'phone' => $phone,
                    'otp' => $otp
                ]);

                // change the phone format here only
                if (substr($phone, 0, 1) == '0') {
                    $Fphone = '234' . substr($phone, 1);
                }

                $body = [
                    'api_key' => config('services.termii.api'),
                    'to' => $Fphone,
                    'from' => 'N-Alert',
                    'sms' => 'Hello ' . $name . ', your phone number confirmation code is ' . $otp . '. Thank you for choosing FixGini Services',
                    'type' => 'plain',
                    'channel' => 'dnd',
                ];
                $baseUrl = $this->termiiUrl();
                $response = Http::withHeaders($this->header())->post($baseUrl, $body);
                if ($response->successful()) {
                    info('OTP response: ' . $response->json()['message']);
                    // return response(['status' => 'success', 'message' => 'OTP sent to phone number successfully'], 200);
                } else {
                    info('OTP response: ' . $response->json()['message']);
                    // return response(['status' => 'failed', 'message' => 'Unable to send OTP to phone successfully'], 500);
                }
                // return response(['status' => 'success', 'message' => 'OTP sent to phone number successfully'], 200);
            }
        } catch (\Throwable $th) {
            info('OTP response: ' . $th->getMessage());
            return response(['status' => 'failed', 'message' => 'Network Issue! Please try again later', 'data' => $th->getMessage()], 500);
        }
    }
}