kongdeqiang
2 天以前 199202813dd8ca536ed2334f5eeb6aba3ad25b21
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package com.boying.controller.phone;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boying.common.R;
import com.boying.common.SystemConfigProperties;
import com.boying.entity.*;
import com.boying.entity.vo.MonthFeePayVo;
import com.boying.service.*;
import com.boying.util.LicensePlateValidator;
import com.google.gson.Gson;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
 
/**
 * @author kdq
 * @version 1.0.0
 * @ClassName MonthFeePayController.java
 * @Description TODO
 * @createTime 2024年12月20日 16:14:00
 */
@RestController
@RequestMapping("ffzf/monthFeePay")
@RequiredArgsConstructor
public class MonthFeePayController {
 
    private final PaymentLogService paymentLogService;
    private final EnterParkService enterParkService;
    private final PayLogService payLogService;
    private final WhiteListService whiteListService;
    private final SystemConfigProperties systemConfigProperties;
    private final WXService wxService;
    private final ParkService parkService;
 
    @PostMapping("/pay")
    public Object pay(MonthFeePayVo vo) {
        if(!LicensePlateValidator.isValidLicensePlate(vo.getCarNo())){
            return R.failed("车牌号填写错误");
        }
        if(vo.getPhone() == null){
            return R.failed("未填写手机号");
        }
        boolean validChineseMobileNumber = isValidChineseMobileNumber(vo.getPhone());
        if(!validChineseMobileNumber){
            return R.failed("手机号有误");
        }
        QueryWrapper<WhiteList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(WhiteList::getCarNo,vo.getCarNo())
                .eq(WhiteList::getPayFlag,1);
        List<WhiteList> list = whiteListService.list(queryWrapper);
        if(list !=null && list.size()>0){
            return R.ok(list.get(0));
        }
        return R.ok(null,"未查询到月票");
    }
    @PostMapping("/findWhiteList")
    public Object findWhiteList(MonthFeePayVo vo) {
        QueryWrapper<WhiteList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(WhiteList::getCarNo,vo.getCarNo())
                .eq(WhiteList::getPhone,vo.getPhone());
        List<WhiteList> list = whiteListService.list(queryWrapper);
        if(list !=null && list.size()>0){
            return R.ok(list.get(0));
        }else {
            return R.failed("未查询到此月票");
        }
    }
 
    /**
     * 获取微信openid
     */
    @PostMapping("getOpenId")
    public Object park(MonthFeePayVo vo){
        String openIdByCode = wxService.getOpenIdByCode(vo.getCode());
        PaymentLog paymentLog = new PaymentLog();
        paymentLog.setType(vo.getType());
        paymentLog.setParkId(vo.getParkId());
        paymentLog.setCarNo(vo.getCarNo());
        paymentLog.setPhone(vo.getPhone());
        paymentLog.setName(vo.getName());
        paymentLog.setMoney(vo.getMoney());
        paymentLog.setStartTime(vo.getStartTime());
        paymentLog.setEndTime(vo.getEndTime());
        paymentLogService.save(paymentLog);
        return payByWX(paymentLog,openIdByCode);
    }
 
    /**
     * 获取阿里支付
     */
    @PostMapping("getAiLi")
    public Object getAiLi(MonthFeePayVo vo){
        PaymentLog paymentLog = new PaymentLog();
        paymentLog.setType(vo.getType());
        paymentLog.setParkId(vo.getParkId());
        paymentLog.setCarNo(vo.getCarNo());
        paymentLog.setPhone(vo.getPhone());
        paymentLog.setName(vo.getName());
        paymentLog.setMoney(vo.getMoney());
        paymentLog.setStartTime(vo.getStartTime());
        paymentLog.setEndTime(vo.getEndTime());
        paymentLogService.save(paymentLog);
        return payByAli(paymentLog);
    }
 
