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/html/app/Livewire/Buyer/PaymentHistory.php
<?php

namespace App\Livewire\Buyer;

use App\Services\ApiEndpoints;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Livewire\Component;

class PaymentHistory extends Component
{
    public $paymentHistories = [];
    public $transactions = [];
    public $user;

    public function mount()
    {
        $this->user = Session::get('user');
        // Session::forget('success');
        Session::forget('error');

        if ($this->user && $this->user['role'] == 'seller') {
            $this->userWallet();
            $this->sellerPaymentHistory();
        } else {
            $this->buyerPaymentHistory();
        }
    }

    public function sellerPaymentHistory()
    {
        try {
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->get(ApiEndpoints::fetchSellerPaymentHistory());
            if ($response->successful()) {
                $this->paymentHistories = $response->json()['data'];
                $this->transactions = $response->json()['transactions'];
                // dd($this->paymentHistories);
                // dd($this->transactions);
                // Session::flash('success', $response->json()['message']);
            } else {
                // Session::flash('error', $response->json()['message']);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            $this->addError('orders', 'Failed to fetch seller gig. Please try again later.');
        }
    }

    public function buyerPaymentHistory()
    {
        try {
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->get(ApiEndpoints::fetchBuyerPaymentHistory());
            if ($response->successful()) {
                $this->paymentHistories = $response->json()['data'];
                // Session::flash('success', $response->json()['message']);
            } else {
                // Session::flash('error', $response->json()['message']);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            $this->addError('orders', 'Failed to fetch seller gig. Please try again later.');
        }
    }

    public $wallet;

    public function userWallet()
    {
        try {
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->get(ApiEndpoints::fetchUserWallet());
            if ($response->successful()) {
                $this->wallet = $response->json()['data'];
                // Session::flash('success', $response->json()['message']);
            } else {
                // Session::flash('error', $response->json()['message']);
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            $this->addError('orders', 'Failed to fetch user wallet. Please try again later.');
        }
    }

    public function withdraw()
    {
        try {
            $apiEndpoints = new ApiEndpoints();
            $headers = $apiEndpoints->header();
            $response = Http::withHeaders($headers)
                ->get(ApiEndpoints::requestWithdraw());
            if ($response->json()['status'] == 'success') {
                Session::flash('success', $response->json()['message']);
                //reload user wallet
                // $this->userWallet();
                $this->redirect('/account/payment/history');
            } else {
                Session::flash('error', $response->json()['message']);
                $this->redirect('/account/payment/history');
            }
        } catch (\Throwable $e) {
            Log::error($e->getMessage());
            Session::flash('error', 'Unable to perform this request. Please try again later');
            $this->redirect('/account/payment/history');
        }
    }

    public function render()
    {
        return view('livewire.buyer.payment-history');
    }
}