migrant-web/app/Http/Controllers/Api/EmployerPaymentController.php
2026-06-19 16:05:22 +05:30

74 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use App\Models\Payment;
use Illuminate\Support\Facades\DB;
class EmployerPaymentController extends Controller
{
/**
* GET /api/employers/payments
* Get the payment transaction history for the authenticated employer.
*/
public function getPayments(Request $request)
{
/** @var User $employer */
$employer = $request->attributes->get('employer');
try {
$employerId = $employer->id;
$page = (int)$request->input('page', 1);
$perPage = (int)$request->input('per_page', 15);
$query = DB::table('subscriptions')
->where('user_id', $employerId)
->orderBy('created_at', 'desc');
$total = $query->count();
$offset = ($page - 1) * $perPage;
$payments = $query->skip($offset)->take($perPage)->get()
->map(function ($sub) {
$planLabel = ucfirst($sub->plan_id) . ' Sponsor Pass';
$startsAt = $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('Y-m-d H:i:s', strtotime($startsAt)),
'formatted_date' => date('M d, Y', strtotime($startsAt)),
];
});
return response()->json([
'success' => true,
'data' => [
'payments' => $payments,
'pagination' => [
'total' => $total,
'per_page' => $perPage,
'current_page' => $page,
'last_page' => max(1, (int)ceil($total / $perPage)),
]
]
], 200);
} catch (\Exception $e) {
logger()->error('Mobile API Employer Get Payments Failure: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'An error occurred while fetching the payment history.',
'error' => app()->environment('local') ? $e->getMessage() : 'Internal Server Error'
], 500);
}
}
}