소스 검색

优化代码

java110 5 년 전
부모
커밋
3986351eeb

+ 1 - 0
java110-bean/src/main/java/com/java110/dto/fee/FeeDto.java

@@ -25,6 +25,7 @@ public class FeeDto extends PageDto implements Serializable {
     public static final String PAYER_OBJ_TYPE_RENTING = "9999";//房源ID
 
     public static final String FEE_FLAG_ONCE = "2006012";
+    public static final String REDIS_PAY_OWE_FEE = "PAY_OWE_FEE_";
 
     private String amount;
     private String incomeObjId;

+ 3 - 0
java110-utils/src/main/java/com/java110/utils/constant/ServiceCodeConstant.java

@@ -583,6 +583,9 @@ public class ServiceCodeConstant {
     //租赁服务费
     public static final String SERVICE_CODE_RENTING_PAY_CONFIRM_PRE = "fee.rentingPayFeeConfirm";
 
+    //欠费
+    public static final String SERVICE_CODE_OWE_FEE_PAY_CONFIRM_PRE = "fee.oweFeePayFeeConfirm";
+
 
     //查询停车位
     public static final String SERVICE_CODE_QUERY_PARKING_SPACE = "parkingSpace.queryParkingSpaces";

+ 43 - 0
service-front/src/main/java/com/java110/front/controller/PaymentController.java

@@ -28,6 +28,9 @@ public class PaymentController extends BaseController {
     @Autowired
     private IToPaySMO toPaySMOImpl;
 
+    @Autowired
+    private IToPayOweFeeSMO toPayOweFeeSMOImpl;
+
     @Autowired
     private IRentingToPaySMO rentingToPaySMOImpl;
 
@@ -63,6 +66,27 @@ public class PaymentController extends BaseController {
         return toPaySMOImpl.toPay(newPd);
     }
 
+    /**
+     * <p>统一下单入口</p>
+     *
+     * @param request
+     * @throws Exception
+     */
+    @RequestMapping(path = "/toOweFeePay", method = RequestMethod.POST)
+    public ResponseEntity<String> toOweFeePay(@RequestBody String postInfo, HttpServletRequest request) {
+        IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
+        /*IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);*/
+        String appId = request.getHeader("APP_ID");
+        if (StringUtil.isEmpty(appId)) {
+            appId = request.getHeader("APP-ID");
+        }
+
+        IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
+                "", "", "", pd.getSessionId(),
+                appId);
+        return toPayOweFeeSMOImpl.toPay(newPd);
+    }
+
     /**
      * <p>统一下单入口</p>
      *
@@ -120,6 +144,25 @@ public class PaymentController extends BaseController {
         return rentingToPaySMOImpl.toPay(newPd);
     }
 
+    /**
+     * <p>出租统一下单入口</p>
+     *
+     * @param request
+     * @throws Exception
+     */
+    @RequestMapping(path = "/oweFeeToPay", method = RequestMethod.POST)
+    public ResponseEntity<String> oweFeeToPay(@RequestBody String postInfo, HttpServletRequest request) {
+        IPageData pd = (IPageData) request.getAttribute(CommonConstant.CONTEXT_PAGE_DATA);
+        String appId = request.getHeader("APP_ID");
+        if (StringUtil.isEmpty(appId)) {
+            appId = request.getHeader("APP-ID");
+        }
+
+        IPageData newPd = PageData.newInstance().builder(pd.getUserId(), pd.getUserName(), pd.getToken(), postInfo,
+                "", "", "", pd.getSessionId(),
+                appId);
+        return rentingToPaySMOImpl.toPay(newPd);
+    }
     /**
      * <p>支付回调Api</p>
      *

+ 9 - 0
service-front/src/main/java/com/java110/front/properties/WechatAuthProperties.java

@@ -29,6 +29,7 @@ public class WechatAuthProperties {
     private String wxPayUnifiedOrder;
     private String wxNotifyUrl;
     private String rentingNotifyUrl;
+    private String oweFeeNotifyUrl;
 
     private String wechatAppId;//微信公众号ID
     private String wechatAppSecret;//微信公众号秘钥
@@ -130,4 +131,12 @@ public class WechatAuthProperties {
     public void setRentingNotifyUrl(String rentingNotifyUrl) {
         this.rentingNotifyUrl = rentingNotifyUrl;
     }
+
+    public String getOweFeeNotifyUrl() {
+        return oweFeeNotifyUrl;
+    }
+
+    public void setOweFeeNotifyUrl(String oweFeeNotifyUrl) {
+        this.oweFeeNotifyUrl = oweFeeNotifyUrl;
+    }
 }

+ 18 - 0
service-front/src/main/java/com/java110/front/smo/payment/IOweFeeToNotifySMO.java

@@ -0,0 +1,18 @@
+package com.java110.front.smo.payment;
+
+import org.springframework.http.ResponseEntity;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 房屋租赁微信通知 支付完成
+ */
+public interface IOweFeeToNotifySMO {
+
+    /**
+     * 支付完成
+     * @param request
+     * @return
+     */
+    public ResponseEntity<String> toNotify(String param,HttpServletRequest request);
+}

+ 17 - 0
service-front/src/main/java/com/java110/front/smo/payment/IToPayOweFeeSMO.java

@@ -0,0 +1,17 @@
+package com.java110.front.smo.payment;
+
+import com.java110.core.context.IPageData;
+import org.springframework.http.ResponseEntity;
+
+/**
+ * 统一下单接口类
+ */
+public interface IToPayOweFeeSMO {
+
+    /**
+     * 下单
+     * @param pd
+     * @return
+     */
+    public ResponseEntity<String> toPay(IPageData pd);
+}

+ 181 - 0
service-front/src/main/java/com/java110/front/smo/payment/impl/OweFeeToNotifySMOImpl.java

@@ -0,0 +1,181 @@
+package com.java110.front.smo.payment.impl;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.java110.core.factory.WechatFactory;
+import com.java110.dto.fee.FeeDto;
+import com.java110.dto.rentingPool.RentingPoolDto;
+import com.java110.dto.smallWeChat.SmallWeChatDto;
+import com.java110.front.properties.WechatAuthProperties;
+import com.java110.front.smo.AppAbstractComponentSMO;
+import com.java110.front.smo.payment.IOweFeeToNotifySMO;
+import com.java110.front.smo.payment.IRentingToNotifySMO;
+import com.java110.utils.cache.CommonCache;
+import com.java110.utils.constant.CommonConstant;
+import com.java110.utils.constant.ServiceConstant;
+import com.java110.utils.util.BeanConvertUtil;
+import com.java110.utils.util.DateUtil;
+import com.java110.utils.util.PayUtil;
+import com.java110.utils.util.StringUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.*;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.HttpStatusCodeException;
+import org.springframework.web.client.RestTemplate;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.*;
+
+@Service("oweFeeToNotifySMOImpl")
+public class OweFeeToNotifySMOImpl implements IOweFeeToNotifySMO {
+    private static final Logger logger = LoggerFactory.getLogger(OweFeeToNotifySMOImpl.class);
+
+    private static final String APP_ID = "992020011134400001";
+
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Autowired
+    private WechatAuthProperties wechatAuthProperties;
+
+    @Override
+    public ResponseEntity<String> toNotify(String param, HttpServletRequest request) {
+        String resXml = "";
+        ResponseEntity responseEntity = null;
+        try {
+
+            Map<String, Object> map = PayUtil.getMapFromXML(param);
+            logger.info("【小程序支付回调】 回调数据: \n" + map);
+            String returnCode = (String) map.get("return_code");
+            if ("SUCCESS".equalsIgnoreCase(returnCode)) {
+                String returnmsg = (String) map.get("result_code");
+                if ("SUCCESS".equals(returnmsg)) {
+                    //更新数据
+                    int result = confirmPayFee(map);
+                    if (result > 0) {
+                        //支付成功
+                        resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>"
+                                + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml>";
+                    }
+                } else {
+                    resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
+                            + "<return_msg><![CDATA[报文为空]></return_msg>" + "</xml>";
+                    logger.info("支付失败:" + resXml);
+                }
+            } else {
+                resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
+                        + "<return_msg><![CDATA[报文为空]></return_msg>" + "</xml>";
+                logger.info("【订单支付失败】");
+            }
+        } catch (Exception e) {
+            logger.error("通知失败", e);
+            resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>"
+                    + "<return_msg><![CDATA[鉴权失败]></return_msg>" + "</xml>";
+        }
+
+        logger.info("【小程序支付回调响应】 响应内容:\n" + resXml);
+        return new ResponseEntity<String>(resXml, HttpStatus.OK);
+    }
+
+    public int confirmPayFee(Map<String, Object> map) {
+        String wId = map.get("wId").toString();
+        wId = wId.replace(" ", "+");
+        SortedMap<String, String> paramMap = new TreeMap<String, String>();
+        ResponseEntity<String> responseEntity = null;
+        for (String key : map.keySet()) {
+            if ("wId".equals(key)) {
+                continue;
+            }
+            paramMap.put(key, map.get(key).toString());
+        }
+        String appId = WechatFactory.getAppId(wId);
+        SmallWeChatDto smallWeChatDto = getSmallWechat(appId);
+
+        if (smallWeChatDto == null) { //从配置文件中获取 小程序配置信息
+            smallWeChatDto = new SmallWeChatDto();
+            smallWeChatDto.setAppId(wechatAuthProperties.getAppId());
+            smallWeChatDto.setAppSecret(wechatAuthProperties.getSecret());
+            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+        }
+        String sign = PayUtil.createSign(paramMap, smallWeChatDto.getPayPassword());
+
+        if (!sign.equals(map.get("sign"))) {
+            throw new IllegalArgumentException("鉴权失败");
+        }
+
+        String orderId = map.get("out_trade_no").toString();
+        String order = CommonCache.getAndRemoveValue(FeeDto.REDIS_PAY_OWE_FEE + orderId);
+
+        if (StringUtil.isEmpty(order)) {
+            return 1;// 说明已经处理过了 再不处理
+        }
+
+        //查询用户ID
+        JSONObject paramIn = JSONObject.parseObject(order);
+        paramIn.put("oId", orderId);
+        String url = ServiceConstant.SERVICE_API_URL + "/api/feeApi/payOweFee";
+        responseEntity = this.callCenterService(restTemplate, "-1", paramIn.toJSONString(), url, HttpMethod.POST);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            return 0;
+        }
+        return 1;
+    }
+
+
+    /**
+     * 调用中心服务
+     *
+     * @return
+     */
+    protected ResponseEntity<String> callCenterService(RestTemplate restTemplate, String userId, String param, String url, HttpMethod httpMethod) {
+
+        ResponseEntity<String> responseEntity = null;
+        HttpHeaders header = new HttpHeaders();
+        header.add(CommonConstant.HTTP_APP_ID.toLowerCase(), APP_ID);
+        header.add(CommonConstant.HTTP_USER_ID.toLowerCase(), userId);
+        header.add(CommonConstant.HTTP_TRANSACTION_ID.toLowerCase(), UUID.randomUUID().toString());
+        header.add(CommonConstant.HTTP_REQ_TIME.toLowerCase(), DateUtil.getDefaultFormateTimeString(new Date()));
+        header.add(CommonConstant.HTTP_SIGN.toLowerCase(), "");
+        HttpEntity<String> httpEntity = new HttpEntity<String>(param, header);
+        //logger.debug("请求中心服务信息,{}", httpEntity);
+        try {
+            responseEntity = restTemplate.exchange(url, httpMethod, httpEntity, String.class);
+        } catch (HttpStatusCodeException e) { //这里spring 框架 在4XX 或 5XX 时抛出 HttpServerErrorException 异常,需要重新封装一下
+            responseEntity = new ResponseEntity<String>(e.getResponseBodyAsString(), e.getStatusCode());
+        } catch (Exception e) {
+            responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
+        } finally {
+            logger.debug("请求地址为,{} 请求中心服务信息,{},中心服务返回信息,{}", url, httpEntity, responseEntity);
+            return responseEntity;
+        }
+
+    }
+
+
+    private SmallWeChatDto getSmallWechat(String appId) {
+
+        ResponseEntity responseEntity = null;
+
+        responseEntity = this.callCenterService(restTemplate, "-1", "",
+                ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
+                        + appId + "&page=1&row=1", HttpMethod.GET);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            return null;
+        }
+        JSONObject smallWechatObj = JSONObject.parseObject(responseEntity.getBody().toString());
+        JSONArray smallWeChats = smallWechatObj.getJSONArray("smallWeChats");
+
+        if (smallWeChats == null || smallWeChats.size() < 1) {
+            return null;
+        }
+
+        return BeanConvertUtil.covertBean(smallWeChats.get(0), SmallWeChatDto.class);
+    }
+
+}

