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);
|
}
|
}
|