paypalPayment = model(PayPalPayment::class); } public function index() { $page = max(1, (int) ($this->request->getGet('page') ?? 1)); $perPage = min(100, max(1, (int) ($this->request->getGet('per_page') ?? 20))); $keyword = $this->request->getGet('q'); $query = $this->paypalPayment->newQuery()->orderBy('created_at', 'DESC'); if ($keyword) { $query->where(function ($q) use ($keyword) { $q->where('transaction_id', 'LIKE', "%{$keyword}%") ->orWhere('payer_email', 'LIKE', "%{$keyword}%") ->orWhere('event_type', 'LIKE', "%{$keyword}%") ->orWhere('order_id', 'LIKE', "%{$keyword}%") ->orWhere('parent_school_id', 'LIKE', "%{$keyword}%"); }); } $result = $this->paginate($query, $page, $perPage); return $this->success($result, 'PayPal transactions retrieved successfully'); } public function show($id = null) { $transaction = $this->paypalPayment->find($id); if (!$transaction) { return $this->error('PayPal transaction not found', Response::HTTP_NOT_FOUND); } return $this->success($transaction, 'PayPal transaction retrieved successfully'); } /** * GET /api/v1/paypal-transactions/transaction/{transactionId} * Get transaction by PayPal transaction ID */ public function getByTransactionId($transactionId = null) { $transaction = $this->paypalPayment->newQuery() ->where('transaction_id', $transactionId) ->first(); if (!$transaction) { return $this->error('PayPal transaction not found', Response::HTTP_NOT_FOUND); } return $this->success($transaction, 'PayPal transaction retrieved successfully'); } /** * GET /api/v1/paypal-transactions/export-csv * Export PayPal transactions to CSV file */ public function exportCsv(): StreamedResponse { $keyword = $this->request->getGet('q'); $query = $this->paypalPayment->newQuery()->orderBy('created_at', 'DESC'); if ($keyword) { $query->where(function ($q) use ($keyword) { $q->where('transaction_id', 'LIKE', "%{$keyword}%") ->orWhere('payer_email', 'LIKE', "%{$keyword}%") ->orWhere('event_type', 'LIKE', "%{$keyword}%") ->orWhere('order_id', 'LIKE', "%{$keyword}%") ->orWhere('parent_school_id', 'LIKE', "%{$keyword}%"); }); } $transactions = $query->get()->toArray(); $filename = 'paypal_transactions_' . date('Ymd_His') . '.csv'; $headers = [ 'Content-Type' => 'text/csv', 'Content-Disposition' => "attachment; filename=\"{$filename}\"", 'Pragma' => 'no-cache', 'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', 'Expires' => '0', ]; return ResponseFacade::streamDownload(function () use ($transactions) { $output = fopen('php://output', 'w'); // CSV headers fputcsv($output, [ 'ID', 'Transaction ID', 'Order ID', 'Parent School ID', 'Email', 'Amount', 'Net Amount', 'Currency', 'Status', 'Event Type', 'Created At', ]); foreach ($transactions as $t) { $t = (array) $t; // Convert to array for consistent access fputcsv($output, [ $t['id'] ?? '', $t['transaction_id'] ?? '', $t['order_id'] ?? '', $t['parent_school_id'] ?? '', $t['payer_email'] ?? '', $t['amount'] ?? '', $t['net_amount'] ?? '', $t['currency'] ?? '', $t['status'] ?? '', $t['event_type'] ?? '', $t['created_at'] ?? '', ]); } fclose($output); }, $filename, $headers); } }