mitao
2025-04-11 9f40113d2c85a66bb5c20430642d0cf6b97c1e74
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
package com.dsh.guns.modular.system.util;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.msg.AlipayMsgClient;
import com.alipay.api.msg.MsgHandler;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dsh.course.util.UUIDUtil;
import com.dsh.guns.modular.system.controller.util.HttpClientUtil;
import com.dsh.guns.modular.system.controller.util.MD5AndKL;
import com.dsh.guns.modular.system.model.OperatorAuthAlipay;
import com.dsh.guns.modular.system.model.OperatorUser;
import com.dsh.guns.modular.system.service.IOperatorAuthService;
import com.dsh.guns.modular.system.service.IOperatorUserService;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
 
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.*;
 
import static com.dsh.guns.modular.system.controller.util.MD5AndKL.byteArrayToHexString;
 
/**
 * 第三方支付工具类
 */
@Component
public class PayMoneyUtil {
    private String aliAppid = "2021004105665036";//支付宝appid
 
    private String appPrivateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCi5i9nW/hGLJ3A06cZxTQdviFC7THpdSihoTYGLr9q006hu0V26ecBMY/o4w5bvIX0Ok/yofmZsVcCJpAPvbXL/uqVrIjnRRxXiaeBFThlxoBUTdunvbUSDYfzlEhJr5NvUKI6H6lz2niXlQGx4qy8Hau4ccWit9kM8jwUvsBVQoFgJA+xrjMvooA7YLopQtpOD+UJr5thApTSf1xrnr1W12yolTLEH15JmNV372cqXrYUuqnY0QsaPtxeqJUGAOcGdVLllQ7easEznP8DFBvDdHATcmp2SHNQDUEWN6MCVPbMgY06NQVqAXxqjTAYSVh+6TRu6bofPmpYC3TZB003AgMBAAECggEBAJAcR2+PA3NBYUYHeFrqBRMS8uX8ZR19kjZ7IgoSLTFaQsP9opRylPSPXhrPVBKAE5leRQAHn4MCSlESwHvMfxo7KFjFTFAc6dffZZpipYQUOc9bGampwJh58/3e/pyBgVMG6J23CPf/HJQtNFSkjd/V9+ayb/9l2dUEL3bC0fAZ/dbx8HsxdLw8wn3fLlWLj68hOMqa2deCZe3JdSVsPbeWqkh56FFsMLug0Nd+Ar4TgRl9/jnhXF0JWiD0LmPUYLhboY7EfUBzN4w1iYbDi1P+3zvoOYsiVKAXox9GMhQ2VzOO2UcSTuizSza2e98mGpabl/GpKmCz+RDFjtkX6eECgYEA2MyCij65eO3aGIm3FUe93DULRBYTfX8qJQSJq2WOWA3mmQlEW6L3O2B5/lG2h+8WmN6iLEs9eHpgycGYp7vAqgrANEn16ACVcuyx0scFtrZfZ+kmHMzFfiUWxJjVYk/6YngsGVBLdw6ueM42C8TTP67X9tU5TdVGoGWuqEj4W98CgYEAwFqwprXOch5Pqk/RPbb49r0Ou03K/UbciWnWWKzUhFFNS8MdlQPoDvQZbMwHLeWsa2VhaKITK3x5biLQb3U+0GLOn6lTvEyrEUH+ucREyLgVYTRAvwBPtnvlrzpyxPk2HnslQjju8WrvvLLBMKWUjlTrTOzhaHT21gz3pHMiOakCgYEAhLmfaXdBITGshb054sNLDtdCkGpbgEcrzAHdLps769iGxkYQHXHFngpQZUwtTUcoNGqIKknd1jZFrv7gsD+XkgKG7PwimehRlkwmCX5ilxtLiVgJRzRt6+5U5AMVD90a0tHzXYP0z2yjj73fBJF5KtGl0a10KZxaYrQdm1UhB00CgYBZZgzx/k9rtHC8LAqIj1CYhHejT92G53c6Gkl3vyOqN4sgKhfGmSEySfrDGPRBPZxr8ZtbIPCd5mUdberH0osWGMYFaJI1UsCy7aQwvGpniz7MhZeN7dweaOjwDs8mgtjHQ96mL4XGCDhR0BZ/wIURvZ/6iaGdhbbu9unlsWj3uQKBgQCmZYdsbbZkd3ev6f8rwyvMz+DrCQyYpY44cegBYuJgrZiQnL2fJioeN7ixX0UM48SfwsZEIrzshP/LGAwnc2MdjxKUl4jLN8SEe0NAjXOnz9Zaw740+aOmLpXcLWdP4uM2gIhWsvW1tEkQZCXmm7c9s/RsU8Pmzv+YL3+fSijOzA==";//支付宝开发者应用私钥
 
