json($response, ['code' => -1, 'msg' => 'USDT 充值当前不可用'], 403); } try { $order = (new UsdtPaymentService())->createRechargeOrder($user, $request->getParam('price')); $quote = PaymentCheckoutQuotes::checkout('usdt', (string) $order->order_no, (int) $user->id); if ($quote['checkout_state'] !== 'payable') { return $this->json($response, ['code' => -1, 'error_code' => $quote['checkout_state']], 409); } return $this->json($response, $quote + [ 'code' => 0, 'msg' => '订单创建成功', 'order_no' => (string) $order->order_no, 'actual_amount' => (string) $order->actual_amount, 'token' => 'USDT', 'network' => 'tron', 'receive_address' => (string) $order->receive_address, 'checkout_link_enabled' => false, 'url' => '', 'expires_at' => (int) $order->expires_at, 'status_url' => '/user/usdt/status', 'poll_interval' => 3000, ]); } catch (PaymentRateUnavailable $exception) { return $this->json($response, ['code' => -1, 'error_code' => 'rate_unavailable'], 422); } catch (\InvalidArgumentException $exception) { return $this->json($response, ['code' => -1, 'msg' => $exception->getMessage()], 422); } catch (\Throwable $exception) { error_log('Native USDT create order failed: ' . $exception->getMessage()); return $this->json($response, ['code' => -1, 'msg' => $this->publicError($exception)], 502); } } public function status($request, $response, $args) { $user = Auth::getUser(); if (!$user->isLogin) { return $this->json($response, ['ret' => 0, 'result' => 0, 'status' => 'not_found'], 404); } $orderNo = $request->getParam('order_no'); if ($orderNo !== null && (!is_string($orderNo) || !preg_match('/^[A-Za-z0-9_-]{1,64}$/D', $orderNo))) { return $this->json($response, ['ret' => 0, 'result' => 0, 'status' => 'not_found'], 404); } $order = UsdtPaymentOrder::where('business_type', 'recharge') ->where('user_id', (int) $user->id) ->whereNotExists(function ($query) { $query->selectRaw('1')->from('cloud_invoice_payments') ->whereColumn('cloud_invoice_payments.provider_order_no', 'usdt_payment_orders.order_no') ->where('cloud_invoice_payments.provider', 'usdt'); }) ->when($orderNo !== null, static fn ($query) => $query->where('order_no', $orderNo)) ->orderBy('id', 'desc') ->first(); if ($order === null) { return $this->json($response, ['ret' => 0, 'result' => 0, 'status' => 'not_found'], 404); } $order = (new UsdtPaymentService())->expire($order); $quote = PaymentCheckoutQuotes::checkout('usdt', (string) $order->order_no, (int) $user->id); $status = (string) $order->status; if (in_array($status, ['pending', 'expired', 'creation_unknown'], true) && in_array($quote['checkout_state'], ['review', 'expired'], true)) { $status = $quote['checkout_state']; } return $this->json($response, [ 'ret' => 1, 'result' => $status === 'paid' ? 1 : 0, 'status' => $status, 'balance' => $status === 'paid' ? number_format((float) $user->newQuery()->where('id', (int) $user->id)->value('money'), 2, '.', '') : null, ]); } private function publicError(\Throwable $exception) { $message = $exception->getMessage(); foreach (['USDT 支付暂未开放', 'USDT 收款地址池', '最低充值金额'] as $allowed) { if (strpos($message, $allowed) !== false) { return $message; } } return 'USDT 充值订单暂时无法创建,请稍后重试'; } private function json($response, array $data, $status = 200) { $response = $response->withStatus((int) $status) ->withHeader('Content-Type', 'application/json; charset=utf-8') ->withHeader('Cache-Control', 'no-store, private'); $response->getBody()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)); return $response; } }