Browse Source

feature 解决账单缴费报错的问题增加回调函数的异常问题

xiahan 3 weeks ago
parent
commit
e2f7a7a55f

+ 12 - 19
zhsw-wechat-common/src/main/java/com/rongwei/wechat/system/wechat/impl/PayMentServiceImpl.java

@@ -15,8 +15,6 @@ import com.rongwei.rwcommon.base.BaseDo;
 import com.rongwei.rwcommon.base.R;
 import com.rongwei.rwcommon.base.exception.CustomException;
 import com.rongwei.rwcommon.utils.SecurityUtil;
-import com.rongwei.rwcommonentity.commonservers.domain.TenantDo;
-import com.rongwei.wechat.system.config.ContextHolder;
 import com.rongwei.wechat.system.config.WeChatAboutApiPara;
 import com.rongwei.wechat.system.dao.CommonBusinessDao;
 import com.rongwei.wechat.system.service.impl.SwBillManagementUnpaidServiceImpl;
@@ -44,7 +42,6 @@ import java.math.BigDecimal;
 import java.math.RoundingMode;
 import java.time.LocalDateTime;
 import java.util.*;
-import java.util.stream.Collectors;
 
 import static com.rongwei.wechat.system.utils.SaveConstans.PayType.*;
 
@@ -203,13 +200,13 @@ public class PayMentServiceImpl implements PayMentService {
         // }
         R error = R.error("失败");
         // dsKeys.remove("incontrol");
-        httpServletRequest.setAttribute("schema",dskey);
+        httpServletRequest.setAttribute("schema", dskey);
         // 获取所有商户的密钥
         List<SwEnterpriseConfigInfoDo> secretKeyList = commonBusinessDao.getSecretKey(Collections.singletonList(dskey));
         String analysisStr = null;
         // String deKey = null;
-        if(secretKeyList.isEmpty()) {
-            log.error("无法根据:{}找到到对应的商户信息",dskey);
+        if (secretKeyList.isEmpty()) {
+            log.error("无法根据:{}找到到对应的商户信息", dskey);
             return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error.toString());
         }
         for (SwEnterpriseConfigInfoDo swEnterpriseConfigInfoDo : secretKeyList) {
@@ -252,6 +249,7 @@ public class PayMentServiceImpl implements PayMentService {
         SwPaymentInitRecordDo paymentInitRecordDo = swPaymentInitRecordService.getOne(new LambdaQueryWrapper<SwPaymentInitRecordDo>()
                 .eq(BaseDo::getDeleted, "0")
                 .eq(SwPaymentInitRecordDo::getOrderno, outTradeNo), false);
+        String state = SUCCESS;
         try {
             // 微信订单号
             String transactionId = transaction.getTransactionId();
@@ -265,18 +263,11 @@ public class PayMentServiceImpl implements PayMentService {
             createPaymentRecordVo.setOrderNo(outTradeNo);
             createPaymentRecordVo.setPaymentAmount(amount);
 
-            log.info("根据商户订单号:{}获取到的下单信息为:{}",outTradeNo,paymentInitRecordDo);
-            String zdIds = null;
+            log.info("根据商户订单号:{}获取到的下单信息为:{}", outTradeNo, paymentInitRecordDo);
+            String zdIds="";
             if (paymentInitRecordDo != null) {
                 zdIds = StringUtils.isBlank(paymentInitRecordDo.getBillid()) ? null : paymentInitRecordDo.getBillid();
-                swPaymentInitRecordService.update(new LambdaUpdateWrapper<SwPaymentInitRecordDo>()
-                        .eq(SwPaymentInitRecordDo::getId, paymentInitRecordDo.getId())
-                        .set(SwPaymentInitRecordDo::getState, SUCCESS)
-                        .set(SwPaymentInitRecordDo::getCallbackcontent, transaction.toString())
-                        .set(BaseDo::getModifydate, new Date())
-                );
             }
-
             createPaymentRecordVo.setZdId(zdIds);
             createPaymentRecordVo.setUserNumber(userNum);
             createPaymentRecordVo.setOpenId(transaction.getPayer().getOpenid());
@@ -285,18 +276,20 @@ public class PayMentServiceImpl implements PayMentService {
             createPaymentRecordVo.setSuccessTime(successTime);
             log.error("开始创建缴费记录:{}", createPaymentRecordVo);
             paymentRecordService.createRecord(createPaymentRecordVo);
+            state = SUCCESS;
         } catch (Exception e) {
+            state = FAIL;
+            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error.toString());
+        } finally {
             if (paymentInitRecordDo != null) {
                 swPaymentInitRecordService.update(new LambdaUpdateWrapper<SwPaymentInitRecordDo>()
                         .eq(SwPaymentInitRecordDo::getId, paymentInitRecordDo.getId())
-                        .set(SwPaymentInitRecordDo::getState, FAIL)
+                        .set(SwPaymentInitRecordDo::getState, state)
                         .set(SwPaymentInitRecordDo::getCallbackcontent, transaction.toString())
                         .set(BaseDo::getModifydate, new Date())
                 );
             }
-            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(error.toString());
-        } finally {
-            ContextHolder.clear();
+            // ContextHolder.clear();
         }
         return ResponseEntity.status(HttpStatus.OK).body("");
     }

+ 4 - 3
zhsw-wechat-common/src/main/java/com/rongwei/wechat/system/wechat/impl/PaymentRecordServiceImpl.java

@@ -140,7 +140,8 @@ public class PaymentRecordServiceImpl implements PaymentRecordService {
             SwUserManagementDo swUserManagementDo = swUserManagementService.getOne(new LambdaQueryWrapper<SwUserManagementDo>()
                     .eq(SwUserManagementDo::getUsernumber, userNUMBER)
                     .eq(BaseDo::getDeleted, "0"));
-            if(swUserManagementDo==null){
+            if (swUserManagementDo == null) {
+                log.error("无法根据户号:{}获取到用户信息", userNUMBER);
                 throw new Exception("无法获取到用户信息");
             }
             List<SwBillManagementUnpaidDo> unpaidDoList = new ArrayList<>();
@@ -159,8 +160,8 @@ public class PaymentRecordServiceImpl implements PaymentRecordService {
             dataUpdate(swBillingRecordDo, swUserManagementDo, bills, unpaidDoList);
         } catch (Exception e) {
             log.error("缴费记录生成失败原因为 户号:{},账单:{},金额:{} - 原因: {} | 异常类型: {}", createPaymentRecordVo.getUserNumber(), createPaymentRecordVo.getZdId(), createPaymentRecordVo.getPaymentAmount(), e.getMessage(), e.getClass().getSimpleName(), e);
+            throw new CustomException("缴费记录生成异常");
         }
-
         return R.ok();
     }
 
@@ -172,7 +173,7 @@ public class PaymentRecordServiceImpl implements PaymentRecordService {
                            List<SwBillManagementUnpaidDo> unpaidDoList) {
         BigDecimal balanceChange = swBillingRecordDo.getAfterpaymentbalance().subtract(swUserManagementDo.getAccountbalance());
         log.debug("开始更新当前用户的余额信息!用户:{}的原余额为:{},缴费金额为:{},实际应缴为:{},金额变化:{}", swUserManagementDo.getUsernumber(),
-                swUserManagementDo.getAccountbalance(),swBillingRecordDo.getPaidin(),swBillingRecordDo.getActualdue(),balanceChange);
+                swUserManagementDo.getAccountbalance(), swBillingRecordDo.getPaidin(), swBillingRecordDo.getActualdue(), balanceChange);
         swUserManagementService.balanceAdd(swUserManagementDo.getId(), balanceChange);
         swBillingRecordService.save(swBillingRecordDo);
         if (!bills.isEmpty()) {