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

在Spring Boot应用中使用Stripe Configuration ID及启用指定支付方式

解决方法

用自定义支付配置ID(pmc_开头)

Stripe Java SDK支持在创建Checkout Session时关联你在Dashboard里配置的自定义支付方案,直接调用setPaymentMethodConfiguration方法传入你的pmc_开头ID即可,注意要使用较新版本的SDK。在代码中添加如下配置:

.setPaymentMethodConfiguration("pmc_你的配置ID")

添加后,Dashboard配置里的所有支付方式会自动生效,无需再手动逐个添加PaymentMethodType枚举值。

手动添加PayPal/Google Pay/Apple Pay

如果不想依赖自定义配置ID,也可以直接传入字符串形式的支付方式标识——addPaymentMethodType方法除了支持枚举,还能接受字符串参数:

  • PayPal:填"paypal"
  • Google Pay:填"google_pay"
  • Apple Pay:填"apple_pay"

修改支付方式部分的代码如下:

.addPaymentMethodType(SessionCreateParams.PaymentMethodType.CARD)
.addPaymentMethodType("paypal")
.addPaymentMethodType("google_pay")
.addPaymentMethodType("apple_pay")

注意:Apple Pay和Google Pay需要满足Stripe的启用条件(比如域名验证、设备环境支持),PayPal则需要先在Stripe Dashboard中开启该支付方式。

修改后的完整代码示例

@Service
@RequiredArgsConstructor
public class StripeService implements StripeServiceInterface {
    private PaymentsRepository paymentsRepository;

    private static void init() {
        Stripe.apiKey = "censored";
    }

    @Override
    public Map<String, String> createSession(StripeCheckoutDTO stripeCheckoutDTO) throws StripeException {
        init();

        SessionCreateParams params = SessionCreateParams.builder()
                // 两种方式二选一即可
                // 方式一:使用自定义支付配置ID
                .setPaymentMethodConfiguration("pmc_你的配置ID")
                // 方式二:手动指定支付方式
                // .addPaymentMethodType(SessionCreateParams.PaymentMethodType.CARD)
                // .addPaymentMethodType("paypal")
                // .addPaymentMethodType("google_pay")
                // .addPaymentMethodType("apple_pay")
                .setMode(SessionCreateParams.Mode.PAYMENT)
                .setSuccessUrl("http://localhost:4200/payment-successful")
                .setCancelUrl("http://localhost:4200/payment-declined")
                .addLineItem(
                        SessionCreateParams.LineItem.builder()
                                .setQuantity(stripeCheckoutDTO.getQuantity())
                                .setPriceData(
                                        SessionCreateParams.LineItem.PriceData.builder()
                                                .setCurrency(stripeCheckoutDTO.getCurrency())
                                                .setUnitAmount(stripeCheckoutDTO.getAmount())
                                                .setProductData(
                                                        SessionCreateParams.LineItem.PriceData.ProductData.builder()
                                                                .setName(stripeCheckoutDTO.getName())
                                                                .build())
                                                .build())
                                .build())
                .build();

        Session session = Session.create(params);
        Map<String, String> responseData = new HashMap<>();

        responseData.put("id", session.getId());

        return responseData;
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 21:00:19