'false', 'usdt_test_mode' => 'true', 'usdt_trongrid_base_url' => 'https://api.trongrid.io', 'usdt_cny_per_token' => '7.2000', 'usdt_min_amount' => '1.00', 'usdt_order_expire_minutes' => '30', 'usdt_late_grace_minutes' => '10', 'usdt_request_timeout_seconds' => '15', ]; public static function values() { $values = self::DEFAULTS; try { $rows = Configurations::whereIn('name', array_merge(array_keys(self::DEFAULTS), [self::API_KEY])) ->get() ->keyBy('name'); foreach (array_keys(self::DEFAULTS) as $key) { if (isset($rows[$key])) { $values[$key] = (string) $rows[$key]->value; } } $values['usdt_api_key_configured'] = isset($rows[self::API_KEY]) && trim((string) $rows[self::API_KEY]->value) !== ''; } catch (\Throwable $exception) { $values['usdt_api_key_configured'] = false; } $values['usdt_enabled'] = self::truthy($values['usdt_enabled']); $values['usdt_test_mode'] = self::truthy($values['usdt_test_mode']); $values['usdt_cny_per_token'] = self::rate($values['usdt_cny_per_token']); $values['usdt_min_amount'] = self::money($values['usdt_min_amount']); $values['usdt_order_expire_minutes'] = max(5, min(1440, (int) $values['usdt_order_expire_minutes'])); $values['usdt_late_grace_minutes'] = max(0, min(1440, (int) $values['usdt_late_grace_minutes'])); $values['usdt_request_timeout_seconds'] = max(3, min(60, (int) $values['usdt_request_timeout_seconds'])); $values['usdt_contract_address'] = self::CONTRACT_ADDRESS; return $values; } public static function runtime($requireEnabled = true) { $values = self::values(); if ($requireEnabled && !$values['usdt_enabled']) { throw new \RuntimeException('USDT 支付暂未开放'); } if (!self::isHttpsUrl($values['usdt_trongrid_base_url'])) { throw new \RuntimeException('TRON API 地址必须是有效的 HTTPS 地址'); } if ((float) $values['usdt_cny_per_token'] <= 0) { throw new \RuntimeException('人民币兑 USDT 汇率无效'); } $values['usdt_trongrid_api_key'] = self::apiKey(); return $values; } public static function runtimeForUser($user) { $values = self::runtime(true); if (!self::canUserAccess($user, $values)) { throw new \RuntimeException('USDT 支付当前处于测试模式,仅管理员可用'); } if (!self::hasEnabledWallet()) { throw new \RuntimeException('USDT 收款地址池暂不可用'); } return $values; } public static function canUserAccess($user, ?array $values = null) { $values = $values === null ? self::values() : $values; if (!self::truthy($values['usdt_enabled'] ?? false)) { return false; } if (!self::truthy($values['usdt_test_mode'] ?? true)) { return true; } return $user !== null && method_exists($user, 'isAdmin') && (bool) $user->isAdmin(); } public static function isUserVisible($user = null) { try { self::runtimeForUser($user); return true; } catch (\Throwable $exception) { return false; } } public static function save(array $values, $apiKey = '') { $apiKey = trim((string) $apiKey); $encrypted = $apiKey === '' ? '' : UsdtCredentialCipher::encrypt($apiKey); $connection = (new Configurations())->getConnection(); $connection->transaction(function () use ($values, $encrypted) { foreach (self::DEFAULTS as $key => $default) { if (array_key_exists($key, $values)) { self::set($key, (string) $values[$key]); } } if ($encrypted !== '') { self::set(self::API_KEY, $encrypted); } }); } public static function apiKey() { $row = Configurations::where('name', self::API_KEY)->first(); if ($row === null || trim((string) $row->value) === '') { return ''; } return UsdtCredentialCipher::decrypt((string) $row->value); } public static function clearApiKey() { Configurations::where('name', self::API_KEY)->delete(); } public static function hasEnabledWallet() { try { $model = new UsdtWallet(); return $model->getConnection()->getSchemaBuilder()->hasTable($model->getTable()) && UsdtWallet::where('enabled', 1)->exists(); } catch (\Throwable $exception) { return false; } } public static function truthy($value) { return in_array(strtolower(trim((string) $value)), ['1', 'true', 'yes', 'on'], true); } public static function money($value) { return PaymentAmount::normalize($value); } public static function rate($value) { if (!is_numeric($value)) { return '0.0000'; } return number_format(round((float) $value, 4), 4, '.', ''); } private static function isHttpsUrl($value) { $value = trim((string) $value); return filter_var($value, FILTER_VALIDATE_URL) !== false && preg_match('#^https://#i', $value) === 1; } private static function set($name, $value) { $row = Configurations::where('name', $name)->first(); if ($row === null) { $row = new Configurations(); $row->name = $name; } $row->value = $value; $row->save(); } }