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

Stripe /create-payment-intent接口返回404问题求助

搞定Stripe集成里的/create-payment-intent 404错误

嘿,我看你现在卡在Stripe支付集成的404问题上了,核心问题其实是Spark Framework和Spring Boot的服务器端口冲突,导致你配置的Spark路由根本没在前端访问的8080端口上生效。咱们一步步来解决:

为啥会出现404?

Spark默认会自己启动一个内嵌服务器,监听4567端口;而Spring Boot默认用的是8080端口。你前端请求的是http://localhost:8080/create-payment-intent,但Spark的路由规则都在4567端口上,自然找不到对应的端点。而且你把Spark的路由配置写在Spring Boot的main方法里,这时候Spring Boot已经启动了Tomcat,两个服务器各跑各的,路由根本搭不上线。

两种解决办法任你选

办法1:让Spark用Spring Boot的8080端口

这个最简单,在初始化Spark路由前,加一行代码指定端口:

public static void main(String[] args) {
  // 先把Spark端口改成和Spring Boot一样的8080
  Spark.port(8080);
  SpringApplication.run(SpringbootClientRabbitmqApplication.class, args);
  Gson gson = new Gson();
  Stripe.apiKey = "sk_test_XXXXXXXXXXXXXXXXX";

  // 后面的post路由代码保持不变
}

⚠️ 注意:这么做的话,Spark会接管8080端口,Spring Boot自带的Tomcat就启动不了了。如果你只是用Spring Boot来启动Spark,不需要其他Spring功能,这个办法没问题;但如果还要用Spring的其他特性,更推荐办法2。

办法2:把Spark集成到Spring Boot的Tomcat里(推荐)

这是更优雅的方式,让Spark和Spring Boot共用一个Tomcat服务器,步骤如下:

  1. 先加Spark的Spring Boot starter依赖(如果还没加):
<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-spring-boot-starter</artifactId>
    <version>2.9.4</version>
</dependency>
  1. 把Spark的路由配置抽出来放到一个配置类里,别塞在main方法里了:
@Configuration
public class SparkRouteConfig {
    private static final Logger logger = LoggerFactory.getLogger(SparkRouteConfig.class);

    @PostConstruct
    public void setupSparkRoutes() {
        Gson gson = new Gson();
        Stripe.apiKey = "sk_test_XXXXXXXXXXXXXXXXX";

        // 配置/create-payment-intent路由
        post("/create-payment-intent", (request, response) -> {
            logger.info("收到create-payment-intent请求,请求体:{}", request.body());
            response.type("application/json");
            CreatePaymentBody postBody = gson.fromJson(request.body(), CreatePaymentBody.class);

            PaymentIntentCreateParams createParams = new PaymentIntentCreateParams.Builder()
                    .setCurrency(postBody.getCurrency())
                    .setAmount(calculateOrderAmount(postBody.getItems()))
                    .build();

            PaymentIntent intent = PaymentIntent.create(createParams);

            return gson.toJson(new CreatePaymentResponse("pk_test_XXXXXXXXXXXXXXXXXX", intent.getClientSecret()));
        });

        // 配置webhook路由
        post("/webhook", (request,response) -> {
            String payload = request.body();
            String sigHeader = request.headers("Stripe-Signature");
            String endpointSecret = "whsec_XXXXXXXXXXXXXXXXX";

            Event event = null;

            try {
                event = Webhook.constructEvent(payload, sigHeader, endpointSecret);
            } catch (SignatureVerificationException e) {
                response.status(400);
                return "";
            }

            switch (event.getType()) {
                case "payment_intent.succeeded":
                    System.out.println("✅ 付款成功!");
                    // 这里可以加订单处理、发送收据等逻辑
                    break;
                case "payment_intent.payment_failed":
                    System.out.println("❌ 付款失败!");
                    break;
                default:
                    response.status(400);
                    return "";
            }

            response.status(200);
            return "";
        });
    }
}
  1. 简化你的主类,只保留Spring Boot启动逻辑,还要确保能扫描到刚才的配置类:
@SpringBootApplication
@ComponentScan(basePackageClasses = {MainController.class, SparkRouteConfig.class})
public class SpringbootClientRabbitmqApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootClientRabbitmqApplication.class, args);
    }
}

额外排查点

  • 确认CreatePaymentBody和CreatePaymentResponse类有正确的getter方法,不然Gson解析请求体的时候会出错(不过这会导致500错误,不是404,但还是要检查)。
  • 启动应用后,看控制台日志,有没有Spark注册路由的信息,比如类似POST /create-payment-intent的日志,确认路由已经生效。
  • 用Postman直接发POST请求到http://localhost:8080/create-payment-intent测试,看看能不能拿到响应,这样能排除前端的问题。

小提示

  • 记得检查你的Stripe密钥是否正确,虽然这不是404的原因,但后续支付流程会用到。
  • 测试Webhook的时候,可以用Stripe CLI来模拟事件,不用非得部署到公网才能测。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 18:42:38