    private String alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAouYvZ1v4RiydwNOnGcU0Hb4hQu0x6XUooaE2Bi6/atNOobtFdunnATGP6OMOW7yF9DpP8qH5mbFXAiaQD721y/7qlayI50UcV4mngRU4ZcaAVE3bp721Eg2H85RISa+Tb1CiOh+pc9p4l5UBseKsvB2ruHHForfZDPI8FL7AVUKBYCQPsa4zL6KAO2C6KULaTg/lCa+bYQKU0n9ca569VtdsqJUyxB9eSZjVd+9nKl62FLqp2NELGj7cXqiVBgDnBnVS5ZUO3mrBM5z/AxQbw3RwE3JqdkhzUA1BFjejAlT2zIGNOjUFagF8ao0wGElYfuk0bum6Hz5qWAt02QdNNwIDAQAB";//支付宝应用公钥
 
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmu8n/4yTHWbn7VOrNc9OsLtDL1bEQ8gC1dHkj8Wy5z0mkaOsjJRIG/28ze12M0V8jdCKuuDr5Z1OPKiqf+XO3ypguEh+mYUVMBM/cZodDFQfTY1TKLWjvQCuaqlA+QUTCK6f7T7stsgyQ1o9Jj0rXZDz6PM4QHSTzjrLIBaeqM5WIBvH+fy/X+QG5Utd+/UT0kc0JyvuKhZ65yVUd/C9VcwJJAPliRsAQNrqYterwAJ9zvw9tF11wj9W0XgJ8Ccu4x3gR1vrlLRJJo/OA97RmxPQ+5hSacWQZCUd1dwiBq+YCrKVHGTj14izRHXrLc0yBlRXo7tBOIqcy3IsvKVthQIDAQAB";//支付宝支付公钥
 
    private String smid = "2088330203191220";//平台支付宝商户号...
 
    private String appid = "wx41d32f362ba0f911";//微信appid
 
    private String appSecret = "cf0ebf950f5926a69041a0e2bbe20f3e";
 
    private String mchId = "1501481761";//微信商户号
 
    private String key = "6f5e0c2dcabfa9c27b5da5836a362fef";//微信商户号
 
    private String callbackPath = "https://online.daowepark.com:443/account";//支付回调网关地址
 
    private String app_cert_path = "C:/cert/alipay/user/app_cert_path.crt";//应用公钥证书路径
 
    private String alipay_cert_path = "C:/cert/alipay/user/alipay_cert_path.crt";//支付宝公钥证书文件路径
 
    private String alipay_root_cert_path = "C:/cert/alipay/user/alipay_root_cert_path.crt";//支付宝CA根证书文件路径
 
    private String certPath = "/usr/playpai/cert/weixin/apiclient_cert.p12";//微信证书
    String signType =  "RSA2" ;
    String serverHost =  "openchannel.alipay.com" ;
 
    @Autowired
    private IOperatorAuthService operatorAuthService;
    @Autowired
    private IOperatorUserService operatorUserService;
 
