# stripe-java **Repository Path**: _me_eric_code/stripe-java ## Basic Information - **Project Name**: stripe-java - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-25 - **Last Updated**: 2025-12-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Stripe支付服务 基于Spring Boot 2.x + DDD架构的Stripe支付集成服务,支持一次性支付和订阅支付。 ## 技术栈 - **基础框架**:Spring Boot 2.7.18 - **JDK版本**:JDK 1.8 - **微服务框架**:Spring Cloud + Alibaba Spring Cloud - **数据库**:MySQL 8.0 - **ORM框架**:MyBatis-Plus 3.5.3.1 - **缓存**:Redis - **连接池**:Druid - **支付SDK**:Stripe Java SDK 24.3.0 - **HTTP客户端**:Forest 1.5.33 - **工具库**:Hutool、Lombok、Fastjson ## 项目结构(DDD分层) ``` stripe-payment-service/ ├── domain/ # 领域层 │ ├── model/ # 领域模型 │ │ ├── aggregate/ # 聚合根(Order、Subscription) │ │ ├── entity/ # 实体(User) │ │ └── valueobject/ # 值对象(Money、CustomerInfo、ProductInfo) │ ├── service/ # 领域服务 │ └── repository/ # 仓储接口 ├── application/ # 应用层 │ ├── service/ # 应用服务 │ │ ├── PaymentApplicationService # 一次性支付服务 │ │ ├── SubscriptionApplicationService # 订阅支付服务 │ │ └── WebhookEventService # Webhook事件处理服务 │ └── dto/ # 数据传输对象 ├── infrastructure/ # 基础设施层 │ ├── repository/ # 仓储实现 │ ├── mapper/ # MyBatis Mapper │ ├── po/ # 持久化对象 │ ├── converter/ # 转换器 │ └── config/ # 配置 └── interfaces/ # 用户接口层 ├── controller/ # 控制器 │ ├── PaymentController # 一次性支付接口 │ ├── SubscriptionController # 订阅支付接口 │ └── WebhookController # Webhook回调接口 ├── vo/ # 视图对象 └── common/ # 通用响应 ``` ## 功能特性 ### 1. 一次性支付(Payment Intent) - 创建支付意图 - 支持优惠券折扣 - 自动创建Stripe客户 - 订单管理和状态跟踪 - 支付成功/失败回调处理 ### 2. 订阅支付(Subscription) - 创建订阅(月度/年度) - 动态创建产品和价格 - 支持优惠券 - 订阅生命周期管理 - 自动续费处理 - 发票支付跟踪 ### 3. Webhook回调处理 支持的事件类型: - `payment_intent.succeeded` - 支付成功 - `payment_intent.payment_failed` - 支付失败 - `payment_intent.canceled` - 支付取消 - `customer.subscription.created` - 订阅创建 - `customer.subscription.updated` - 订阅更新 - `customer.subscription.deleted` - 订阅删除 - `invoice.paid` - 发票支付成功 - `invoice.payment_failed` - 发票支付失败 ## 数据库表结构 ### 1. 用户表(t_user) - 用户基本信息 - Stripe客户ID关联 ### 2. 订单表(t_order) - 订单信息 - 支付意图关联 - 订单状态管理 ### 3. 订阅履约表(t_subscription) - 订阅信息 - 订阅周期管理 - 订阅状态跟踪 ### 4. 支付事件表(t_payment_event) - Webhook事件记录 - 事件处理状态 - 重试机制 ## 快速开始 ### 1. 环境要求 - JDK 1.8+ - MySQL 8.0+ - Redis 6.0+ - Maven 3.6+ ### 2. 数据库初始化 执行数据库脚本: ```bash mysql -u root -p < sql/schema.sql ``` ### 3. 配置文件 修改 `src/main/resources/application.yml`: ```yaml # 数据库配置 spring: datasource: url: jdbc:mysql://localhost:3306/stripe_payment username: root password: your_password # Redis配置 redis: host: localhost port: 6379 password: your_redis_password # Stripe配置 stripe: api-key: sk_test_your_stripe_secret_key webhook-secret: whsec_your_webhook_secret ``` ### 4. 启动服务 ```bash mvn clean install mvn spring-boot:run ``` 服务启动后访问:http://localhost:8080/stripe-payment ## API接口文档 ### 1. 创建一次性支付 **接口地址**:`POST /stripe-payment/payment/create-payment-intent` **请求示例**: ```json { "userId": 1, "email": "test@example.com", "username": "测试用户", "productCode": "PROD001", "productName": "测试商品", "amount": 99.99, "currency": "USD", "couponCode": "DISCOUNT10" } ``` **响应示例**: ```json { "code": 200, "message": "操作成功", "data": { "orderNo": "ORD1234567890", "clientSecret": "pi_xxx_secret_xxx", "paymentIntentId": "pi_xxx", "orderStatus": "PENDING" } } ``` ### 2. 创建订阅 **接口地址**:`POST /stripe-payment/subscription/create` **请求示例**: ```json { "userId": 1, "email": "test@example.com", "username": "测试用户", "productCode": "SUB_MONTHLY", "productName": "月度会员", "amount": 29.99, "currency": "USD", "planInterval": "MONTH", "couponCode": "DISCOUNT10" } ``` **响应示例**: ```json { "code": 200, "message": "操作成功", "data": { "orderNo": "ORD1234567890", "subscriptionNo": "SUB1234567890", "stripeSubscriptionId": "sub_xxx", "stripeCustomerId": "cus_xxx", "subscriptionStatus": "incomplete", "clientSecret": "pi_xxx_secret_xxx" } } ``` ### 3. Webhook回调 **接口地址**:`POST /stripe-payment/webhook/stripe` **请求头**: - `Stripe-Signature`: Stripe签名验证 Stripe会自动发送事件到此接口,需要在Stripe Dashboard配置Webhook URL。 ## 前端集成示例 ### 使用Stripe Elements完成支付 ```javascript // 1. 调用后端API获取clientSecret const response = await fetch('/stripe-payment/payment/create-payment-intent', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ userId: 1, email: 'test@example.com', username: '测试用户', productCode: 'PROD001', productName: '测试商品', amount: 99.99, currency: 'USD' }) }); const { data } = await response.json(); const { clientSecret } = data; // 2. 使用Stripe.js完成支付 const stripe = Stripe('pk_test_your_publishable_key'); const { error, paymentIntent } = await stripe.confirmCardPayment(clientSecret, { payment_method: { card: cardElement, billing_details: { name: '测试用户', email: 'test@example.com' } } }); if (error) { console.error('支付失败:', error); } else if (paymentIntent.status === 'succeeded') { console.log('支付成功!'); } ``` ## 测试 ### 运行单元测试 ```bash mvn test ``` ### 测试类说明 - `PaymentIntentTest` - 一次性支付测试 - `SubscriptionTest` - 订阅支付测试 ### Stripe测试卡号 - 成功支付:`4242 4242 4242 4242` - 需要验证:`4000 0025 0000 3155` - 支付失败:`4000 0000 0000 9995` ## 优惠券配置 系统内置测试优惠券: - `DISCOUNT10` - 10%折扣 - `DISCOUNT20` - 20%折扣 - `SAVE5` - 固定减$5 可在 `PaymentDomainService` 中自定义优惠券规则。 ## 监控和日志 - **Druid监控**:http://localhost:8080/stripe-payment/druid/ - 用户名:admin - 密码:admin123 - **日志级别**:可在 `application.yml` 中配置 ## 架构设计原则 - **高内聚,低耦合**:DDD分层架构,职责清晰 - **单一职责原则**:每个类只负责一类功能 - **依赖倒置**:依赖抽象而非具体实现 - **充血模型**:业务逻辑封装在领域对象中 - **事件驱动**:通过Webhook实现异步事件处理 ## 常见问题 ### 1. Webhook签名验证失败 确保 `application.yml` 中的 `webhook-secret` 配置正确,可从Stripe Dashboard获取。 ### 2. 支付失败 检查Stripe API Key是否正确,是否使用测试环境的Key。 ### 3. 数据库连接失败 确认MySQL服务已启动,数据库配置正确。 ## License MIT License ## 作者 Stripe Payment Team ## 更新日志 ### v1.0.0 (2024-01-01) - 初始版本 - 支持一次性支付 - 支持订阅支付 - 支持Webhook回调 - 完整的DDD架构实现