json($response, ['code' => -1, 'msg' => UserFeatureRestrictionService::RECHARGE_MESSAGE], 503); } try { $order = (new PaytaroPaymentService())->createOrder( $user, $request->getParam('price'), $request->getServerParam('REMOTE_ADDR', ''), $request->getHeaderLine('User-Agent'), $this->currentReturnBaseUrl($request) ); return $this->json($response, [ 'code' => 0, 'msg' => '订单创建成功', 'amount' => number_format((float) $order->amount, 2, '.', ''), 'url' => (string) $order->payment_value, 'payment_kind' => (string) $order->payment_kind, 'payment_label' => '支付宝扫码支付', 'status_url' => '/user/paytaro/status', 'poll_interval' => 3000, ]); } catch (\InvalidArgumentException $exception) { return $this->json($response, ['code' => -1, 'msg' => $exception->getMessage()], 422); } catch (\Throwable $exception) { error_log('Paytaro 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 = PaytaroOrder::where('user_id', (int) $user->id) ->whereNotExists(function ($query) { $query->selectRaw('1')->from('cloud_invoice_payments') ->whereColumn('cloud_invoice_payments.provider_order_no', 'paytaro_orders.order_no') ->where('cloud_invoice_payments.provider', 'paytaro'); }) ->orderBy('id', 'desc')->first(); if ($order === null) { return $this->json($response, ['ret' => 0, 'result' => 0, 'status' => 'not_found'], 404); } 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, ]); } public function callback($request, $response, $args) { $payload = $this->paymentPayload($request); if ($payload === null) { return $this->plain($response, 'fail', 400); } try { (new PaytaroPaymentService())->processCallback($payload); return $this->plain($response, 'success', 200); } catch (\Throwable $exception) { error_log('Paytaro callback rejected: ' . $exception->getMessage()); return $this->plain($response, 'fail', 400); } } public function handleReturn($request, $response, $args) { $payload = $this->paymentPayload($request); try { $orderNo = (new PaytaroPaymentService())->returnOrderNo(is_array($payload) ? $payload : []); if (!PaytaroOrder::where('order_no', $orderNo)->exists()) { return $this->plain($response, '订单不存在', 404); } if (CloudInvoicePayment::where('provider_order_no', $orderNo) ->where('provider', 'paytaro')->exists()) { return $response->withRedirect('/user/cloud/invoices', 302); } return $response->withRedirect('/user/code?payment=paytaro', 302); } catch (\Throwable $exception) { error_log('Paytaro return rejected: ' . $exception->getMessage()); return $this->plain($response, '支付返回信息无效', 400); } } private function paymentPayload($request) { if (strlen((string) $request->getServerParam('QUERY_STRING', '')) > 16384) { return null; } $method = strtoupper((string) $request->getMethod()); if ($method === 'POST') { $length = trim((string) $request->getHeaderLine('Content-Length')); if (ctype_digit($length) && (int) $length > 16384) { return null; } $payload = $request->getParsedBody(); if (!is_array($payload) || $payload === []) { $payload = $request->getQueryParams(); } } else { $payload = $request->getQueryParams(); } return is_array($payload) ? $payload : null; } private function publicError(\Throwable $exception) { $message = $exception->getMessage(); foreach (['Paytaro 支付暂未开放', 'Paytaro 当前处于测试模式', 'Paytaro 配置不完整', 'Paytaro 商户密钥尚未配置', 'Paytaro 下单过于频繁', '当前访问地址', '当前访问域名'] as $publicMessage) { if (strpos($message, $publicMessage) !== false) { return str_replace('Paytaro', '支付宝', $message); } } return '支付宝支付网关暂时不可用,请稍后重试'; } private function currentReturnBaseUrl($request) { $uri = $request->getUri(); $scheme = strtolower((string) $uri->getScheme()); $host = strtolower(trim((string) $uri->getHost())); $port = $uri->getPort(); if ($scheme !== 'https' || $host === '' || filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) === false) { throw new \RuntimeException('当前访问地址必须使用已配置的 HTTPS 域名'); } $allowedHosts = []; $this->addAllowedHost($allowedHosts, Config::get('baseUrl', '')); try { $names = []; for ($index = 1; $index <= 7; $index++) { $names[] = 'host_' . $index; } foreach (Configurations::whereIn('name', $names)->get() as $configuration) { foreach (preg_split('/[\s,]+/', (string) $configuration->value) as $configuredHost) { $this->addAllowedHost($allowedHosts, $configuredHost); } } } catch (\Throwable $exception) { // The primary baseUrl remains available when optional domain rows cannot be read. } if (!isset($allowedHosts[$host])) { throw new \RuntimeException('当前访问域名未加入面板多域名配置,不能作为支付返回地址'); } return 'https://' . $host . ($port !== null && (int) $port !== 443 ? ':' . (int) $port : ''); } private function addAllowedHost(array &$allowedHosts, $value) { $value = trim((string) $value); if ($value === '') { return; } $candidate = strpos($value, '://') === false ? 'https://' . $value : $value; $host = strtolower(trim((string) parse_url($candidate, PHP_URL_HOST))); if ($host !== '' && filter_var($host, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME) !== false) { $allowedHosts[$host] = true; } } 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; } private function plain($response, $body, $status) { $response = $response->withStatus((int) $status)->withHeader('Content-Type', 'text/plain; charset=utf-8'); $response->getBody()->write((string) $body); return $response; } }