Helius
2021-12-01 c402154474b5bf897ec9c59155fb4d85e5871ef4
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package com.xzx.gc.pay.service;
 
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.service.WxPayService;
import com.xzx.gc.common.Result;
import com.xzx.gc.common.constant.Constants;
import com.xzx.gc.common.constant.PayEnum;
import com.xzx.gc.common.dto.log.PayInfoLog;
import com.xzx.gc.common.exception.wx.WxPayNotifyResponseException;
import com.xzx.gc.common.utils.BusinessUtil;
import com.xzx.gc.common.utils.ExceptionUtils;
import com.xzx.gc.common.utils.RedisUtil;
import com.xzx.gc.common.utils.wxpay.WXPayUtil;
import com.xzx.gc.entity.AccountInfo;
import com.xzx.gc.entity.PayInfo;
import com.xzx.gc.entity.PayWxRecord;
import com.xzx.gc.pay.dto.PayParamDTO;
import com.xzx.gc.pay.dto.PayResultDTO;
import com.xzx.gc.pay.mapper.PayInfoMapper;
import com.xzx.gc.pay.mapper.PayWxRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
 
@Service
@Transactional
@Slf4j
public class WeiXinService {
    @Autowired
    private WxPayService wxPayService;
 
 
    @Autowired
    private PayService payService;
 
    @Autowired
    private PayInfoMapper payInfoMapper;
 
    @Autowired
    private HttpServletRequest request;
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private PayWxRecordMapper payWxRecordMapper;
 
    @Autowired
    private PayInfoLogService payInfoLogService;
 
    @Autowired
    private BusinessUtil businessUtil;
 
    @Autowired
    private AccountService accountService;
 
    /**
     * 生成小程序支付签名
     * @param payResultDTO
     * @return
     * @throws Exception
     */
    private String geWebSign(PayResultDTO payResultDTO) throws Exception {
        Map<String, String> packageParams = new HashMap<String, String>();
        packageParams.put("appId", wxPayService.getConfig().getAppId());
        packageParams.put("timeStamp", payResultDTO.getTimeStamp());
        packageParams.put("nonceStr", payResultDTO.getNonce());
        packageParams.put("package", payResultDTO.getPrepayId());
        packageParams.put("signType", "MD5");
        String packageSign = WXPayUtil.generateSignature(packageParams, wxPayService.getConfig().getMchKey());
        return  packageSign;
    }
 
    /**
     * 生成安卓APP签名
     * @param payResultDTO
     * @return
     * @throws Exception
     */
    private String geAnroidSign(PayResultDTO payResultDTO) throws Exception {
        Map<String, String> packageParams = new HashMap<String, String>();
        packageParams.put("appid", wxPayService.getConfig().getAppId());
        packageParams.put("partnerid", wxPayService.getConfig().getMchId());
        packageParams.put("prepayid", payResultDTO.getPrepayId());
        packageParams.put("package", payResultDTO.getExtra());
        packageParams.put("noncestr", payResultDTO.getNonce());
        packageParams.put("timestamp", payResultDTO.getTimeStamp());
        String packageSign = WXPayUtil.generateSignature(packageParams, wxPayService.getConfig().getMchKey());
        return  packageSign;
    }
 
