'false', 'paytaro_test_mode' => 'true', 'paytaro_gateway_url' => self::MAPI_GATEWAY_URL, 'paytaro_callback_base_url' => '', 'paytaro_app_id' => '', 'paytaro_method_uuid' => '', 'paytaro_min_amount' => '1.00', 'paytaro_max_amount' => '10000.00', 'paytaro_request_timeout_seconds' => '15', ]; public static function values() { $values = self::DEFAULTS; if ($values['paytaro_callback_base_url'] === '') { $values['paytaro_callback_base_url'] = rtrim((string) Config::get('baseUrl', ''), '/'); } try { $rows = Configurations::whereIn('name', array_merge(array_keys(self::DEFAULTS), [self::SECRET_KEY])) ->get() ->keyBy('name'); foreach (array_keys(self::DEFAULTS) as $key) { if (isset($rows[$key])) { $values[$key] = (string) $rows[$key]->value; } } $values['paytaro_secret_configured'] = isset($rows[self::SECRET_KEY]) && trim((string) $rows[self::SECRET_KEY]->value) !== ''; } catch (\Throwable $exception) { $values['paytaro_secret_configured'] = false; } // Paytaro is intentionally pinned to the documented MAPI endpoint. $values['paytaro_gateway_url'] = self::MAPI_GATEWAY_URL; $values['paytaro_enabled'] = self::truthy($values['paytaro_enabled']); $values['paytaro_test_mode'] = self::truthy($values['paytaro_test_mode']); $values['paytaro_min_amount'] = PaymentAmount::normalize($values['paytaro_min_amount']); $values['paytaro_max_amount'] = PaymentAmount::normalize($values['paytaro_max_amount']); $values['paytaro_request_timeout_seconds'] = max(3, min(60, (int) $values['paytaro_request_timeout_seconds'])); return $values; } public static function runtime($requireEnabled = true) { $values = self::values(); if ($requireEnabled && !$values['paytaro_enabled']) { throw new \RuntimeException('Paytaro 支付暂未开放'); } foreach (['paytaro_gateway_url', 'paytaro_callback_base_url', 'paytaro_app_id', 'paytaro_method_uuid'] as $key) { if (trim((string) $values[$key]) === '') { throw new \RuntimeException('Paytaro 配置不完整:' . $key); } } if (!self::isHttpsUrl($values['paytaro_gateway_url']) || !self::isHttpsUrl($values['paytaro_callback_base_url'])) { throw new \RuntimeException('Paytaro 网关和回调地址必须使用 HTTPS'); } return array_merge($values, self::callbackCredentials()); } public static function runtimeForUser($user) { $values = self::runtime(true); if (!self::canUserAccess($user, $values)) { throw new \RuntimeException('Paytaro 当前处于测试模式,仅管理员可用'); } return $values; } public static function callbackCredentials() { $values = self::values(); $appId = trim((string) $values['paytaro_app_id']); if (!preg_match('/^[A-Z0-9]{11}$/', $appId)) { throw new \RuntimeException('Paytaro 商户 App ID 尚未正确配置'); } if (!$values['paytaro_secret_configured']) { throw new \RuntimeException('Paytaro 商户密钥尚未配置'); } $row = Configurations::where('name', self::SECRET_KEY)->first(); if ($row === null) { throw new \RuntimeException('Paytaro 商户密钥尚未配置'); } $secret = PaytaroCredentialCipher::decrypt((string) $row->value); if ($secret === '') { throw new \RuntimeException('Paytaro 商户密钥尚未配置'); } return ['paytaro_app_id' => $appId, self::SECRET_KEY => $secret]; } public static function canUserAccess($user, ?array $values = null) { $values = $values === null ? self::values() : $values; if (!self::truthy($values['paytaro_enabled'] ?? false)) { return false; } if (!self::truthy($values['paytaro_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, $secret = '') { $secret = trim((string) $secret); $encryptedSecret = $secret === '' ? '' : PaytaroCredentialCipher::encrypt($secret); $connection = (new Configurations())->getConnection(); $connection->transaction(function () use ($values, $encryptedSecret) { foreach (self::DEFAULTS as $key => $default) { if (array_key_exists($key, $values)) { self::set($key, (string) $values[$key]); } } if ($encryptedSecret !== '') { self::set(self::SECRET_KEY, $encryptedSecret); } }); } public static function truthy($value) { return in_array(strtolower(trim((string) $value)), ['1', 'true', 'yes', 'on'], true); } public static function isHttpsUrl($value) { $value = trim((string) $value); if (filter_var($value, FILTER_VALIDATE_URL) === false || preg_match('#^https://#i', $value) !== 1) { return false; } $parts = parse_url($value); return is_array($parts) && empty($parts['user']) && empty($parts['pass']) && empty($parts['query']) && empty($parts['fragment']); } private static function set($name, $value) { $row = Configurations::where('name', $name)->first(); if ($row === null) { $row = new Configurations(); $row->name = $name; } $row->value = (string) $value; $row->save(); } }