You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/UsdtController.php

96 lines
3.7 KiB
PHP

<?php
namespace App\Controllers;
use App\Models\UsdtPaymentOrder;
use App\Services\Auth;
use App\Services\Payment\UsdtConfig;
use App\Services\Payment\UsdtPaymentService;
use App\Services\UserFeatureRestrictionService;
class UsdtController extends BaseController
{
public function create($request, $response, $args)
{
$user = Auth::getUser();
if (UserFeatureRestrictionService::isRechargeRestricted($user)) {
return $this->json($response, [
'code' => -1,
'msg' => UserFeatureRestrictionService::RECHARGE_MESSAGE,
], 503);
}
if (!UsdtConfig::canUserAccess($user)) {
return $this->json($response, ['code' => -1, 'msg' => 'USDT 充值当前不可用'], 403);
}
try {
$order = (new UsdtPaymentService())->createRechargeOrder($user, $request->getParam('price'));
return $this->json($response, [
'code' => 0,
'msg' => '订单创建成功',
'amount' => number_format((float) $order->amount, 2, '.', ''),
'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 (\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);
}
$order = UsdtPaymentOrder::where('business_type', 'recharge')
->where('user_id', (int) $user->id)
->orderBy('id', 'desc')
->first();
if ($order === null) {
return $this->json($response, ['ret' => 0, 'result' => 0, 'status' => 'not_found'], 404);
}
(new UsdtPaymentService())->expire($order);
return $this->json($response, [
'ret' => 1,
'result' => (string) $order->status === 'paid' ? 1 : 0,
'status' => (string) $order->status,
'balance' => (string) $order->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;
}
}