如何实现Stripe订阅结账时让客户从3个产品中选其一?
实现Stripe结账页面单款订阅产品选择功能
你当前的问题是:将3款订阅产品全部加入line_items数组,导致Stripe Checkout页面直接展示所有产品,无法让客户单选。以下是两种可行的解决方法:
方案1:前端选品后再创建结账会话
这是最常用的方案,先在你的页面让用户选择订阅方案,再根据用户选择创建对应产品的结账会话。
前端单选界面示例(HTML)
<form action="/your-backend-checkout-endpoint" method="POST"> <div> <input type="radio" name="selected_plan" id="plan_ddd" value="price_ddd" checked> <label for="plan_ddd">订阅方案A</label> </div> <div> <input type="radio" name="selected_plan" id="plan_eee" value="price_eee"> <label for="plan_eee">订阅方案B</label> </div> <div> <input type="radio" name="selected_plan" id="plan_fff" value="price_fff"> <label for="plan_fff">订阅方案C</label> </div> <button type="submit">确认订阅</button> </form>
后端修改后的PHP代码
// 获取用户提交的选中价格ID $selectedPrice = $_POST['selected_plan']; $result = $stripe->checkout->sessions->create([ 'mode' => 'subscription', 'line_items' => [ [ 'price' => $selectedPrice, 'quantity' => 1, ], ], 'customer_email' => $useremail, 'subscription_data' => [ 'trial_period_days' => 14, 'trial_settings' => [ 'end_behavior' => [ 'missing_payment_method' => 'cancel' ] ] ], 'ui_mode' => 'embedded', 'payment_method_collection' => 'if_required', 'return_url' => site_url("billing/callback") . "?session_id={CHECKOUT_SESSION_ID}" ]);
方案2:使用Stripe定价表(Pricing Table)
如果想直接在Stripe提供的界面里让用户选品,可以用Stripe官方的定价表组件:
- 登录Stripe后台,进入「产品」->「定价表」,创建包含3款订阅产品的定价表,设置为单选模式。
- 获取定价表的嵌入代码,添加到你的前端页面即可。
- 用户选择产品后,定价表会自动创建对应产品的结账会话,无需你手动构建
line_items。
注意事项
- 方案1自由度更高,可完全自定义前端UI;方案2快速便捷,使用Stripe官方优化的选品界面。
- 确保你的Stripe PHP SDK是最新稳定版,避免API兼容性问题。
内容的提问来源于stack exchange,提问作者arun kumar
相关产品推荐
相关产品推荐

