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

如何在Stripe的PaymentIntent中使用银行卡令牌?需示例

使用Stripe PaymentIntent结合银行卡令牌实现支付(Java示例)

你现有的Charge是一次性创建并完成支付,而PaymentIntent需要创建+确认两步完成(这是Stripe推荐的现代支付流程)。下面是和你现有代码对应的PaymentIntent实现:

import com.stripe.exception.StripeException;
import com.stripe.model.PaymentIntent;
import java.util.HashMap;
import java.util.Map;

public PaymentIntent createAndConfirmPaymentIntent(Integer price, String token, String product) throws StripeException {
    Stripe.apiKey = stripeSecret;

    // 构建PaymentIntent创建参数
    Map<String, Object> createParams = new HashMap<>();
    createParams.put("amount", price);
    createParams.put("currency", "eur");
    // 注意:Charge用source传令牌,PaymentIntent改用payment_method
    createParams.put("payment_method", token);
    createParams.put("description", "Payment produto: " + product);
    // 设置confirm=true,创建时直接完成支付确认
    createParams.put("confirm", true);

    // 创建并确认PaymentIntent
    PaymentIntent paymentIntent = PaymentIntent.create(createParams);

    return paymentIntent;
}

关键差异说明

  • 参数替换:把Charge里的source换成PaymentIntent的payment_method,传入你的银行卡令牌即可
  • 支付流程:PaymentIntent默认需要确认动作,这里通过confirm=true一步完成创建+确认;如果需要分步骤操作(比如先创建Intent,后续再触发确认),可以去掉confirm=true,之后单独调用paymentIntent.confirm()方法
  • 返回对象:返回PaymentIntent而非Charge,可通过getStatus()方法获取支付状态(比如succeeded表示支付成功)

如果需要分步骤实现(创建和确认分开),可以参考下面的代码:

// 创建未确认的PaymentIntent
public PaymentIntent createPaymentIntent(Integer price, String token, String product) throws StripeException {
    Stripe.apiKey = stripeSecret;

    Map<String, Object> createParams = new HashMap<>();
    createParams.put("amount", price);
    createParams.put("currency", "eur");
    createParams.put("payment_method", token);
    createParams.put("description", "Payment produto: " + product);

    return PaymentIntent.create(createParams);
}

// 单独调用确认完成支付
public PaymentIntent confirmPaymentIntent(String paymentIntentId) throws StripeException {
    Stripe.apiKey = stripeSecret;
    PaymentIntent paymentIntent = PaymentIntent.retrieve(paymentIntentId);
    return paymentIntent.confirm();
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 06:10:05