    /**
     * 微信预支付
     *
     * @param wxPayUnifiedOrderRequest
     * @param payParamDTO
     * @return
     */
    public Result<PayResultDTO> pay(WxPayUnifiedOrderRequest wxPayUnifiedOrderRequest, PayParamDTO payParamDTO) {
        Result<PayResultDTO> res=new Result<>();
        PayResultDTO payResultDTO = new PayResultDTO();
        try {
            String userId = request.getHeader("userId");
 
            WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(wxPayUnifiedOrderRequest);
            PayInfo payInfo = new PayInfo();
            payInfo.setOrderId(result.getPrepayId());
            payInfo.setCreateUserId(userId);
 
            if(businessUtil.isApp(request.getHeader("clientType"))){
                //需要服务费
                payInfo.setMoney(businessUtil.changeMoney(payParamDTO.getMoney()));
                payInfo.setFee(businessUtil.changeMoney(payParamDTO.getFee()));
            }else {
                payInfo.setMoney(payParamDTO.getTotalFee().toString());
            }
 
            AccountInfo accountInfo1 =accountService.findByUserId(userId);
 
            if (accountInfo1 == null) {
                return Result.error(-1, "账户不存在");
            }
 
            payInfo.setAccountId(accountInfo1.getAccountId());
            payInfo.setPayType(PayEnum.待支付.getValue());
            payInfo.setOpenId(payParamDTO.getOpenid());
            payInfo.setStatus(PayEnum.充值.getValue());
            payInfo.setPayOrderId(payParamDTO.getOutTradeNo());
            payService.addPay(payInfo);
 
            PayInfoLog payInfoLog=new PayInfoLog();
            BeanUtil.copyProperties(payInfo,payInfoLog);
            payInfoLog.setAccount(accountInfo1.getAccountName());
            payInfoLog.setAccountClass(PayEnum.微信.getValue());
            payInfoLog.setPayType(PayEnum.充值交易.getValue());
            payInfoLogService.add(payInfoLog);
 
            payResultDTO.setNonce(payParamDTO.getNonceStr());
            payResultDTO.setTimeStamp(Convert.toStr(DateUtil.currentSeconds()));
            payResultDTO.setMchId(wxPayService.getConfig().getMchId());
 
            if(businessUtil.isWeb(payParamDTO.getClientType())){
                payResultDTO.setPrepayId("prepay_id="+result.getPrepayId());
                payResultDTO.setSign(geWebSign(payResultDTO));
            }else if(businessUtil.isApp(payParamDTO.getClientType())){
                payResultDTO.setPrepayId(result.getPrepayId());
                payResultDTO.setExtra("Sign=WXPay");
                payResultDTO.setSign(geAnroidSign(payResultDTO));
            }
            payResultDTO.setAppId(wxPayService.getConfig().getAppId());
            payResultDTO.setPayOrderId(payInfo.getPayOrderId());
            res.setData(payResultDTO);
            return res;
        } catch (Exception e) {
            ExceptionUtils.err("微信预支付失败",e);
        }
        return res;
    }
 
    /**
     * 微信回调
     *
     */
    public String payBack(String xmlResult) {
        String payOrderId="";
        boolean lock=false;
        try {
            //记录微信回调日志
            record(xmlResult);
            WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlResult);
            payOrderId = result.getOutTradeNo();
            // 锁+去重
            if(redisUtil.setnx(Constants.REDIS_PAY_KEY+"wx:payback:"+payOrderId,payOrderId)) {
                lock=true;
                PayInfo payInfo = payInfoMapper.selectByPrimaryKey(payOrderId);
                if (payInfo != null) {
                    if (payInfo.getStatus().equals(PayEnum.已支付.getValue())) {
                        return WxPayNotifyResponse.success("处理成功!");
                    }else{
                        Map map = new HashMap();
                        map.put("userId", payInfo.getCreateUserId());
                        map.put("money", payInfo.getMoney());
                        map.put("status", PayEnum.已支付.getValue());
                        map.put("payOrderId", payInfo.getPayOrderId());
                        map.put("fee",payInfo.getFee());
                        payService.webChatRechargeCallback(map);
                    }
                }
            }
            return WxPayNotifyResponse.success("处理成功!");
        } catch (Exception e) {
            log.error("微信回调失败,订单:"+payOrderId, e);
            throw new WxPayNotifyResponseException(WxPayNotifyResponse.fail(e.getMessage()));
        }finally {
            if(lock) {
                redisUtil.del(Constants.REDIS_PAY_KEY+"wx:payback:"+payOrderId);
            }
        }
    }
 
 
    private void record(String xmlResult){
        WxPayOrderNotifyResult res= WxPayOrderNotifyResult.fromXML(xmlResult);
        PayWxRecord payWxRecord=new PayWxRecord();
        BeanUtils.copyProperties(res,payWxRecord);
        payWxRecord.setCreateTime(DateUtil.now());
        payWxRecordMapper.insertSelective(payWxRecord);
    }
 
 
