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

Stripe Billing recurring payments场景下支付信息收集的技术咨询

嘿,刚好我之前做过Stripe Billing的订阅(也就是你说的recurring payments)功能,来给你详细梳理下怎么收集支付信息、Payments和Billing的交互,还有可以参考的示例~

1. 收集支付信息的正确姿势

首先纠正下:不用单独用普通的Stripe Payments表单,因为订阅场景需要把支付方式和客户、订阅绑定,直接用单次支付表单没法完成这个关联。正确的做法是用Stripe Elements(或者更省心的Checkout托管页面)来收集支付信息,这样既能保证PCI合规(不用自己碰敏感的银行卡号),又能直接把支付方式关联到订阅。

方式一:自定义表单(用Stripe Elements)

这是灵活性最高的方式,适合需要自定义页面UI的场景:

  • 前端:加载Stripe.js,渲染Elements组件(推荐用paymentElement,支持信用卡、支付宝等多种支付方式)
<!-- 加载Stripe的JS库 -->
<script src="https://js.stripe.com/v3/"></script>
<!-- 支付信息输入框容器 -->
<div id="payment-element"></div>
<button id="subscribe-btn">确认订阅</button>

<script>
  // 初始化Stripe实例(替换成你的公钥)
  const stripe = Stripe('pk_test_xxxxxx');
  const elements = stripe.elements();

  // 创建并挂载Payment Element
  const paymentElement = elements.create('payment');
  paymentElement.mount('#payment-element');

  // 处理订阅提交
  document.getElementById('subscribe-btn').addEventListener('click', async () => {
    // 先调用后端创建订阅的接口,拿到客户端密钥
    const { clientSecret } = await fetch('/api/create-subscription', {
      method: 'POST',
      body: JSON.stringify({ priceId: 'price_xxxxxx' }) // 替换成你的订阅价格ID
    }).then(res => res.json());

    // 确认支付
    const { error } = await stripe.confirmPayment({
      elements,
      clientSecret,
      confirmParams: {
        return_url: 'https://your-domain.com/subscription-success'
      }
    });

    if (error) {
      // 处理支付错误,比如提示用户
      alert(error.message);
    }
  });
</script>
  • 后端:创建客户、关联支付方式、生成订阅(以Node.js为例)
const stripe = require('stripe')('sk_test_xxxxxx'); // 替换成你的私钥

app.post('/api/create-subscription', async (req, res) => {
  const { priceId } = req.body;

  // 先创建一个客户(如果用户已经有账户,直接用已有客户ID)
  const customer = await stripe.customers.create();

  // 创建订阅会话,生成客户端密钥
  const subscription = await stripe.subscriptions.create({
    customer: customer.id,
    items: [{ price: priceId }],
    payment_behavior: 'default_incomplete',
    expand: ['latest_invoice.payment_intent'],
  });

  res.json({
    clientSecret: subscription.latest_invoice.payment_intent.client_secret
  });
});

方式二:Stripe Checkout托管页面(更简单)

如果不需要自定义UI,直接用Stripe提供的Checkout页面最省心,全程由Stripe托管支付流程,你只需要生成一个订阅链接:

// 后端代码(Node.js)
app.post('/api/create-checkout-session', async (req, res) => {
  const session = await stripe.checkout.sessions.create({
    payment_method_types: ['card'],
    line_items: [{
      price: 'price_xxxxxx', // 你的订阅价格ID
      quantity: 1,
    }],
    mode: 'subscription', // 指定为订阅模式
    success_url: 'https://your-domain.com/sub-success?session_id={CHECKOUT_SESSION_ID}',
    cancel_url: 'https://your-domain.com/sub-cancel',
  });

  res.json({ url: session.url }); // 把这个URL返回给前端,让用户跳转过去
});

前端拿到URL后直接跳转,用户在Stripe的页面输入支付信息,完成后Stripe自动创建订阅,还会通知你后续的扣款结果。

2. Stripe Payments和Billing的交互流程

其实二者是互补的关系:

  • Stripe Payments负责单次支付的处理和支付信息收集(通过Elements或Checkout)
  • Stripe Billing负责订阅生命周期管理(自动扣款、发票生成、订阅变更等)

完整的交互流程是这样的:

  1. 用户选择订阅方案,前端发起请求到后端
  2. 后端调用Stripe API创建客户(或复用已有客户),并生成订阅会话/客户端密钥
  3. 前端用Stripe Elements收集支付信息,或者跳转至Checkout页面
  4. Stripe验证支付信息,生成支付方式ID,并关联到客户
  5. Stripe Billing自动按订阅周期发起扣款,通过webhook通知你扣款成功/失败的状态
  6. 你根据webhook的通知更新用户的订阅状态(比如解锁会员功能)
3. 可参考的实现示例

Stripe官方有很多开箱即用的示例,你可以直接照搬调整:

  • 自定义订阅表单示例:包含前端Elements渲染、后端订阅创建、webhook处理的完整流程,支持多种技术栈(React、Vue、Node.js、Python等)
  • Checkout订阅示例:直接生成订阅链接的极简示例,适合快速搭建订阅功能
  • 还有官方的订阅管理仪表盘示例,展示如何处理订阅升级、降级、暂停等操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:34:03