rentaiming
2024-05-29 d5209a22297137cba55e500fe9457866e3919f9e
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package com.ruoyi.order.service.impl;
 
import static com.ruoyi.order.util.tencent.WXPay.requestRefundService;
import static com.ruoyi.order.util.tencent.WXPay.requestUnifiedorderService;
 
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.order.domain.pojo.Paylog;
import com.ruoyi.order.mapper.PaylogMapper;
import com.ruoyi.order.service.IPaylogService;
import com.ruoyi.order.util.SinataUtil;
import com.ruoyi.order.util.alipay.config.AlipayConfig;
import com.ruoyi.order.util.alipay.util.PayDemoActivity;
import com.ruoyi.order.util.tencent.common.Configure;
import com.ruoyi.order.util.tencent.common.Signature;
import com.ruoyi.order.util.tencent.common.XMLParser;
import com.ruoyi.order.util.tencent.protocol.AppPayReqData;
import com.ruoyi.order.util.tencent.protocol.UnifiedorderReqData;
import com.ruoyi.system.api.domain.OrderAuctionBond;
import com.ruoyi.system.api.domain.dto.BondVO;
import com.ruoyi.system.api.domain.dto.RefundDTO;
import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import com.ruoyi.system.api.feignClient.OrderClient;
import org.springframework.stereotype.Service;
 
/**
 * <p>
 * 支付记录 服务实现类
 * </p>
 *
 * @author mitao
 * @since 2024-05-22
 */
@Service
public class PaylogServiceImpl extends ServiceImpl<PaylogMapper, Paylog> implements IPaylogService {
 
    @Resource
    private PaylogServiceImpl   paylogServiceImpl;
 
    @Resource
    private IPaylogService iPaylogService;
 
    @Resource
    private OrderClient orderClient;
 
