xiaoyong931011
2023-02-10 71f2eb9359fb4e4d9caaf4fd07bcc2b50e7674ac
src/main/java/cc/mrbird/febs/pay/util/WeixinServiceUtil.java
@@ -3,10 +3,11 @@
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.properties.XcxProperties;
import cc.mrbird.febs.common.utils.SpringContextHolder;
import cc.mrbird.febs.pay.model.BrandWCPayRequestData;
import cc.mrbird.febs.pay.model.JsApiPayBusiness;
import cc.mrbird.febs.pay.model.JsApiPayReqData;
import cc.mrbird.febs.pay.model.JsApiPayResData;
import cc.mrbird.febs.pay.model.*;
import cc.mrbird.febs.pay.service.impl.JsApiPayComService;
import cc.mrbird.febs.pay.service.impl.RefundService;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@@ -41,14 +42,19 @@
      return buildBrandWCPayRequestData(desc, outTradeNo, price, openId, attach, wecharPaynotifyUrl);
   }
   public BrandWCPayRequestData createRechargeWallet(String desc, String outTradeNo, int price, String openId, String attach) throws Exception {
      String wecharRechargePaynotifyUrl = xcxProperties.getWecharRechargePaynotifyUrl();
      return buildBrandWCPayRequestData(desc, outTradeNo, price, openId, attach, wecharRechargePaynotifyUrl);
   }
   private BrandWCPayRequestData buildBrandWCPayRequestData(String desc, String outTradeNo, int price, String openId, String attach, String notifyUrl) throws Exception {
      // 创建微信支付预付接口
      JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
      String idAddr = getIpAddr(WebUtil.getRequest());
      String mchID = WechatConfigure.WECHARPAY_MCHID;
      String paySecret = WechatConfigure.WECHARPAY_SECRET;
      String appId = WechatConfigure.MINIPROGRAM_APPID;
      String mchID = xcxProperties.getWecharpayMchid();
      String paySecret = xcxProperties.getWecharpaySecret();
      String appId = xcxProperties.getXcxAppid();
      JsApiPayReqData jsApiPayReqData = new JsApiPayReqData(
            appId, mchID, paySecret, notifyUrl, desc,
@@ -81,48 +87,93 @@
    * @param opUserID 操作员id,默认为商户号
    * @return
    */
//   public boolean comRefund(String outTradeNo, String outRefundNo,int totalFee,
//         int refundFee, String opUserID){
//      LogUtil.info("#---outTradeNo:{}#---outRefundNo:{}#---totalFee:{}#---refundFee:{}",
//            outTradeNo,outRefundNo,totalFee,refundFee);
//      BusParameterSettings mchID = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_MCHID, HostInterceptor.getCompanyId());
//      if(opUserID==null){
//         opUserID=mchID.getParamValue();
//      }
//      RefundResData result=null;
//      boolean flag=false;
//      try {
//         JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
//         Long companyId=HostInterceptor.getCompanyId();
//         BusParameterSettings paySecret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_SECRET, companyId);
//         BusParameterSettings appId = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_APPID, companyId);
//         BusParameterSettings certLocalPath = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_CERTLOCAL_PATH, companyId);
//
//         RefundReqData refundReqData=new RefundReqData(mchID.getParamValue(),appId.getParamValue(),paySecret.getParamValue(), outTradeNo, outRefundNo, totalFee, refundFee,opUserID);
//         RefundService refundService=new RefundService();
//         HttpsRequest2 request2= (HttpsRequest2) refundService.getServiceRequest();
//         request2.setCertLocalPath(certLocalPath.getParamValue());
//         request2.setMchId(mchID.getParamValue());
//
//         result = jsApiPayBusiness.refundComOrder(refundService, refundReqData);
//         LogUtil.info("#退款,企业付款到个人---result:{}",result);
//         //如果返回余额不足时
//         if (StringUtils.equals(AppConstance.REFUND_FAIL_NOTENOUGH, result.getErr_code())) {
//            LogUtil.info("#退款失败,{}!", "商户余额不足");
//            throw new GlobleException("退款失败,请联系管理员!");
//         }
//      } catch (Exception e) {
//         LogUtil.error("#企业付款到个人异常#退款#outTradeNo:{}#opUserID:{}", e, outTradeNo,opUserID);
//      }
//
//      if (result.getResult_code().equals(WechatConfigure.SUCCESS)) {
//          flag=true;
//      }else{
//          flag=false;
//          throw new GlobleException(result.getErr_code_des());
//      }
//      return flag;
//   }
   public boolean comRefund(String outTradeNo, String outRefundNo,int totalFee,
         int refundFee, String opUserID){
      log.info("#---outTradeNo:{}#---outRefundNo:{}#---totalFee:{}#---refundFee:{}",
            outTradeNo,outRefundNo,totalFee,refundFee);
      String mchID = xcxProperties.getWecharpayMchid();
      if(opUserID==null){
         opUserID=mchID;
      }
      RefundResData result=null;
      boolean flag=false;
      try {
         JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
         String paySecret = xcxProperties.getWecharpaySecret();
         String appId = xcxProperties.getXcxAppid();
         String certLocalPath = xcxProperties.getCertLocalPath();
         RefundReqData refundReqData=new RefundReqData(mchID,appId,paySecret, outTradeNo, outRefundNo, totalFee, refundFee,opUserID);
         RefundService refundService=new RefundService();
         HttpsRequest2 request2= (HttpsRequest2) refundService.getServiceRequest();
         request2.setCertLocalPath(certLocalPath);
         request2.setMchId(mchID);
         result = jsApiPayBusiness.refundComOrder(refundService, refundReqData);
         log.info("#退款,企业付款到个人---result:{}",result);
         //如果返回余额不足时,商户余额不足时微信返回状态码(退款失败时)
         if (StrUtil.equals("NOTENOUGH", result.getErr_code())) {
            log.info("#退款失败,{}!", "商户余额不足");
            flag=false;
         }
      } catch (Exception e) {
         log.error("#企业付款到个人异常#退款#outTradeNo:{}#opUserID:{}", e, outTradeNo,opUserID);
         flag=false;
      }
      if (result.getResult_code().equals("SUCCESS")) {
          flag=true;
      }else{
          flag=false;
      }
      return flag;
   }
   /**@Description 用户提现提现审核,企业付款到个人
    @date 2017年5月26日
    @atuhor jiangyouyao
     * @param desc 描述
    * @param outTradeNo  提现申请编号
    * @param totalFee 提现金额(分)
    * @param openid
    * @return
    */
   public boolean comPay(String desc, String outTradeNo,int totalFee, String openid){
      log.info("后台同意提现申请..."+ desc+outTradeNo+totalFee+openid);
      boolean flag=false;
      try {
         JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
         String paySecret = xcxProperties.getWecharpaySecret();
         String appId = xcxProperties.getXcxAppid();
         String certLocalPath = xcxProperties.getCertLocalPath();
         String mchID = xcxProperties.getWecharpayMchid();
         JsApiPayComReqData jsApiPayComReqData=new JsApiPayComReqData(mchID,
               appId,
               paySecret,
               desc,outTradeNo,totalFee,openid,"NO_CHECK","");
         JsApiPayComService jsApiPayComService=new JsApiPayComService();
         HttpsRequest2 request2= (HttpsRequest2) jsApiPayComService.getServiceRequest();
         request2.setCertLocalPath(certLocalPath);
         request2.setMchId(mchID);
         JsApiPayComResData result = jsApiPayBusiness.payComOrder(jsApiPayComService, jsApiPayComReqData);
         log.info("#提现,企业付款到个人---result:{}",result);
         if (result.getResult_code().equals("SUCCESS")) {
            flag=true;
         }else{
            log.info("#提现,企业付款到个人失败,{}!", "商户余额不足");
            flag=false;
         }
      } catch (Exception e) {
         log.error("#企业付款到个人异常#提现#outTradeNo:{}#opUserID:{}", e, outTradeNo,openid);
         flag=false;
      }
      return flag;
   }
   
   /**@Description 支付获取远程设备的ip
      @date 2017年6月27日