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

使用Stripe ExpressCheckoutElement时Apple/Google Pay支付方式ID获取问题

解决Stripe ExpressCheckoutElement获取Apple Pay支付方式ID的问题

当使用ExpressCheckoutElement处理Apple Pay支付时,你不需要手动调用createPaymentMethod来获取pm_开头的支付方式ID。ExpressCheckoutElement的onConfirm回调会自动提供包含支付方式信息的confirmParams对象,直接从中提取payment_method字段即可(该字段就是pm_开头的支付方式ID)。

具体实现步骤

  1. 正确配置Elements与ExpressCheckoutElement
    确保初始化时设置好Apple Pay启用、Google Pay禁用等参数:
const options = {
  mode: 'donation',
  amount: 5000, // 单位为最小货币单位,例:50美元对应5000
  currency: 'usd',
  automatic_payment_methods: {
    enabled: true,
  },
  expressCheckout: {
    applePay: 'always',
    googlePay: 'never',
    buttonType: 'donate',
  },
};

const elements = stripe.elements(options);
const expressCheckoutElement = elements.create('expressCheckout');
expressCheckoutElement.mount('#express-checkout-container');
  1. 在onConfirm回调中获取并使用支付方式ID
    直接从confirmParams中提取支付方式ID,用于确认PaymentIntent:
expressCheckoutElement.on('confirm', async (event) => {
  const { error, confirmParams } = event;
  if (error) {
    console.error(error);
    return;
  }

  try {
    // 提前在服务端创建PaymentIntent,获取其client_secret
    const clientSecret = 'pi_xxx_secret_xxx';
    
    const result = await stripe.confirmPayment({
      clientSecret,
      confirmParams: {
        payment_method: confirmParams.payment_method, // 这里就是pm_开头的ID
        return_url: 'https://your-domain.com/payment-result',
      },
    });

    if (result.paymentIntent.status === 'succeeded') {
      // 处理支付成功逻辑
      console.log('捐赠支付完成');
    } else {
      console.error('支付未完成');
    }
  } catch (err) {
    console.error(err);
  }
});

关键注意点

  • 提前创建PaymentIntent时,无需指定支付方式,仅需设置金额、货币、模式等基础参数即可。
  • 若直接使用服务端返回的PaymentIntent ID而非client_secret,可调用stripe.confirmPaymentIntent方法,传入payment_method参数即可。

内容的提问来源于stack exchange,提问作者Nairi Areg Hatspanyan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 13:32:20