File size: 6,357 Bytes
52d9fb0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <?php
namespace App\Http\Controllers\User;
use App\Constants\Status;
use App\Http\Controllers\Controller;
use App\Lib\FormProcessor;
use App\Models\AdminNotification;
use App\Models\Transaction;
use App\Models\Withdrawal;
use App\Models\WithdrawMethod;
use Illuminate\Http\Request;
class WithdrawController extends Controller
{
public function withdrawMoney()
{
$withdrawMethod = WithdrawMethod::active()->get();
$pageTitle = 'Withdraw Money';
return view('Template::user.withdraw.methods', compact('pageTitle','withdrawMethod'));
}
public function withdrawStore(Request $request)
{
$request->validate([
'method_code' => 'required',
'amount' => 'required|numeric'
]);
$method = WithdrawMethod::where('id', $request->method_code)->active()->firstOrFail();
$user = auth()->user();
if ($request->amount < $method->min_limit) {
$notify[] = ['error', 'Your requested amount is smaller than minimum amount'];
return back()->withNotify($notify)->withInput($request->all());
}
if ($request->amount > $method->max_limit) {
$notify[] = ['error', 'Your requested amount is larger than maximum amount'];
return back()->withNotify($notify)->withInput($request->all());
}
if ($request->amount > $user->balance) {
$notify[] = ['error', 'Insufficient balance for withdrawal'];
return back()->withNotify($notify)->withInput($request->all());
}
$charge = $method->fixed_charge + ($request->amount * $method->percent_charge / 100);
$afterCharge = $request->amount - $charge;
if ($afterCharge <= 0) {
$notify[] = ['error', 'Withdraw amount must be sufficient for charges'];
return back()->withNotify($notify)->withInput($request->all());
}
$finalAmount = $afterCharge * $method->rate;
$withdraw = new Withdrawal();
$withdraw->method_id = $method->id; // wallet method ID
$withdraw->user_id = $user->id;
$withdraw->amount = $request->amount;
$withdraw->currency = $method->currency;
$withdraw->rate = $method->rate;
$withdraw->charge = $charge;
$withdraw->final_amount = $finalAmount;
$withdraw->after_charge = $afterCharge;
$withdraw->trx = getTrx();
$withdraw->save();
session()->put('wtrx', $withdraw->trx);
return to_route('user.withdraw.preview');
}
public function withdrawPreview()
{
$withdraw = Withdrawal::with('method','user')->where('trx', session()->get('wtrx'))->where('status', Status::PAYMENT_INITIATE)->orderBy('id','desc')->firstOrFail();
$pageTitle = 'Withdraw Preview';
return view('Template::user.withdraw.preview', compact('pageTitle','withdraw'));
}
public function withdrawSubmit(Request $request)
{
$withdraw = Withdrawal::with('method','user')->where('trx', session()->get('wtrx'))->where('status', Status::PAYMENT_INITIATE)->orderBy('id','desc')->firstOrFail();
$method = $withdraw->method;
if ($method->status == Status::DISABLE) {
abort(404);
}
$formData = @$method->form->form_data ?? [];
$formProcessor = new FormProcessor();
$validationRule = $formProcessor->valueValidation($formData);
$request->validate($validationRule);
$userData = $formProcessor->processFormData($request, $formData);
$user = auth()->user();
if ($user->ts) {
$response = verifyG2fa($user,$request->authenticator_code);
if (!$response) {
$notify[] = ['error', 'Wrong verification code'];
return back()->withNotify($notify)->withInput($request->all());
}
}
if ($withdraw->amount > $user->balance) {
$notify[] = ['error', 'Your request amount is larger then your current balance.'];
return back()->withNotify($notify)->withInput($request->all());
}
$withdraw->status = Status::PAYMENT_PENDING;
$withdraw->withdraw_information = $userData;
$withdraw->save();
$user->balance -= $withdraw->amount;
$user->save();
$transaction = new Transaction();
$transaction->user_id = $withdraw->user_id;
$transaction->amount = $withdraw->amount;
$transaction->post_balance = $user->balance;
$transaction->charge = $withdraw->charge;
$transaction->trx_type = '-';
$transaction->details = 'Withdraw request via ' . $withdraw->method->name;
$transaction->trx = $withdraw->trx;
$transaction->remark = 'withdraw';
$transaction->save();
$adminNotification = new AdminNotification();
$adminNotification->user_id = $user->id;
$adminNotification->title = 'New withdraw request from '.$user->username;
$adminNotification->click_url = urlPath('admin.withdraw.data.details',$withdraw->id);
$adminNotification->save();
notify($user, 'WITHDRAW_REQUEST', [
'method_name' => $withdraw->method->name,
'method_currency' => $withdraw->currency,
'method_amount' => showAmount($withdraw->final_amount,currencyFormat:false),
'amount' => showAmount($withdraw->amount,currencyFormat:false),
'charge' => showAmount($withdraw->charge,currencyFormat:false),
'rate' => showAmount($withdraw->rate,currencyFormat:false),
'trx' => $withdraw->trx,
'post_balance' => showAmount($user->balance,currencyFormat:false),
]);
$notify[] = ['success', 'Withdraw request sent successfully'];
return to_route('user.withdraw.history')->withNotify($notify);
}
public function withdrawLog(Request $request)
{
$pageTitle = "Withdrawal Log";
$withdraws = Withdrawal::where('user_id', auth()->id())->where('status', '!=', Status::PAYMENT_INITIATE);
if ($request->search) {
$withdraws = $withdraws->where('trx',$request->search);
}
$withdraws = $withdraws->with('method')->orderBy('id','desc')->paginate(getPaginate());
return view('Template::user.withdraw.log', compact('pageTitle','withdraws'));
}
}
|