cms-frontend/app/Http/Controllers/Api/TopUpController.php

122 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Services\Api\ApiService;
use Illuminate\Http\Request;
class TopUpController extends Controller
{
protected ApiService $apiService;
public function __construct(ApiService $apiService)
{
$this->apiService = $apiService;
}
public function index()
{
try {
$response = $this->apiService->get('/top-ups');
if ($response->successful()) {
return view('pages.top-up', [
'topUps' => $response->json()['data']
]);
}
return back()->with('error', 'Unable to fetch top-up records.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
public function store(Request $request)
{
$validated = $request->validate([
'member_id' => 'required|string',
'amount' => 'required|numeric|min:0',
'payment_method' => 'required|string',
'reference_number' => 'required|string|unique:top_ups,reference_number',
'status' => 'required|string|in:pending,completed,failed',
'notes' => 'nullable|string'
]);
try {
$response = $this->apiService->post('/top-ups', $validated);
if ($response->successful()) {
return redirect()->route('top-up')
->with('success', 'Top-up transaction created successfully.');
}
return back()->withErrors($response->json()['errors']);
} catch (\Exception $e) {
return back()->with('error', 'Unable to create top-up transaction.');
}
}
public function getSettings()
{
try {
$response = $this->apiService->get('/top-up-settings');
if ($response->successful()) {
return view('pages.top-up-settings', [
'settings' => $response->json()['data']
]);
}
return back()->with('error', 'Unable to fetch top-up settings.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
public function updateSettings(Request $request)
{
$validated = $request->validate([
'min_amount' => 'required|numeric|min:0',
'max_amount' => 'required|numeric|min:0|gt:min_amount',
'allowed_payment_methods' => 'required|array',
'processing_fee' => 'required|numeric|min:0',
'is_enabled' => 'required|boolean'
]);
try {
$response = $this->apiService->put('/top-up-settings', $validated);
if ($response->successful()) {
return redirect()->route('top-up-settings')
->with('success', 'Top-up settings updated successfully.');
}
return back()->withErrors($response->json()['errors']);
} catch (\Exception $e) {
return back()->with('error', 'Unable to update top-up settings.');
}
}
public function getTransactionHistory(Request $request)
{
$filters = $request->validate([
'start_date' => 'nullable|date',
'end_date' => 'nullable|date|after_or_equal:start_date',
'status' => 'nullable|string|in:pending,completed,failed',
'payment_method' => 'nullable|string'
]);
try {
$response = $this->apiService->get('/top-ups/history', $filters);
if ($response->successful()) {
return response()->json($response->json());
}
return response()->json(['error' => 'Unable to fetch transaction history'], 400);
} catch (\Exception $e) {
return response()->json(['error' => 'Service unavailable'], 503);
}
}
}