Browse Source

feature 回调函数优化

xiahan 4 months ago
parent
commit
df7288e200

+ 2 - 1
zhsw-common/src/main/java/com/rongwei/zhsw/system/wechat/PayMentService.java

@@ -3,6 +3,7 @@ package com.rongwei.zhsw.system.wechat;
 import com.rongwe.zhsw.system.vo.PrepayNoticeVo;
 import com.rongwe.zhsw.system.vo.WxPrepayOrderVo;
 import com.rongwei.rwcommon.base.R;
+import org.springframework.http.ResponseEntity;
 
 import java.io.IOException;
 
@@ -15,5 +16,5 @@ import java.io.IOException;
 public interface PayMentService {
     R paymentInitiation(WxPrepayOrderVo vo);
 
-    R prepayNotice(PrepayNoticeVo prepayNoticeVo,String dskey) throws IOException;
+    ResponseEntity<String> prepayNotice(PrepayNoticeVo prepayNoticeVo, String dskey) throws IOException;
 }

+ 2 - 1
zhsw-common/src/main/java/com/rongwei/zhsw/system/wechat/RefundService.java

@@ -3,6 +3,7 @@ package com.rongwei.zhsw.system.wechat;
 import com.rongwe.zhsw.system.vo.PrepayNoticeVo;
 import com.rongwe.zhsw.system.vo.RefundInitiateVo;
 import com.rongwei.rwcommon.base.R;
+import org.springframework.http.ResponseEntity;
 
 /**
  * RefundService class
@@ -13,5 +14,5 @@ import com.rongwei.rwcommon.base.R;
 public interface RefundService {
     R refundByRecord(RefundInitiateVo vo);
 
-    R refundNotice(PrepayNoticeVo prepayNoticeVo,String dsKey);
+    ResponseEntity<String> refundNotice(PrepayNoticeVo prepayNoticeVo, String dsKey);
 }

+ 8 - 5
zhsw-common/src/main/java/com/rongwei/zhsw/system/wechat/impl/PayMentServiceImpl.java

@@ -22,6 +22,8 @@ import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 
 import javax.crypto.Cipher;
@@ -121,7 +123,7 @@ public class PayMentServiceImpl implements PayMentService {
      * @return
      */
     @Override
-    public R prepayNotice(PrepayNoticeVo prepayNoticeVo, String dskey) {
+    public ResponseEntity<String> prepayNotice(PrepayNoticeVo prepayNoticeVo, String dskey) {
         log.info("获取到的支付成功的回调:{},dskey:{}", prepayNoticeVo, dskey);
         List<String> dsKeys;
         if (StringUtils.isBlank(dskey)) {
@@ -131,6 +133,7 @@ public class PayMentServiceImpl implements PayMentService {
         } else {
             dsKeys = Collections.singletonList(dskey);
         }
+        R error = R.error("失败");
         dsKeys.remove("incontrol");
         // 获取所有商户的密钥
         List<SwEnterpriseConfigInfoDo> secretKeyList = commonBusinessDao.getSecretKey(dsKeys);
@@ -159,7 +162,7 @@ public class PayMentServiceImpl implements PayMentService {
         }
         if (StringUtils.isBlank(analysisStr)) {
             log.error("回调函数解析异常");
-            return R.error();
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error.toString());
         }
         ObjectMapper mapper = new ObjectMapper();
         WeChatPayTransactionVo transaction = null;
@@ -169,17 +172,17 @@ public class PayMentServiceImpl implements PayMentService {
         } catch (IOException e) {
             e.printStackTrace();
             log.error("JSON转换异常");
-            return R.error();
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error.toString());
         }
         // 微信订单号
         String transactionId = transaction.getTransactionId();
         // 商户号
         String outTradeNo = transaction.getOutTradeNo();
         Date successTime = transaction.getSuccessTime();
-        log.debug("支付回调:{}",transaction);
+        log.debug("支付回调:{}", transaction);
         log.error("开始更新缴费记录信息");
         // 更新缴费记录相关信息
         swBillingRecordService.getBaseMapper().updateWeChatPayInfo(deKey, outTradeNo, transactionId, analysisStr, successTime);
-        return R.ok();
+        return ResponseEntity.status(HttpStatus.OK).body("");
     }
 }

+ 10 - 8
zhsw-common/src/main/java/com/rongwei/zhsw/system/wechat/impl/RefundServiceImpl.java

@@ -29,6 +29,8 @@ import org.apache.commons.lang.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.web.context.request.RequestContextHolder;
 import org.springframework.web.context.request.ServletRequestAttributes;
