wlzboy
2 天以前 8cb5d3440208a3be3e772e65f1bd0ec63031ba62
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
package com.ruoyi.system.service.impl;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.system.service.IPaymentModuleService;
import com.ruoyi.payment.application.service.PaymentService;
import com.ruoyi.payment.interfaces.dto.PaymentRequest;
import com.ruoyi.payment.interfaces.dto.PaymentResponse;
import com.ruoyi.payment.domain.model.PaymentOrder;
import com.ruoyi.payment.infrastructure.config.AlipayConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * 支付模块调用Service实现
 * TODO: 实际项目中需要替换为真实的支付模块HTTP调用
 * 
 * @author ruoyi
 * @date 2025-01-15
 */
@Service
public class PaymentModuleServiceImpl implements IPaymentModuleService {
    
    private static final Logger log = LoggerFactory.getLogger(PaymentModuleServiceImpl.class);
        @Autowired
        private PaymentService paymentService;
        
    @Autowired
    private AlipayConfig alipayConfig;
    
    @Value("${payment.module.url:http://localhost:8081/pay}")
    private String paymentModuleUrl;
    
    @Override
    public Map<String, Object> createQrCode(String outTradeNo, BigDecimal amount, String provider, 
                                           String subject, String attach, String callbackUrl) {
        log.info("调用支付模块创建二维码: outTradeNo={}, amount={}, provider={}, subject={}, callbackUrl={}", 
                 outTradeNo, amount, provider, subject, callbackUrl);
        
        try {
            PaymentRequest request = new PaymentRequest();
            request.setBizOrderId(outTradeNo);
            request.setAmount(amount.multiply(new BigDecimal(100)).intValue());
            request.setSubject(subject);
            request.setDescription(attach);
            request.setCallbackUrl(callbackUrl);
            PaymentResponse response;
            if (provider != null && "WECHAT".equalsIgnoreCase(provider)) {
                response = paymentService.createWechatNativePayment(request);
            } else {
                // 根据配置决定使用哪种支付宝支付方式
                String paymentMethod = alipayConfig.getPaymentMethod();
                if ("THIRD_PARTY".equalsIgnoreCase(paymentMethod) || "THIRD-PARTY".equalsIgnoreCase(paymentMethod)) {
                    response = paymentService.createAlipayThirdPartyPrecreate(request);
                } else {
                    response = paymentService.createAlipayPrecreate(request);
                }
            }
            Map<String, Object> result = new HashMap<>();
            result.put("paymentRefId", response.getOrderId() != null ? response.getOrderId().toString() : null);
            result.put("codeUrl", response.getQrBase64());
            java.time.LocalDateTime expireAt = response.getExpireAt();
            result.put("expireTime", expireAt != null ? java.util.Date.from(expireAt.atZone(java.time.ZoneId.systemDefault()).toInstant()) : new Date(System.currentTimeMillis() + 2 * 60 * 60 * 1000));
            return result;
        } catch (Exception ex) {
            log.error("调用支付模块创建二维码异常", ex);
            throw ex;
        }
    }
    
    @Override
    public Map<String, Object> queryPaymentStatus(String paymentRefId, String outTradeNo) {
        log.info("调用支付模块查询支付状态: paymentRefId={}, outTradeNo={}", paymentRefId, outTradeNo);
        
        try {
            Long orderId = null;
            try {
                orderId = paymentRefId != null ? Long.parseLong(paymentRefId) : null;
            } catch (NumberFormatException e) {
                log.warn("paymentRefId非数字,无法查询订单: {}", paymentRefId);
            }
            Map<String, Object> result = new HashMap<>();
            if (orderId == null) {
                result.put("status", "PENDING");
                result.put("tradeNo", null);
                result.put("payTime", null);
                return result;
            }
            PaymentOrder order = paymentService.getOrder(orderId);
            if (order == null) {
                result.put("status", "PENDING");
                result.put("tradeNo", null);
                result.put("payTime", null);
                return result;
            }
            String status = order.getStatus();
            if ("SUCCEEDED".equalsIgnoreCase(status)) {
                result.put("status", "PAID");
            } else if ("FAILED".equalsIgnoreCase(status) || "CANCELED".equalsIgnoreCase(status) || "EXPIRED".equalsIgnoreCase(status)) {
                result.put("status", "FAILED");
            } else {
                result.put("status", "PENDING");
            }
            result.put("tradeNo", order.getChannelTradeNo());
            java.time.LocalDateTime paidAt = order.getPaidAt();
            result.put("payTime", paidAt != null ? java.util.Date.from(paidAt.atZone(java.time.ZoneId.systemDefault()).toInstant()) : null);
            return result;
        } catch (Exception ex) {
            log.error("调用支付模块查询支付状态异常", ex);
            throw ex;
        }
    }
}