// Process withdrawals
public function pwithdrawal(Request $request)
{
$withdrawal = Withdrawal::where('id', $request->id)->first();
$user = User::where('id', $withdrawal->user)->first();
$settings = Settings::find(1);
// Mock the server response or use your desired logic to create a custom response
$mockResponse = [
'success' => true,
'message' => 'Withdrawal processed successfully',
'data' => [
'action' => true, // Simulate a successful withdrawal
'balance' => $user->account_bal - $withdrawal->to_deduct, // Calculate the new balance after deduction
'reverse' => $user->account_bal, // No reversal in this example
],
];
// Process the mock response
if ($mockResponse['success']) {
// Access the response data
$data = $mockResponse['data'];
$action = $data['action'];
$balance = floatval($data['balance']);
$reverse = floatval($data['reverse']);
if ($action) {
if ($settings->deduction_option == 'AdminApprove') {
$user->account_bal = $balance;
$user->save();
}
$withdrawal->status = 'Processed';
$withdrawal->save();
$message = "This is to inform you that your withdrawal request of $settings->currency$withdrawal->amount has been approved and funds have been sent to your preferred bank account";
Mail::to($user->email)->send(new NewNotification($message, 'Successful Withdrawal', $user->name));
} else {
if ($withdrawal->user == $user->id) {
if ($settings->deduction_option == 'userRequest') {
$user->account_bal = $reverse;
$user->save();
}
$withdrawal->delete();
if ($request->emailsend == 'true') {
Mail::to($user->email)->send(new NewNotification($request->reason, $request->subject, $user->name));
}
}
}
return redirect()->route('mwithdrawals')->with('success', 'Action Successful!');
} else {
// Handle the failure case
return redirect()->back()->with('message', $mockResponse['message']);
}
}