@@ -178,7 +180,7 @@ public class RefundServiceImpl implements RefundService {
      * @return
      */
     @Override
-    public R refundNotice(PrepayNoticeVo prepayNoticeVo, String dsKey) {
+    public ResponseEntity<String> refundNotice(PrepayNoticeVo prepayNoticeVo, String dsKey) {
         log.info("微信退款申请成功回调函数: {},: {}", prepayNoticeVo, dsKey);
         List<String> dsKeys;
         if (StringUtils.isBlank(dsKey)) {
@@ -193,6 +195,7 @@ public class RefundServiceImpl implements RefundService {
         List<SwEnterpriseConfigInfoDo> secretKeyList = commonBusinessDao.getSecretKey(dsKeys);
         String analysisStr = null;
         String deKey = null;
+        R error = R.error("失败");
         for (SwEnterpriseConfigInfoDo swEnterpriseConfigInfoDo : secretKeyList) {
             try {
                 byte[] apiV3Key = swEnterpriseConfigInfoDo.getMerchantsecretkey().getBytes("UTF8");
@@ -216,7 +219,7 @@ public class RefundServiceImpl implements RefundService {
         }
         if (StringUtils.isBlank(analysisStr)) {
             log.error("回调函数解析异常");
-            return R.error();
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error.toString());
         }
         ObjectMapper mapper = new ObjectMapper();
         WeChatRefundNoticeVo weChatRefundNoticeVo;
@@ -225,9 +228,9 @@ public class RefundServiceImpl implements RefundService {
         } catch (IOException e) {
             e.printStackTrace();
             log.error("JSON转换异常");
-            return R.error();
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error.toString());
         }
-        log.debug("josn解析正常:{}",weChatRefundNoticeVo);
+        log.debug("json解析正常:{}",weChatRefundNoticeVo);
         ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
         HttpServletRequest request = attributes.getRequest();
         // 设置租户信息 自动切库
@@ -239,20 +242,19 @@ public class RefundServiceImpl implements RefundService {
                 .eq(BaseDo::getDeleted, "0").eq(SwRefundRequestRecordDO::getMerchantrefundnumber, outRefundNo));
         if (swRefundRequestRecordDO == null) {
             log.error("退款记录失败");
-            throw new CustomException("退款失败");
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error.toString());
         }
         if (!"SUCCESS".equals(weChatRefundNoticeVo.getRefundStatus())) {
             log.error("退款失败");
             // 更新缴费记录相关信息
             swRefundRequestRecordService.getBaseMapper().updateWeChatRefundInfo(deKey, outRefundNo,
                     refundId, analysisStr, weChatRefundNoticeVo.getSuccessTime(), "4");
-            return R.ok();
+            return ResponseEntity.status(HttpStatus.OK).body("");
         }
         // 更新缴费记录相关信息
         swRefundRequestRecordService.getBaseMapper().updateWeChatRefundInfo(deKey, outRefundNo,
                 refundId, analysisStr, weChatRefundNoticeVo.getSuccessTime(), "3");
-
         swRefundRequestRecordService.refundApplication(swRefundRequestRecordDO.getId());
-        return R.ok();
+        return ResponseEntity.status(HttpStatus.OK).body("");
     }
 }

+ 3 - 1
zhsw-server/src/main/java/com/rongwei/zhsw/system/controller/RefundController.java

@@ -8,9 +8,11 @@ import com.rongwei.zhsw.system.wechat.impl.RefundServiceImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 
 /**
  * RefundController class
@@ -44,7 +46,7 @@ public class RefundController {
      * @return
      */
     @PostMapping("/notice/{dskey}")
-    public R refundNotice(@RequestBody PrepayNoticeVo prepayNoticeVo, @PathVariable("dskey") String dskey) {
+    public ResponseEntity<String> refundNotice(@RequestBody PrepayNoticeVo prepayNoticeVo, @PathVariable("dskey") String dskey, HttpServletResponse httpServletResponse) {
         return refundService.refundNotice(prepayNoticeVo, dskey);
     }
 }

+ 2 - 1
zhsw-server/src/main/java/com/rongwei/zhsw/system/controller/weChat/PayMentController.java

@@ -10,6 +10,7 @@ import com.rongwei.zhsw.system.wechat.impl.PayMentServiceImpl;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.*;
 
 @RestController
@@ -56,7 +57,7 @@ public class PayMentController {
      * @return
      */
     @PostMapping("/prepay/notice/{dskey}")
-    public R prepayNotice(@RequestBody PrepayNoticeVo prepayNoticeVo, @PathVariable("dskey") String dskey) {
+    public ResponseEntity<String> prepayNotice(@RequestBody PrepayNoticeVo prepayNoticeVo, @PathVariable("dskey") String dskey) {
         return payMentService.prepayNotice(prepayNoticeVo, dskey);
     }
 }