File: /var/www/html/app/Livewire/Auth/BecomeASeller.php
<?php
namespace App\Livewire\Auth;
use Livewire\Component;
use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
class BecomeASeller extends Component
{
public $nin;
public $name;
public $last_name;
public $middle_name;
public $dob;
public $email;
public $gender;
public $phone;
public $nationality_id;
public $countries = [];
public $password;
public $device_name;
public $role = "seller";
protected $rules = [
"nin" => "required|string|unique:seller_nins,nin",
"name" => "required|string|max:100",
"last_name" => "required|string|max:100",
"middle_name" => "required|string|max:100",
"dob" => "required",
"email" => "required|email|unique:users,email",
"gender" => "required|string|max:10",
"password" => "required|valid_password",
"phone" => "required|digits:11",
"nationality_id" => "required|exists:countries,id",
"device_name" => "required|string|max:255",
'role' => 'required|string|in:seller',
];
public function mount()
{
$this->countries = Cache::remember('countries', 60 * 60 * 24 * 30, function () {
return Http::get(ApiEndpoints::getCountry())->json()['data'];
});
$this->device_name = Session::get('device_name');
}
// public function registerSeller()
// {
// try {
// $this->validate();
// $body = [
// 'name' => $this->name,
// 'last_name' => $this->last_name,
// 'middle_name' => $this->middle_name,
// 'dob' => $this->dob,
// 'email' => $this->email,
// 'gender' => $this->gender,
// 'phone' => $this->phone,
// 'password' => $this->password,
// 'nationality_id' => $this->nationality_id,
// 'device_name' => $this->device_name,
// 'nin' => $this->nin,
// 'role' => $this->role,
// ];
// $response = Http::post(ApiEndpoints::registerSeller(), $body);
// dd($response->json());
// if ($response->successful()) {
// Session::flash('success', 'A 6 digit OTP has been sent to your phone number');
// return redirect()->to('/verify-phone');
// } else {
// throw new \Exception('Failed to register seller.' . $response->error());
// }
// } catch (\Throwable $th) {
// info($th->getMessage());
// $this->addError('registration', 'Failed to register seller: ' . $th->getMessage());
// Session::flash('error', 'Failed to register seller: ' . $th->getMessage());
// }
// }
public function verifyNIN()
{
try {
// Validate NIN
$this->validate([
'nin' => 'required|digits:11',
"name" => "required|string|max:100",
"email" => "required|email|unique:users,email",
"password" => "required|valid_password",
"nationality_id" => "required|exists:countries,id",
]);
// Make API call to validate NIN
$response = Http::post(ApiEndpoints::validateNIN(), [
'nin' => $this->nin,
]);
// If the response is successful, proceed with registration
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(['user_phone' => $this->phone]);
// Notify success and proceed to registration
$this->registerSeller();
Session::flash('success', 'A 6-digit OTP has been sent to your phone number.');
} else {
$this->addError('nin', $response->json()['message']);
}
} catch (\Throwable $th) {
$this->addError('nin', $th->getMessage());
Session::flash('error', 'Failed to validate NIN: ' . $th->getMessage());
}
}
public function registerSeller()
{
$this->validate();
try {
$body = [
'nin' => $this->nin,
'name' => $this->name,
'lastname' => $this->last_name,
'middlename' => $this->middle_name,
'dob' => $this->dob,
'email' => $this->email,
'gender' => $this->gender,
'phone' => $this->phone,
'password' => $this->password,
'nationality_id' => $this->nationality_id,
'device_name' => $this->device_name,
'role' => $this->role,
'ip_address' => Session::get('country')['ip'],
];
// Make API call to register seller
$response = Http::post(ApiEndpoints::registerSeller(), $body);
if ($response->successful()) {
Session::flash('success', 'A 6-digit OTP has been sent to your phone number');
return redirect()->to('/verify-phone');
} else {
throw new \Exception('Failed to register seller: ' . $response->json()['error']);
}
} catch (\Throwable $th) {
$this->addError('registration', 'Failed to register seller: ' . $th->getMessage());
Session::flash('error', 'Failed to register seller: ' . $th->getMessage());
}
}
public function render()
{
return view('livewire.auth.become-a-seller');
}
}