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

Quarkus+Kotlin调用Stripe API创建支付链接遇415/400错误求助

问题分析与解决方案

核心问题排查

你遇到的415(媒体类型不支持)和400(参数缺失)错误,根源在两个关键问题:

  1. 认证头格式错误:Stripe API要求Authorization头必须以Bearer 为前缀,直接传入密钥会导致认证失败,进而引发参数或媒体类型的误判。
  2. 表单参数序列化失败:自定义请求类未使用@BeanParam注解,Quarkus RestClient无法正确将对象转换为application/x-www-form-urlencoded格式的参数,导致媒体类型不兼容或参数格式错误。

分步修复

1. 修正认证头格式

构造Authorization头时必须添加Bearer 前缀:

val authorizationHeader = "Bearer $secretKey"

2. 修复表单参数序列化

将请求类标记为@BeanParam,让Quarkus正确解析表单字段:

@BeanParam
data class CreatePaymentLinkRequest(
    @FormParam("line_items[0][price]")
    val lineItemPrice: String,

    @FormParam("line_items[0][quantity]")
    val lineItemQuantity: Int
)

同时更新RestClient接口,使用@BeanParam接收请求参数:

@Path("/v1/payment_links") // 必须添加Stripe API的v1前缀
@RegisterRestClient(configKey = "stripe-link")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
interface StripeCaller {
    @POST
    fun createPaymentLink(
        @BeanParam request: CreatePaymentLinkRequest,
        @HeaderParam("Authorization") authorizationHeader: String
    ): PaymentLinkList
}

3. 验证RestClient配置

确保application.properties中Stripe的RestClient配置正确指向官方API地址:

stripe-link/mp-rest/url=https://api.stripe.com
stripe-link/mp-rest/connect-timeout=5000
stripe-link/mp-rest/read-timeout=5000

可选方案:直接使用MultiValuedMap传递参数

如果不想依赖自定义对象的序列化,可以直接构造表单参数Map,避免序列化问题:

// 更新StripeCaller接口
@POST
fun createPaymentLink(
    @Form params: MultiValuedMap<String, String>,
    @HeaderParam("Authorization") authorizationHeader: String
): PaymentLinkList

// 在Controller中构造参数
fun createPaymentLink(request: CreatePaymentLinkRequest): Response {
    val authorizationHeader = "Bearer $secretKey"
    val params = MultivaluedHashMap<String, String>().apply {
        add("line_items[0][price]", request.lineItemPrice)
        add("line_items[0][quantity]", request.lineItemQuantity.toString())
    }
    val link = stripeCaller.createPaymentLink(params, authorizationHeader)
    return Success(data = link).respond()
}

额外注意事项

  • 确保Stripe的API密钥拥有创建支付链接的权限
  • 测试时可先通过Stripe官方CLI或Postman直接发送表单请求验证参数正确性,再排查代码问题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 15:23:12