    /**
     * 添加分账接收方
     * @param number 商户号或者openId
     * @param name 商户全称或者个人姓名
     * @return
     */
    public ResultUtil addReceiver(String number,String name) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("mch_id", mchId);
        map.put("appid", appid);
        String nonce_str = UUIDUtil.getRandomCode(16);
        map.put("nonce_str", nonce_str);
        // 将这个字符串使用json格式拼接起来
        Map<String, Object> body = new HashMap<>();
        body.put("type", "MERCHANT_ID");
        body.put("account", number);
        body.put("name", name);
        body.put("relation_type", "PARTNER");
        JSONObject jsonObject = new JSONObject(body);
        String jsonString = jsonObject.toString();
        map.put("receiver",jsonString );
        String s = this.weixinSignature(map);
        map.put("sign", s);
        String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        StringBuffer xmlString = new StringBuffer();
        Set<String> strings = map.keySet();
        String[] keys = {};
        keys = strings.toArray(keys);
        Arrays.sort(keys);
        xmlString.append("<xml>");
        for (int l = 0; l < keys.length; l++) {
            xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">");
        }
        xmlString.append("</xml>");
 
        Map<String, String> map1 = null;
        String body1 = null;
        try {
            body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将结果xml解析成map
        body1 = body1.replaceAll("<!\\[CDATA\\[", "");
        body1 = body1.replaceAll("]]>", "");
        try {
            map1 = this.xmlToMap(body1, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        String return_code = map1.get("return_code");
        if ("SUCCESS".equals(return_code)) {
            String result_code = map1.get("result_code");
            if ("SUCCESS".equals(result_code)) {
                return ResultUtil.success();
            } else {
//                System.err.println(map1.get("err_code_des"));
                return ResultUtil.error(map1.get("err_code_des"));
            }
        } else {
//            System.err.println(map1.get("return_msg") + appid + "----" + mchId);
            return ResultUtil.error(map1.get("return_msg"), new JSONObject());
        }
    }
    /**
     * 发起分账
     * @param order 微信订单号
     * @return
     */
    public ResultUtil fenzhang(String order) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("mch_id", mchId);
        map.put("appid", appid);
        String nonce_str = UUIDUtil.getRandomCode(16);
        map.put("nonce_str", nonce_str);
        map.put("transaction_id", order);
        // 将这个字符串使用json格式拼接起来
        Map<String, Object> body = new HashMap<>();
        body.put("type", "MERCHANT_ID");
        // todo 分账接收方 计算分账金额
        body.put("account", "1233");
        body.put("amount", "0");
        body.put("description", "订单分账");
        JSONObject jsonObject = new JSONObject(body);
        String jsonString = jsonObject.toString();
        map.put("receiver",jsonString);
        String s = this.weixinSignature(map);
        map.put("sign", s);
        String url = "https://api.mch.weixin.qq.com/secapi/pay/profitsharing";
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        StringBuffer xmlString = new StringBuffer();
        Set<String> strings = map.keySet();
        String[] keys = {};
        keys = strings.toArray(keys);
        Arrays.sort(keys);
        xmlString.append("<xml>");
        for (int l = 0; l < keys.length; l++) {
            xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">");
        }
        xmlString.append("</xml>");
 
        Map<String, String> map1 = null;
        String body1 = null;
        try {
            body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将结果xml解析成map
        body1 = body1.replaceAll("<!\\[CDATA\\[", "");
        body1 = body1.replaceAll("]]>", "");
        try {
            map1 = this.xmlToMap(body1, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        String return_code = map1.get("return_code");
        if ("SUCCESS".equals(return_code)) {
            String result_code = map1.get("result_code");
            if ("SUCCESS".equals(result_code)) {
                return ResultUtil.success();
            } else {
//                System.err.println(map1.get("err_code_des"));
                return ResultUtil.error(map1.get("err_code_des"));
            }
        } else {
//            System.err.println(map1.get("return_msg") + appid + "----" + mchId);
            return ResultUtil.error(map1.get("return_msg"), new JSONObject());
        }
    }
    /**
     * 删除分账接收方
     * @param number 商户号或者openId
     * @param name 商户全称或者个人姓名
     * @return
     */
    public ResultUtil deleteReceiver(String number,String name) throws Exception {
        Map<String, Object> map = new HashMap<>();
        map.put("mch_id", mchId);
        map.put("appid", appid);
        String nonce_str = UUIDUtil.getRandomCode(16);
        map.put("nonce_str", nonce_str);
        // 将这个字符串使用json格式拼接起来
        Map<String, Object> body = new HashMap<>();
        body.put("type", "MERCHANT_ID");
        body.put("account", number);
        body.put("name", name);
        body.put("relation_type", "PARTNER");
        JSONObject jsonObject = new JSONObject(body);
        String jsonString = jsonObject.toString();
        map.put("receiver",jsonString );
        String s = this.weixinSignature(map);
        map.put("sign", s);
        String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver";
        //设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        StringBuffer xmlString = new StringBuffer();
        Set<String> strings = map.keySet();
        String[] keys = {};
        keys = strings.toArray(keys);
        Arrays.sort(keys);
        xmlString.append("<xml>");
        for (int l = 0; l < keys.length; l++) {
            xmlString.append("<" + keys[l] + ">" + map.get(keys[l]) + "</" + keys[l] + ">");
        }
        xmlString.append("</xml>");
 
        Map<String, String> map1 = null;
        String body1 = null;
        try {
            body1 = HttpClientUtil.pushHttpRequsetXml(url, xmlString.toString(), new HashMap<>()).getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //将结果xml解析成map
        body1 = body1.replaceAll("<!\\[CDATA\\[", "");
        body1 = body1.replaceAll("]]>", "");
        try {
            map1 = this.xmlToMap(body1, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        String return_code = map1.get("return_code");
        if ("SUCCESS".equals(return_code)) {
            String result_code = map1.get("result_code");
            if ("SUCCESS".equals(result_code)) {
                return ResultUtil.success();
            } else {
//                System.err.println(map1.get("err_code_des"));
                return ResultUtil.error(map1.get("err_code_des"));
            }
        } else {
//            System.err.println(map1.get("return_msg") + appid + "----" + mchId);
            return ResultUtil.error(map1.get("return_msg"), new JSONObject());
        }
    }
    /**
     * 微信下单的签名算法
     *
     * @param map
     * @return
     */
    private String weixinSignature(Map<String, Object> map) {
        try {
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            List<Map.Entry<String, Object>> infoIds = new ArrayList<Map.Entry<String, Object>>(entries);
            // 对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)
            Collections.sort(infoIds, new Comparator<Map.Entry<String, Object>>() {
                public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
                    return (o1.getKey()).toString().compareTo(o2.getKey());
                }
            });
            // 构造签名键值对的格式
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> item : infoIds) {
                if (item.getKey() != null || item.getKey() != "") {
                    String key = item.getKey();
                    Object val = item.getValue();
                    if (!(val == "" || val == null)) {
                        sb.append(key + "=" + val + "&");
                    }
                }
            }
            sb.append("key=" + key);
            String sign = sha256_HMAC(sb.toString(), key).toUpperCase();
            return sign;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * sha256_HMAC加密
     * @param message 消息
     * @param secret  秘钥
     * @return 加密后字符串
     */
    public  String sha256_HMAC(String message, String secret) {
        String hash = "";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
            sha256_HMAC.init(secret_key);
            byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
            hash = byteArrayToHexString(bytes);
        } catch (Exception e) {
            System.out.println("Error HmacSHA256 ===========" + e.getMessage());
        }
        return hash;
    }
 
    /**
     * 处理直付通审核通过和拒绝消息
     */
    public void getMessage() {
        System.err.println("进入");
        final AlipayMsgClient alipayMsgClient = AlipayMsgClient.getInstance(aliAppid);
        try {
            alipayMsgClient.setConnector(serverHost);
        } catch (Exception e) {
            e.printStackTrace();
        }
        alipayMsgClient.setSecurityConfig(signType, appPrivateKey, alipay_public_key);
        try {
            alipayMsgClient.connect();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        alipayMsgClient.setMessageHandler( new  MsgHandler() {
            /**
             * 客户端接收到消息后回调此方法
             *  @param  msgApi 接收到的消息的消息api名
             *  @param  msgId 接收到的消息的消息id
             *  @param  bizContent 接收到的消息的内容,json格式
             */
            public   void   onMessage (String msgApi, String msgId, String bizContent)   {
                // 直付通进件审核通过
                if (StringUtils.equals(msgApi,"ant.merchant.expand.indirect.zft.passed")){
                    // 修改运营商状态 将返回的商户号填入运营商
                    System.out.println( "receive message. msgApi:"  + msgApi +  " msgId:"  + msgId +  " bizContent:"  + bizContent);
                    JSONObject json = JSONObject.parseObject(bizContent);
                    // 商户号
                    String smid = json.getString("smid");
                    // 审核备注信息
                    String memo = json.getString("memo");
                    // 订单id
                    String orderId = json.getString("order_id");
                    // 通过订单id查询
                    OperatorAuthAlipay auth = operatorAuthService.getOne(new QueryWrapper<OperatorAuthAlipay>()
                            .eq("orderNo",orderId));
                    auth.setSmid(smid);
                    auth.setAuditState(2);
                    auth.setRefuseReason(memo);
                    operatorAuthService.updateById(auth);
                    Integer operatorId = auth.getOperatorId();
                    OperatorUser operatorId1 = operatorUserService.getOne(new QueryWrapper<OperatorUser>()
                            .eq("operatorId", operatorId));
                    operatorId1.setAlipayNum(smid);
                    operatorId1.setAlipayAudit(2);
                    operatorUserService.updateById(operatorId1);
                }
                // 直付通进件审核失败
                if (StringUtils.equals(msgApi,"ant.merchant.expand.indirect.zft.rejected")){
                    JSONObject json = JSONObject.parseObject(bizContent);
                    // 商户号
                    String orderId = json.getString("order_id");
                    // 官方拒绝理由
                    String reason = json.getString("reason");
                    // 通过订单id查询
                    OperatorAuthAlipay auth = operatorAuthService.getOne(new QueryWrapper<OperatorAuthAlipay>()
                            .eq("orderNo",orderId));
                    auth.setAuditState(3);
                    auth.setRefuseReason(reason);
                    operatorAuthService.updateById(auth);
                    Integer operatorId = auth.getOperatorId();
                    OperatorUser operatorId1 = operatorUserService.getOne(new QueryWrapper<OperatorUser>()
                            .eq("operatorId", operatorId));
                    operatorId1.setAlipayAudit(3);
                    operatorUserService.updateById(operatorId1);
                }
            }
        });
    }
    /**
     * 支付宝支付
     */
    public ResultUtil alipay(String body, String subject, String passbackParams, String outTradeNo, String amount, String notifyUrl) {
//        //构造client
//        CertAlipayRequest certAlipayRequest = new CertAlipayRequest ();
//        //设置网关地址
//        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
//        //设置应用Id
//        certAlipayRequest.setAppId(aliAppid);
//        //设置应用私钥
//        certAlipayRequest.setPrivateKey(appPrivateKey);
//        //设置请求格式,固定值json
//        certAlipayRequest.setFormat("json");
//        //设置字符集
//        certAlipayRequest.setCharset("UTF-8");
//        //设置签名类型
//        certAlipayRequest.setSignType("RSA2");
//        //设置应用公钥证书路径
//        certAlipayRequest.setCertPath(app_cert_path);
//        //设置支付宝公钥证书路径
//        certAlipayRequest.setAlipayPublicCertPath(alipay_cert_path);
//        //设置支付宝根证书路径
//        certAlipayRequest.setRootCertPath(alipay_root_cert_path);
//        //构造client
//        AlipayClient alipayClient = null;
//        try {
//            alipayClient = new DefaultAlipayClient(certAlipayRequest);
//        } catch (AlipayApiException e) {
//            e.printStackTrace();
//        }
//        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
//        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest ();
//        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
//        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel ();
//        model.setBody(body);
//        model.setSubject (subject);
//        model.setOutTradeNo (outTradeNo);
//        model.setTimeoutExpress ("30m" );
//        model.setTotalAmount (amount);
//        model.setProductCode ( "QUICK_MSECURITY_PAY" );
//        model.setPassbackParams(passbackParams);//自定义参数
//        request.setBizModel ( model );
//        request.setNotifyUrl (callbackPath + notifyUrl);
//        try  {
//            //这里和普通的接口调用不同,使用的是sdkExecute
//            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
//            Map<String, String> map = new HashMap<>();
//            map.put("orderString", response.getBody());
//            System.out.println(map);//就是orderString 可以直接给客户端请求,无需再做处理。
//            return ResultUtil.success(map);
//        }  catch (AlipayApiException e ) {
//            e.printStackTrace();
//        }
 
 
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setBody(body);//对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body。
        model.setSubject(subject);//商品的标题/交易标题/订单标题/订单关键字等。
        model.setOutTradeNo(outTradeNo);//商户网站唯一订单号
        model.setTimeoutExpress("30m");
        model.setTotalAmount(amount);//付款金额
        model.setProductCode("QUICK_MSECURITY_PAY");
        model.setPassbackParams(passbackParams);//自定义参数
 
 
        //分账
//        Integer    coursePackagePayments = coursePackageClient.queryByCode(outTradeNo);
//        Integer paymentCompetitions = competitionsClient.queryByCode(outTradeNo);
//        Integer siteBookings = siteClient.queryByCode(outTradeNo);
//        List<Integer> stores = new ArrayList<>();
//        stores.add(coursePackagePayments);
//        stores.add(paymentCompetitions);
//        stores.add(siteBookings);
//
//        OperatorUser operatorUser = siteClient.queryOperator(stores);
//
//        String alipayProportion = operatorUser.getAlipayProportion();
//        String alipayNum = operatorUser.getAlipayNum();
//
//        ExtendParams extendParams = new ExtendParams();
////        extendParams.setSysServiceProviderId("YOUR_SERVICE_PROVIDER_ID");
//        model.setExtendParams(extendParams);
//
//        RoyaltyInfo royaltyInfo = new RoyaltyInfo();
////        royaltyInfo.setRoyaltyType("transfer");
//
//
//        RoyaltyDetailInfos royaltyDetailInfo1 = new RoyaltyDetailInfos();
//        royaltyDetailInfo1.setTransOutType("userId");
//        royaltyDetailInfo1.setTransOut(aliAppid);
//        royaltyDetailInfo1.setTransInType("loginName");
//        royaltyDetailInfo1.setTransIn("18398968484");
//
//        royaltyDetailInfo1.setDesc("分账描述1");
//        royaltyDetailInfo1.setAmountPercentage(alipayProportion);
//        List<RoyaltyDetailInfos> royaltyDetailInfos = new ArrayList<>();
//
//
//
//        royaltyInfo.setRoyaltyDetailInfos(royaltyDetailInfos);
//        model.setRoyaltyInfo(royaltyInfo);
//        System.err.println("=================="+royaltyInfo);
        //
        request.setBizModel(model);
        request.setNotifyUrl(callbackPath + notifyUrl);
        try {
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            Map<String, String> map = new HashMap<>();
            map.put("orderString", response.getBody());
            map.put("returnId", passbackParams);
            System.out.println(map);//就是orderString 可以直接给客户端请求,无需再做处理。
            return ResultUtil.success(map);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }
 
 
 
 
    /**
     * 支付宝查询订单支付状态
     *
     * @param out_trade_no
     * @return
     * @throws Exception
     */
    public AlipayTradeQueryResponse queryALIOrder(String out_trade_no) throws Exception {
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", aliAppid, appPrivateKey, "json", "UTF-8", alipay_public_key, "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizContent("{" +
                "  \"out_trade_no\":\"" + out_trade_no + "\"" +
                "}");
        AlipayTradeQueryResponse response = alipayClient.execute(request);
        return response;
 
    }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    /**
     * 获取请求内容
     *
     * @param request
     * @return
     * @throws IOException
     */
    private String getParam(HttpServletRequest request) throws IOException {
        // 读取参数
        InputStream inputStream;
        StringBuilder sb = new StringBuilder();
        inputStream = request.getInputStream();
        String s;
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        while ((s = in.readLine()) != null) {
            sb.append(s);
        }
        in.close();
        inputStream.close();
        return sb.toString();
    }
 
 
 
 
 
 
 
 
 
 
    /**
     * xml转map
     *
     * @param xml
     * @param charset
     * @return
     * @throws UnsupportedEncodingException
     * @throws DocumentException
     */
    public static Map<String, String> xmlToMap(String xml, String charset) throws UnsupportedEncodingException, DocumentException {
 
        Map<String, String> respMap = new HashMap<String, String>();
 
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new ByteArrayInputStream(xml.getBytes(charset)));
        Element root = doc.getRootElement();
        xmlToMap(root, respMap);
        return respMap;
    }
 
    public static Map<String, String> xmlToMap(Element tmpElement, Map<String, String> respMap) {
        if (tmpElement.isTextOnly()) {
            respMap.put(tmpElement.getName(), tmpElement.getText());
            return respMap;
        }
 
        @SuppressWarnings("unchecked")
        Iterator<Element> eItor = tmpElement.elementIterator();
        while (eItor.hasNext()) {
            Element element = eItor.next();
            xmlToMap(element, respMap);
        }
        return respMap;
    }
}