From 4e3e18cdb0d75c098b68353ef8c86cdd7c0f79b2 Mon Sep 17 00:00:00 2001
From: rk <94314517@qq.com>
Date: 星期四, 21 八月 2025 15:27:34 +0800
Subject: [PATCH] 111
---
server/admin/src/main/java/com/doumee/api/business/PaymentCallback.java | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 108 insertions(+), 2 deletions(-)
diff --git a/server/admin/src/main/java/com/doumee/api/business/PaymentCallback.java b/server/admin/src/main/java/com/doumee/api/business/PaymentCallback.java
index b7a6a64..33f43ee 100644
--- a/server/admin/src/main/java/com/doumee/api/business/PaymentCallback.java
+++ b/server/admin/src/main/java/com/doumee/api/business/PaymentCallback.java
@@ -1,9 +1,15 @@
package com.doumee.api.business;
+import com.alibaba.fastjson.JSON;
+import com.doumee.config.wx.TransferDetailEntityNew;
import com.doumee.config.wx.WxMiniConfig;
+import com.doumee.core.constants.ResponseStatus;
+import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.ApiResponse;
import com.doumee.service.business.OrdersService;
+import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
+import com.wechat.pay.java.core.RSAPublicKeyConfig;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
@@ -20,7 +26,11 @@
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
import java.io.InputStreamReader;
+import java.util.HashMap;
+import java.util.Map;
/**
* Created by IntelliJ IDEA.
@@ -41,7 +51,7 @@
*
* @return
*/
- @PostMapping("/api/wxPayNotify")
+ @PostMapping("/web/wxPayNotify")
public ApiResponse wxPay_notify(HttpServletRequest request) {
try {
ServletInputStream inputStream = request.getInputStream();
@@ -76,6 +86,8 @@
NotificationParser parser = new NotificationParser(config);
+
+
Transaction result = parser.parse(requestParam, Transaction.class);
//鑷畾涔夎鍗曞彿
String outTradeNo = result.getOutTradeNo();
@@ -105,7 +117,7 @@
return ApiResponse.failed("");
}
}
- @PostMapping("/api/wxRefundNotify")
+ @PostMapping("/web/wxRefundNotify")
public ApiResponse wxRefundNotify(HttpServletRequest request) {
log.error("寰俊閫�娆惧洖璋冪粨鏋滃紑濮�===========" );
try {
@@ -156,4 +168,98 @@
}
}
+
+
+
+ /**
+ * 寰俊鍟嗘埛闆剁嚎杞处 - 鍥炶皟閫氱煡
+ * @Context娉ㄨВ 鎶奌TTP璇锋眰涓婁笅鏂囧璞℃敞鍏ヨ繘鏉ワ紝HttpServletRequest銆丠ttpServletResponse銆乁riInfo 绛�
+ * @return
+ */
+ @PostMapping(value = "/web/wechat/transferNotify")
+ public ApiResponse wxPayCallback( HttpServletRequest request) {
+ Map<String,String> errMap = new HashMap<>();
+ try {
+ log.info("寰俊鍟嗘埛闆剁嚎杞处 - 鍥炶皟閫氱煡 /wxpay/callback");
+ TransferDetailEntityNew entity = wxPaySuccessCallback(request);
+ log.info("transfer ok.{}",entity);
+ //鍥炶皟鎴愬姛鍚庡鐞嗚嚜宸辩殑涓氬姟
+ if(entity != null){
+ }
+ return ApiResponse.success("澶勭悊鎴愬姛");
+ } catch (Exception e) {
+ log.error("寰俊鍟嗘埛闆剁嚎杞处 - 鍥炶皟閫氱煡 /wxpay/callback锛氬紓甯革紒", e);
+ errMap.put("code", "FAIL");
+ errMap.put("message", "鏈嶅姟鍣ㄥ唴閮ㄩ敊璇�");
+ return ApiResponse.failed("澶勭悊澶辫触");
+ }
+ }
+
+ public TransferDetailEntityNew wxPaySuccessCallback(HttpServletRequest request) throws IOException {
+ String requestBody = getBodyString(request, "UTF-8");
+ //璇佷功搴忓垪鍙凤紙寰俊骞冲彴锛� 楠岀鐨勨�滃井淇℃敮浠樺钩鍙拌瘉涔︹�濇墍瀵瑰簲鐨勫钩鍙拌瘉涔﹀簭鍒楀彿
+ String wechatPaySerial = request.getHeader("Wechatpay-Serial");
+ //寰俊浼犻�掕繃鏉ョ殑绛惧悕 楠岀鐨勭鍚嶅��
+ String wechatSignature = request.getHeader("Wechatpay-Signature");
+ //楠岀鐨勬椂闂存埑
+ String wechatTimestamp = request.getHeader("Wechatpay-Timestamp");
+ //楠岀鐨勯殢鏈哄瓧绗︿覆
+ String wechatpayNonce = request.getHeader("Wechatpay-Nonce");
+ // 1. 鏋勯�� RequestParam
+ RequestParam requestParam = new RequestParam.Builder()
+ .serialNumber(wechatPaySerial)
+ .nonce(wechatpayNonce)
+ .signature(wechatSignature)
+ .timestamp(wechatTimestamp)
+ .body(requestBody)
+ .build();
+ // 2. 鏋勫缓Config RSAPublicKeyConfig
+ Config config = new RSAPublicKeyConfig.Builder()
+ .merchantId(WxMiniConfig.wxProperties.getMchId()) //寰俊鏀粯鐨勫晢鎴峰彿
+ .privateKeyFromPath(WxMiniConfig.wxProperties.getPrivateKeyPath()) // 鍟嗘埛API璇佷功绉侀挜鐨勫瓨鏀捐矾寰�
+ .publicKeyFromPath(WxMiniConfig.wxProperties.getWechatPubKeyPath()) //寰俊鏀粯鍏挜鐨勫瓨鏀捐矾寰�
+ .publicKeyId(WxMiniConfig.wxProperties.getWechatPayPublicKeyId()) //寰俊鏀粯鍏挜ID
+ .merchantSerialNumber(WxMiniConfig.wxProperties.getSerialNumer()) //鍟嗘埛API璇佷功搴忓垪鍙�
+ .apiV3Key("") //APIv3瀵嗛挜
+ .build();
+ log.info("WxPayService.wxPaySuccessCallback request : wechatPaySerial is [{}] , wechatSignature is [{}] , wechatTimestamp is [{}] , wechatpayNonce is [{}] , requestBody is [{}]",wechatPaySerial,wechatSignature,wechatTimestamp,wechatpayNonce,requestBody);
+ // 3. 鍒濆鍖� NotificationParser
+ NotificationParser parser = new NotificationParser((NotificationConfig) config);
+ try {
+ TransferDetailEntityNew entity = parser.parse(requestParam, TransferDetailEntityNew.class);
+ log.info("WxPayService.wxPaySuccessCallback responseBody: {}", entity != null ? JSON.toJSONString(entity) : null);
+ return entity;
+ } catch (Exception e) {
+ log.error("Exception occurred while processing", e);
+ throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"绯荤粺鍐呴儴閿欒");
+ }
+ }
+
+ /**
+ * 鑾峰彇post璇锋眰涓殑Body
+ *
+ * @param request httpRequest
+ * @return body瀛楃涓�
+ */
+ public static String getBodyString(HttpServletRequest request, String charSet) throws IOException {
+ StringBuilder sb = new StringBuilder();
+ InputStream inputStream = null;
+ BufferedReader reader = null;
+ try {
+ inputStream = request.getInputStream();
+ //璇诲彇娴佸苟灏嗘祦鍐欏嚭鍘�,閬垮厤鏁版嵁娴佷腑鏂�;
+ reader = new BufferedReader(new InputStreamReader(inputStream, charSet));
+ String line;
+ while ((line = reader.readLine()) != null) {
+ sb.append(line);
+ }
+ } catch (IOException e) {
+ log.error("鑾峰彇requestBody寮傚父", e);
+ } finally {
+ inputStream.close();
+ reader.close();
+ }
+ return sb.toString();
+ }
+
}
--
Gitblit v1.9.3