wrj 10 месяцев назад
Родитель
Сommit
9dd87c1f62

+ 65 - 25
nightFragrance-admin/src/main/java/com/ylx/web/controller/massage/PayController.java

@@ -341,37 +341,77 @@ public class PayController {
 
     @RequestMapping("/refund")
     @ResponseBody
-    public String refund(@RequestParam(required = false) String outRefundNo, BigDecimal amount,@RequestParam(required = false) String transactionId, @RequestParam(required = false) String outTradeNo) {
+    public String refund(@RequestParam(required = false) String outRefundNo, @RequestParam(required = false) BigDecimal amount,@RequestParam(required = false) String transactionId, @RequestParam(required = false) String outTradeNo) {
         return rechargeService.refund(outRefundNo,transactionId,outTradeNo,amount);
     }
 
-    /**
-     * 退款通知
-     */
-    @RequestMapping(value = "/refundNotify", method = {RequestMethod.POST, RequestMethod.GET})
-    @ResponseBody
+//    /**
+//     * 退款通知
+//     */
+//    @RequestMapping(value = "/refundNotify", method = {RequestMethod.POST, RequestMethod.GET})
+//    @ResponseBody
+//    @ApiOperation("微信退款回调接口")
+//    public String refundNotify(HttpServletRequest request) {
+//        String xmlMsg = HttpKit.readData(request);
+//        log.info("退款通知=" + xmlMsg);
+//        Map<String, String> params = WxPayKit.xmlToMap(xmlMsg);
+//        log.info("退款通知parms:{}",params);
+//        String returnCode = params.get("event_type");
+//        // 注意重复通知的情况,同一订单号可能收到多次通知,请注意一定先判断订单状态
+//        if (returnCode.equals("REFUND.SUCCESS")) {
+//            String reqInfo = params.get("resource");
+//            String decryptData = WxPayKit.decryptData(reqInfo, WxPayApiConfigKit.getWxPayApiConfig().getPartnerKey());
+//            log.info("退款通知解密后的数据=" + decryptData);
+////            refundVoucherService.
+//            // 更新订单信息
+//            // 发送通知等
+//            Map<String, String> xml = new HashMap<String, String>(2);
+//            xml.put("return_code", "SUCCESS");
+//            xml.put("return_msg", "OK");
+//            return WxPayKit.toXml(xml);
+//        }
+//        return null;
+//    }
     @ApiOperation("微信退款回调接口")
-    public String refundNotify(HttpServletRequest request) {
-        String xmlMsg = HttpKit.readData(request);
-        log.info("退款通知=" + xmlMsg);
-        Map<String, String> params = WxPayKit.xmlToMap(xmlMsg);
-        log.info("退款通知parms:{}",params);
-        String returnCode = params.get("event_type");
-        // 注意重复通知的情况,同一订单号可能收到多次通知,请注意一定先判断订单状态
-        if (returnCode.equals("REFUND.SUCCESS")) {
-            String reqInfo = params.get("resource");
-            String decryptData = WxPayKit.decryptData(reqInfo, WxPayApiConfigKit.getWxPayApiConfig().getPartnerKey());
-            log.info("退款通知解密后的数据=" + decryptData);
-//            refundVoucherService.
-            // 更新订单信息
-            // 发送通知等
-            Map<String, String> xml = new HashMap<String, String>(2);
-            xml.put("return_code", "SUCCESS");
-            xml.put("return_msg", "OK");
-            return WxPayKit.toXml(xml);
+    @RequestMapping(value = "/refundNotify", method = {org.springframework.web.bind.annotation.RequestMethod.POST, org.springframework.web.bind.annotation.RequestMethod.GET})
+    public void refundWechatCallback(HttpServletRequest request, HttpServletResponse response) {
+        log.info("微信退款回调接口====================================>>>>微信退款回调接口");
+        Map<String, String> map = new HashMap<>(12);
+        try {
+            String timestamp = request.getHeader("Wechatpay-Timestamp");
+            String nonce = request.getHeader("Wechatpay-Nonce");
+            String serialNo = request.getHeader("Wechatpay-Serial");
+            String signature = request.getHeader("Wechatpay-Signature");
+
+            log.info("timestamp:{} nonce:{} serialNo:{} signature:{}", timestamp, nonce, serialNo, signature);
+            String result = HttpKit.readData(request);
+            log.info("退款通知密文 {}", result);
+
+            // 需要通过证书序列号查找对应的证书,verifyNotify 中有验证证书的序列号
+            String plainText = WxPayKit.verifyNotify(serialNo, result, signature, nonce, timestamp,
+                    wxPayProperties.getMchKey(), wxPayProperties.getPlatFormPath());
+
+            log.info("退款通知明文 {}", plainText);
+
+            if (StrUtil.isNotEmpty(plainText)) {
+                response.setStatus(200);
+                map.put("code", "SUCCESS");
+                map.put("message", "SUCCESS");
+                // 处理业务逻辑
+
+            } else {
+                response.setStatus(500);
+                map.put("code", "ERROR");
+                map.put("message", "签名错误");
+            }
+            response.setHeader("Content-type", ContentType.JSON.toString());
+            response.getOutputStream().write(JSONUtil.toJsonStr(map).getBytes(StandardCharsets.UTF_8));
+            response.flushBuffer();
+        } catch (Exception e) {
+            log.error("系统异常", e);
         }
-        return null;
     }
 
 
+
 }