File: /var/www/vaspayment.com/app/Http/Controllers/ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Key;
use App\Models\Cable;
use App\Models\Product;
use App\Models\DataPlan;
use App\Models\Electricity;
use App\Models\Transaction;
use App\Models\Business;
use Illuminate\Http\Request;
use App\Models\AirtimeCommission;
use App\Models\AirtimeToCashRequest;
use App\Models\DataCategory;
use Illuminate\Support\Facades\Http;
class ProductController extends Controller
{
private function baseUrl()
{
return config('app.honour_world');
}
private function header()
{
$email = config('app.mail');
$token = Key::where('email', $email)->value('live_key');
$headers = [
"Authorization" => "Bearer " . $token,
"Accept" => "application/json",
"Content-Type" => "application/json",
];
return $headers;
}
public function __construct()
{
if (config('app.verification') === true) {
$this->middleware(['auth', 'verified']);
} else {
$this->middleware(['auth']);
}
}
public function index()
{
header('Cache-Control: public, max-age=604800');
$products = Product::orderBy('created_at', 'desc')->get();
return view('product.index', compact('products'));
}
public function showProduct(Product $product, Request $request, DataCategory $category)
{
header('Cache-Control: public, max-age=604800');
$user = auth()->user();
$userId = $user->id;
$email = config('app.mail');
$transactions = Transaction::where('type', 'airtime')->where('user_id', $userId)->latest()->paginate(10);
$business = Business::where('email', $email)->first();
if ($product->id == 1) {
// Airtime API
$nets = ['MTN', 'GLO', 'AIRTEL', '9MOBILE'];
if (auth()->user()->role == 'agent') {
$transactions = Transaction::where('type', 'airtime')->latest()->paginate(10);
}
return view('dashboard.agent.products.show', compact('product', 'transactions', 'nets', 'business'));
} elseif ($product->id == 2) {
$datas = Transaction::where('type', 'data')->where('user_id', $userId)->latest()->paginate(10);
if (auth()->user()->role == 'customer') {
return view('dashboard.agent.products.data.index', compact('product', 'datas'));
} else {
// Data API
try {
$plans = $this->baseUrl() . '/api/v2/data';
$response = Http::withHeaders($this->header())->get($plans);
if ($response->successful()) {
$plans = $response->json()['data'];
$soldData = Transaction::where('type', 'data')->latest();
return view('dashboard.agent.products.data.list', compact('product', 'soldData', 'plans', 'datas'));
} else {
return redirect()->back()->with('error', $response->json(['error'])[0]['msg']);
}
} catch (\Exception $e) {
return redirect()->back()->with('error', 'Failed to connect to the API. Please check your internet connection and try again later.');
}
$plans = DataPlan::get();
return view('dashboard.agent.products.data.index', compact('product', 'datas', 'plans'));
}
} elseif ($product->id == 3) {
// Cable API
$cableC = Transaction::where('type', 'cable')->where('user_id', $userId)->latest()->paginate(10);
$cables = Cable::select('id', 'name', 'type', 'reseller_price', 'price', 'month', 'code')->latest()->get();
$gotv = Cable::select('id', 'name', 'type', 'reseller_price', 'price', 'month', 'code')->where('type', 'GOTV')->latest()->get();
$dstv = Cable::select('id', 'name', 'type', 'reseller_price', 'price', 'month', 'code')->where('type', 'DSTV')->latest()->get();
return view('dashboard.agent.products.cable.index', compact('product', 'cables', 'cableC', 'gotv', 'dstv'));
} elseif ($product->id == 4) {
// Electicity ID
$electricity = Transaction::where('type', 'electricity')->where('user_id', $userId)->latest()->paginate(10);
$provider = Electricity::select('id', 'provider', 'status')->where('status', '0')->get();
return view('dashboard.agent.products.electricity.index', compact('product', 'electricity', 'provider'));
} elseif ($product->id == 5) {
//Airtime to Cash ID
$selectedNetwork = $request->input('network');
// Fetch commission data based on the selected network
$commissionData = AirtimeCommission::where('network', $selectedNetwork)->first();
$airtimetocash = AirtimeToCashRequest::where('user_id', $userId)->latest()->paginate(30);
return view('dashboard.agent.products.airtime.airtimetocash', compact('product', 'airtimetocash', 'selectedNetwork'));
}
}
}