Initial V2 commit
This commit is contained in:
151
app/Http/Controllers/WalletController.php
Normal file
151
app/Http/Controllers/WalletController.php
Normal file
@@ -0,0 +1,151 @@
|
||||
<?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/passengerWallet/*.php, ride/driverWallet/*.php
|
||||
*/
|
||||
class WalletController extends Controller
|
||||
{
|
||||
/** GET /v2/wallet/passenger */
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$id = $request->input('_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->input('_jwt_user_id');
|
||||
$bal = DB::connection('primary')->table('passengerWallet')
|
||||
->where('passenger_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->input('_jwt_user_id');
|
||||
|
||||
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 */
|
||||
public function update(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate(['balance' => 'required|numeric|min:0']);
|
||||
|
||||
$id = $request->input('_jwt_user_id');
|
||||
|
||||
DB::connection('primary')->table('passengerWallet')
|
||||
->where('passenger_id', $id)
|
||||
->update(['balance' => $request->input('balance')]);
|
||||
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
|
||||
/** DELETE /v2/wallet/passenger */
|
||||
public function destroy(Request $request): JsonResponse
|
||||
{
|
||||
$id = $request->input('_jwt_user_id');
|
||||
DB::connection('primary')->table('passengerWallet')
|
||||
->where('passenger_id', $id)->delete();
|
||||
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
|
||||
/** GET /v2/wallet/passenger/transactions */
|
||||
public function transactions(Request $request): JsonResponse
|
||||
{
|
||||
$id = $request->input('_jwt_user_id');
|
||||
$page = (int) $request->input('page', 1);
|
||||
$limit = min((int) $request->input('limit', 20), 50);
|
||||
|
||||
// Get from payments table (completed rides)
|
||||
$payments = 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' => $payments]);
|
||||
}
|
||||
|
||||
/** POST /v2/wallet/passenger/token */
|
||||
public function addToken(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'token' => 'required|string',
|
||||
'amount' => 'required|numeric|min:0.01',
|
||||
]);
|
||||
|
||||
$id = $request->input('_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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user