    @PostMapping("/paySuccess")
    public Object paySuccess(PaymentLog vo,Integer flag) {
        if(flag == null || flag == 0){
            return  R.failed("缴费失败");
        }else {
            //缴费成功,新增白名单
            WhiteList whiteList = new WhiteList();
            whiteList.setPayFlag(1);
            whiteList.setCarNo(vo.getCarNo());
            whiteList.setStartTime(vo.getStartTime());
            whiteList.setEndTime(vo.getEndTime());
            whiteList.setPhone(vo.getPhone());
            if(vo.getType()==1){
                QueryWrapper<Park> wrapper = new QueryWrapper<>();
                wrapper.lambda()
                        .select(Park::getId);
                List<Park> list = parkService.list(wrapper);
                String collect = list.stream().map(Park::getId).map(String::valueOf).collect(Collectors.joining(","));
                whiteList.setParkIds(collect);
                whiteList.setType(1);
            }else {
                whiteList.setParkIds(vo.getParkId()+"");
                whiteList.setParkId(vo.getParkId());
                whiteList.setType(1);
            }
            whiteListService.save(whiteList);
            return R.ok();
        }
 
 
    }
 
    /**
     * 微信缴费
     */
    @PostMapping("payByWX")
    public Object payByWX(PaymentLog paymentLog,String openId){
        System.out.println("开始支付");
        String xtyhpay = systemConfigProperties.getXTYHPAY();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        LocalDateTime date3 = LocalDateTime.now();
        Map<String,Object> map = new HashMap<>();
        map.put("encoding","UTF-8");
        map.put("signMethod","01");
        map.put("sdkAppId",systemConfigProperties.getXTYHMERID());
        map.put("txnType","1006");
        map.put("txnSubType","100603");
        map.put("txnAccType","02");
        map.put("secMerAppId",systemConfigProperties.getXTYHAPPID());
        map.put("txnSubOpenid",openId);
        map.put("txnProductId",paymentLog.getId()+"");
        map.put("aesWay","01");
        map.put("merId",systemConfigProperties.getXTYHMERID());
        map.put("merName",systemConfigProperties.getXTYHMERNAME());
        map.put("backEndUrl",systemConfigProperties.getYPRESULT());
        map.put("txnOrderId",paymentLog.getId()+""+format);
        map.put("txnOrderTime",format);
        map.put("txnOrderBody",paymentLog.getCarNo()+"月票缴费");
        map.put("txnAmt",Math.round(paymentLog.getMoney() * 100)+"");
        map.put("txnCcyType","156");
 
        Map<String, String> stringStringMap = wxService.addSign(map);
        map.put("certId",stringStringMap.get("certId"));
        map.put("signAture",stringStringMap.get("signAture"));
        Gson gson =new Gson();
        String str = gson.toJson(map);
        System.out.println("请求报文:"+str);
 
        String body = HttpRequest.post(xtyhpay)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(str)
                .timeout(15000)
                .execute()
                .body();
        System.out.println(paymentLog.getId()+"响应报文:"+body);
 
        JSONObject map1 = JSON.parseObject(body);
        String respCode = (String)map1.get("respCode");
        if(respCode.equals("0000")){
            String respTxnSsn = (String)map1.get("respTxnSsn");
            String respTxnTime = (String)map1.get("respTxnTime");
            String respData = (String)map1.get("respData");
            Map map2 = gson.fromJson(respData, Map.class);
            String appId = (String)map2.get("appId");
            String timeStamp = (String)map2.get("timeStamp");
            String nonceStr = (String)map2.get("nonceStr");
            String signType = (String)map2.get("signType");
            String package1 = (String)map2.get("package");
            String paySign = (String)map2.get("paySign");
            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("appId",appId);
            resultMap.put("timeStamp",timeStamp);
            resultMap.put("nonceStr",nonceStr);
            resultMap.put("signType",signType);
            resultMap.put("package",package1);
            resultMap.put("paySign",paySign);
            PayLog payLog = new PayLog();
            payLog.setOutParkId(paymentLog.getId());
            payLog.setCode2(respTxnSsn);
            payLog.setPayTime(LocalDateTime.now());
            payLog.setTxnOrderId(paymentLog.getId()+""+format);
            payLog.setTxnOrderTime(format);
            payLog.setRespTxnTime(respTxnTime);
            payLog.setType(1);
            payLogService.save(payLog);
            return R.ok(resultMap,null);
        }
        String respMsg = (String)map1.get("respMsg");
        return R.failed(respCode+":"+respMsg);
    }
 