//    @Scheduled(cron = "${wx.pay.cron}")
//    @Async
//    public void syncPay(){
//        log.trace("********定时修复支付订单开始【每1分钟执行1次】*********");
//        Example example=new Example(PayInfo.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("payType",PayEnum.充值.getValue());
//        criteria.andEqualTo("status",PayEnum.待支付.getValue());
//        criteria.andCondition("IFNULL(check_num,0)<2");
//        criteria.andCondition("to_days(create_time) = to_days(now())");
//        List<PayInfo> payInfos = payInfoMapper.selectByExample(example);
//
//        if(CollUtil.isNotEmpty(payInfos)){
//            for (PayInfo payInfo : payInfos) {
//                String createTime = payInfo.getCreateTime();
//                String date=DateUtil.now();
//                long between = DateUtil.between(DateUtil.parse(createTime,DateUtils.DATE_FORMAT_YMDHMS), DateUtil.parse(date,DateUtils.DATE_FORMAT_YMDHMS), DateUnit.MINUTE);
//                //3分钟后进行查询
//                if(between<3){
//                    continue;
//                }
//                String payOrderId = payInfo.getPayOrderId();
//                Integer checkNum = payInfo.getCheckNum();
//                if(checkNum==null){
//                    checkNum=0;
//                }
//                WxPayOrderQueryResult wxPayOrderQueryResult = null;
//                boolean lock=false;
//                try {
//                    wxPayOrderQueryResult = wxPayService.queryOrder(null, payOrderId);
//                    String returnCode = wxPayOrderQueryResult.getReturnCode();
//                    String resultCode = wxPayOrderQueryResult.getResultCode();
//                    if("SUCCESS".equals(returnCode)&&"SUCCESS".equals(resultCode)){
//                        //SUCCESS—支付成功,REFUND—转入退款,NOTPAY—未支付,CLOSED—已关闭,REVOKED—已撤销(刷卡支付),USERPAYING--用户支付中,PAYERROR--支付失败(其他原因,如银行返回失败)
//                        String tradeState = wxPayOrderQueryResult.getTradeState();
//                        log.trace("充值交易流水号:{},交易状态:{}",payOrderId,tradeState);
//                        if("SUCCESS".equals(tradeState)){
//                            // 锁+去重
//                            if(redisUtil.setnx(Constants.REDIS_PAY_KEY+"wx:payback:"+payOrderId,payOrderId)) {
//                                lock=true;
//                                PayInfo payInfo2 = payInfoMapper.selectByPrimaryKey(payOrderId);
//                                if (payInfo2 != null) {
//                                    if (payInfo2.getStatus().equals(PayEnum.已支付.getValue())) {
//                                        continue;
//                                    }else{
//                                        Map map = new HashMap();
//                                        map.put("userId", payInfo2.getCreateUserId());
//                                        map.put("money", payInfo2.getMoney());
//                                        map.put("status", PayEnum.已支付.getValue());
//                                        map.put("payOrderId", payInfo2.getPayOrderId());
//                                        map.put("fee", payInfo2.getFee());
//                                        payService.webChatRechargeCallback(map);
//                                        log.info("支付订单:{}已自动修复成功",payOrderId);
//                                    }
//                                }
//                            }
//                        }else{
//                            PayInfo payInfo2=new PayInfo();
//                            payInfo2.setPayOrderId(payInfo.getPayOrderId());
//                            checkNum=2;
//                            payInfo2.setCheckNum(checkNum);
//                            payInfoMapper.updateByPrimaryKeySelective(payInfo2);
//                        }
//                    }
//                } catch (WxPayException e) {
//                    log.warn("主动调用微信支付查询出现异常");
//                    PayInfo payInfo2=new PayInfo();
//                    payInfo2.setPayOrderId(payInfo.getPayOrderId());
//                    checkNum=2;
//                    payInfo2.setCheckNum(checkNum);
//                    payInfoMapper.updateByPrimaryKeySelective(payInfo2);
//                }finally {
//                    if(lock) {
//                        redisUtil.del(Constants.REDIS_PAY_KEY+"wx:payback:"+payOrderId);
//                    }
//                }
//            }
//        }
//        log.trace("********定时修复支付订单完成【每3分钟执行1次】*********");
//
//    }
 
    /**
     * 修复支付
     * @param payOrderId
     */
    public Result payFix(String payOrderId) {
        PayInfo payInfo2 = payInfoMapper.selectByPrimaryKey(payOrderId);
        if (payInfo2 != null) {
            if (payInfo2.getStatus().equals(PayEnum.已支付.getValue())) {
                return Result.error("当前支付订单已支付成功");
            }else{
                Map map = new HashMap();
                map.put("userId", payInfo2.getCreateUserId());
                map.put("money", payInfo2.getMoney());
                map.put("status", PayEnum.已支付.getValue());
                map.put("payOrderId", payInfo2.getPayOrderId());
                map.put("fee", payInfo2.getFee());
                payService.webChatRechargeCallback(map);
                return Result.success();
            }
        }
        return Result.error("当前支付订单号不存在");
 
    }
}