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

Stripe React Native:使用IntentConfiguration保存支付方法的问题

Stripe Payment Sheet 无法保存支付方法问题解决

你当前使用Stripe React Native的延迟创建支付意向流程,遇到两类用户的支付方法保存问题:

  • 订阅用户:需要setupFutureUsage='OffSession'但无法保存支付方法
  • 一次性支付用户:无论是否设置setupFutureUsage,都无法保存银行卡信息

核心问题分析

问题根源在于后端没有根据用户类型和前端的保存选择动态设置setup_future_usage参数,同时前端需要将用户类型和保存意愿传递给后端,让支付意向创建时明确是否需要保存支付方法。

具体调整步骤

1. 前端传递必要参数到后端

修改handleCheckout函数,在调用后端创建支付意向接口时,额外传递用户类型(订阅/一次性)和是否保存支付方法的标识:

const handleCheckout = async ({ checkoutData, paymentMethod, shouldSavePaymentMethod, intentCreationCallback }) => {
  try {
    // 调用后端接口,传递用户类型和保存意愿
    const response = await fetch('/create-payment-intent', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        customer_id: customer,
        total_in_cents: checkoutData.totalInCents,
        metadata: checkoutData.metadata,
        is_subscription: checkoutData.isSubscription, // 假设checkoutData包含订阅标识
        should_save: shouldSavePaymentMethod // 直接使用Payment Sheet提供的参数
      })
    });
    const { client_secret, payment_intent_id } = await response.json();
    // 回调确认支付意向
    intentCreationCallback({ clientSecret: client_secret });
  } catch (error) {
    // 处理错误
  }
};

2. 后端动态设置setup_future_usage

修改HandleCreatePaymentIntent函数,根据前端传递的参数动态配置setup_future_usage:

def HandleCreatePaymentIntent(customer_id, total_in_cents, metadata, is_subscription, should_save):
    # 根据用户类型和保存意愿设置setup_future_usage
    setup_future_usage = None
    if is_subscription:
        # 订阅用户需要后续自动扣费,强制设置为off_session
        setup_future_usage = "off_session"
    elif should_save:
        # 一次性支付用户选择保存时,设置为on_session
        setup_future_usage = "on_session"
    
    payment_intent = stripe.PaymentIntent.create(
            amount=total_in_cents,
            currency='usd',
            customer=customer_id,
            capture_method='manual',
            metadata=metadata,
            setup_future_usage=setup_future_usage,
            automatic_payment_methods={"enabled": True},
        )

    return payment_intent.client_secret, payment_intent.id

3. 确保Ephemeral Key权限正确

创建Ephemeral Key时,必须包含payment_methods.write和payment_methods.read权限,这样Payment Sheet才能访问和保存用户的支付方法:

def create_ephemeral_key(customer_id, stripe_version):
    return stripe.EphemeralKey.create(
        customer=customer_id,
        stripe_version=stripe_version,
        permissions=["payment_methods.write", "payment_methods.read"]
    )

4. 验证保存结果

支付完成后,登录Stripe Dashboard:

  • 进入对应Customer详情页
  • 查看「支付方法」列表,确认银行卡是否已成功关联

关键注意事项

  • 订阅用户的off_session模式需要首次支付为on_session验证(即用户在场完成支付),后续才能自动发起off_session扣费
  • 一次性支付用户的on_session模式仅在用户明确选择保存时设置,避免不必要的支付方法存储
  • 确保传递给Payment Sheet的customerId和customerEphemeralKeySecret完全匹配且有效

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 01:55:14