Air
2024-11-04 29c405353029f033e187e8dc033b93385365ee43
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
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
package com.nova.sankuai.service.impl;
 
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nova.sankuai.domain.api.baofu.BaofuCheckVo;
import com.nova.sankuai.domain.api.baofu.BaofuRecordPayInfo;
import com.nova.sankuai.domain.api.baofu.RecordDto;
import com.nova.sankuai.domain.api.yinsheng.enums.SignStatusEnum;
import com.nova.sankuai.domain.entity.*;
import com.nova.sankuai.domain.enums.vipservice.VipCardOrderEnum;
import com.nova.sankuai.domain.vo.VipEffectiveTimeVo;
import com.nova.sankuai.infra.config.CommonException;
import com.nova.sankuai.infra.mapper.CustomerMapper;
import com.nova.sankuai.infra.mapper.VipCardOrderMapper;
import com.nova.sankuai.infra.utils.UserUtil;
import com.nova.sankuai.infra.utils.baofu.FormatUtil;
import com.nova.sankuai.infra.utils.baofu.PayHttpUtil;
import com.nova.sankuai.infra.utils.baofu.SecurityUtil;
import com.nova.sankuai.infra.utils.baofu.rsa.RsaCodingUtil;
import com.nova.sankuai.infra.utils.baofu.rsa.SignatureUtils;
import com.nova.sankuai.security.UserDetail;
import com.nova.sankuai.service.IBaofuService;
import com.nova.sankuai.service.IBaofuSignRecordService;
import com.nova.sankuai.service.ICustomerUserService;
import com.nova.sankuai.service.IVipCardService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
 
@Service
@RequiredArgsConstructor
public class BaofuServiceImpl implements IBaofuService {
    @Value("${baofu.pfx}")
    private String pfxpath;
    @Value("${baofu.cer}")
    private String  cerpath;
    @Value("${baofu.aes-key}")
    private String AesKey;
    @Value("${baofu.version}")
    private String version;
    @Value("${baofu.url}")
    private String url;
    @Value("${baofu.terminal_id}")
    private String terminalId;
    @Value("${baofu.member_id}")
    private String memberId;
    @Value("${shantai.notificationDomain}")
    private String notiUrl;
 
//    private final CustomerUserMapper customerUserMapper;
    private final ICustomerUserService customerUserService;
    private final CustomerMapper customerMapper;
 
    private final VipCardOrderMapper vipCardOrderMapper;
 
    private final IVipCardService vipCardService;
 
    private final IBaofuSignRecordService baofuSignRecordService;
 
//    private final IVipCardOrderService vipCardOrderService;
 
    //私钥密码
    private static final String priKeyPass = "zBb3B8"; // "100025773_286941";
 
    private static final Logger log = LoggerFactory.getLogger("baofuLogger");
 
    @Resource
    private PayHttpUtil payHttpUtil;
 
    private BaofuRecordPayInfo recordPayInfo(String uniqueCode, String error) {
        BaofuRecordPayInfo recordPayInfo = new BaofuRecordPayInfo();
        recordPayInfo.setError(error);
        recordPayInfo.setUniqueCode(uniqueCode);
        return recordPayInfo;
    }
 
    private Map<String,String> paramImpl(Map<String,String> DateArry) {
        DateArry.put("terminal_id", terminalId);// 终端号
        DateArry.put("member_id", memberId);// 商户号,宝付提供给商户的唯一编号
        return DateArry;
    }
 
    @Override
    public BaofuCheckVo checkProtocolSignByBf(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        UserDetail user = UserUtil.getUser(token);
        if (user == null || user.getId() == null) {
            throw new CommonException("Token无效");
        }
        CustomerUser customerUser = customerUserService.getById(user.getId());
        if (customerUser == null) {
            throw new CommonException("用户不存在");
        }
        BaofuCheckVo checkVo = new BaofuCheckVo();
        BaofuSignRecord signRecord = null;
        List<BaofuSignRecord> signRecords = baofuSignRecordService.list(Wrappers.lambdaQuery(BaofuSignRecord.class).
                eq(BaofuSignRecord::getCustomerUserId, customerUser.getId()).orderByDesc(BaofuSignRecord::getCreationDate));
        if (!signRecords.isEmpty()) {
            signRecord = signRecords.get(0);
        }
        Customer customer = customerMapper.getLatestCustomerRecord(customerUser.getPhone());
        if (signRecord != null && SignStatusEnum.SUCCESS.getCode().equals(signRecord.getSignResult())) {
            checkVo.setSign(true);
            checkVo.setSignCardNumber(signRecord.getSignCardNumber());
            checkVo.setSignIdNo(signRecord.getSignIdNo());
            checkVo.setSignName(signRecord.getSignName());
            checkVo.setSignPhone(signRecord.getSignPhone());
        } else {
            checkVo.setSign(false);
            if (customer != null) {
                checkVo.setSignName(customer.getName());
                checkVo.setSignIdNo(customer.getIdCard());
            }
        }
        if (customer != null) {
            checkVo.setBankName(customer.getBankName());
            checkVo.setBankCode(customer.getBankCode());
        }
        return checkVo;
    }
 
