getParam('method', ''))); try { $order = (new StripePaymentService())->createOrder( $user, $request->getParam('price'), $method, (string) $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->checkout_url, 'payment_kind' => 'payurl', 'payment_label' => $method === 'alipay' ? 'Stripe · 支付宝' : 'Stripe · 银行卡', 'status_url' => '/user/stripe/status', 'poll_interval' => 3000, ]); } catch (\InvalidArgumentException $exception) { return $this->json($response, [ 'code' => -1, 'msg' => $exception->getMessage(), ], 422); } catch (\Throwable $exception) { error_log('Stripe 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 = StripeOrder::where('user_id', (int) $user->id) ->whereNotExists(function ($query): void { $query->selectRaw('1')->from('cloud_invoice_payments') ->whereColumn( 'cloud_invoice_payments.provider_order_no', 'stripe_orders.order_no' )->whereIn('cloud_invoice_payments.provider', [ 'stripe_card', 'stripe_alipay', ]); })->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 webhook($request, $response, $args) { try { $body = $request->getBody(); if ($body->isSeekable()) { $body->rewind(); } $rawBody = $body->getContents(); (new StripePaymentService())->processWebhook( $rawBody, $request->getHeaderLine('Stripe-Signature') ); return $this->plain($response, 'ok', 200); } catch (\Throwable $exception) { error_log('Stripe webhook rejected: ' . $exception->getMessage()); return $this->plain($response, 'invalid webhook', 400); } } public function handleReturn($request, $response, $args) { $sessionId = trim((string) $request->getParam('session_id', '')); if (!preg_match('/^cs_(?:test|live)_[A-Za-z0-9]+$/D', $sessionId)) { return $this->plain($response, '支付返回信息无效', 400); } $order = StripeOrder::where('checkout_session_id', $sessionId)->first(); if ($order === null) { return $this->plain($response, '订单不存在', 404); } if (CloudInvoicePayment::where('provider_order_no', $order->order_no) ->whereIn('provider', ['stripe_card', 'stripe_alipay'])->exists()) { return $response->withRedirect('/user/cloud/invoices', 302); } return $response->withRedirect('/user/code?payment=stripe', 302); } private function publicError(\Throwable $exception): string { $message = $exception->getMessage(); foreach (['Stripe 支付暂未开放', 'Stripe 支付宝暂未开放', 'Stripe 银行卡暂未开放', 'Stripe 当前处于测试模式', 'Stripe 下单过于频繁', '充值金额', '当前访问地址', '当前访问域名'] as $publicMessage) { if (strpos($message, $publicMessage) !== false) { return $message; } } return 'Stripe 支付网关暂时不可用,请稍后重试'; } private function currentReturnBaseUrl($request): string { $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. } if (!isset($allowedHosts[$host])) { throw new \RuntimeException( '当前访问域名未加入面板多域名配置,不能作为支付返回地址' ); } return 'https://' . $host . ($port !== null && (int) $port !== 443 ? ':' . (int) $port : ''); } private function addAllowedHost(array &$allowedHosts, $value): void { $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, int $status = 200) { $response = $response->withStatus($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, string $body, int $status) { $response = $response->withStatus($status) ->withHeader('Content-Type', 'text/plain; charset=utf-8'); $response->getBody()->write($body); return $response; } }