no message

This commit is contained in:
2025-08-21 17:33:28 +08:00
commit 9d744548d1
27 changed files with 2880 additions and 0 deletions

View File

@ -0,0 +1,177 @@
package com.sczx.pay.controller;
import com.sczx.pay.dto.PaymentRequest;
import com.sczx.pay.dto.PaymentResponse;
import com.sczx.pay.dto.RefundRequest;
import com.sczx.pay.service.WechatPayService;
import com.sczx.pay.utils.WXPayUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.HashMap;
import java.util.Map;
/**
* 微信支付控制器
*/
@RestController
@RequestMapping("/api/payment")
public class PaymentController {
private static final Logger logger = LoggerFactory.getLogger(PaymentController.class);
@Autowired
private WechatPayService wechatPayService;
/**
* 小程序统一下单接口
*/
@PostMapping("/unifiedOrder")
public PaymentResponse unifiedOrder(@RequestBody PaymentRequest request) {
logger.info("收到支付请求: {}", request);
return wechatPayService.unifiedOrder(request);
}
/**
* 查询订单接口
*/
@GetMapping("/query/{companyId}/{outTradeNo}")
public Map<String, String> orderQuery(@PathVariable Long companyId, @PathVariable String outTradeNo) {
logger.info("收到订单查询请求公司ID: {}, 订单号: {}", companyId, outTradeNo);
try {
return wechatPayService.orderQuery(companyId, outTradeNo);
} catch (Exception e) {
logger.error("订单查询异常公司ID: {}, 订单号: {}", companyId, outTradeNo, e);
Map<String, String> errorResult = new HashMap<>();
errorResult.put("return_code", "FAIL");
errorResult.put("return_msg", "查询异常: " + e.getMessage());
return errorResult;
}
}
/**
* 关闭订单接口
*/
@PostMapping("/close/{companyId}/{outTradeNo}")
public Map<String, String> closeOrder(@PathVariable Long companyId, @PathVariable String outTradeNo) {
logger.info("收到关闭订单请求公司ID: {}, 订单号: {}", companyId, outTradeNo);
try {
return wechatPayService.closeOrder(companyId, outTradeNo);
} catch (Exception e) {
logger.error("关闭订单异常公司ID: {}, 订单号: {}", companyId, outTradeNo, e);
Map<String, String> errorResult = new HashMap<>();
errorResult.put("return_code", "FAIL");
errorResult.put("return_msg", "关闭异常: " + e.getMessage());
return errorResult;
}
}
/**
* 申请退款接口
*/
@PostMapping("/refund")
public Map<String, String> refund(@RequestBody RefundRequest request) {
logger.info("收到退款请求: {}", request);
try {
return wechatPayService.refund(request);
} catch (Exception e) {
logger.error("退款异常公司ID: {}, 订单号: {}", request.getCompanyId(), request.getOutTradeNo(), e);
Map<String, String> errorResult = new HashMap<>();
errorResult.put("return_code", "FAIL");
errorResult.put("return_msg", "退款异常: " + e.getMessage());
return errorResult;
}
}
/**
* 查询退款接口
*/
@GetMapping("/refundQuery/{companyId}")
public Map<String, String> refundQuery(@PathVariable Long companyId, @RequestParam String outTradeNo) {
logger.info("收到退款查询请求公司ID: {}, 订单号: {}", companyId, outTradeNo);
try {
return wechatPayService.refundQuery(companyId, outTradeNo);
} catch (Exception e) {
logger.error("退款查询异常公司ID: {}, 订单号: {}", companyId, outTradeNo, e);
Map<String, String> errorResult = new HashMap<>();
errorResult.put("return_code", "FAIL");
errorResult.put("return_msg", "退款查询异常: " + e.getMessage());
return errorResult;
}
}
/**
* 微信支付结果通知
*/
@PostMapping("/notify/{companyId}")
public String notify(@PathVariable Long companyId, HttpServletRequest request) {
try {
// 读取微信回调数据
StringBuilder sb = new StringBuilder();
BufferedReader reader = request.getReader();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
String xmlData = sb.toString();
logger.info("收到微信支付通知公司ID: {}, 数据: {}", companyId, xmlData);
// 解析XML数据
Map<String, String> notifyMap = WXPayUtil.xmlToMap(xmlData);
// 验证签名
if (!wechatPayService.verifyNotifySign(companyId, notifyMap)) {
logger.warn("微信支付通知签名验证失败公司ID: {}", companyId);
return buildResponse("FAIL", "签名失败");
}
String returnCode = notifyMap.get("return_code");
if (!"SUCCESS".equals(returnCode)) {
logger.warn("微信支付通知返回失败公司ID: {}: {}", companyId, notifyMap.get("return_msg"));
return buildResponse("FAIL", "返回失败");
}
String resultCode = notifyMap.get("result_code");
if (!"SUCCESS".equals(resultCode)) {
logger.warn("微信支付业务失败公司ID: {}: {}", companyId, notifyMap.get("err_code_des"));
return buildResponse("FAIL", "业务失败");
}
// 处理支付成功的业务逻辑
String outTradeNo = notifyMap.get("out_trade_no");
String transactionId = notifyMap.get("transaction_id");
String totalFee = notifyMap.get("total_fee");
// 更新数据库中的订单状态
boolean success = wechatPayService.processPaySuccessNotify(companyId, notifyMap);
if (success) {
logger.info("支付成功公司ID: {}, 订单号: {}, 微信交易号: {}, 金额: {}",
companyId, outTradeNo, transactionId, totalFee);
return buildResponse("SUCCESS", "OK");
} else {
logger.error("更新支付状态失败公司ID: {}, 订单号: {}", companyId, outTradeNo);
return buildResponse("FAIL", "更新支付状态失败");
}
} catch (Exception e) {
logger.error("处理微信支付通知异常公司ID: {}", companyId, e);
return buildResponse("FAIL", "处理异常");
}
}
private String buildResponse(String returnCode, String returnMsg) {
Map<String, String> response = new HashMap<>();
response.put("return_code", returnCode);
response.put("return_msg", returnMsg);
try {
return WXPayUtil.mapToXml(response);
} catch (Exception e) {
return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[构建响应异常]]></return_msg></xml>";
}
}
}