File: /var/www/api.vaspayment.com/app/Http/Controllers/API/NINVerificationController.php
<?php
namespace App\Http\Controllers\API;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Wallet;
use App\Models\Transaction;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\ValidationException;
class NINVerificationController extends Controller
{
private function baseUrl()
{
return config('app.nin_verification');
}
private function token()
{
// Get credential
$apiKey = config('app.monnify_key');
$secretKey = config('app.monnify_sk_key');
$credentials = "{$apiKey}:{$secretKey}";
$base64Credentials = base64_encode($credentials);
return $token = "{$base64Credentials}";
}
private function header()
{
// Generate Access token
$bearer = Http::withHeaders(['Authorization' => 'Basic ' . $this->token()])->post(config('app.login_url'));
$bearerToken = $bearer->json()['responseBody']['accessToken'];
$header = [
"Authorization" => "Bearer $bearerToken",
"Accept" => "application/json",
"Content-Type" => "application/json",
];
return $header;
}
public function verify(Request $request)
{
try {
$validated = $request->validate([
'nin' => 'required|min:11|numeric',
'wallet_id' => 'required|exists:wallets,id',
]);
} catch (ValidationException $e) {
$message = $e->getMessage();
return $this->validationErrorResponse($message);
}
$body = [
"nin" => $validated['nin'],
];
try {
$response = $this->postToNinEndpoint($body);
if ($response->successful()) {
info($response->json());
return $this->handleSuccessfulResponse($response, $validated['wallet_id']);
} else {
return $this->handleUnsuccessfulResponse($response);
}
} catch (\Throwable $th) {
$message = $th->getMessage();
return $this->errorResponse($message);
}
}
private function validationErrorResponse($message)
{
return response()->json([
'status' => 'error',
'message' => $message,
], 422);
}
private function postToNinEndpoint($body)
{
$ninEndpoint = $this->baseUrl();
return Http::withHeaders($this->header())->post($ninEndpoint, $body);
}
private function handleSuccessfulResponse($response, $walletId)
{
$responseData = $response->json();
if (isset($responseData['requestSuccessful']) && isset($responseData['responseMessage']) && isset($responseData['responseCode'])) {
if ($responseData['requestSuccessful'] === true) {
$this->updateUserInformation($responseData['responseBody'], $walletId);
$this->updateUserWallet($responseData['responseBody'], $walletId);
$this->recordTransaction($walletId);
$message = $responseData['responseBody'];
return $this->successResponse($message);
} else {
$message = $responseData['message'];
return $this->errorResponse($message);
}
}
}
private function handleUnsuccessfulResponse($response)
{
$errorMessage = $response->json()['responseMessage'];
return $this->errorResponse($errorMessage);
}
private function updateUserInformation($responseBody, $walletId)
{
$user = User::findOrFail($walletId);
return response()->json(["ninBody" => $responseBody]);
}
private function updateUserWallet($responseBody, $walletId)
{
$wallet = Wallet::where('id', $walletId)->first();
if ($wallet) {
$amount = "100"; // change this to admin setting later
$wallet->update([
'balance' => $wallet->balance -= $amount,
'status' => 0, // now set active
]);
}
}
private function recordTransaction($walletId)
{
$reference = "VASPAYMENT" . "NIN" . "" . date("YmdHis") . "" . $walletId;
$trans = new Transaction();
$trans->reference = $reference;
$trans->amount = "100"; // add this to admin feature
$trans->commission = "0";
$trans->status = 200;
$trans->type = "NIN";
$trans->network = "AGENT VERIFICATION";
$trans->destination = "Your NIN verification for customer is successful";
$trans->wallet_id = $walletId;
$trans->save();
}
private function successResponse($message)
{
return response()->json([
'status' => 'success',
'message' => $message,
], 200);
}
private function errorResponse($message)
{
return response()->json([
'status' => 'error',
'message' => $message,
], 422);
}
}