|
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Controllers\AdminController;
|
|
|
|
|
|
use App\Models\UsdtChainTransaction;
|
|
|
|
|
|
use App\Models\UsdtPaymentOrder;
|
|
|
|
|
|
use App\Models\UsdtWallet;
|
|
|
|
|
|
use App\Services\AdminPermission;
|
|
|
|
|
|
use App\Services\AdminDataMasker;
|
|
|
|
|
|
use App\Services\Payment\TronGridClient;
|
|
|
|
|
|
use App\Services\Payment\UsdtConfig;
|
|
|
|
|
|
use App\Services\Payment\UsdtWalletService;
|
|
|
|
|
|
|
|
|
|
|
|
class UsdtController extends AdminController
|
|
|
|
|
|
{
|
|
|
|
|
|
public function index($request, $response, $args)
|
|
|
|
|
|
{
|
|
|
|
|
|
$this->setViewTheme('venix');
|
|
|
|
|
|
$config = UsdtConfig::values();
|
|
|
|
|
|
$orders = null;
|
|
|
|
|
|
$wallets = new \Illuminate\Support\Collection();
|
|
|
|
|
|
$chainEvents = new \Illuminate\Support\Collection();
|
|
|
|
|
|
$walletText = '';
|
|
|
|
|
|
$summary = ['total' => 0, 'pending' => 0, 'paid' => 0, 'paid_amount' => '0.00', 'attention' => 0];
|
|
|
|
|
|
$tableError = '';
|
|
|
|
|
|
$keyword = trim((string) $request->getParam('keyword', ''));
|
|
|
|
|
|
$status = trim((string) $request->getParam('status', ''));
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
$query = UsdtPaymentOrder::query()
|
|
|
|
|
|
->leftJoin('user', 'user.id', '=', 'usdt_payment_orders.user_id')
|
|
|
|
|
|
->select(
|
|
|
|
|
|
'usdt_payment_orders.id',
|
|
|
|
|
|
'usdt_payment_orders.business_type',
|
|
|
|
|
|
'usdt_payment_orders.user_id',
|
|
|
|
|
|
'usdt_payment_orders.amount',
|
|
|
|
|
|
'usdt_payment_orders.actual_amount',
|
|
|
|
|
|
'usdt_payment_orders.status',
|
|
|
|
|
|
'usdt_payment_orders.txid',
|
|
|
|
|
|
'usdt_payment_orders.receive_address',
|
|
|
|
|
|
'usdt_payment_orders.created_at',
|
|
|
|
|
|
'user.email as user_email'
|
|
|
|
|
|
);
|
|
|
|
|
|
if ($keyword !== '') {
|
|
|
|
|
|
if (AdminPermission::isSuperAdmin()) {
|
|
|
|
|
|
$query->where(function ($builder) use ($keyword) {
|
|
|
|
|
|
$builder->where('usdt_payment_orders.txid', 'like', '%' . $keyword . '%')
|
|
|
|
|
|
->orWhere('usdt_payment_orders.receive_address', 'like', '%' . $keyword . '%')
|
|
|
|
|
|
->orWhere('user.email', 'like', '%' . $keyword . '%');
|
|
|
|
|
|
if (ctype_digit($keyword)) {
|
|
|
|
|
|
$builder->orWhere('usdt_payment_orders.id', (int) $keyword)
|
|
|
|
|
|
->orWhere('usdt_payment_orders.user_id', (int) $keyword);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
} elseif (ctype_digit($keyword)) {
|
|
|
|
|
|
$query->where(function ($builder) use ($keyword) {
|
|
|
|
|
|
$builder->where('usdt_payment_orders.id', (int) $keyword)
|
|
|
|
|
|
->orWhere('usdt_payment_orders.user_id', (int) $keyword);
|
|
|
|
|
|
});
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$query->whereRaw('1 = 0');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (in_array($status, ['pending', 'paid', 'expired', 'failed'], true)) {
|
|
|
|
|
|
$query->where('usdt_payment_orders.status', $status);
|
|
|
|
|
|
}
|
|
|
|
|
|
$orders = $query->orderBy('usdt_payment_orders.id', 'desc')->paginate(30);
|
|
|
|
|
|
$orders->setPath('/admin/usdt');
|
|
|
|
|
|
$orders->appends(['keyword' => $keyword, 'status' => $status]);
|
|
|
|
|
|
|
|
|
|
|
|
$wallets = UsdtWallet::orderBy('enabled', 'desc')->orderBy('sort', 'asc')->orderBy('id', 'asc')->get();
|
|
|
|
|
|
$walletText = UsdtWalletService::text();
|
|
|
|
|
|
$chainEvents = UsdtChainTransaction::whereIn('status', ['unmatched', 'error'])
|
|
|
|
|
|
->orderBy('id', 'desc')
|
|
|
|
|
|
->limit(50)
|
|
|
|
|
|
->get();
|
|
|
|
|
|
foreach ($chainEvents as $event) {
|
|
|
|
|
|
$event->setAttribute('amount_display', $this->formatAtomic((int) $event->amount_atomic));
|
|
|
|
|
|
$event->setAttribute('block_time_seconds', (int) floor((int) $event->block_timestamp / 1000));
|
|
|
|
|
|
if (AdminDataMasker::shouldMask()) {
|
|
|
|
|
|
$event->setAttribute('from_address', AdminDataMasker::account($event->from_address));
|
|
|
|
|
|
$event->setAttribute('to_address', AdminDataMasker::account($event->to_address));
|
|
|
|
|
|
$event->setAttribute('txid', '');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (AdminDataMasker::shouldMask()) {
|
|
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
|
|
$order->setAttribute('user_email', AdminDataMasker::email($order->user_email));
|
|
|
|
|
|
$order->setAttribute('receive_address', AdminDataMasker::account($order->receive_address));
|
|
|
|
|
|
$order->setAttribute('txid', '');
|
|
|
|
|
|
}
|
|
|
|
|
|
foreach ($wallets as $wallet) {
|
|
|
|
|
|
$wallet->setAttribute('address', AdminDataMasker::account($wallet->address));
|
|
|
|
|
|
if (trim((string) $wallet->last_error) !== '') {
|
|
|
|
|
|
$wallet->setAttribute('last_error', '扫描异常,详细信息仅超级管理员可查看');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
$walletText = preg_replace_callback('/^[^|\r\n]+/m', function ($match) {
|
|
|
|
|
|
return AdminDataMasker::account(trim($match[0])) . ' ';
|
|
|
|
|
|
}, $walletText);
|
|
|
|
|
|
}
|
|
|
|
|
|
$summary['total'] = UsdtPaymentOrder::count();
|
|
|
|
|
|
$summary['pending'] = UsdtPaymentOrder::whereIn('status', ['pending', 'expired'])->count();
|
|
|
|
|
|
$summary['paid'] = UsdtPaymentOrder::where('status', 'paid')->count();
|
|
|
|
|
|
$summary['paid_amount'] = number_format((float) UsdtPaymentOrder::where('status', 'paid')->sum('amount'), 2, '.', '');
|
|
|
|
|
|
$summary['attention'] = UsdtChainTransaction::whereIn('status', ['unmatched', 'error'])->count();
|
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
|
$tableError = '尚未导入 sql/usdt_native_payment.sql,原生 USDT 数据表当前不可用。';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->view()
|
|
|
|
|
|
->assign('usdt_config', $config)
|
|
|
|
|
|
->assign('usdt_wallet_text', $walletText)
|
|
|
|
|
|
->assign('usdt_wallets', $wallets)
|
|
|
|
|
|
->assign('usdt_has_wallets', !$wallets->isEmpty())
|
|
|
|
|
|
->assign('usdt_chain_events', $chainEvents)
|
|
|
|
|
|
->assign('usdt_has_chain_events', !$chainEvents->isEmpty())
|
|
|
|
|
|
->assign('usdt_orders', $orders)
|
|
|
|
|
|
->assign('usdt_summary', $summary)
|
|
|
|
|
|
->assign('usdt_table_error', $tableError)
|
|
|
|
|
|
->assign('usdt_filters', ['keyword' => $keyword, 'status' => $status])
|
|
|
|
|
|
->display('admin/usdt/index.tpl');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function save($request, $response, $args)
|
|
|
|
|
|
{
|
|
|
|
|
|
$payload = $request->getParsedBody();
|
|
|
|
|
|
$payload = is_array($payload) ? $payload : [];
|
|
|
|
|
|
try {
|
|
|
|
|
|
$this->assertSchemaReady();
|
|
|
|
|
|
$values = $this->validateConfig($payload);
|
|
|
|
|
|
$wallets = UsdtWalletService::parse($payload['usdt_wallets'] ?? '');
|
|
|
|
|
|
if ($values['usdt_enabled'] === 'true' && $wallets === []) {
|
|
|
|
|
|
throw new \InvalidArgumentException('启用 USDT 支付前至少配置一个有效的 TRON 收款地址');
|
|
|
|
|
|
}
|
|
|
|
|
|
$apiKey = trim((string) ($payload['usdt_trongrid_api_key'] ?? ''));
|
|
|
|
|
|
if (strlen($apiKey) > 256) {
|
|
|
|
|
|
throw new \InvalidArgumentException('TronGrid API Key 不能超过 256 个字符');
|
|
|
|
|
|
}
|
|
|
|
|
|
UsdtConfig::save($values, $apiKey);
|
|
|
|
|
|
if (UsdtConfig::truthy($payload['usdt_clear_api_key'] ?? 'false')) {
|
|
|
|
|
|
UsdtConfig::clearApiKey();
|
|
|
|
|
|
}
|
|
|
|
|
|
UsdtWalletService::sync($wallets);
|
|
|
|
|
|
return $this->json($response, 1, '原生 USDT 配置保存成功');
|
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
|
error_log('Native USDT config save failed: ' . $exception->getMessage());
|
|
|
|
|
|
return $this->json($response, 0, $exception->getMessage(), 422);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function test($request, $response, $args)
|
|
|
|
|
|
{
|
|
|
|
|
|
try {
|
|
|
|
|
|
$wallet = UsdtWallet::where('enabled', 1)->orderBy('sort', 'asc')->first();
|
|
|
|
|
|
if ($wallet === null) {
|
|
|
|
|
|
throw new \RuntimeException('请先保存至少一个收款地址');
|
|
|
|
|
|
}
|
|
|
|
|
|
(new TronGridClient(UsdtConfig::runtime(false)))->testConnection((string) $wallet->address);
|
|
|
|
|
|
return $this->json($response, 1, '连接成功,已读取 TRON 已确认交易接口');
|
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
|
error_log('TRON connection test failed: ' . $exception->getMessage());
|
|
|
|
|
|
return $this->json($response, 0, '连接失败:' . $exception->getMessage(), 502);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function validateConfig(array $payload)
|
|
|
|
|
|
{
|
|
|
|
|
|
$apiUrl = rtrim(trim((string) ($payload['usdt_trongrid_base_url'] ?? '')), '/');
|
|
|
|
|
|
if (!filter_var($apiUrl, FILTER_VALIDATE_URL) || preg_match('#^https://#i', $apiUrl) !== 1) {
|
|
|
|
|
|
throw new \InvalidArgumentException('TRON API 地址必须是有效的 HTTPS 地址');
|
|
|
|
|
|
}
|
|
|
|
|
|
$rate = UsdtConfig::rate($payload['usdt_cny_per_token'] ?? '0');
|
|
|
|
|
|
if ((float) $rate < 0.1 || (float) $rate > 1000) {
|
|
|
|
|
|
throw new \InvalidArgumentException('人民币兑 USDT 汇率必须在 0.1 到 1000 之间');
|
|
|
|
|
|
}
|
|
|
|
|
|
$minimum = UsdtConfig::money($payload['usdt_min_amount'] ?? '1.00');
|
|
|
|
|
|
if ((float) $minimum < 0.02 || (float) $minimum > 1000000) {
|
|
|
|
|
|
throw new \InvalidArgumentException('最低充值金额必须在 0.02 到 1000000 之间');
|
|
|
|
|
|
}
|
|
|
|
|
|
$expire = (int) ($payload['usdt_order_expire_minutes'] ?? 30);
|
|
|
|
|
|
$grace = (int) ($payload['usdt_late_grace_minutes'] ?? 10);
|
|
|
|
|
|
$timeout = (int) ($payload['usdt_request_timeout_seconds'] ?? 15);
|
|
|
|
|
|
if ($expire < 5 || $expire > 1440 || $grace < 0 || $grace > 1440 || $timeout < 3 || $timeout > 60) {
|
|
|
|
|
|
throw new \InvalidArgumentException('订单时间或请求超时参数超出允许范围');
|
|
|
|
|
|
}
|
|
|
|
|
|
return [
|
|
|
|
|
|
'usdt_enabled' => UsdtConfig::truthy($payload['usdt_enabled'] ?? 'false') ? 'true' : 'false',
|
|
|
|
|
|
'usdt_test_mode' => UsdtConfig::truthy($payload['usdt_test_mode'] ?? 'false') ? 'true' : 'false',
|
|
|
|
|
|
'usdt_trongrid_base_url' => $apiUrl,
|
|
|
|
|
|
'usdt_cny_per_token' => $rate,
|
|
|
|
|
|
'usdt_min_amount' => $minimum,
|
|
|
|
|
|
'usdt_order_expire_minutes' => (string) $expire,
|
|
|
|
|
|
'usdt_late_grace_minutes' => (string) $grace,
|
|
|
|
|
|
'usdt_request_timeout_seconds' => (string) $timeout,
|
|
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function json($response, $ret, $message, $status = 200)
|
|
|
|
|
|
{
|
|
|
|
|
|
$response = $response->withStatus((int) $status)->withHeader('Content-Type', 'application/json; charset=utf-8');
|
|
|
|
|
|
$response->getBody()->write(json_encode(['ret' => (int) $ret, 'msg' => (string) $message], JSON_UNESCAPED_UNICODE));
|
|
|
|
|
|
return $response;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function formatAtomic($atomic)
|
|
|
|
|
|
{
|
|
|
|
|
|
return intdiv($atomic, 1000000) . '.' . str_pad((string) ($atomic % 1000000), 6, '0', STR_PAD_LEFT);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function assertSchemaReady()
|
|
|
|
|
|
{
|
|
|
|
|
|
$connection = (new UsdtPaymentOrder())->getConnection();
|
|
|
|
|
|
foreach (['usdt_wallets', 'usdt_payment_orders', 'usdt_chain_transactions'] as $table) {
|
|
|
|
|
|
if (!$connection->getSchemaBuilder()->hasTable($table)) {
|
|
|
|
|
|
throw new \RuntimeException('请先导入 sql/usdt_native_payment.sql');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|