<?php
namespace App\Http\Controllers\User\Auth;
use Exception;
use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Admin\BasicSettings;
use App\Traits\User\RegisteredUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rules\Password;
use App\Providers\Admin\BasicSettingsProvider;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Validation\ValidationException;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers, RegisteredUsers;
protected $basic_settings;
public function __construct()
{
$this->basic_settings = BasicSettingsProvider::get();
$this->middleware(function($request, $next) {
if($this->basic_settings->user_registration == false) return redirect()->route('frontend.index');
return $next($request);
});
}
/**
* Show the application registration form.
*
* @return \Illuminate\View\View
*/
public function showRegistrationForm() {
$client_ip = request()->ip() ?? false;
$user_country = geoip()->getLocation($client_ip)['country'] ?? "";
$basic_settings = BasicSettings::first();
$page_title = "User Registration";
return view('user.auth.register',compact(
'page_title',
'user_country'
));
}
/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
public function register(Request $request)
{
$validated = $this->validator($request->all())->validate();
$basic_settings = $this->basic_settings;
$validated = Arr::except($validated,['agree']);
$validated['email_verified'] = ($basic_settings->email_verification == true) ? false : true;
$validated['sms_verified'] = ($basic_settings->sms_verification == true) ? false : true;
$validated['kyc_verified'] = ($basic_settings->kyc_verification == true) ? false : true;
$validated['password'] = Hash::make($validated['password']);
$validated['username'] = make_username($validated['firstname'],$validated['lastname']);
if(User::where("username",$validated['username'])->exists()) {
throw ValidationException::withMessages([
'unknown' => "Username already exists!",
]);
}
event(new Registered($user = $this->create($validated)));
$this->guard()->login($user);
return $this->registered($request, $user);
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
public function validator(array $data) {
$basic_settings = $this->basic_settings;
$password_rule = "required|string|min:6";
if($basic_settings->secure_password) {
$password_rule = ["required",Password::min(8)->letters()->mixedCase()->numbers()->symbols()->uncompromised()];
}
$agree_rule = "nullable";
if($basic_settings->agree_policy) {
$agree_rule = 'required|in:on';
}
return Validator::make($data,[
'firstname' => 'required|string|max:60',
'lastname' => 'required|string|max:60',
'email' => 'required|string|email|max:150|unique:users,email',
'password' => $password_rule,
'agree' => $agree_rule,
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\Models\User
*/
protected function create(array $data)
{
return User::create($data);
}
/**
* The user has been registered.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function registered(Request $request, $user)
{
try{
$this->createUserWallets($user);
$this->createMerchant($user);
return redirect()->intended(route('user.dashboard'));
}catch(Exception $e) {
return redirect()->back()->with(['error' => ['Something went wrong! Please try again']]);
}
}
}
Get access token to initiates payment transaction.
generate-token
| Parameter | Type | Comments |
|---|---|---|
| client_id | string | Enter merchant API client/primary key |
| secret_id | string | Enter merchant API secret key |
| env | string | Enter merchant API environment |
| merchant_id | string | Enter merchant API merchant id |
Request Example (guzzle)
<?php
require_once('vendor/autoload.php');
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $base_url. 'v1/generate-token', [
'headers' => [
'accept' => 'application/json',
'content-type' => 'application/json',
],
'form_params' => [
'client_id' => '$client_id',
'secret_id' => 'secret_id',
'env' => 'env',
'merchant_id' => 'merchant_id',
],
]);
echo $response->getBody();
**Response: SUCCESS (200 OK)**
{
"message": {
"success": [
"Successfully token is generated"
]
},
"data": {
"token":"eyJpdiI6InpkczhjTjhQdVhUL2lKQ0pSUUx6aUE9PSIsInZhbHVlIjoiVGVBTVBDTXltbjNZcEIvdEJveGpTSno3TU5NRUtnVkhCZ1pHTFNCUnZGQ2UxMnYxN202cEE1YVRDTEFsc0ZERExoTjdtL0dTL2xoU3QzeUJJOExiMUx5T0w1L0llUXhTUkU1cWVLWEdEbEplb0dKNXcwbTNRM0VxdkUwYzZuNFdtNkhMQ0pRZysyNWkvdzBxSlBoSVBSOGFTekNnR2RXNHVtcG9lMGZOTmNCcm1hR3c5Sk9KTnB4Y3ltZDl6cm90MThrR21Ca3B1azc3bXRiQ0J6SW96UVo1elNkU1ZqeE05bTcwWGp1MEUxWlJFdnNWTmpSbnVpeW92b2U4dXZkUGgyb1VmK0luaGdyaFlsVTZlcVpVRnZlTG1DeFF6Ykk2T2h6Z3JzbnIyNHpNdHowSE5JdDR0Y0pZT20zUm1XYW8iLCJtYWMiOiJlY2M4NGE1OGUzYzkzYzk0YzljNmVmNjE0YWI0ZDIwOGI3NDQ2YWEyY2ZhNzc0NzE4ZmY1ZmYyMz
IyZmQzNDY1IiwidGFnIjoiIn0=",
},
"type": "success"
}
**Response: ERROR (400 FAILED)**
{
"message": {
"error": [
"Invalid credentials."
]
},
"data": null,
"type": "error"
}