diff --git a/app/Helpers/Functions.php b/app/Helpers/Functions.php index d10b2de..8f8b804 100644 --- a/app/Helpers/Functions.php +++ b/app/Helpers/Functions.php @@ -41,17 +41,33 @@ if (!function_exists('admin_settings_batch')) { } } -if (!function_exists('origin_url')) { +if (!function_exists('source_base_url')) { /** - * 根据 HTTP_ORIGIN 拼接完整 URL + * 获取来源基础URL,优先Referer,其次Host * @param string $path * @return string */ - function origin_url(string $path = ''): string + function source_base_url(string $path = ''): string { - $origin = request()->getSchemeAndHttpHost(); // 自动带端口 - $origin = rtrim($origin, '/'); + $baseUrl = ''; + $referer = request()->header('Referer'); + + if ($referer) { + $parsedUrl = parse_url($referer); + if (isset($parsedUrl['scheme']) && isset($parsedUrl['host'])) { + $baseUrl = $parsedUrl['scheme'] . '://' . $parsedUrl['host']; + if (isset($parsedUrl['port'])) { + $baseUrl .= ':' . $parsedUrl['port']; + } + } + } + + if (!$baseUrl) { + $baseUrl = request()->getSchemeAndHttpHost(); + } + + $baseUrl = rtrim($baseUrl, '/'); $path = ltrim($path, '/'); - return $origin . '/' . $path; + return $baseUrl . '/' . $path; } } diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 56c3538..0a41a56 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -53,7 +53,7 @@ class PaymentService return $this->payment->pay([ 'notify_url' => $notifyUrl, - 'return_url' => origin_url('/#/order/' . $order['trade_no']), + 'return_url' => source_base_url('/#/order/' . $order['trade_no']), 'trade_no' => $order['trade_no'], 'total_amount' => $order['total_amount'], 'user_id' => $order['user_id'],