Implement new API controllers for wallet management and discount code validation. This includes adding an Artisan command for exchange rate updates and refining the API routing structure. Additionally, perform a significant cleanup of the repository by removing obsolete migrations, debug scripts, and temporary test files. Security and configuration improvements include restricting CORS origins via environment variables and updating the system settings schema. - Add `WalletController` and `DiscountCodeController` APIs - Add `UpdateExchangeRates` command - Remove redundant migrations and debug/test scripts - Secure CORS configuration using `CORS_ALLOWED_ORIGINS` - Update `STATUS.md` to reflect current phase progress
164 lines
4.8 KiB
PHP
164 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Models\User;
|
|
use App\Models\Wallet;
|
|
use App\Services\WalletService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class WalletController
|
|
{
|
|
public function __construct(protected WalletService $walletService) {}
|
|
|
|
public function balance(Request $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
if (!$user) {
|
|
return response()->json(['message' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
$wallet = $user->wallet()->first();
|
|
|
|
if (!$wallet) {
|
|
return response()->json([
|
|
'balance' => 0,
|
|
'currency' => 'IRR',
|
|
]);
|
|
}
|
|
|
|
return response()->json([
|
|
'balance' => $wallet->balance,
|
|
'currency' => 'IRR',
|
|
]);
|
|
}
|
|
|
|
public function recharge(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'amount' => ['required', 'numeric', 'min:10000'],
|
|
'description' => ['nullable', 'string', 'max:500'],
|
|
]);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validation failed',
|
|
'errors' => $e->errors(),
|
|
], 422);
|
|
}
|
|
|
|
$user = $request->user();
|
|
|
|
if (!$user) {
|
|
return response()->json(['message' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
$wallet = $user->wallet()->first();
|
|
|
|
if (!$wallet) {
|
|
$wallet = Wallet::create([
|
|
'user_id' => $user->id,
|
|
'balance' => 0,
|
|
]);
|
|
}
|
|
|
|
$transaction = $this->walletService->deposit(
|
|
$wallet,
|
|
$validated['amount'],
|
|
$validated['description'] ?? 'شارژ آنلاین کیف پول',
|
|
);
|
|
|
|
return response()->json([
|
|
'message' => 'شارژ با موفقیت انجام شد',
|
|
'wallet_id' => $wallet->id,
|
|
'balance' => $wallet->fresh()->balance,
|
|
'transaction_id' => $transaction->id,
|
|
'amount' => $transaction->amount,
|
|
]);
|
|
}
|
|
|
|
public function transactions(Request $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
if (!$user) {
|
|
return response()->json(['message' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
$wallet = $user->wallet()->first();
|
|
|
|
if (!$wallet) {
|
|
return response()->json(['transactions' => [], 'total' => 0]);
|
|
}
|
|
|
|
$perPage = $request->query('per_page', 15);
|
|
|
|
$transactions = $wallet->transactions()
|
|
->orderByDesc('created_at')
|
|
->paginate($perPage);
|
|
|
|
return response()->json([
|
|
'transactions' => $transactions->map(fn ($t) => [
|
|
'id' => $t->id,
|
|
'amount' => $t->amount,
|
|
'type' => $t->type,
|
|
'description' => $t->description,
|
|
'created_at' => $t->created_at->toIso8601String(),
|
|
]),
|
|
'total' => $transactions->total(),
|
|
'current_page' => $transactions->currentPage(),
|
|
'last_page' => $transactions->lastPage(),
|
|
]);
|
|
}
|
|
|
|
public function adminRecharge(Request $request): JsonResponse
|
|
{
|
|
$user = $request->user();
|
|
|
|
if (!$user || !$user->isSuperAdmin()) {
|
|
return response()->json(['message' => 'Forbidden'], 403);
|
|
}
|
|
|
|
try {
|
|
$validated = $request->validate([
|
|
'user_id' => ['required', 'exists:users,id'],
|
|
'amount' => ['required', 'numeric', 'min:1'],
|
|
'description' => ['nullable', 'string', 'max:500'],
|
|
]);
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'message' => 'Validation failed',
|
|
'errors' => $e->errors(),
|
|
], 422);
|
|
}
|
|
|
|
$targetUser = User::findOrFail($validated['user_id']);
|
|
$wallet = $targetUser->wallet()->first();
|
|
|
|
if (!$wallet) {
|
|
$wallet = Wallet::create([
|
|
'user_id' => $targetUser->id,
|
|
'balance' => 0,
|
|
]);
|
|
}
|
|
|
|
$transaction = $this->walletService->deposit(
|
|
$wallet,
|
|
$validated['amount'],
|
|
$validated['description'] ?? 'شارژ دستی توسط ادمین',
|
|
);
|
|
|
|
return response()->json([
|
|
'message' => 'شارژ دستی با موفقیت انجام شد',
|
|
'wallet_id' => $wallet->id,
|
|
'target_user_id' => $targetUser->id,
|
|
'balance' => $wallet->fresh()->balance,
|
|
'transaction_id' => $transaction->id,
|
|
'amount' => $transaction->amount,
|
|
]);
|
|
}
|
|
} |