75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Api\Controller;
|
|
use App\Loan;
|
|
use App\UsersWallet;
|
|
use App\AccountLog;
|
|
use Illuminate\Http\Request;
|
|
use App\Token;
|
|
use App\Users;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class LoanController extends Controller
|
|
{
|
|
public function institution()
|
|
{
|
|
$name = DB::table('settings')->where('key', 'loan_institution')->value('value') ?? 'Global Finance';
|
|
return $this->success(['institution' => $name]);
|
|
}
|
|
|
|
public function submit(Request $request)
|
|
{
|
|
$user_id = Token::getUserIdByToken(Token::getToken());
|
|
$user = \App\Users::find($user_id);
|
|
if (!$user) return $this->error('Please login first');
|
|
|
|
$amount = $request->input('amount');
|
|
$card_image = $request->input('card_image', '');
|
|
$cardbm_image = $request->input('cardbm_image', '');
|
|
$hand_image = $request->input('hand_image', '');
|
|
|
|
if (!$amount || !is_numeric($amount) || $amount <= 0) {
|
|
return $this->error('Please enter a valid amount');
|
|
}
|
|
if (!$card_image || !$cardbm_image || !$hand_image) {
|
|
return $this->error('Please upload all required documents');
|
|
}
|
|
|
|
$pending = Loan::where('user_id', $user->id)->where('status', 0)->first();
|
|
if ($pending) {
|
|
return $this->error('You already have a pending loan application');
|
|
}
|
|
|
|
$institution = DB::table('settings')->where('key', 'loan_institution')->value('value') ?? 'Global Finance';
|
|
|
|
Loan::create([
|
|
'user_id' => $user->id,
|
|
'amount' => $amount,
|
|
'card_image' => $card_image,
|
|
'cardbm_image' => $cardbm_image,
|
|
'hand_image' => $hand_image,
|
|
'institution' => $institution,
|
|
'status' => 0,
|
|
'create_time' => now(),
|
|
]);
|
|
|
|
return $this->success('Loan application submitted successfully');
|
|
}
|
|
|
|
public function myLoans(Request $request)
|
|
{
|
|
$user_id = Token::getUserIdByToken(Token::getToken());
|
|
$user = \App\Users::find($user_id);
|
|
if (!$user) return $this->error('Please login first');
|
|
|
|
$list = Loan::where('user_id', $user->id)->orderBy('id', 'desc')->limit(20)->get();
|
|
$statusMap = ['0' => 'Pending', '1' => 'Approved', '2' => 'Rejected'];
|
|
foreach ($list as $item) {
|
|
$item->status_text = $statusMap[$item->status] ?? '';
|
|
}
|
|
return $this->success($list);
|
|
}
|
|
}
|