донат шоп, управление магазином, яндекс метрика
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Services\BalanceService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class GameBalanceController extends Controller
|
||||
{
|
||||
public function show(int $accountId, BalanceService $balances): JsonResponse
|
||||
{
|
||||
return response()->json(['account_id' => $accountId, 'balance' => $balances->balance($accountId)]);
|
||||
}
|
||||
|
||||
public function spend(Request $request, int $accountId, BalanceService $balances): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'amount' => ['required', 'numeric', 'decimal:0,2', 'gt:0'],
|
||||
'idempotency_key' => ['required', 'string', 'max:160'],
|
||||
'description' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
try {
|
||||
$transaction = $balances->spend(
|
||||
$accountId,
|
||||
(string) $data['amount'],
|
||||
'game:'.$data['idempotency_key'],
|
||||
$data['description'] ?? null,
|
||||
);
|
||||
} catch (InvalidArgumentException $exception) {
|
||||
return response()->json(['message' => $exception->getMessage()], 422);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'transaction_id' => $transaction->id,
|
||||
'account_id' => $transaction->account_id,
|
||||
'amount' => $transaction->amount,
|
||||
'balance' => $transaction->balance_after,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user