Helius
2021-03-17 9974b1fffff1cad712b7c30fb4c708ef45ec4b8c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.matrix.component.wechat.externalInterface.weixinUtil;
 
import com.matrix.component.wechat.externalInterface.common.HttpsRequest2;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.component.wechat.externalInterface.business.JsApiPayBusiness;
import com.matrix.component.wechat.externalInterface.common.WechatConfigure;
import com.matrix.component.wechat.externalInterface.protocol.paramProtocol.BrandWCPayRequestData;
import com.matrix.component.wechat.externalInterface.protocol.payComProtocol.JsApiPayComReqData;
import com.matrix.component.wechat.externalInterface.protocol.payComProtocol.JsApiPayComResData;
import com.matrix.component.wechat.externalInterface.protocol.payProtocol.JsApiPayReqData;
import com.matrix.component.wechat.externalInterface.protocol.payProtocol.JsApiPayResData;
import com.matrix.component.wechat.externalInterface.protocol.queryProtocol.ScanPayQueryReqData;
import com.matrix.component.wechat.externalInterface.protocol.queryProtocol.ScanPayQueryResData;
import com.matrix.component.wechat.externalInterface.protocol.refundProtocol.RefundReqData;
import com.matrix.component.wechat.externalInterface.protocol.refundProtocol.RefundResData;
import com.matrix.component.wechat.externalInterface.service.JsApiPayComService;
import com.matrix.component.wechat.externalInterface.service.RefundService;
import com.matrix.component.wechat.externalInterface.service.ScanPayQueryService;
import com.matrix.system.common.bean.BusParameterSettings;
import com.matrix.system.common.constance.AppConstance;
import com.matrix.system.common.dao.BusParameterSettingsDao;
import com.matrix.system.common.interceptor.HostInterceptor;
import com.matrix.system.shopXcx.api.tools.WxShopOrderUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
 
/**微信工具类
 * @author jiangyouyao
 *
 */
@Service(value="weixinServiceUtil")
public class WeixinServiceUtil {
    static Logger logger=Logger.getLogger(WeixinServiceUtil.class);
 
    /**
     * 是否为debug模式
     */
    @Value("${debug}")
    private String debug;
 
    @Autowired
    private WxShopOrderUtil wxShopOrderUtil;
 
    @Autowired
    BusParameterSettingsDao busParameterSettingsDao;
 
 
 
 
    
