From 4fabfe4dbd2eb28d07a4350597d314958cc1c281 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期四, 09 十月 2025 11:16:43 +0800
Subject: [PATCH] 优化

---
 server/web/src/main/java/com/doumee/api/web/PaymentCallback.java |  121 ++++++++++++++++++++++++++++++++++-----
 1 files changed, 104 insertions(+), 17 deletions(-)

diff --git a/server/web/src/main/java/com/doumee/api/web/PaymentCallback.java b/server/web/src/main/java/com/doumee/api/web/PaymentCallback.java
index ebf0845..3918ef8 100644
--- a/server/web/src/main/java/com/doumee/api/web/PaymentCallback.java
+++ b/server/web/src/main/java/com/doumee/api/web/PaymentCallback.java
@@ -2,17 +2,29 @@
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.StringUtils;
+import com.doumee.core.model.ApiResponse;
 import com.doumee.core.wx.WxMiniConfig;
 import com.doumee.service.business.GoodsorderService;
-import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
-import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
-import com.github.binarywang.wxpay.config.WxPayConfig;
+import com.doumee.service.business.RefundService;
+import com.wechat.pay.java.core.RSAAutoCertificateConfig;
+import com.wechat.pay.java.core.notification.NotificationConfig;
+import com.wechat.pay.java.core.notification.NotificationParser;
+import com.wechat.pay.java.core.notification.RequestParam;
+import com.wechat.pay.java.service.partnerpayments.jsapi.model.Transaction;
+import com.wechat.pay.java.service.refund.model.RefundNotification;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.shiro.SecurityUtils;
+import org.apache.shiro.mgt.DefaultSecurityManager;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
 
 /**
  * Created by IntelliJ IDEA.
@@ -31,23 +43,52 @@
     /**
      * 銆愬井淇℃敮浠樸�戝紓姝ラ�氱煡
      *
-     * @param xmlResult
      * @return
      */
     @PostMapping("/api/wxPayNotify")
-    public String wxPay_notify(@RequestBody String xmlResult) {
-        log.info(xmlResult);
-        if (StringUtils.isBlank(xmlResult)) return null;
+    public ApiResponse wxPay_notify(HttpServletRequest request) {
         try {
-            WxPayOrderNotifyResult result = WxMiniConfig.wxPayService.parseOrderNotifyResult(xmlResult);
+            ServletInputStream inputStream = request.getInputStream();
+            StringBuffer stringBuffer = new StringBuffer();
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
+            String s;
+            //璇诲彇鍥炶皟璇锋眰浣�
+            while ((s = bufferedReader.readLine()) != null) {
+                stringBuffer.append(s);
+            }
+            String body = stringBuffer.toString();
+            String timestamp = request.getHeader("Wechatpay-Timestamp");
+            String nonce = request.getHeader("Wechatpay-Nonce");
+            String signType = request.getHeader("Wechatpay-Signature-Type");
+            String serialNo = request.getHeader("wechatpay-Serial");
+            String signature = request.getHeader("Wechatpay-Signature");
+            RequestParam requestParam = new RequestParam.Builder()
+                    .serialNumber(serialNo)
+                    .nonce(nonce)
+                    .signType(signType)
+                    .signature(signature)
+                    .timestamp(String.valueOf(timestamp))
+                    .body(body)
+                    .build();
+
+            NotificationConfig config = new RSAAutoCertificateConfig.Builder()
+                    .merchantId(WxMiniConfig.wxProperties.getMchId())
+                    .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath())
+                    .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer())
+                    .apiV3Key(WxMiniConfig.wxProperties.getApiV3Key())
+                    .build();
+
+
+            NotificationParser parser = new NotificationParser(config);
+            Transaction result = parser.parse(requestParam, Transaction.class);
             //鑷畾涔夎鍗曞彿
             String outTradeNo = result.getOutTradeNo();
             //寰俊璁㈠崟鍙�
             String paymentNo = result.getTransactionId();
-            if ("SUCCESS".equals(result.getReturnCode())) {
-                // 鏀粯鎴愬姛
+            if ("SUCCESS".equals(result.getTradeState().name())) {
+                // 鏀粯鎴愬姛ge
                 switch (result.getAttach()) {
-                    //瀹堕暱鏀粯璁㈠崟
+                    //鏀粯璁㈠崟鍥炶皟
                     case "createGoodsOrder": {
                         goodsorderService.payNotify(outTradeNo,paymentNo);
                         break;
@@ -56,21 +97,67 @@
             } else {
                 // 鏀粯澶辫触
                 switch (result.getAttach()) {
-                    //瀹堕暱鏀粯璁㈠崟
-                    case "createOrder": {
-
+                    case "createGoodsOrder": {
                         break;
                     }
                 }
-                return WxPayNotifyResponse.fail(result.getReturnMsg());
             }
-            return WxPayNotifyResponse.success("澶勭悊鎴愬姛!");
+            return ApiResponse.success("澶勭悊鎴愬姛!");
         } catch (Exception e) {
             e.printStackTrace();
             log.error("寰俊鍥炶皟缁撴灉寮傚父,寮傚父鍘熷洜{}", e.getLocalizedMessage());
-            return WxPayNotifyResponse.fail(e.getMessage());
+            return ApiResponse.failed("");
         }
     }
+    @PostMapping("/api/wxRefundNotify")
+    public ApiResponse wxRefundNotify(HttpServletRequest request) {
+        log.error("寰俊閫�娆惧洖璋冪粨鏋滃紑濮�===========" );
+        try {
+            DefaultSecurityManager securityManager = new DefaultSecurityManager();
+            SecurityUtils.setSecurityManager(securityManager);
+            ServletInputStream inputStream = request.getInputStream();
+            StringBuffer stringBuffer = new StringBuffer();
+            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
+            String s;
+            //璇诲彇鍥炶皟璇锋眰浣�
+            while ((s = bufferedReader.readLine()) != null) {
+                stringBuffer.append(s);
+            }
+            String body = stringBuffer.toString();
+            String timestamp = request.getHeader("Wechatpay-Timestamp");
+            String nonce = request.getHeader("Wechatpay-Nonce");
+            String signType = request.getHeader("Wechatpay-Signature-Type");
+            String serialNo = request.getHeader("wechatpay-Serial");
+            String signature = request.getHeader("Wechatpay-Signature");
+            RequestParam requestParam = new RequestParam.Builder()
+                    .serialNumber(serialNo)
+                    .nonce(nonce)
+                    .signType(signType)
+                    .signature(signature)
+                    .timestamp(String.valueOf(timestamp))
+                    .body(body)
+                    .build();
 
+            NotificationConfig config = new RSAAutoCertificateConfig.Builder()
+                    .merchantId(WxMiniConfig.wxProperties.getMchId())
+                    .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath())
+                    .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer())
+                    .apiV3Key(WxMiniConfig.wxProperties.getApiV3Key())
+                    .build();
+
+
+            NotificationParser parser = new NotificationParser(config);
+            RefundNotification result = parser.parse(requestParam, RefundNotification.class);
+//            if ("SUCCESS".equals(result.getRefundStatus().name())) {
+                // 鏀粯鎴愬姛ge
+                     goodsorderService.closeGoodsorderDone(result);
+//            }
+            return ApiResponse.success("澶勭悊鎴愬姛");
+        } catch (Exception e) {
+            e.printStackTrace();
+            log.error("寰俊鍥炶皟缁撴灉寮傚父,寮傚父鍘熷洜{}", e.getLocalizedMessage());
+            return ApiResponse.failed("");
+        }
+    }
 
 }

--
Gitblit v1.9.3