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

新手求助:Java 1.8+Tomcat 7无框架集成Stripe支付网关

纯Java Servlet/JSP集成Stripe支付(无Spark)

完全可以不用Spark实现,以下是适配你环境的完整实操方案:

一、前置环境检查

确保你已准备好:

  • Java 1.8 + Tomcat 7.0(Eclipse Kepler本地部署)
  • 已添加兼容Java 1.8的Stripe Java SDK(推荐v20.129.0版本,新版可能要求Java 11+)
  • MySQL 5.7(可选,用于存储订单记录,若不需要可跳过相关代码)

二、核心代码实现

1. 商品选择页(product.jsp)

展示可选商品,提交支付请求到Servlet:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品购买</title>
</head>
<body>
    <h3>选择商品</h3>
    <form action="stripe-payment" method="post">
        <div>
            <label>
                <input type="radio" name="product" value="1" checked>
                商品A - 99元
            </label>
        </div>
        <div>
            <label>
                <input type="radio" name="product" value="2">
                商品B - 199元
            </label>
        </div>
        <br>
        <input type="submit" value="立即支付">
    </form>

    <%-- 显示失败提示(从Servlet跳转回来时) --%>
    <% String errorMsg = (String) request.getAttribute("errorMsg");
       if(errorMsg != null) { %>
        <p style="color:red;">* <%= errorMsg %></p>
    <% } %>
</body>
</html>

2. 支付处理Servlet(StripePaymentServlet.java)

纯Java无框架代码,处理支付请求、调用Stripe API、处理响应:

import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.PaymentIntent;
import com.stripe.param.PaymentIntentCreateParams;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/stripe-payment")
public class StripePaymentServlet extends HttpServlet {

    // 初始化Stripe密钥(替换为你的测试密钥)
    static {
        Stripe.apiKey = "sk_test_你的测试密钥";
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String productId = request.getParameter("product");
        long amount = 0;

        // 根据商品设置金额(Stripe要求金额为最小单位,人民币是分)
        switch (productId) {
            case "1":
                amount = 9900; // 99元 = 9900分
                break;
            case "2":
                amount = 19900; // 199元 = 19900分
                break;
            default:
                request.setAttribute("errorMsg", "无效的商品选择");
                request.getRequestDispatcher("product.jsp").forward(request, response);
                return;
        }

        try {
            // 创建PaymentIntent(Stripe核心支付对象)
            PaymentIntentCreateParams params = PaymentIntentCreateParams.builder()
                    .setAmount(amount)
                    .setCurrency("cny") // 人民币
                    .setDescription("商品支付:" + (productId.equals("1") ? "商品A" : "商品B"))
                    .build();

            PaymentIntent paymentIntent = PaymentIntent.create(params);

            // 支付成功(此处为简化逻辑,实际需结合前端Stripe.js完成支付确认)
            // 可选:将订单记录存入MySQL
            // saveOrderToDB(productId, amount, paymentIntent.getId());

            request.setAttribute("successMsg", "支付成功!支付ID:" + paymentIntent.getId());
            request.getRequestDispatcher("paymentResult.jsp").forward(request, response);

        } catch (StripeException e) {
            // 支付失败,捕获Stripe异常
            request.setAttribute("errorMsg", "支付失败:" + e.getMessage());
            request.getRequestDispatcher("product.jsp").forward(request, response);
        }
    }

    // 可选:保存订单到MySQL的方法
    private void saveOrderToDB(String productId, long amount, String paymentIntentId) {
        // 此处编写JDBC代码连接MySQL,插入订单记录
        // 示例:
        // String sql = "INSERT INTO orders(product_id, amount, payment_intent_id, status) VALUES (?, ?, ?, 'success')";
        // 使用PreparedStatement执行插入
    }
}

3. 支付结果页(paymentResult.jsp)

展示支付结果,失败时提供返回商品页的入口:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>支付结果</title>
</head>
<body>
    <% String successMsg = (String) request.getAttribute("successMsg");
       String errorMsg = (String) request.getAttribute("errorMsg");

       if(successMsg != null) { %>
        <h3 style="color:green;">✅ <%= successMsg %></h3>
        <p>感谢您的购买!</p>
    <% } else if(errorMsg != null) { %>
        <h3 style="color:red;">❌ <%= errorMsg %></h3>
        <a href="product.jsp">返回商品页重新购买</a>
    <% } %>
</body>
</html>

三、关键步骤说明

  1. 商品选择:用户在product.jsp选择商品,提交到StripePaymentServlet
  2. 支付处理:Servlet根据商品金额创建Stripe PaymentIntent,捕获API异常
  3. 结果响应:
    • 支付成功:跳转到paymentResult.jsp显示成功提示
    • 支付失败:携带错误信息跳转回product.jsp,显示失败提示

四、视频教程推荐

  • 搜索“Java Servlet JSP 集成Stripe支付 零基础实操”,选择时长15-30分钟的纯Servlet实现视频
  • 搜索“Stripe PaymentIntent Java 原生实现”,聚焦核心API调用逻辑的教程,适合新手快速理解

内容的提问来源于stack exchange,提问作者Shubham sahane Official

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 06:47:47