    /**
     * 支付"175.9.82.254"
     * WebUtil.getRequest().getRemoteAddr()
     * getRemortIP(WebUtil.getRequest())
     * @Description 公众号支付,生成预支付订单
       @date 2017年6月27日
       @atuhor jiangyouyao
     * @param desc 描述信息
     * @param outTradeNo 订单id
     * @param price 订单价格,类型为整型,单位是分,不是元,等于你的实际价格*100
     * @param openId  支付者的微信openId
     * @return 预支付订单返回的结果对象(该结果对象已封装),在H5页面使用该对象信息
     * @throws Exception
     */
    public BrandWCPayRequestData createOrder(String desc,String outTradeNo, int price, String openId,String attach) throws Exception {
        // 创建微信支付预付接口
        JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
        String idAddr = getIpAddr(WebUtil.getRequest());
 
        Long companyId=HostInterceptor.getCompanyId();
 
        BusParameterSettings mchID = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_MCHID, companyId);
        BusParameterSettings paySecret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_SECRET, companyId);
        BusParameterSettings notifyUrl = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_NOTIFYURL, companyId);
 
        BusParameterSettings appId = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_APPID, companyId);
 
        JsApiPayReqData jsApiPayReqData = new JsApiPayReqData(appId.getParamValue(),mchID.getParamValue(),
                paySecret.getParamValue(),
                notifyUrl.getParamValue(),
                desc, outTradeNo, price, idAddr, openId, attach);
 
        // 创建预付订单并返回请求结果
        JsApiPayResData result = jsApiPayBusiness.createPrapareOrder(jsApiPayReqData);
        // 把预付订单的信息存放在request域中
        WebUtil.getRequest().setAttribute("msg", result.toString());
        LogUtil.info("#---返回码:return_code = {}" , result.getReturn_code());
        LogUtil.info("#---签名信息:return_msg = {}" , result.getReturn_msg());
        if (result.getReturn_code().equals("SUCCESS")) {
            // 请求成功, 构建BrandWCPayRequest发起支付需要的参数
            BrandWCPayRequestData payData = new BrandWCPayRequestData(result.getPrepay_id(),appId.getParamValue(),paySecret.getParamValue());
            WebUtil.getRequest().setAttribute("payData", payData);
            return payData;
        }else{
            LogUtil.error("创建微信支付订单失败msg={}",result.getReturn_msg());
            throw new GlobleException("创建微信支付订单失败,请检查程序配置");
        }
 
    }
    
    
    /**@Description 支付后,向微信发送请求、查询订单,看订单是否真的支付成功了
       @date 2017年6月27日
       @atuhor jiangyouyao
     * @param transactionID 微信生成的订单id transactionID和trUuid任意一个即可,另外一个可为空,若两个参数都不为空则transactionID优先
     * @param trUuid 系统(项目中)产生的原订单id
     * @param orderPrice 原订单的价格
     * @return
     */
    public boolean isPaySuccess(String transactionID,String trUuid){
            boolean isSucess=false;
            try {
                JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
                ScanPayQueryReqData scanPayQueryReqData = new ScanPayQueryReqData(transactionID, trUuid);
                ScanPayQueryService scanPayQueryService = new ScanPayQueryService();
                ScanPayQueryResData result = jsApiPayBusiness.queryPrapareOrder(scanPayQueryService, scanPayQueryReqData);
                WebUtil.getRequest().setAttribute("msg", result.toString());
                //订单金额相等
                //isSucess=result.getTotal_fee().equals(orderPrice.multiply(new BigDecimal("100")).doubleValue()+"");
                
                if (result.getReturn_code().equals("SUCCESS")) {
                    isSucess=true;
                }
            }  catch (Exception e) {
                LogUtil.error("#查询订单异常#transactionID:{}#trUuid:{}", e, transactionID,trUuid);
            }
            return isSucess;
    }
    /**@Description 用户提现提现审核,企业付款到个人
       @date 2017年5月26日
       @atuhor jiangyouyao
     * @param desc 描述
     * @param outTradeNo  提现申请编号
     * @param totalFee 提现金额(分)
     * @param openid
     * @return
     */
    public JsApiPayComResData comPay(String desc, String outTradeNo,int totalFee, String openid,Long companyId){
        JsApiPayComResData result=null;
        try {
            JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
            // TODO 企业付款
 
            BusParameterSettings mchID = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_MCHID, companyId);
            BusParameterSettings appId = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.MINIPROGRAM_APPID, companyId);
            BusParameterSettings paySecret = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_SECRET, companyId);
 
            BusParameterSettings certLocalPath = busParameterSettingsDao.selectCompanyParamByCode(AppConstance.WECHARPAY_CERTLOCAL_PATH, companyId);
 
            JsApiPayComReqData jsApiPayComReqData=new JsApiPayComReqData(mchID.getParamValue(),
                    appId.getParamValue(),
                    paySecret.getParamValue(),
                    desc,outTradeNo,totalFee,openid,"NO_CHECK","");
            JsApiPayComService jsApiPayComService=new JsApiPayComService();
 
            HttpsRequest2 request2= (HttpsRequest2) jsApiPayComService.getServiceRequest();
            request2.setCertLocalPath(certLocalPath.getParamValue());
            request2.setMchId(mchID.getParamValue());
 
 
            result = jsApiPayBusiness.payComOrder(jsApiPayComService, jsApiPayComReqData);
 
            LogUtil.info("#提现,企业付款到个人---result:{}",result);
            if (result.getResult_code().equals("SUCCESS")) {
                return result;
            }else{
                 throw new GlobleException(result.getErr_code_des());
            }
        } catch (Exception e) {
            LogUtil.error("#企业付款到个人异常#提现#outTradeNo:{}#opUserID:{}", e, outTradeNo,openid);
        }
        return null;
    }
    
    
    /**@Description 退款,企业付款到个人
       @date 2017年5月26日
       @atuhor jiangyouyao
     * @param outTradeNo 商户订单编号(原订单编号)
     * @param outRefundNo 退款编号
     * @param totalFee 原订单金额(分)
     * @param refundFee 退款金额(分)
     * @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;
    }
    
    /**@Description 支付获取远程设备的ip
       @date 2017年6月27日
       @atuhor jiangyouyao
     * @param request
     * @return
     */
    public String getIpAddr(HttpServletRequest request) { 
           String ip = request.getHeader("x-forwarded-for");
          /* if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
               ip = request.getHeader("Proxy-Client-IP");
           }
           if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
               ip = request.getHeader("WL-Proxy-Client-IP");
           } */
           if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 
               ip = request.getRemoteAddr(); 
           } 
           if(ip!=null){
            ip=ip.split(",")[0];
           }
           return ip; 
       } 
    
    
}