194 lines
7.0 KiB
PHP
194 lines
7.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* Wallet Controller
|
|
* Replaces: ride/passenger/**
|
|
* متحكم المحفظة (Wallet Controller)
|
|
*
|
|
* الغرض من الملف:
|
|
* إدارة العمليات المالية للركاب، بما في ذلك عرض الرصيد، شحن المحفظة، وعرض سجل العمليات.
|
|
*
|
|
* كيفية العمل:
|
|
* 1. يتواصل مع جداول (passengerWallet) و (payments) لجلب البيانات المالية.
|
|
* 2. يسمح للركاب بإضافة أموال لمحفظتهم وتحديث رصيدهم.
|
|
* 3. يعرض قائمة بالمعاملات المالية السابقة (Transactions).
|
|
*/
|
|
class WalletController extends Controller
|
|
{
|
|
/** GET /v2/wallet/passenger */
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$wallet = DB::connection('primary')->table('passengerWallet')
|
|
->where('passenger_id', $id)->first();
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'data' => $wallet ?? ['passenger_id' => $id, 'balance' => '0.00'],
|
|
]);
|
|
}
|
|
|
|
/** GET /v2/wallet/passenger/balance */
|
|
public function balance(Request $request): JsonResponse
|
|
{
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$userType = $request->attributes->get('_jwt_user_type');
|
|
|
|
if ($userType === 'driver') {
|
|
$bal = DB::connection('primary')->table('captain_wallet')
|
|
->where('captain_id', $id)->value('balance') ?? '0.00';
|
|
} else {
|
|
$bal = DB::connection('primary')->table('passengerWallet')
|
|
->where('passenger_id', $id)->value('balance') ?? '0.00';
|
|
}
|
|
|
|
return response()->json(['status' => 'success', 'data' => ['balance' => $bal]]);
|
|
}
|
|
|
|
/** GET /v2/wallet/driver/balance (Explicit) */
|
|
public function driverBalance(Request $request): JsonResponse
|
|
{
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$bal = DB::connection('primary')->table('captain_wallet')
|
|
->where('captain_id', $id)->value('balance') ?? '0.00';
|
|
|
|
return response()->json(['status' => 'success', 'data' => ['balance' => $bal]]);
|
|
}
|
|
|
|
/** POST /v2/wallet/passenger */
|
|
public function addFunds(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'amount' => 'required|numeric|min:0.01',
|
|
'payment_method' => 'required|string',
|
|
]);
|
|
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$userType = $request->attributes->get('_jwt_user_type');
|
|
|
|
if ($userType !== 'passenger') {
|
|
return response()->json(['status' => 'failure', 'message' => 'Only passengers can add funds manually'], 403);
|
|
}
|
|
|
|
DB::connection('primary')->beginTransaction();
|
|
try {
|
|
$wallet = DB::connection('primary')->table('passengerWallet')
|
|
->where('passenger_id', $id)->lockForUpdate()->first();
|
|
|
|
if ($wallet) {
|
|
DB::connection('primary')->table('passengerWallet')
|
|
->where('passenger_id', $id)
|
|
->increment('balance', $request->input('amount'));
|
|
} else {
|
|
DB::connection('primary')->table('passengerWallet')->insert([
|
|
'passenger_id' => $id,
|
|
'balance' => $request->input('amount'),
|
|
]);
|
|
}
|
|
|
|
// Record transaction
|
|
DB::connection('primary')->table('passengerWalletTransactions')->insert([
|
|
'passenger_id' => $id,
|
|
'amount' => $request->input('amount'),
|
|
'type' => 'credit',
|
|
'payment_method' => $request->input('payment_method'),
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
DB::connection('primary')->commit();
|
|
|
|
$newBalance = DB::connection('primary')->table('passengerWallet')
|
|
->where('passenger_id', $id)->value('balance');
|
|
|
|
return response()->json([
|
|
'status' => 'success',
|
|
'data' => ['balance' => $newBalance],
|
|
]);
|
|
} catch (\Exception $e) {
|
|
DB::connection('primary')->rollBack();
|
|
return response()->json(['status' => 'failure', 'message' => 'Transaction failed'], 500);
|
|
}
|
|
}
|
|
|
|
/** PUT /v2/wallet/passenger — ADMIN ONLY */
|
|
public function update(Request $request): JsonResponse
|
|
{
|
|
// Only admins can directly set balance
|
|
$userType = $request->attributes->get('_jwt_user_type');
|
|
if ($userType !== 'admin') {
|
|
return response()->json(['status' => 'failure', 'message' => 'Unauthorized'], 403);
|
|
}
|
|
|
|
$request->validate([
|
|
'balance' => 'required|numeric|min:0',
|
|
'user_id' => 'required|string',
|
|
'type' => 'required|in:passenger,driver',
|
|
]);
|
|
|
|
$table = $request->input('type') === 'driver' ? 'captain_wallet' : 'passengerWallet';
|
|
$key = $request->input('type') === 'driver' ? 'captain_id' : 'passenger_id';
|
|
|
|
DB::connection('primary')->table($table)
|
|
->where($key, $request->input('user_id'))
|
|
->update(['balance' => $request->input('balance')]);
|
|
|
|
return response()->json(['status' => 'success']);
|
|
}
|
|
|
|
|
|
/** GET /v2/wallet/transactions */
|
|
public function transactions(Request $request): JsonResponse
|
|
{
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
$userType = $request->attributes->get('_jwt_user_type');
|
|
$page = (int) $request->input('page', 1);
|
|
$limit = min((int) $request->input('limit', 20), 50);
|
|
|
|
if ($userType === 'driver') {
|
|
$transactions = DB::connection('primary')->table('payments')
|
|
->where('driverID', $id)
|
|
->orderBy('created_at', 'desc')
|
|
->skip(($page - 1) * $limit)
|
|
->take($limit)
|
|
->get();
|
|
} else {
|
|
$transactions = DB::connection('primary')->table('payments')
|
|
->where('passengerID', $id)
|
|
->orderBy('created_at', 'desc')
|
|
->skip(($page - 1) * $limit)
|
|
->take($limit)
|
|
->get();
|
|
}
|
|
|
|
return response()->json(['status' => 'success', 'data' => $transactions]);
|
|
}
|
|
|
|
/** POST /v2/wallet/passenger/token */
|
|
public function addToken(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'token' => 'required|string',
|
|
'amount' => 'required|numeric|min:0.01',
|
|
]);
|
|
|
|
$id = $request->attributes->get('_jwt_user_id');
|
|
|
|
DB::connection('primary')->table('payment_tokens_passenger')->insert([
|
|
'token' => $request->input('token'),
|
|
'passengerId' => $id,
|
|
'dateCreated' => now(),
|
|
'amount' => $request->input('amount'),
|
|
'isUsed' => 0,
|
|
]);
|
|
|
|
return response()->json(['status' => 'success'], 201);
|
|
}
|
|
}
|