You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel集成Stripe预构建结账页面无法跳转至支付表单问题求助

Laravel集成Stripe预构建结账页面无法跳转问题修复方案

问题背景

在Laravel框架中集成Stripe预构建结账页面功能,点击提交按钮后无法正常跳转至Stripe支付表单页,期望实现提交后携带商品信息跳转至Stripe支付页的效果。

原有实现步骤

步骤1:安装Stripe PHP依赖

执行安装命令:
composer require stripe/stripe-php

步骤2:前端视图代码(home.blade.php)

<!DOCTYPE html>
<html>
  <head>
    <title>Buy cool new product</title>
    <link rel="stylesheet" href="style.css">
    <script src="https://js.stripe.com/v3/"></script>

  </head>
  <body>
    <section>
      <div class="product">
        <img
          src="https://i.imgur.com/EHyR2nP.png"
          alt="The cover of Stubborn Attachments"

        />
        <div class="description">

          <h3>Stubborn Attachments</h3>
          <h5>.00</h5>
        </div>
      </div>
      <form action="/create-checkout-session" method="POST">@csrf
        <button type="submit" id="checkout-button">Checkout</button>
      </form>
    </section>
  </body>
</html>

步骤3:原有路由配置代码

Route::post('/create-checkout-session',function(){
  

   // require 'vendor/autoload.php';
    \Stripe\Stripe::setApiKey('sk_test_FfKP0notiUK0s5XOrs7O3MKR');
    
    header('Content-Type: application/json');
    
    $YOUR_DOMAIN = 'http://localhost:8000';
    
    $checkout_session = \Stripe\Checkout\Session::create([
      'line_items' => [[
        # TODO: replace this with the `price` of the product you want to sell
        'price' => 'price_1RaJ35tHwG6zssijf9MneD9SQ',
        'quantity' => 1,
      ]],
      'payment_method_types' => [
        'card',
      ],
      'mode' => 'payment',
      'success_url' => $YOUR_DOMAIN . '/success.html',
      'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
    ]);
    
    header("HTTP/1.1 303 See Other");
    header("Location: " . $checkout_session->url);
});

修复方案

常见问题排查点

  • 确认Stripe测试密钥和你账号后台的测试密钥完全一致,不要有多余空格或字符
  • 代码中的price参数值为示例值,必须替换为你自己Stripe后台创建产品后生成的对应price ID,且测试模式下不能混用生产环境的price ID
  • 路由代码中不需要设置Content-Type: application/json头,该头会干扰后续跳转逻辑,直接删除该行即可
  • 不要使用PHP原生header函数做跳转,改用Laravel自带的跳转方法避免框架逻辑冲突
  • 确认本地PHP环境已开启curl扩展,Stripe SDK依赖curl发起接口请求
  • 新增异常捕获逻辑,可直接输出错误信息快速定位问题

修正后路由代码

Route::post('/create-checkout-session',function(){
    try {
        // 替换为你自己Stripe后台的测试密钥
        \Stripe\Stripe::setApiKey('你的Stripe测试密钥');
        
        $YOUR_DOMAIN = 'http://localhost:8000';
        
        $checkout_session = \Stripe\Checkout\Session::create([
            'line_items' => [[
                // 替换为你自己Stripe后台生成的price ID
                'price' => '你的产品price ID',
                'quantity' => 1,
            ]],
            'payment_method_types' => [
                'card',
            ],
            'mode' => 'payment',
            'success_url' => $YOUR_DOMAIN . '/success.html',
            'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
        ]);
        
        // 使用Laravel原生方法跳转外部链接
        return redirect()->away($checkout_session->url);
    } catch (\Stripe\Exception\ApiErrorException $e) {
        // 测试阶段可输出错误排查,上线后删除该输出
        dd($e->getMessage());
    }
});

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 01:18:02