77 lines
2.8 KiB
PHP
77 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Employer;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Inertia\Inertia;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class PaymentController extends Controller
|
|
{
|
|
private function resolveCurrentUser(): ?User
|
|
{
|
|
// Prefer Laravel's built-in auth (most reliable)
|
|
if (auth()->check()) {
|
|
return auth()->user();
|
|
}
|
|
|
|
// Fallback: session-based user (registration auto-login flow)
|
|
$sess = session('user');
|
|
$sessId = is_array($sess) ? ($sess['id'] ?? null) : ($sess->id ?? null);
|
|
|
|
return $sessId ? User::find($sessId) : null;
|
|
}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$user = $this->resolveCurrentUser();
|
|
|
|
if (!$user) {
|
|
return redirect()->route('employer.login');
|
|
}
|
|
|
|
// Payments are stored in the subscriptions table during registration
|
|
$subscriptions = DB::table('subscriptions')
|
|
->where('user_id', $user->id)
|
|
->latest('id')
|
|
->get();
|
|
|
|
$payments = $subscriptions->map(function ($sub) {
|
|
$planLabel = ucfirst($sub->plan_id) . ' Sponsor Pass Subscription';
|
|
$date = \Carbon\Carbon::parse($sub->starts_at ?? $sub->created_at);
|
|
|
|
return [
|
|
'id' => $sub->id,
|
|
'amount' => (float) $sub->amount_aed,
|
|
'currency' => 'AED',
|
|
'description' => $planLabel,
|
|
'status' => $sub->status === 'active' ? 'success' : $sub->status,
|
|
'date' => $date->format('M d, Y'),
|
|
'time' => $date->format('h:i A'),
|
|
'invoice_no' => 'INV-' . str_pad($sub->id, 6, '0', STR_PAD_LEFT),
|
|
'transaction_id' => $sub->paytabs_transaction_id,
|
|
'expires_at' => $sub->expires_at ? \Carbon\Carbon::parse($sub->expires_at)->format('M d, Y') : null,
|
|
];
|
|
})->toArray();
|
|
|
|
// Latest active subscription for the summary cards
|
|
$activeSub = $subscriptions->where('status', 'active')->first();
|
|
$expiresAt = $activeSub && $activeSub->expires_at
|
|
? \Carbon\Carbon::parse($activeSub->expires_at)->format('M d, Y')
|
|
: null;
|
|
$currentPlan = $activeSub
|
|
? ucfirst($activeSub->plan_id) . ' Sponsor Pass'
|
|
: null;
|
|
$subStatus = ucfirst($user->subscription_status ?? 'none');
|
|
|
|
return Inertia::render('Employer/PaymentHistory', [
|
|
'payments' => $payments,
|
|
'currentPlan' => $currentPlan,
|
|
'expiresAt' => $expiresAt,
|
|
'subscriptionStatus' => $subStatus,
|
|
]);
|
|
}
|
|
}
|