    /**
     * 阿里缴费
     */
    @PostMapping("payByAli")
    public Object payByAli(PaymentLog paymentLog){
        String xtyhpay = systemConfigProperties.getXTYHPAY();
        LocalDateTime date3 = LocalDateTime.now();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String format = sdf.format(new Date());
        Map<String,Object> map = new HashMap<>();
        map.put("encoding","UTF-8");
        map.put("signMethod","01");
        map.put("sdkAppId",systemConfigProperties.getXTYHMERID());
        map.put("txnType","1007");
        map.put("txnSubType","100702");
        map.put("txnAccType","03");
        map.put("aesWay","01");
        map.put("merId",systemConfigProperties.getXTYHMERID());
        map.put("merName",systemConfigProperties.getXTYHMERNAME());
        map.put("backEndUrl",systemConfigProperties.getXTYHRESULT());
        map.put("txnOrderId",paymentLog.getId()+""+format);
        map.put("txnOrderTime",format);
        map.put("txnOrderBody",paymentLog.getCarNo()+"月票缴费");
        map.put("txnAmt",Math.round(paymentLog.getMoney() * 100)+"");
        map.put("txnCcyType","156");
 
        Map<String, String> stringStringMap = wxService.addSign(map);
        map.put("certId",stringStringMap.get("certId"));
        map.put("signAture",stringStringMap.get("signAture"));
        Gson gson =new Gson();
        String str = gson.toJson(map);
        System.out.println("请求报文:"+str);
        String body = HttpRequest.post(xtyhpay)
                .header(Header.CONTENT_TYPE, "application/json")
                .body(str)
                .timeout(15000)
                .execute()
                .body();
        System.out.println(paymentLog.getId()+"响应报文:"+body);
 
        JSONObject map1 = JSON.parseObject(body);
        String respCode = (String)map1.get("respCode");
        if(respCode.equals("0000")){
            String respTxnSsn = (String)map1.get("respTxnSsn");
            String respData = (String)map1.get("respData");
            String respTxnTime = (String)map1.get("respTxnTime");
            Map map2 = gson.fromJson(respData, Map.class);
            String qrCode = (String)map2.get("qrCode");
            PayLog payLog = new PayLog();
            payLog.setOutParkId(paymentLog.getId());
            payLog.setCode2(respTxnSsn);
            payLog.setQrCode(qrCode);
            payLog.setPayTime(LocalDateTime.now());
            payLog.setTxnOrderId(paymentLog.getId()+""+format);
            payLog.setTxnOrderTime(format);
            payLog.setRespTxnTime(respTxnTime);
            payLog.setType(1);
            payLogService.save(payLog);
            return R.ok(null,qrCode);
        }
        String respMsg = (String)map1.get("respMsg");
        return R.failed(respCode+":"+respMsg);
    }
 
    /**
     * 邢台银行支付回调接口
     * @param
     * @return
     */
    @PostMapping("/getResult")
    public Object getResult(HttpServletRequest request){
        System.out.println("getResult收到访问");
        String respCode = request.getParameter("respCode");
        String respTxnSsn = request.getParameter("respTxnSsn");
        System.out.println(respCode+":"+respTxnSsn);
        if(respCode != null){
            if(respCode.equals("0000")){
                //交易成功,根据流水号查询出场记录
                PayLog payLog = payLogService.findByPayCode(respTxnSsn);
                if(payLog == null){
                    return R.failed("未查询到流水记录");
                }else {
                    PaymentLog byPayCode = paymentLogService.getById(payLog.getOutParkId());
                    if(byPayCode == null){
                        return R.failed("未查询到流水");
                    }else {
                        if(byPayCode.getStatus() == 1){
                            return R.ok(null,"月票已支付");
                        }else {
                           byPayCode.setStatus(1);
                            paySuccess(byPayCode,1);
                            return R.ok(null,"回调成功");
                        }
                    }
                }
            }else if(respCode.equals("0002") || respCode.equals("0003") || respCode.equals("0003") || respCode.equals("0012")){
                //交易成功,超时,未明,稍后发起查询
            }else if(respCode.equals("0011")){
                //交易
            }
        }else {
            return R.failed("未接收到入参");
        }
        return R.failed(null,"请求失败");
    }
 
    //判断手机号
    public static boolean isValidChineseMobileNumber(String number) {
        // 正则表达式
        String regex = "^(13[0-9]|14[57]|15[0-35-9]|17[0-9]|18[0-9])\\d{8}$";
        // 编译正则表达式
        Pattern pattern = Pattern.compile(regex);
        // 匹配
        Matcher matcher = pattern.matcher(number);
        // 返回是否匹配结果
        return matcher.matches();
    }
}