+ 171 - 0
service-front/src/main/java/com/java110/front/smo/payment/impl/ToPayOweFeeSMOImpl.java

@@ -0,0 +1,171 @@
+package com.java110.front.smo.payment.impl;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.java110.core.context.IPageData;
+import com.java110.core.context.PageData;
+import com.java110.core.factory.GenerateCodeFactory;
+import com.java110.dto.app.AppDto;
+import com.java110.dto.fee.FeeDto;
+import com.java110.dto.owner.OwnerAppUserDto;
+import com.java110.dto.rentingPool.RentingPoolDto;
+import com.java110.dto.smallWeChat.SmallWeChatDto;
+import com.java110.front.properties.WechatAuthProperties;
+import com.java110.front.smo.AppAbstractComponentSMO;
+import com.java110.front.smo.payment.IToPayOweFeeSMO;
+import com.java110.utils.cache.CommonCache;
+import com.java110.utils.constant.ServiceConstant;
+import com.java110.utils.util.Assert;
+import com.java110.utils.util.BeanConvertUtil;
+import com.java110.vo.ResultVo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Service;
+import org.springframework.web.client.RestTemplate;
+
+import java.math.BigDecimal;
+import java.util.HashMap;
+import java.util.Map;
+
+@Service("toPayOweFeeSMOImpl")
+public class ToPayOweFeeSMOImpl extends AppAbstractComponentSMO implements IToPayOweFeeSMO {
+    private static final Logger logger = LoggerFactory.getLogger(ToPayOweFeeSMOImpl.class);
+
+
+    @Autowired
+    private RestTemplate restTemplate;
+
+    @Autowired
+    private RestTemplate outRestTemplate;
+
+
+    @Autowired
+    private WechatAuthProperties wechatAuthProperties;
+
+    @Override
+    public ResponseEntity<String> toPay(IPageData pd) {
+        return super.businessProcess(pd);
+    }
+
+    @Override
+    protected void validate(IPageData pd, JSONObject paramIn) {
+
+        Assert.jsonObjectHaveKey(paramIn, "communityId", "请求报文中未包含communityId节点");
+        Assert.jsonObjectHaveKey(paramIn, "roomId", "请求报文中未包含房屋信息节点");
+        Assert.jsonObjectHaveKey(paramIn, "appId", "请求报文中未包含appId节点");
+
+    }
+
+    @Override
+    protected ResponseEntity<String> doBusinessProcess(IPageData pd, JSONObject paramIn) throws Exception {
+
+        ResponseEntity responseEntity = null;
+
+        SmallWeChatDto smallWeChatDto = getSmallWechat(pd, paramIn);
+
+        if (smallWeChatDto == null) { //从配置文件中获取 小程序配置信息
+            smallWeChatDto = new SmallWeChatDto();
+            smallWeChatDto.setAppId(wechatAuthProperties.getAppId());
+            smallWeChatDto.setAppSecret(wechatAuthProperties.getSecret());
+            smallWeChatDto.setMchId(wechatAuthProperties.getMchId());
+            smallWeChatDto.setPayPassword(wechatAuthProperties.getKey());
+        }
+
+        //查询用户ID
+        paramIn.put("userId", pd.getUserId());
+        String url = ServiceConstant.SERVICE_API_URL + "/api/feeApi/listOweFees?page=1&row=50&communityId=" + paramIn.getString("communityId") + "&payObjId=" + paramIn.getString("roomId") + "&payObjType=3333";
+        responseEntity = super.callCenterService(restTemplate, pd, "", url, HttpMethod.GET);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            return responseEntity;
+        }
+        JSONObject orderInfo = JSONObject.parseObject(responseEntity.getBody().toString());
+        JSONArray fees = orderInfo.getJSONArray("data");
+
+        if (fees == null || fees.size() < 1) {
+            return ResultVo.createResponseEntity(ResultVo.CODE_ERROR, "未包含欠费费用");
+        }
+        String orderId = GenerateCodeFactory.getOId();
+        double money = 0.0;
+        BigDecimal tmpMoney = new BigDecimal(money);
+        BigDecimal feePrice = null;
+        for (int feeIndex = 0; feeIndex < fees.size(); feeIndex++) {
+            feePrice = new BigDecimal(fees.getJSONObject(feeIndex).getDouble("feePrice"));
+            tmpMoney = tmpMoney.add(feePrice);
+        }
+        money = tmpMoney.setScale(2, BigDecimal.ROUND_HALF_EVEN).doubleValue();
+        String appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA;
+        if (AppDto.WECHAT_OWNER_APP_ID.equals(pd.getAppId())) {
+            appType = OwnerAppUserDto.APP_TYPE_WECHAT;
+        } else if (AppDto.WECHAT_MINA_OWNER_APP_ID.equals(pd.getAppId())) {
+            appType = OwnerAppUserDto.APP_TYPE_WECHAT_MINA;
+        } else {
+            appType = OwnerAppUserDto.APP_TYPE_APP;
+        }
+        Map tmpParamIn = new HashMap();
+        tmpParamIn.put("userId", pd.getUserId());
+        tmpParamIn.put("appType", appType);
+        responseEntity = super.getOwnerAppUser(pd, restTemplate, tmpParamIn);
+        logger.debug("查询用户信息返回报文:" + responseEntity);
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            throw new IllegalArgumentException("未查询用户信息异常" + tmpParamIn);
+        }
+
+        JSONObject userResult = JSONObject.parseObject(responseEntity.getBody().toString());
+        int total = userResult.getIntValue("total");
+        if (total < 1) {
+            //未查询到用户信息
+            throw new IllegalArgumentException("未查询微信用户");
+        }
+
+        JSONObject realUserInfo = userResult.getJSONArray("data").getJSONObject(0);
+
+        String openId = realUserInfo.getString("openId");
+
+        Map result = super.java110Payment(outRestTemplate, paramIn.getString("feeName"), paramIn.getString("tradeType"),
+                orderId, money, openId, smallWeChatDto,wechatAuthProperties.getOweFeeNotifyUrl());
+        responseEntity = new ResponseEntity(JSONObject.toJSONString(result), HttpStatus.OK);
+        if (!"0".equals(result.get("code"))) {
+            return responseEntity;
+        }
+        JSONObject saveFees = new JSONObject();
+        saveFees.put("orderId", orderId);
+        saveFees.put("money", money);
+        saveFees.put("roomId", paramIn.getString("roomId"));
+        saveFees.put("communityId", paramIn.getString("communityId"));
+        saveFees.put("fees", fees);
+        CommonCache.setValue(FeeDto.REDIS_PAY_OWE_FEE + orderId, saveFees.toJSONString(), CommonCache.PAY_DEFAULT_EXPIRE_TIME);
+        return responseEntity;
+    }
+
+
+    private SmallWeChatDto getSmallWechat(IPageData pd, JSONObject paramIn) {
+
+        ResponseEntity responseEntity = null;
+
+        pd = PageData.newInstance().builder(pd.getUserId(), "", "", pd.getReqData(),
+                "", "", "", "",
+                pd.getAppId());
+        responseEntity = this.callCenterService(restTemplate, pd, "",
+                ServiceConstant.SERVICE_API_URL + "/api/smallWeChat.listSmallWeChats?appId="
+                        + paramIn.getString("appId") + "&page=1&row=1", HttpMethod.GET);
+
+        if (responseEntity.getStatusCode() != HttpStatus.OK) {
+            return null;
+        }
+        JSONObject smallWechatObj = JSONObject.parseObject(responseEntity.getBody().toString());
+        JSONArray smallWeChats = smallWechatObj.getJSONArray("smallWeChats");
+
+        if (smallWeChats == null || smallWeChats.size() < 1) {
+            return null;
+        }
+
+        return BeanConvertUtil.covertBean(smallWeChats.get(0), SmallWeChatDto.class);
+    }
+
+
+}

+ 1 - 0
service-front/src/main/resources/wechatAuth.properties

@@ -7,6 +7,7 @@ java110.auth.wechat.mchId=mchId
 java110.auth.wechat.wxPayUnifiedOrder=https://api.mch.weixin.qq.com/pay/unifiedorder
 java110.auth.wechat.wxNotifyUrl=https://app.demo.winqi.cn/app/payment/notify
 java110.auth.wechat.rentingNotifyUrl=https://app.demo.winqi.cn/app/payment/rentingNotify
+java110.auth.wechat.oweFeeNotifyUrl=https://app.demo.winqi.cn/app/payment/oweFeeToPay
 
 # ¹«ÖÚºÅÐÅÏ¢
 java110.auth.wechat.wechatAppId=key