    /**
     * 预绑卡
     * @param recordDto
     * @param userDetail
     * @return
     */
    @Override
    public BaofuRecordPayInfo startAuth(RecordDto recordDto, UserDetail userDetail) {
        CustomerUser userByPhone = getUser(userDetail.getPhone());
        BaofuSignRecord baofuSignRecordDb = null;
        List<BaofuSignRecord> signRecords = baofuSignRecordService.list(Wrappers.
                lambdaQuery(BaofuSignRecord.class).eq(BaofuSignRecord::getCustomerUserId, userByPhone.getId())
                .orderByDesc(BaofuSignRecord::getCreationDate));
        if (!signRecords.isEmpty()) {
            baofuSignRecordDb = signRecords.get(0);
        }
        if (baofuSignRecordDb != null
                && SignStatusEnum.SUCCESS.getCode().equals(baofuSignRecordDb.getSignResult())) {
            throw new CommonException("该用户已是宝付成功签约用户");
        }
 
        try {
            String signName = recordDto.getSignName();// 签约用户姓名
            String signCardNumber = FormatUtil.toStringTrim(recordDto.getSignCardNumber());// 签约银行卡号
            String signPhone = recordDto.getSignPhone();// 签约银行预留手机号
            String signIdNo = FormatUtil.toStringTrim(recordDto.getSignIdNo());// 签约身份证号
            String userId = String.valueOf(userByPhone.getId());
            if (StringUtils.isBlank(signName) || StringUtils.isBlank(signCardNumber)
                    || StringUtils.isBlank(signPhone) || StringUtils.isBlank(signIdNo) || StringUtils.isBlank(userId)) {
                return recordPayInfo(null,"宝付预绑卡参数不正确!");
            }
//            String Cardinfo = "6222022201002502849||460028197610126094|15289944674||";//账户信息[银行卡号|持卡人姓名|证件号|手机号|银行卡安全码|银行卡有效期]
            String Cardinfo = signCardNumber + "|" + signName + "|" + signIdNo + "|" + signPhone + "||";//账户信息[银行卡号|持卡人姓名|证件号|手机号|银行卡安全码|银行卡有效期]
            String send_time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//报文发送日期时间
//            String  pfxpath ="D:\\CER_EN_DECODE\\AgreementPay\\bfkey_100025773@@200001173.pfx";//商户私钥
//            String  cerpath = "D:\\CER_EN_DECODE\\AgreementPay\\bfkey_100025773@@200001173.cer";//宝付公钥
 
//            String AesKey = "4f66405c4f66405c";//商户自定义(可随机生成  商户自定义(AES key长度为=16位))
            String dgtl_envlp = "01|"+AesKey;//使用接收方的公钥加密后的对称密钥,并做Base64转码,明文01|对称密钥,01代表AES[密码商户自定义]
//            log.info("密码dgtl_envlp:"+dgtl_envlp);
            dgtl_envlp = RsaCodingUtil.encryptByPubCerFile(SecurityUtil.Base64Encode(dgtl_envlp), cerpath);//公钥加密
 
 
//            log.info("SHA-1摘要[Cardinfo]结果:"+SecurityUtil.sha1X16(Cardinfo, "UTF-8"));
//
//            log.info("卡信息:"+Cardinfo+",长度:"+Cardinfo.length());
            String outTradeNo = IdUtil.fastSimpleUUID().substring(0, 24);// 报文流水号
            Cardinfo = SecurityUtil.AesEncrypt(SecurityUtil.Base64Encode(Cardinfo), AesKey);//先BASE64后进行AES加密
            Map<String,String> DateArry = new TreeMap<String,String>();
            DateArry.put("send_time", send_time);
            DateArry.put("msg_id", outTradeNo);//报文流水号
            DateArry.put("version", version);
 
            DateArry.put("txn_type", "01");//交易类型
            DateArry.put("dgtl_envlp", dgtl_envlp);
            DateArry.put("user_id", userId);//用户在商户平台唯一ID
            DateArry.put("card_type", "101");//卡类型  101    借记卡,102 信用卡
            DateArry.put("id_card_type", "01");//证件类型
            DateArry.put("acc_info",Cardinfo);
 
            DateArry = paramImpl(DateArry);
 
            String SignVStr = FormatUtil.coverMap2String(DateArry);
//        log.info("SHA-1摘要字串:"+SignVStr);
            String signature = SecurityUtil.sha1X16(SignVStr, "UTF-8");//签名
 
//        log.info("SHA-1摘要结果:"+signature);
 
            String Sign = SignatureUtils.encryptByRSA(signature, pfxpath, priKeyPass);
//        log.info("RSA签名结果:"+Sign);
            DateArry.put("signature", Sign);//签名域
//            log.info("宝付支付预绑卡入参:{}", JSON.toJSONString(DateArry));
            String PostString  = payHttpUtil.RequestForm(url + "/cutpayment/protocol/backTransRequest", DateArry);
//            log.info("宝付支付预绑卡返回结果:{}", PostString);
 
            Map<String, String> ReturnData = FormatUtil.getParm(PostString);
 
            Map<String, String> returnDataBase = FormatUtil.getParm(PostString);// 当有异常时录入数据表的返回数据
            returnDataBase.remove("dgtl_envlp");
            returnDataBase.put("outTradeNo", outTradeNo);
 
            if(!ReturnData.containsKey("signature")){
                log.error("宝付支付预绑卡缺少验签参数:{}", PostString);
                payRecord(DateArry, "bp", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
 
                return recordPayInfo(null,"宝付预绑卡缺少验签参数");
            }
 
            String RSign=ReturnData.get("signature");
//        log.info("返回的验签值:"+RSign);
            ReturnData.remove("signature");//需要删除签名字段
            returnDataBase.remove("signature");
 
            String RSignVStr = FormatUtil.coverMap2String(ReturnData);
//        log.info("返回SHA-1摘要字串:"+RSignVStr);
            String RSignature = SecurityUtil.sha1X16(RSignVStr, "UTF-8");//签名
//        log.info("返回SHA-1摘要结果:"+RSignature);
 
            if(!SignatureUtils.verifySignature(cerpath,RSignature,RSign)){
                log.error("宝付支付预绑卡验签失败:{}", PostString);//验签成功
                payRecord(DateArry, "bp", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                return recordPayInfo(null,"宝付预绑卡验签失败");
            }
 
            if(!ReturnData.containsKey("resp_code")){
                log.error("宝付支付预绑卡缺少resp_code参数:{}", PostString);
                payRecord(DateArry, "bp", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                return recordPayInfo(null,"宝付预绑卡缺少resp_code参数");
            }
 
            if(ReturnData.get("resp_code").toString().equals("S")){
                if(!ReturnData.containsKey("dgtl_envlp")){
                    log.error("宝付支付预绑卡缺少dgtl_envlp参数:{}", PostString);
                    payRecord(DateArry, "bp", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                    return recordPayInfo(null,"宝付预绑卡缺少dgtl_envlp参数");
                }
                String RDgtlEnvlp = SecurityUtil.Base64Decode(RsaCodingUtil.decryptByPriPfxFile(ReturnData.get("dgtl_envlp"), pfxpath, priKeyPass));
//            log.info("返回数字信封:"+RDgtlEnvlp);
                String RAesKey=FormatUtil.getAesKey(RDgtlEnvlp);//获取返回的AESkey
//            log.info("返回的AESkey:"+RAesKey);
                String uniqueCode = SecurityUtil.Base64Decode(SecurityUtil.AesDecrypt(ReturnData.get("unique_code"),RAesKey));
                log.info("宝付支付预绑卡成功:{}", PostString);
                payRecord(DateArry, "bp", "宝付支付预绑卡成功", ReturnData.get("biz_resp_code"), 1, null);
 
                // 开始入库操作
                BaofuSignRecord baofuSignRecord;
                if (baofuSignRecordDb == null) {
                    baofuSignRecord = new BaofuSignRecord();
                    baofuSignRecord.setCustomerUserId(userByPhone.getId());
                    baofuSignRecord.setSignIdNo(signIdNo);
                    baofuSignRecord.setSignName(recordDto.getSignName());
                    baofuSignRecord.setSignPhone(recordDto.getSignPhone());
                    baofuSignRecord.setCreationDate(new Date());
                } else {
                    baofuSignRecord = baofuSignRecordDb;
                }
                baofuSignRecord.setSignResult(SignStatusEnum.WAIT_CONFIRM.getCode());
                baofuSignRecord.setLastUpdatedDate(new Date());
                baofuSignRecord.setSignCardNumber(signCardNumber);
 
                baofuSignRecordService.saveOrUpdate(baofuSignRecord);
 
                return recordPayInfo(uniqueCode,null);
            } else {
                log.info("宝付支付预绑卡失败:{}", PostString);
                payRecord(DateArry, "bp", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                return recordPayInfo(null,"宝付预绑卡失败!");
            }
        } catch(Exception ex) {
            log.error("宝付支付预绑卡异常:{}",ex);
            throw new CommonException("宝付支付预绑卡出现错误!");
        }
    }
 
    private void recordImpl(BaofuSignRecord baofuSignRecord, Boolean isSucc) {
        baofuSignRecord.setLastUpdatedDate(new Date());
        if (isSucc != null) {
            baofuSignRecord.setSignResult(isSucc?SignStatusEnum.SUCCESS.getCode():SignStatusEnum.FAILED.getCode());
        }
        baofuSignRecordService.saveOrUpdate(baofuSignRecord);
    }
 
    /**
     * 确认绑卡
     * @param recordDto
     * @param userDetail
     * @return
     */
    @Override
    public BaofuRecordPayInfo auth(RecordDto recordDto, UserDetail userDetail) {
        CustomerUser userByPhone = getUser(userDetail.getPhone());
        BaofuSignRecord baofuSignRecordDb = null;
        List<BaofuSignRecord> baofuRecords = baofuSignRecordService.list(Wrappers.
                lambdaQuery(BaofuSignRecord.class).eq(BaofuSignRecord::getCustomerUserId, userByPhone.getId()).
                orderByDesc(BaofuSignRecord::getCreationDate));
        if (!baofuRecords.isEmpty()) {
            baofuSignRecordDb = baofuRecords.get(0);
        }
        if (baofuSignRecordDb == null) {
            throw new CommonException("请先进行宝付预绑卡操作!");
        }
 
        try {
            String SMSStr = recordDto.getValidateCode();// 短信验证码
            String uniqueCodeStr = recordDto.getUniqueCode();// 预绑卡唯一码
 
            String userId = String.valueOf(userByPhone.getId());
            if (StringUtils.isBlank(userId)
                    || StringUtils.isBlank(uniqueCodeStr) || StringUtils.isBlank(SMSStr)) {
                return recordPayInfo(null,"宝付确认绑卡参数不正确!");
            }
            String send_time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//报文发送日期时间
//            String  pfxpath ="D:\\CER_EN_DECODE\\AgreementPay\\bfkey_100025773@@200001173.pfx";//商户私钥
//            String  cerpath = "D:\\CER_EN_DECODE\\AgreementPay\\bfkey_100025773@@200001173.cer";//宝付公钥
 
 
//            String SMSStr="123456";//短信验证码,测试环境随机6位数;生产环境验证码预绑卡成功后发到用户手机。确认绑卡时回传。
 
//            String AesKey = "4f66405c4f66405c";//商户自定义(可随机生成  商户自定义(AES key长度为=16位))
            String dgtl_envlp = "01|"+AesKey;//使用接收方的公钥加密后的对称密钥,并做Base64转码,明文01|对称密钥,01代表AES[密码商户自定义]
 
//            log.info("密码dgtl_envlp:"+dgtl_envlp);
            dgtl_envlp = RsaCodingUtil.encryptByPubCerFile(SecurityUtil.Base64Encode(dgtl_envlp), cerpath);//公钥加密
            String UniqueCode = uniqueCodeStr + "|" + SMSStr;//预支付唯一码(预绑卡返回的值)[格式:预签约唯一码|短信验证码]
//            log.info("预签约唯一码:"+UniqueCode);
            UniqueCode = SecurityUtil.AesEncrypt(SecurityUtil.Base64Encode(UniqueCode), AesKey);//先BASE64后进行AES加密
//            log.info("AES结果:"+UniqueCode);
            String outTradeNo = IdUtil.fastSimpleUUID().substring(0, 24);// 报文流水号
 
            Map<String,String> DateArry = new TreeMap<String,String>();
            DateArry.put("send_time", send_time);
            DateArry.put("msg_id", outTradeNo);//报文流水号
            DateArry.put("version", version);
            DateArry.put("txn_type", "02");//交易类型
            DateArry.put("dgtl_envlp", dgtl_envlp);
            DateArry.put("unique_code", UniqueCode);//预签约唯一码
 
            DateArry = paramImpl(DateArry);
 
            String SignVStr = FormatUtil.coverMap2String(DateArry);
//            log.info("SHA-1摘要字串:"+SignVStr);
            String signature = SecurityUtil.sha1X16(SignVStr, "UTF-8");//签名
//            log.info("SHA-1摘要结果:"+signature);
            String Sign = SignatureUtils.encryptByRSA(signature, pfxpath, priKeyPass);
//            log.info("RSA签名结果:"+Sign);
            DateArry.put("signature", Sign);//签名域
 
            String PostString  = payHttpUtil.RequestForm(url + "/cutpayment/protocol/backTransRequest", DateArry);
//            log.info("请求返回:"+PostString);
 
            Map<String, String> ReturnData = FormatUtil.getParm(PostString);
            Map<String, String> returnDataBase = FormatUtil.getParm(PostString);// 当有异常时录入数据表的返回数据
            returnDataBase.remove("dgtl_envlp");
            returnDataBase.put("outTradeNo", outTradeNo);
 
            if(!ReturnData.containsKey("signature")){
                log.error("宝付支付确认绑卡缺少验签参数:{}", PostString);
                payRecord(DateArry, "bc", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                recordImpl(baofuSignRecordDb, false);
                return recordPayInfo(null,"宝付确认绑卡缺少验签参数!");
            }
 
            String RSign=ReturnData.get("signature");
//        log.info("返回的验签值:"+RSign);
            ReturnData.remove("signature");//需要删除签名字段
            String RSignVStr = FormatUtil.coverMap2String(ReturnData);
//        log.info("返回SHA-1摘要字串:"+RSignVStr);
            String RSignature = SecurityUtil.sha1X16(RSignVStr, "UTF-8");//签名
//        log.info("返回SHA-1摘要结果:"+RSignature);
 
            if(!SignatureUtils.verifySignature(cerpath,RSignature,RSign)){
                log.error("宝付支付确认绑卡验签失败:{}", PostString);
                payRecord(DateArry, "bc", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                recordImpl(baofuSignRecordDb, false);
                return recordPayInfo(null,"宝付确认绑卡缺少验签参数!");
            }
            if(!ReturnData.containsKey("resp_code")){
                log.error("宝付支付确认绑卡缺少resp_code参数:{}", PostString);
                payRecord(DateArry, "bc", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                recordImpl(baofuSignRecordDb, false);
                return recordPayInfo(null,"宝付确认绑卡缺少resp_code参数!");
            }
//            log.info("protocol_no:" + ReturnData.get("protocol_no"));
            if(ReturnData.get("resp_code").toString().equals("S")){
                if(!ReturnData.containsKey("dgtl_envlp")){
                    log.error("宝付支付确认绑卡缺少dgtl_envlp参数:{}", PostString);
                    payRecord(DateArry, "bc", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                    recordImpl(baofuSignRecordDb, false);
                    return recordPayInfo(null,"宝付确认绑卡缺少dgtl_envlp参数!");
                }
                String RDgtlEnvlp = SecurityUtil.Base64Decode(RsaCodingUtil.decryptByPriPfxFile(ReturnData.get("dgtl_envlp"), pfxpath, priKeyPass));
//            log.info("返回数字信封:"+RDgtlEnvlp);
                String RAesKey=FormatUtil.getAesKey(RDgtlEnvlp);//获取返回的AESkey
//            log.info("返回的AESkey:"+RAesKey);
                String protocolNo = SecurityUtil.Base64Decode(SecurityUtil.AesDecrypt(ReturnData.get("protocol_no"),RAesKey));
                log.info("宝付支付确认绑卡成功:"+PostString);
 
                payRecord(DateArry, "bc", "宝付支付确认绑卡成功", ReturnData.get("biz_resp_code"), 1, userId);
                baofuSignRecordDb.setSignUniqueCode(protocolNo);
                recordImpl(baofuSignRecordDb, true);
                return recordPayInfo(protocolNo,null);
            }else if(ReturnData.get("resp_code").toString().equals("I")){
                log.info("宝付支付确认绑卡处理中:{}", PostString);
                payRecord(DateArry, "bc", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 3, userId);
                recordImpl(baofuSignRecordDb, null);
                return recordPayInfo("wait","宝付确认绑卡处理中!");
            }else if(ReturnData.get("resp_code").toString().equals("F")){
                log.error("宝付支付确认绑卡失败:{}", PostString);
                payRecord(DateArry, "bc", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                recordImpl(baofuSignRecordDb, false);
                return recordPayInfo(null,"宝付确认绑卡失败!");
            }else{
                //异常不得做为订单状态。
                log.error("宝付支付确认绑卡异常不得做为订单状态:{}", PostString);
                payRecord(DateArry, "bc", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                recordImpl(baofuSignRecordDb, false);
                return recordPayInfo(null,"宝付确认绑卡异常!");
            }
        } catch (Exception ex) {
            log.error("宝付支付确认绑卡异常:{}",ex);
            throw new CommonException("宝付支付确认绑卡出现错误!");
        }
    }
 
    /**
     * 预支付
     * @param recordDto
     * @param userDetail
     * @return
     */
    @Override
    public BaofuRecordPayInfo readyPay(RecordDto recordDto, UserDetail userDetail) {
        CustomerUser userByPhone = getUser(userDetail.getPhone());
        BaofuSignRecord baofuSignRecordDb = null;
        List<BaofuSignRecord> baofuRecords = baofuSignRecordService.list(Wrappers.
                lambdaQuery(BaofuSignRecord.class).eq(BaofuSignRecord::getCustomerUserId, userByPhone.getId()).
                orderByDesc(BaofuSignRecord::getCreationDate));
        if (!baofuRecords.isEmpty()) {
            baofuSignRecordDb = baofuRecords.get(0);
        }
        if (baofuSignRecordDb == null) {
            throw new CommonException("请先进行宝付绑卡操作!");
        }
 
        // 判断是否绑卡成功
        if (!SignStatusEnum.SUCCESS.getCode().equals(baofuSignRecordDb.getSignResult())) {
            throw new CommonException("请重新进行宝付绑卡操作!");
        }
 
        // 获致到确认绑卡的唯一码
        String uniqueCode = baofuSignRecordDb.getSignUniqueCode();
        if (StringUtils.isBlank(uniqueCode)) {
            throw new CommonException("请再次进行宝付绑卡操作!");
        }
 
        Customer customer = customerMapper.getLatestCustomerRecord(userByPhone.getPhone());
 
        try {
            String userId = String.valueOf(userByPhone.getId());
            if (StringUtils.isBlank(userId)) {
                return recordPayInfo(null,"宝付预支付参数不正确!");
            }
 
            String send_time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//报文发送日期时间
 
            String dgtl_envlp = "01|"+AesKey;//使用接收方的公钥加密后的对称密钥,并做Base64转码,明文01|对称密钥,01代表AES[密码商户自定义]
//        log.info("密码dgtl_envlp:"+dgtl_envlp);
            dgtl_envlp = RsaCodingUtil.encryptByPubCerFile(SecurityUtil.Base64Encode(dgtl_envlp), cerpath);//公钥加密
            String ProtocolNo = uniqueCode;//签约协议号(确认绑卡返回)
//            log.info("签约协议号:"+ProtocolNo);
            ProtocolNo = SecurityUtil.AesEncrypt(SecurityUtil.Base64Encode(ProtocolNo), AesKey);//先BASE64后进行AES加密
//        log.info("签约协议号AES结果:"+ProtocolNo);
 
            String ReturnUrl= notiUrl + "/api/v2/callback/bf/pushAuditCallback";
            String outTradeNo = recordDto.getPartnerThirdOrderId();
            //生成订单号,36位字符
//            String outTradeNo = ConstantsUtil.getOrder();// 商户订单号
            Map<String,String> DateArry = new TreeMap<String,String>();
            DateArry.put("send_time", send_time);
            DateArry.put("msg_id", recordDto.getOrderId());//报文流水号
            DateArry.put("version", version);
            DateArry.put("txn_type", "05");//交易类型(参看:文档中《交易类型枚举》)
            DateArry.put("trans_id", outTradeNo);// 商户订单号
            DateArry.put("dgtl_envlp", dgtl_envlp);
            DateArry.put("user_id", userId);//用户在商户平台唯一ID (和绑卡时要一致)
            DateArry.put("protocol_no", ProtocolNo);//签约协议号(密文)
            DateArry.put("txn_amt", String.valueOf(recordDto.getAmount()));//交易金额 [单位:分  例:1元则提交100],此处注意数据类型的转转,建议使用BigDecimal类弄进行转换为字串
 
 
            Map<String,String> RiskItem = new HashMap<String,String>();
            RiskItem.put("goodsCategory", "02");//行业类目 详见附录《行业类目》
            RiskItem.put("userLoginId", userDetail.getPhone());//用户在商户系统中的登陆名(手机号、邮箱等标识)
            RiskItem.put("userEmail", "");
            RiskItem.put("userMobile", userDetail.getPhone());//用户手机号
            RiskItem.put("registerUserName", userByPhone.getName());//用户在商户系统中注册使用的名字
            RiskItem.put("identifyState", "1");//用户在平台是否已实名,1:是 ;0:不是
            RiskItem.put("userIdNo", baofuSignRecordDb.getSignIdNo());//用户身份证号
            RiskItem.put("registerTime", new SimpleDateFormat("YYYYMMDDHHMMSS").format(userByPhone.getCreationDate()));//格式为:YYYYMMDDHHMMSS
            RiskItem.put("registerIp", customer.getClientIp());//用户在商户端注册时留存的IP
            RiskItem.put("chName", baofuSignRecordDb.getSignName());//持卡人姓名
            RiskItem.put("chIdNo", baofuSignRecordDb.getSignIdNo());//持卡人身份证号
            RiskItem.put("chCardNo", baofuSignRecordDb.getSignCardNumber());//持卡人银行卡号
            RiskItem.put("chMobile", baofuSignRecordDb.getSignPhone());//持卡人手机
            RiskItem.put("chPayIp", customer.getClientIp());//持卡人支付IP
            RiskItem.put("deviceOrderNo", "");//加载设备指纹中的订单号
 
            DateArry.put("risk_item", JSONArray.toJSON(RiskItem).toString());//放入风控参数
            DateArry.put("return_url", ReturnUrl);//最多填写三个地址,不同地址用间使用‘|’分隔
 
            DateArry = paramImpl(DateArry);
 
            String SignVStr = FormatUtil.coverMap2String(DateArry);
//         log.info("SHA-1摘要字串:"+SignVStr);
            String signature = SecurityUtil.sha1X16(SignVStr, "UTF-8");//签名
//         log.info("SHA-1摘要结果:"+signature);
            String Sign = SignatureUtils.encryptByRSA(signature, pfxpath, priKeyPass);
//         log.info("RSA签名结果:"+Sign);
            DateArry.put("signature", Sign);//签名域
 
            // 开始创建订单
 
            String PostString  = payHttpUtil.RequestForm(url + "/cutpayment/protocol/backTransRequest", DateArry);
//            log.info("请求返回:"+PostString);
 
            Map<String, String> ReturnData = FormatUtil.getParm(PostString);
            Map<String, String> returnDataBase = FormatUtil.getParm(PostString);// 当有异常时录入数据表的返回数据
            returnDataBase.remove("dgtl_envlp");
            returnDataBase.put("outTradeNo", outTradeNo);
 
            if(!ReturnData.containsKey("signature")){
                log.error("宝付支付预支付缺少验签参数:{}", PostString);
                payRecord(DateArry, "bs", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                return recordPayInfo(null,"宝付预支付缺少验签参数!");
            }
            String RSign=ReturnData.get("signature");
//         log.info("返回的验签值:"+RSign);
            ReturnData.remove("signature");//需要删除签名字段
            String RSignVStr = FormatUtil.coverMap2String(ReturnData);
//         log.info("返回SHA-1摘要字串:"+RSignVStr);
            String RSignature = SecurityUtil.sha1X16(RSignVStr, "UTF-8");//签名
//         log.info("返回SHA-1摘要结果:"+RSignature);
 
            if(!SignatureUtils.verifySignature(cerpath,RSignature,RSign)){
                log.error("宝付支付预支付验签失败:{}", PostString);
                payRecord(DateArry, "bs", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                return recordPayInfo(null,"宝付预支付验签失败!");
            }
            if(!ReturnData.containsKey("resp_code")){
                log.error("宝付支付预支付缺少resp_code参数:{}", PostString);
                payRecord(DateArry, "bs", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                return recordPayInfo(null,"宝付预支付缺少resp_code参数!");
            }
            if(ReturnData.get("resp_code").toString().equals("S")){
                if(!ReturnData.containsKey("dgtl_envlp")){
                    log.error("宝付支付预支付缺少dgtl_envlp参数:{}", PostString);
                    payRecord(DateArry, "bs", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                    return recordPayInfo(null,"宝付预支付缺少dgtl_envlp参数!");
                }
                if(!ReturnData.containsKey("unique_code")){
//                    throw new Exception("缺少unique_code参数!");
                    log.error("宝付支付预支付缺少unique_code参数:{}", PostString);
                    payRecord(DateArry, "bs", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                    return recordPayInfo(null,"宝付预支付缺少unique_code参数!");
                }
                String RDgtlEnvlp = SecurityUtil.Base64Decode(RsaCodingUtil.decryptByPriPfxFile(ReturnData.get("dgtl_envlp"), pfxpath, priKeyPass));
//                log.info("返回数字信封:"+RDgtlEnvlp);
                String RAesKey=FormatUtil.getAesKey(RDgtlEnvlp);//获取返回的AESkey
//                log.info("返回的AESkey:"+RAesKey);
                String returnUniqueCode = SecurityUtil.Base64Decode(SecurityUtil.AesDecrypt(ReturnData.get("unique_code"),RAesKey));
                log.info("宝付支付预支付成功:"+PostString);
                payRecord(DateArry, "bs", "宝付支付预支付成功", ReturnData.get("biz_resp_code"), 1, null);
                return recordPayInfo(returnUniqueCode,null);
            }else{
                log.error("宝付支付预支付失败:{}", PostString);
                payRecord(DateArry, "bs", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, null);
                return recordPayInfo(null,"宝付预支付失败!");
            }
        } catch (Exception ex) {
            log.error("宝付支付预支付失败:{}", ex);
            throw new CommonException("宝付预支付失败!");
        }
    }
 
    private void vipCardOrderImpl(VipCardOrder vipCardOrder) {
        vipCardOrderMapper.update(null, Wrappers.<VipCardOrder>lambdaUpdate()
                .set(VipCardOrder::getStatus, vipCardOrder.getStatus())
                .eq(VipCardOrder::getId, vipCardOrder.getId()));
    }
 
    public String callBack(HttpServletRequest request) {
        String result = "OK";
        try {
            request.setCharacterEncoding("utf-8");//utf-8编码接收
            Map<String,String> RDateArry = new TreeMap<String,String>();
 
            Enumeration<String> e = request.getParameterNames();
            while(e.hasMoreElements()) {
                String paraStr = e.nextElement();
                if(paraStr != null){
                    RDateArry.put(paraStr, request.getParameter(paraStr));//接收参数,(使用TreeMap自动排序)
                }
            }
 
            log.info("宝付支付回调返回参数:{}", JSONArray.toJSONString(RDateArry));
 
            String transId = RDateArry.get("trans_id");// 获取订单号
            if (StringUtils.isBlank(transId)) {
                log.error("宝付支付回调缺少订单号:{}", JSONArray.toJSON(RDateArry));
                return "NO";
            }
 
            // 根据订单号获取userId
            VipCardOrder vipCardOrder = vipCardOrderMapper.selectOne(new LambdaQueryWrapper<VipCardOrder>()
                    .eq(VipCardOrder::getPartnerThirdOrderId, transId));
            if (vipCardOrder == null) {
                log.error("宝付支付回调数据表中没有相关数据:{}", JSONArray.toJSON(RDateArry));
                return "NO";
            }
 
            if (VipCardOrderEnum.STATUS_END.getCode().equals(vipCardOrder.getStatus())) {// 已成功了
                log.info("宝付支付回调已成功过:{},返回参数:{}", vipCardOrder, JSONArray.toJSON(RDateArry));
                return "OK";
            }
 
            String userId = String.valueOf(vipCardOrder.getUserId());
 
            if(!RDateArry.containsKey("signature")){
//                throw new Exception("缺少验签参数!");
                log.error("宝付支付回调缺少验签参数:{}", JSONArray.toJSON(RDateArry));
                payRecord(RDateArry, "be", RDateArry.get("biz_resp_msg"), RDateArry.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                result = "NO";
                return result;
            }
 
            String RSign=RDateArry.get("signature");
            RDateArry.remove("signature");//需要删除签名字段
            String RSignVStr = FormatUtil.coverMap2String(RDateArry);
            String RSignature = SecurityUtil.sha1X16(RSignVStr, "UTF-8");//签名
 
            if(!SignatureUtils.verifySignature(cerpath,RSignature,RSign)){
//                log.info("Yes");//验签失败
                log.error("宝付支付回调验签失败:{}", JSONArray.toJSON(RDateArry));
                payRecord(RDateArry, "be", RDateArry.get("biz_resp_msg"), RDateArry.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                result = "NO";
                return result;
            }
            if(!RDateArry.containsKey("resp_code")){
//                throw new Exception("缺少resp_code参数!");
                log.error("宝付支付回调缺少resp_code参数:{}", JSONArray.toJSON(RDateArry));
                payRecord(RDateArry, "be", RDateArry.get("biz_resp_msg"), RDateArry.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                result = "NO";
                return result;
            }
            if(RDateArry.get("resp_code").toString().equals("S")){
 
                VipCard card = vipCardService.getOne(Wrappers.<VipCard>lambdaQuery()
                        .eq(VipCard::getCardId, vipCardOrder.getCardId()).last(" limit 1 "));
 
                if (card == null) {
                    payRecord(RDateArry, "be", "对应会员卡不存在", "bf00003", 2, userId);
                    log.error("宝付支付回调失败,对应会员卡不存在,原始数据:" + vipCardOrder);
                    result = "NO";
                    return result;
//                                throw new CommonException("对应会员卡不存在");
                }
                log.info("宝付支付回调成功:[trans_id:"+RDateArry.get("trans_id")+"]");
                payRecord(RDateArry, "br", "宝付支付回调成功", RDateArry.get("biz_resp_code"), 1, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_END.getCode());
                vipCardOrderImpl(vipCardOrder);
                VipEffectiveTimeVo vipEffectiveTimeVo = customerUserService.calculateExpiredDate(vipCardOrder.getUserId(), card.getValidDate(), null, null);
                customerUserService.updateVipEffectiveTime(vipCardOrder, vipEffectiveTimeVo);
            }else if(RDateArry.get("resp_code").toString().equals("I")){
//                log.info("订单处理中!");
                log.info("宝付支付回调处理中:{}", JSONArray.toJSON(RDateArry));
                payRecord(RDateArry, "be", RDateArry.get("biz_resp_msg"), RDateArry.get("biz_resp_code"), 3, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                result = "NO";
            }else if(RDateArry.get("resp_code").toString().equals("F")){
//                log.info("订单失败!");
                log.error("宝付支付回调失败:{}", JSONArray.toJSON(RDateArry));
                payRecord(RDateArry, "be", RDateArry.get("biz_resp_msg"), RDateArry.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                result = "NO";
            }else{
//                throw new Exception("反回异常!");//异常不得做为订单状态。
                //异常不得做为订单状态。
                log.error("宝付支付回调不得做为订单状态:{}", JSONArray.toJSON(RDateArry));
                payRecord(RDateArry, "be", RDateArry.get("biz_resp_msg"), RDateArry.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                result = "NO";
            }
        } catch (Exception ex) {
            log.error("宝付支付回调出现异常:{}", ex);
            result = "NO";
        }
        return result;
    }
 
    /**
     * 确认支付
     * @param recordDto
     * @param userDetail
     * @return
     */
    @Override
    public BaofuRecordPayInfo confirmPay(RecordDto recordDto, UserDetail userDetail) {
        CustomerUser userByPhone = getUser(userDetail.getPhone());
        if (StringUtils.isBlank(recordDto.getPartnerThirdOrderId())) {
            throw new CommonException("订单号不能为空!");
        }
        VipCardOrder vipCardOrder = vipCardOrderMapper.selectOne(new LambdaQueryWrapper<VipCardOrder>()
                .eq(VipCardOrder::getPartnerThirdOrderId, recordDto.getPartnerThirdOrderId()));
        if (vipCardOrder == null) {
            throw new CommonException("订单不存在!");
        }
        VipCard card = vipCardService.getOne(new LambdaQueryWrapper<VipCard>()
                .eq(VipCard::getCardId, vipCardOrder.getCardId()));
        if (card == null) {
            throw new CommonException("不存在此类会员卡!");
        }
 
        try {
            String userId = String.valueOf(userByPhone.getId());
            String SMSStr = recordDto.getValidateCode();
            String uniqueCodeStr = recordDto.getUniqueCode();
            if (StringUtils.isBlank(userId) || StringUtils.isBlank(SMSStr) || StringUtils.isBlank(uniqueCodeStr)) {
                return recordPayInfo(null,"宝付确认支付参数不正确!");
            }
 
            String send_time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//报文发送日期时间
 
//            String SMSStr="123456";//短信验证码,测试环境随机6位数;生产环境验证码预绑卡成功后发到用户手机。确认绑卡时回传。
 
            String dgtl_envlp = "01|"+AesKey;//使用接收方的公钥加密后的对称密钥,并做Base64转码,明文01|对称密钥,01代表AES[密码商户自定义]
 
            dgtl_envlp = RsaCodingUtil.encryptByPubCerFile(SecurityUtil.Base64Encode(dgtl_envlp), cerpath);//公钥加密
            String UniqueCode = uniqueCodeStr + "|"+SMSStr;//预支付唯一码(预支付返回的值)[格式:预支付一码|短信验证码]
            UniqueCode = SecurityUtil.AesEncrypt(SecurityUtil.Base64Encode(UniqueCode), AesKey);//先BASE64后进行AES加密
 
            String CardInfo="";//信用卡(格式):信用卡有效期|安全码;借记卡:传空
 
            //暂不支持信用卡
            //CardInfo = SecurityUtil.AesEncrypt(SecurityUtil.Base64Encode(CardInfo), AesKey);//先BASE64后进行AES加密
 
            Map<String,String> DateArry = new TreeMap<String,String>();
            DateArry.put("send_time", send_time);
            DateArry.put("msg_id", "TISN"+System.currentTimeMillis());//报文流水号
            DateArry.put("version", version);
            DateArry.put("txn_type", "06");//交易类型
            DateArry.put("dgtl_envlp", dgtl_envlp);
            DateArry.put("unique_code", UniqueCode);//预支付唯一码
            DateArry.put("card_info", CardInfo);//卡信息
 
            DateArry = paramImpl(DateArry);
 
            String SignVStr = FormatUtil.coverMap2String(DateArry);
            String signature = SecurityUtil.sha1X16(SignVStr, "UTF-8");//签名
            String Sign = SignatureUtils.encryptByRSA(signature, pfxpath, priKeyPass);
            DateArry.put("signature", Sign);//签名域
 
            String PostString  = payHttpUtil.RequestForm(url + "/cutpayment/protocol/backTransRequest", DateArry);
 
            Map<String, String> ReturnData = FormatUtil.getParm(PostString);
 
            if(!ReturnData.containsKey("signature")){
                log.error("宝付支付确认支付缺少验签参数:{}", PostString);
                payRecord(DateArry, "bf", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                return recordPayInfo(null,"宝付确认支付缺少验签参数!");
            }
            String RSign=ReturnData.get("signature");
//         log.info("返回的验签值:"+RSign);
            ReturnData.remove("signature");//需要删除签名字段
            String RSignVStr = FormatUtil.coverMap2String(ReturnData);
//         log.info("返回SHA-1摘要字串:"+RSignVStr);
            String RSignature = SecurityUtil.sha1X16(RSignVStr, "UTF-8");//签名
//         log.info("返回SHA-1摘要结果:"+RSignature);
            DateArry.put("trans_id", vipCardOrder.getPartnerThirdOrderId());// 订单号
 
            if(!SignatureUtils.verifySignature(cerpath,RSignature,RSign)){
                log.error("宝付支付确认支付验签失败:{}", PostString);
                payRecord(DateArry, "bf", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                return recordPayInfo(null,"宝付确认支付验签失败!");
            }
            if(!ReturnData.containsKey("resp_code")){
                log.error("宝付支付确认绑卡缺少resp_code参数:{}", PostString);
                payRecord(DateArry, "bf", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                return recordPayInfo(null,"宝付确认绑卡缺少resp_code参数!");
            }
            if(ReturnData.get("resp_code").toString().equals("S")){
                log.info("宝付支付确认支付成功:{}", PostString);
                payRecord(DateArry, "bf", "宝付支付确认支付成功", ReturnData.get("biz_resp_code"), 1, userId);
                vipCardOrder.setOrderId(ReturnData.get("order_id"));
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_END.getCode());
                vipCardOrderImpl(vipCardOrder);
                VipEffectiveTimeVo vipEffectiveTimeVo = customerUserService.calculateExpiredDate(vipCardOrder.getUserId(), card.getValidDate(), null, null);
                customerUserService.updateVipEffectiveTime(vipCardOrder, vipEffectiveTimeVo);
                return recordPayInfo("ok",null);
            }else if(ReturnData.get("resp_code").toString().equals("I")){
                log.info("宝付支付确认支付处理中:{}", PostString);
                payRecord(DateArry, "bf", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 3, userId);
                vipCardOrder.setOrderId(ReturnData.get("order_id"));
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_WAIT.getCode());
                vipCardOrderImpl(vipCardOrder);
                return recordPayInfo("wait",null);
            }else if(ReturnData.get("resp_code").toString().equals("F")){
                log.error("宝付支付确认支付失败:{}", PostString);
                payRecord(DateArry, "bf", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                return recordPayInfo(null,"宝付支付确认支付失败");
            }else{
                //异常不得做为订单状态。
                log.error("宝付支付确认支付异常不得做为订单状态:{}", PostString);
                payRecord(DateArry, "bf", ReturnData.get("biz_resp_msg"), ReturnData.get("biz_resp_code"), 2, userId);
                vipCardOrder.setStatus(VipCardOrderEnum.STATUS_INVALID.getCode());
                vipCardOrderImpl(vipCardOrder);
                return recordPayInfo(null,"宝付支付确认支付失败!");
            }
        } catch (Exception ex) {
            log.error("宝付支付确认支付异常:{}",ex);
            throw new CommonException("宝付支付确认支付异常!");
        }
    }
 
    private CustomerUser getUser(String phone) {
        LambdaQueryWrapper<CustomerUser> customerUserWrapper = new LambdaQueryWrapper<>();
        customerUserWrapper.eq(CustomerUser::getPhone, phone).eq(CustomerUser::getDeleteFlag, 0);
        CustomerUser customerUser = customerUserService.getOne(customerUserWrapper);
        if (customerUser == null) {
            throw new CommonException("用户不存在");
        }
        return customerUser;
    }
 
    /**
     * 支付前用户校验
     *
     * @param userId
     * @return
     */
    public boolean checkBeforePayment(Long userId) {
        BaofuSignRecord signRecord = null;
        List<BaofuSignRecord> signRecords = baofuSignRecordService.list(Wrappers.
                lambdaQuery(BaofuSignRecord.class).eq(BaofuSignRecord::getCustomerUserId, userId)
                .orderByDesc(BaofuSignRecord::getCreationDate));
        if (!signRecords.isEmpty()) {
            signRecord = signRecords.get(0);
        }
        return signRecord != null && SignStatusEnum.SUCCESS.getCode().equals(signRecord.getSignResult());
    }
 
    private void payRecord(Map<String,String> Parms, String recordType, String erroMsg,String code,Integer payStatus, String userId) {
        userId = userId == null ? Parms.get("user_id") : userId;
        if (userId != null) {
            PayRecord payRecord = new PayRecord();
            LocalDateTime payDate = LocalDateTime.now();
            payRecord.setPayDate(payDate);
            payRecord.setUserId(Long.parseLong(userId));
            payRecord.setPayType(recordType);
            payRecord.setPayStatus(payStatus == null ? 3 : payStatus);
            payRecord.setPayError(erroMsg);
            payRecord.setPayErrorCode(code == null ? "bf00002" : code);
            payRecord.setOutTradeNo(Parms.get("trans_id")==null?Parms.get("msg_id"):Parms.get("trans_id"));
 
            payHttpUtil.payRecordImpl(payRecord);
        }
    }
}