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

Laravel集成Stripe报错:No such price: 'false'及支付失败求助

Laravel集成Stripe订阅报错排查与解决

报错1:Stripe\Exception\InvalidRequestException - No such price: 'false'

问题根源

调用$user->newSubscription('default', $plan)时,第二个参数传入的是本地Plan模型实例,但Laravel Cashier要求该参数必须是Stripe的Price ID字符串或Stripe Price对象。如果Plan::find($request->plan)未找到对应记录,$plan会是null,最终被转换为false传给Stripe,触发该报错。

修复步骤

  1. 确保本地Plan模型包含存储Stripe Price ID的字段(例如stripe_price_id)。
  2. 传递正确的Price ID并增加计划存在性校验:
$plan = Plan::find($request->plan);
// 校验计划是否存在
if (!$plan) {
    abort(404, '指定订阅计划不存在');
}

// 传递Stripe Price ID而非模型实例
$stripeSubscription = $user->newSubscription(
    'default', 
    $plan->stripe_price_id // 替换为你的实际字段名
)->create($paymentId);

报错2:支付尝试失败,需完成额外操作才可继续

问题根源

这是Stripe的支付认证限制(如3D Secure验证),当支付方式需要用户额外验证时,Stripe会返回requires_action状态,当前代码未处理该场景。

修复步骤

  1. 捕获订阅创建时的支付动作异常,引导用户完成认证:
try {
    $stripeSubscription = $user->newSubscription(
        'default', 
        $plan->stripe_price_id
    )->create($paymentId);
} catch (\Laravel\Cashier\Exceptions\PaymentActionRequired $e) {
    // 跳转至Stripe的支付认证页面
    return redirect($e->payment->next_action->redirect_to_url->url);
} catch (\Exception $e) {
    // 处理其他异常
    return back()->withErrors(['message' => $e->getMessage()]);
}
  1. 补充支付方式的边界校验:
if (!$paymentMethod) {
    return back()->withErrors(['message' => '请选择有效支付方式']);
}
  1. 确认前端传递的payment_method是Stripe生成的有效支付ID(格式如pm_xxx)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 20:12:15