198 lines
7.4 KiB
PHP
Executable File
198 lines
7.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Models\Configuration;
|
|
use App\Models\PaymentTransaction;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class PaymentTransactionController extends BaseApiController
|
|
{
|
|
protected PaymentTransaction $transaction;
|
|
protected Configuration $config;
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->transaction = model(PaymentTransaction::class);
|
|
$this->config = model(Configuration::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)));
|
|
$paymentId = $this->request->getGet('payment_id');
|
|
$status = $this->request->getGet('status');
|
|
|
|
$query = $this->transaction->newQuery();
|
|
|
|
if ($paymentId) {
|
|
$query->where('payment_id', $paymentId);
|
|
}
|
|
|
|
if ($status) {
|
|
$query->where('payment_status', $status);
|
|
}
|
|
|
|
$result = $this->paginate($query, $page, $perPage);
|
|
|
|
return $this->success($result, 'Payment transactions retrieved successfully');
|
|
}
|
|
|
|
public function show($id = null)
|
|
{
|
|
$transaction = $this->transaction->find($id);
|
|
if (!$transaction) {
|
|
return $this->error('Payment transaction not found', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
return $this->success($transaction, 'Payment transaction retrieved successfully');
|
|
}
|
|
|
|
/**
|
|
* GET /api/v1/payment-transactions/payment/{paymentId}
|
|
* Get all transactions for a specific payment
|
|
*/
|
|
public function getByPayment($paymentId = null)
|
|
{
|
|
if (!$paymentId) {
|
|
return $this->error('Payment ID is required', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
try {
|
|
$transactions = $this->transaction->getTransactionsByPaymentId($paymentId);
|
|
|
|
if (empty($transactions)) {
|
|
return $this->error('Transactions not found for the payment', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
return $this->success($transactions, 'Payment transactions retrieved successfully');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Get transactions by payment error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to retrieve payment transactions', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* POST /api/v1/payment-transactions
|
|
* Create a new payment transaction (installment)
|
|
*/
|
|
public function store()
|
|
{
|
|
$data = $this->payloadData();
|
|
if (empty($data)) {
|
|
return $this->error('Invalid request data', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$rules = [
|
|
'payment_id' => 'required|integer',
|
|
'transaction_id' => 'required|unique:payment_transactions,transaction_id',
|
|
'amount' => 'required|numeric|min:0',
|
|
'payment_method' => 'required|string|max:50',
|
|
'transaction_date' => 'permit_empty|valid_date[Y-m-d H:i:s]',
|
|
'transaction_fee' => 'permit_empty|numeric|min:0',
|
|
'payment_reference' => 'permit_empty|string|max:255',
|
|
'is_full_payment' => 'permit_empty|in:0,1',
|
|
];
|
|
|
|
$errors = $this->validateRequest($data, $rules);
|
|
if (!empty($errors)) {
|
|
return $this->respondValidationError($errors);
|
|
}
|
|
|
|
$schoolYear = $this->config->getConfig('school_year');
|
|
$semester = $this->config->getConfig('semester');
|
|
|
|
$transactionData = [
|
|
'transaction_id' => $data['transaction_id'],
|
|
'payment_id' => (int) $data['payment_id'],
|
|
'transaction_date' => $data['transaction_date'] ?? date('Y-m-d H:i:s'),
|
|
'amount' => (float) $data['amount'],
|
|
'payment_method' => $data['payment_method'],
|
|
'payment_status' => 'Pending', // Default status as per CodeIgniter version
|
|
'transaction_fee' => isset($data['transaction_fee']) ? (float) $data['transaction_fee'] : 0,
|
|
'payment_reference' => $data['payment_reference'] ?? null,
|
|
'is_full_payment' => isset($data['is_full_payment']) ? ((int) $data['is_full_payment'] ? 1 : 0) : 0,
|
|
'school_year' => $schoolYear,
|
|
'semester' => $semester,
|
|
];
|
|
|
|
try {
|
|
$transaction = $this->transaction->create($transactionData);
|
|
return $this->respondCreated($transaction->toArray(), 'Payment transaction created successfully');
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Payment transaction creation error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to create payment transaction', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* PATCH /api/v1/payment-transactions/{transactionId}/status
|
|
* Update payment status by transaction ID
|
|
*/
|
|
public function updateStatus($transactionId = null)
|
|
{
|
|
if (!$transactionId) {
|
|
return $this->error('Transaction ID is required', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$data = $this->payloadData();
|
|
if (empty($data) || !isset($data['status'])) {
|
|
return $this->error('Status is required', Response::HTTP_BAD_REQUEST);
|
|
}
|
|
|
|
$status = (string) $data['status'];
|
|
|
|
$rules = [
|
|
'status' => 'required|string|max:50',
|
|
];
|
|
|
|
$errors = $this->validateRequest($data, $rules);
|
|
if (!empty($errors)) {
|
|
return $this->respondValidationError($errors);
|
|
}
|
|
|
|
try {
|
|
// First check if transaction exists by transaction_id (string) or id (integer)
|
|
$transaction = null;
|
|
if (is_numeric($transactionId)) {
|
|
$transaction = $this->transaction->find((int) $transactionId);
|
|
}
|
|
|
|
if (!$transaction) {
|
|
$transaction = $this->transaction->getTransactionById($transactionId);
|
|
}
|
|
|
|
if (!$transaction) {
|
|
return $this->error('Transaction not found', Response::HTTP_NOT_FOUND);
|
|
}
|
|
|
|
// Update by transaction_id if it's a string, otherwise by id
|
|
$updated = false;
|
|
if (is_numeric($transactionId)) {
|
|
$updated = $this->transaction->update((int) $transactionId, ['payment_status' => $status]);
|
|
} else {
|
|
$updated = $this->transaction->updateTransactionStatus($transactionId, $status);
|
|
}
|
|
|
|
if ($updated) {
|
|
// Fetch updated transaction
|
|
$updatedTransaction = is_numeric($transactionId)
|
|
? $this->transaction->find((int) $transactionId)
|
|
: $this->transaction->getTransactionById($transactionId);
|
|
|
|
return $this->success([
|
|
'status' => 'success',
|
|
'transaction' => $updatedTransaction,
|
|
], 'Transaction status updated successfully');
|
|
} else {
|
|
return $this->respondError('Failed to update transaction status', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
} catch (\Throwable $e) {
|
|
log_message('error', 'Update transaction status error: ' . $e->getMessage());
|
|
return $this->respondError('Failed to update transaction status', Response::HTTP_INTERNAL_SERVER_ERROR);
|
|
}
|
|
}
|
|
}
|