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