    @Override
    public R<Map<String, Object>> getPayInfo(Integer uid, Integer type, String subject, String body, String orderNO,String openId, HttpServletRequest request) {
        Double price = 0.0;
 
        try {
            if(judgeContainsStr(orderNO)){
                price=  Double.valueOf(1);
                body = body;
                subject = subject;
                Map<String, Object> map = new HashMap<String, Object>();
                if (type == 1) {
                    // 支付宝预下单
                    return paylogServiceImpl.alipay(orderNO, subject, body, price, request);
                } else {
                    // 微信预下单
                    return paylogServiceImpl.wxpay(2, orderNO, body,openId, price, request);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("获取异常");
    }
 
 
    /**
     * 服务器异步通知处理支付宝
     *
     * @param request
     * @param res
     */
    @Override
    public void notifyUrl(HttpServletRequest request, HttpServletResponse res) {
        HttpServletResponse response = (HttpServletResponse) res;
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out;
        try {
            out = response.getWriter();
            // 获取支付宝POST过来反馈信息
            Map<String, String> params = new HashMap<String, String>();
            Map requestParams = request.getParameterMap();
            log.debug("AlipayController.notifyUrl__requestParams:\n" + requestParams);
            for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
                String name = (String) iter.next();
                String[] values = (String[]) requestParams.get(name);
                String valueStr = "";
                for (int i = 0; i < values.length; i++) {
                    valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
                }
                // 乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
                valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
                params.put(name, valueStr);
            }
 
            Paylog paylog1= getPayLog_alipay(request);
 
            // 验证成功
            if ("TRADE_FINISHED".equals(paylog1.getTradeStatus())) {
                System.out.println("AlipayController.notifyUrl__验证成功:success");
                // 支付失败
            } else if ("TRADE_SUCCESS".equals(paylog1.getTradeStatus())) {
                // 支付成功
                try {
                    LambdaQueryWrapper<Paylog> wrapper = Wrappers.lambdaQuery();
                    wrapper.eq(Paylog::getOutTradeNo,paylog1.getOutTradeNo());
                    Paylog one = iPaylogService.getOne(wrapper);
                    if(SinataUtil.isEmpty(one)) {
                        iPaylogService.save(paylog1);
                        if (paylog1.getOutTradeNo().contains("BO")) {
                            BondVO bondVO=new BondVO();
                            bondVO.setOrderNO(paylog1.getOutTradeNo());
                            orderClient.UpdateBond(bondVO, SecurityConstants.INNER);
 
                        }
 
                    }
 
 
 
                    // ——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
                    log.debug("AlipayController.notifyUrl__回调处理:success");
                    out.println("success"); // 请不要修改或删除
                    // ——请根据您的业务逻辑来编写程序(以上代码仅作参考)——
                    log.debug("AlipayController.notifyUrl__回调处理:success");
                    out.println("success"); // 请不要修改或删除
                } catch (Exception e) {
                    log.debug("AlipayController.notifyUrl__回调逻辑代码处理异常!fail");
                    // 返回失败
                    out.println("fail");
                    e.printStackTrace();
                }
                // ////////////////////////////////////////////////////////////////////////////////////////
            } else {// 验证失败
                log.debug("AlipayController.notifyUrl__回调处理失败!fail");
                out.println("fail");
            }
        } catch (
                IOException e) {
            log.debug("AlipayController.notifyUrl__支付宝服务器异步通知数据处理失败!");
            e.printStackTrace();
        }
    }
 
///////////////////////////////////////////////////////////////////////////////////////////////////
 
    /**
     * 微信支付回调(参考财付通回调接口)
     *
     * @param request
     * @param response
     */
    @Override
    public void wxnotify(HttpServletRequest request, HttpServletResponse response) {
        try {
            System.out.println("微信支付回调!!!!!!!!!!!!!!!!!!!!!!!!!");
            // 异步通知返回报文
            StringBuffer notityXml = new StringBuffer();
            String inputLine;
            while ((inputLine = request.getReader().readLine()) != null) {
                notityXml.append(inputLine);
            }
            request.getReader().close();
            // log.debug("WxpayController.notify__notityXml:\n" + notityXml);
            System.out.println("WxpayController.notify__notityXml:\n" + notityXml);
 
            // 验证签名
            if (Signature.checkIsSignValidFromResponseString(1, notityXml.toString())
                    || Signature.checkIsSignValidFromResponseString(2, notityXml.toString())) {
                Map<String, Object> map = XMLParser.getMapFromXML(notityXml.toString());
                // log.debug("WxpayController.notify__map:\n" + map);
                // 接口返回状态
                String result_code = (String) map.get("result_code");
                if ("SUCCESS".equals(result_code)) {
                    // // 商户订单号
                    String out_trade_no = (String) map.get("out_trade_no");
                    // // 微信支付交易号
                    String trade_no = (String) map.get("transaction_id");
                    // // 金额,以分为单位
                    String total_fee = (String) map.get("total_fee");
                    // // 优惠金额
                    // String discount = (String) map.get("discount");
                    // // 支付完成时间
                    String time_end = (String) map.get("time_end");
                    // // 支付者唯一Id(对应买家账号的一个加密串 )
                    String buyer_id = (String) map.get("buyer_id");
 
                    ///////////////////////////// 这里程序处理支付回调逻辑
                    ///////////////////////////// ////////////////////
 
                    LambdaQueryWrapper<Paylog> wrapper = Wrappers.lambdaQuery();
                    wrapper.eq(Paylog::getOutTradeNo,out_trade_no);
                    Paylog one = iPaylogService.getOne(wrapper);
                    if(SinataUtil.isEmpty(one)) {
                        Paylog paylog = new Paylog();
                        paylog.setOutTradeNo(out_trade_no);
                        paylog.setPayType(2);
                        paylog.setBuyerId(buyer_id);
                        paylog.setTradeNo(trade_no);
                        paylog.setPayMoney(Double.parseDouble(total_fee) / 100);
                        paylog.setState(1);
                        System.out.println("支付完成时间" + time_end);
                        paylog.setAddTime(LocalDateTime.now());
                        iPaylogService.save(paylog);
                        if (paylog.getOutTradeNo().contains("BO")) {
                            BondVO bondVO=new BondVO();
                            bondVO.setOrderNO(paylog.getOutTradeNo());
                            orderClient.UpdateBond(bondVO, SecurityConstants.INNER);
 
                        }
 
 
                        log.debug("WxpayController.notify__回调处理成功:SUCCESS");
                        response.getOutputStream().print("success");
                    }
                    log.debug("WxpayController.notify__回调处理成功:SUCCESS");
                    response.getOutputStream().print("success");
                } else {
                    log.debug("WxpayController.notify__回调处理:验证状态错误!" + result_code);
                    System.out.println("验证状态错误!" + result_code);
                }
            } else {
                log.debug("WxpayController.notify__回调处理:通知签名验证失败!");
                System.out.println("通知签名验证失败!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 支付宝订单退款
     *
     * @author tzj
     * @param out_trade_no
     *            商品订单号
     * @param trade_no
     *            支付宝交易号
     * @param refund_amount
     *            退款金额
     * @return
     */
    public boolean refundForAlipay(String out_trade_no, String trade_no, Double refund_amount) {
        try {
            // 实例化客户端
            AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                    AlipayConfig.app_id, AlipayConfig.private_key, "json", AlipayConfig.input_charset,
                    AlipayConfig.ali_public_key,"RSA2");
            // 实例化具体API对应的request类,类名称和接口名称对应
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            // SDK已经封装掉了公共参数,这里只需要传入业务参数
            // 此次只是参数展示,未进行字符串转义,实际情况下请转义
            request.setBizContent("{" + "\"out_trade_no\":\"" + out_trade_no + "\", " + "\"trade_no\":\"" + trade_no
                    + "\", " + "\"refund_amount\":" + refund_amount + ", " + "\"refund_reason\":\"正常退款\","
                    + "\"out_request_no\":\"HZ01RF001\"" + "}");
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            // 调用成功,则处理业务逻辑
            if (response.isSuccess()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
 
    /**
     * 获取支付信息支付(签约app支付2.0)
     *
     * @param subject
     * @param body
     * @param price
     * @param request
     * @return
     */
    public R<Map<String, Object>> alipay(String orderNo, String subject, String body, Double price,
                                         HttpServletRequest request) {
        try {
            // 接口封装支付宝请求参数
            // Map<String, Object> mData = new HashMap<String, Object>();
 
            // 构建请求支付签名参数
            Map<String, Object> pay = PayDemoActivity.appPay(subject, body, price, orderNo);
            /*
             * Set<Entry<String, String>> entrySet = pay.entrySet();
             * Iterator<Entry<String, String>> iterator = entrySet.iterator();
             * while (iterator.hasNext()) { Entry<String, String> next =
             * iterator.next(); mData.put(next.getKey(), next.getValue()); }
             */
            return R.ok(pay);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("支付异常!");
        }
    }
 
 
    /**
     * 统一下单
     *
     * @param apptype
     * @param outTradeNo
     * @param body
     * @param price
     * @param request
     * @return
     */
    public static R<Map<String, Object>> wxpay(Integer apptype, String outTradeNo, String body,String openId, Double price,
                                               HttpServletRequest request) {
        // 获取预支付接口返回参数
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> appPayMap = new HashMap<String, Object>();
        try {
            // 构建接口请求参数
            UnifiedorderReqData unifiedorderReqData = new UnifiedorderReqData(outTradeNo, body, price, Configure.wx_notify_url,
                    "JSAPI", openId);
            // 请求接口获取返回接口
            String result = requestUnifiedorderService(2, unifiedorderReqData);
            System.out.println(result);
            System.out.println("WxpayController.createOrder__result:\n" + result);
            // 获取预支付接口返回参数
            map = XMLParser.getMapFromXML(result);
            System.out.println("WxpayController.createOrder__result:\n" + result);
            // 捕获预支付接口错误提示
            if ("FAIL".equals(map.get("result_code")) || "FAIL".equals(map.get("return_code"))) {
                return R.fail(String.valueOf(map.get("return_msg")));
            }
 
            // 对获取预支付返回接口参数进行封装(生成支付订单接口数据)
            AppPayReqData appPay = new AppPayReqData(apptype, (String) map.get("appid"), (String) map.get("mch_id"),
                    (String) map.get("prepay_id"), unifiedorderReqData.getNonce_str());
 
            // 对获取预支付返回接口参数进行封装(生成支付订单接口数据)
            appPayMap.put("appid", appPay.getAppid());// 小程序ID
            appPayMap.put("nonceStr", appPay.getNoncestr());// 随机字符串(32位)
            appPayMap.put("package", appPay.get_package());// 扩展字段(暂填写固定值Sign=WXPay)
            appPayMap.put("partnerId", appPay.getPartnerid());// 商户号
            appPayMap.put("prepayId", appPay.getPrepayid());// 预支付编号(微信返回的支付交易会话ID)
            appPayMap.put("timeStamp", appPay.getTimestamp());// 时间戳
            appPayMap.put("sign", appPay.getSign());// 根据API给的签名规则进行签名
            return R.ok(appPayMap);
        } catch (Exception e) {
            System.out.println("统一下单_API_处理异常!");
            e.printStackTrace();
        }
        return R.fail("统一下单失败");
    }
 
 
 
    /**
     * 微信退款
     *
     * @param transactionID
     *            【支付交易号】是微信系统为每一笔支付交易分配的订单号,通过这个订单号可以标识这笔交易,
     *            它由支付订单API支付成功时返回的数据里面获取到。建议优先使用
     * @param outTradeNo
     *            【商品订单编号】商户系统内部的订单号,transaction_id
     *            、out_trade_no二选一,如果同时存在优先级:transaction_id>out_trade_no
     * @param outRefundNo
     *            【退款编号】商户系统内部的退款单号,商户系统内部唯一,同一退款单号多次请求只退一笔
     *            订单总金额,单位为分
     * @param refundFee
     *            退款总金额,单位为分,可以做部分退款
     * @return
     */
    public static boolean refundForWxpay(Integer apptype, String transactionID, String outTradeNo, String outRefundNo,
                                         Integer totalFee, Integer refundFee, String pay_type) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            // 构建接口请求参数
            com.tencent.protocol.RefundReqData refundReqData = new com.tencent.protocol.RefundReqData(transactionID, outTradeNo, outRefundNo, totalFee, refundFee,
                    pay_type);
 
            // 请求接口返回结果
            String result = requestRefundService(apptype, refundReqData);
            System.out.println("微信退款返回内容:" + result);
            // 获取预支付接口返回参数
            map = XMLParser.getMapFromXML(result);
            System.out.println("微信退款返回参数:" + map);
            // 退款成功处理
            if ("SUCCESS".equals(map.get("result_code"))) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }
 
    /**
     * 获取支付宝
     *
     * @param request
     * @return
     * @throws Exception
     */
    private Paylog getPayLog_alipay(HttpServletRequest request) throws IOException {
        //////// 获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)////////
        // 商户订单号
        String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "UTF-8");
        // 支付宝交易号
        String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "UTF-8");
        // 交易状态
        String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "UTF-8");
        // 支付者唯一Id
        String buyer_id = new String(request.getParameter("buyer_id").getBytes("ISO-8859-1"), "UTF-8");
        // 支付帐号
        String buyer_email = "";
        if (SinataUtil.isNotEmpty(request.getParameter("buyer_logon_id"))) {
            buyer_email = new String(request.getParameter("buyer_logon_id").getBytes("ISO-8859-1"), "UTF-8");
        }
        // 支付金额
        String total_fee = new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "UTF-8");
        // 支付时间
        String notify_time = new String(request.getParameter("notify_time").getBytes("ISO-8859-1"), "UTF-8");
 
        //////// 获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以下仅供参考)////////
        Paylog paylog = new Paylog();
        paylog.setOutTradeNo(out_trade_no);
        paylog.setPayType(1);
        paylog.setBuyerId(buyer_id);
        paylog.setTradeNo(trade_no);
        paylog.setPayMoney(Double.parseDouble(total_fee));
        paylog.setState(1);
        paylog.setAddTime(LocalDateTime.now());
        paylog.setTradeStatus(trade_status.toString());
        return paylog;
    }
 
        public boolean judgeContainsStr(String cardNum) {
            String regex=".*[a-zA-Z]+.*";
            Matcher m= Pattern.compile(regex).matcher(cardNum);
            return m.matches();
        }
 
    @Override
    public Map<String, Object> refund(List<RefundDTO> refundDTOS) {
        Map<String, Object> map = new HashMap<>();
        Set<String> orderNoList = refundDTOS.stream().map(RefundDTO::getOrderNo)
                .collect(Collectors.toSet());
        if (StringUtils.isNotEmpty(orderNoList)) {
            Map<String, Paylog> paylogMap = iPaylogService.lambdaQuery()
                    .in(Paylog::getOutTradeNo, orderNoList).eq(Paylog::getState, 1).list().stream()
                    .collect(Collectors.toMap(Paylog::getOutTradeNo, Function.identity()));
            List<Paylog> updList = new ArrayList<>();
            for (RefundDTO refundDTO : refundDTOS) {
                String orderNo = refundDTO.getOrderNo();
                BigDecimal amount = refundDTO.getAmount();
                Paylog paylog = paylogMap.get(orderNo);
                Optional.of(paylog).ifPresent(pay -> {
                    Boolean res = handleRefund(pay, amount, orderNo);
                    map.put(orderNo, res);
                    if (res) {
                        pay.setState(3);
                        updList.add(pay);
                    }
                });
            }
            iPaylogService.updateBatchById(updList);
        }
        return map;
    }
 
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("1", true);
        map.put("2", false);
        map.put("3", true);
        map.put("4", false);
        map.put("5", true);
        List<String> collect = map.entrySet().stream().map(entry -> {
            if (!(boolean) entry.getValue()) {
                return entry.getKey();
            }
            return null;
        }).filter(Objects::nonNull).collect(Collectors.toList());
        System.out.println(collect);
    }
 
    private Boolean handleRefund(Paylog paylog, BigDecimal amount,
            String orderNo) {
        boolean res = false;
        if (StringUtils.isNotNull(paylog)) {
            if (paylog.getPayType() == 1) {
                res = paylogServiceImpl.refundForAlipay(paylog.getOutTradeNo(),
                        paylog.getTradeNo(), amount.doubleValue());
            }
            if (paylog.getPayType() == 2) {
                String refundMoney = SinataUtil.doubleRetainTwo(
                        amount.doubleValue() * 100d);
                Integer refundFee = Integer.parseInt(
                        refundMoney.substring(0, refundMoney.length() - 3));
                String money = SinataUtil.doubleRetainTwo(paylog.getPayMoney() * 100d);
                Integer totalFee = Integer.parseInt(
                        money.substring(0, money.length() - 3));
                res = paylogServiceImpl.refundForWxpay(2, paylog.getTradeNo(),
                        paylog.getOutTradeNo(), orderNo, totalFee,
                        refundFee, "4");
            }
        }
        return res;
    }
}