wlzboy
2025-11-27 668e570bd1db6bd00e4293b6977e6d3d051053ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.ruoyi.payment.example;
 
import com.ruoyi.payment.domain.event.PaymentSuccessEvent;
import com.ruoyi.payment.domain.model.PaymentOrder;
import com.ruoyi.payment.domain.model.PaymentTransaction;
import com.ruoyi.payment.interfaces.callback.PaymentCallback;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
 
/**
 * 支付模块集成使用示例
 * 
 * 当支付模块作为jar包集成到主项目时,主项目可以通过以下三种方式接收支付成功通知:
 * 
 * 方式1:监听Spring事件(推荐)
 * 方式2:实现PaymentCallback接口(推荐)
 * 方式3:提供HTTP回调URL(用于跨服务调用)
 * 
 * 注意:此文件仅作为示例,实际使用时应在主项目中创建类似的实现。
 * 
 * @author ruoyi
 */
@Slf4j
@Component
public class PaymentIntegrationExample implements PaymentCallback {
 
    // ==================== 方式1:监听Spring事件 ====================
    
    /**
     * 监听支付成功事件
     * 
     * 优点:
     * - Spring标准事件机制,解耦性好
     * - 可以有多个监听器
     * - 支持异步处理(添加@Async)
     * 
     * 使用方式:
     * 在主项目的任意@Component类中添加此方法
     */
    @EventListener
    public void handlePaymentSuccess(PaymentSuccessEvent event) {
        PaymentOrder order = event.getOrder();
        PaymentTransaction transaction = event.getTransaction();
        
        log.info("【事件监听】收到支付成功通知");
        log.info("业务订单号: {}", order.getBizOrderId());
        log.info("支付金额: {} 分", order.getAmount());
        log.info("支付渠道: {}", order.getChannel());
        log.info("渠道交易号: {}", order.getChannelTradeNo());
        log.info("支付时间: {}", order.getPaidAt());
        
        // 处理业务逻辑
        processOrderPaymentSuccess(order, transaction);
    }
 
    // ==================== 方式2:实现PaymentCallback接口 ====================
    
    /**
     * 支付成功回调
     * 
     * 优点:
     * - 接口约束,明确回调方法
     * - 类型安全
     * - IDE智能提示
     * 
     * 使用方式:
     * 在主项目中创建类实现PaymentCallback接口并注册为Bean
     */
    @Override
    public void onPaymentSuccess(PaymentOrder order, PaymentTransaction transaction) {
        log.info("【接口回调】收到支付成功通知");
        log.info("业务订单号: {}", order.getBizOrderId());
        log.info("支付金额: {} 分", order.getAmount());
        
        // 处理业务逻辑
        processOrderPaymentSuccess(order, transaction);
    }
 
    // ==================== 方式3:HTTP回调URL ====================
    
    /**
     * HTTP回调方式
     * 
     * 在创建支付时传入callbackUrl:
     * 
     * PaymentRequest request = new PaymentRequest();
     * request.setCallbackUrl("https://your-domain.com/api/payment/notify");
     * 
     * 然后在主项目中创建接收HTTP回调的接口:
     * 
     * @RestController
     * @RequestMapping("/api/payment")
     * public class PaymentNotifyController {
     *     
     *     @PostMapping("/notify")
     *     public String receiveNotify(@RequestBody String payload,
     *                                  @RequestHeader("X-Signature") String signature,
     *                                  @RequestHeader("X-Nonce") String nonce,
     *                                  @RequestHeader("X-Timestamp") String timestamp) {
     *         // 1. 验证签名
     *         String signData = payload + nonce + timestamp;
     *         String calculated = SignUtil.hmacSha256(signData, "your-secret-key");
     *         if (!signature.equals(calculated)) {
     *             return "签名验证失败";
     *         }
     *         
     *         // 2. 解析数据
     *         JSONObject data = JSON.parseObject(payload);
     *         String bizOrderId = data.getString("bizOrderId");
     *         Integer amount = data.getInteger("amount");
     *         
     *         // 3. 处理业务逻辑
     *         processOrderPayment(bizOrderId, amount);
     *         
     *         return "success";
     *     }
     * }
     */
 
    // ==================== 业务处理示例 ====================
    
    /**
     * 处理订单支付成功的业务逻辑
     */
    private void processOrderPaymentSuccess(PaymentOrder order, PaymentTransaction transaction) {
        String bizOrderId = order.getBizOrderId();
        
        // 示例:更新业务订单状态
        // orderService.updateOrderStatus(bizOrderId, "PAID");
        
        // 示例:发送支付成功通知
        // notificationService.sendPaymentSuccessNotification(bizOrderId);
        
        // 示例:触发后续业务流程
        // fulfillmentService.startFulfillment(bizOrderId);
        
        log.info("订单 {} 支付成功,业务处理完成", bizOrderId);
    }
}