migrant-web/app/Http/Controllers/Api/EmployerPaymentController.php
2026-05-29 17:49:37 +05:30

87 lines
3.2 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;
// Auto-seed a couple of payments if none exist for a richer UX
$paymentsCount = Payment::where('user_id', $employerId)->count();
if ($paymentsCount === 0) {
// Determine their plan
$subscription = DB::table('subscriptions')->where('user_id', $employerId)->first();
$planName = $subscription ? (ucfirst($subscription->plan_id) . ' Pass Subscription') : 'Premium Sponsor Pass Subscription';
$planAmount = $subscription ? $subscription->amount_aed : 199.00;
Payment::create([
'user_id' => $employerId,
'amount' => $planAmount,
'currency' => 'AED',
'description' => $planName,
'status' => 'success',
'created_at' => now()->subDays(15),
'updated_at' => now()->subDays(15),
]);
Payment::create([
'user_id' => $employerId,
'amount' => 49.00,
'currency' => 'AED',
'description' => 'OCR Document Vetting Fee',
'status' => 'success',
'created_at' => now()->subDays(5),
'updated_at' => now()->subDays(5),
]);
}
$payments = Payment::where('user_id', $employerId)
->latest()
->get()
->map(function ($payment) {
return [
'id' => $payment->id,
'amount' => (float)$payment->amount,
'currency' => $payment->currency,
'description' => $payment->description,
'status' => $payment->status,
'date' => $payment->created_at->format('Y-m-d H:i:s'),
'formatted_date' => $payment->created_at->format('M d, Y'),
];
});
return response()->json([
'success' => true,
'data' => [
'payments' => $payments
]
], 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);
}
}
}