lotteryPlayer(); abort_if($player === null, 500, 'lottery_player missing'); $currency = $this->resolveCurrencyCode($request, $player); if ($currency instanceof JsonResponse) { return $currency; } try { $data = $this->transferService->transferIn( $player, $currency, (int) $request->validated('amount'), (string) $request->validated('idempotent_key'), ); } catch (WalletOperationException $e) { return ApiResponse::error( LotteryMessage::wallet($request, $e->lotteryCode), $e->lotteryCode, null, $e->httpStatus, ); } return ApiResponse::success($data); } private function resolveCurrencyCode(Request $request, Player $player): string|JsonResponse { $raw = $request->input('currency'); if (is_string($raw) && $raw !== '') { $code = strtoupper(substr(trim($raw), 0, 16)); } else { $fallback = $player->default_currency ?? config('lottery.default_currency', 'NPR'); $code = strtoupper(substr(trim((string) $fallback), 0, 16)); } if (! preg_match('/^[A-Z0-9]{1,16}$/', $code)) { return ApiResponse::error( __('wallet.invalid_currency'), ErrorCode::WalletInvalidCurrency->value, null, 400, ); } return $code; } }