| | |
| | | package com.gkhy.exam.pay.service.impl; |
| | | |
| | | import cn.com.jit.new_vstk.Bean.EnvelopResult; |
| | | import cn.com.jit.new_vstk.Bean.SignResult; |
| | | import cn.com.jit.new_vstk.Bean.VerifyResult; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.gkhy.exam.pay.dto.rep.CoalPayRepDto; |
| | | import com.gkhy.exam.pay.dto.rep.CoalPayStudentRep; |
| | | import com.gkhy.exam.pay.dto.req.CoalPayDto; |
| | | import com.gkhy.exam.pay.dto.req.CoalPayReq; |
| | | import com.gkhy.exam.pay.dto.req.CoalPayStudentReq; |
| | | import com.gkhy.exam.pay.dto.req.CoalPayTypeReq; |
| | | import com.gkhy.exam.pay.dto.req.*; |
| | | import com.gkhy.exam.pay.entity.*; |
| | | import com.gkhy.exam.pay.mapper.CoalCategoryMapper; |
| | | import com.gkhy.exam.pay.mapper.CoalPayCategoryMapper; |
| | |
| | | import com.gkhy.exam.pay.service.CoalPayStudentService; |
| | | import com.gkhy.exam.pay.utils.PayUtils; |
| | | import com.gkhy.exam.pay.utils.ResultVo; |
| | | import com.gkhy.exam.pay.utils.Sign; |
| | | import com.gkhy.exam.pay.utils.SignDto; |
| | | import com.ruoyi.common.constant.ResultConstants; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | |
| | | import com.ruoyi.common.utils.SecurityUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.system.mapper.SysDeptMapper; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.dom4j.Element; |
| | | import org.redisson.api.RLock; |
| | | import org.redisson.api.RedissonClient; |
| | | import org.springframework.beans.BeanUtils; |
| | |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.IOException; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.math.BigDecimal; |
| | | import java.util.*; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.stream.Collectors; |
| | | |
| | | @Service |
| | | @Slf4j |
| | | public class CoalPayServiceImpl extends ServiceImpl<CoalPayMapper, CoalPay> implements CoalPayService { |
| | | |
| | | @Resource |
| | |
| | | //学员数据 |
| | | List<CoalPayStudent> coalPayStudents = coalPayStudentService.selectByCoalPayId(pay.getId()); |
| | | List<CoalPayStudent> havePay = coalPayStudents.stream() |
| | | .filter(stu -> stu.getPayStatus() != null && stu.getPayStatus().equals(1)) |
| | | .filter(stu -> stu.getPayStatus() != null && stu.getPayStatus()==1) |
| | | .collect(Collectors.toList()); |
| | | coalPayRepDto.setTotalNum(coalPayStudents.size()); |
| | | coalPayRepDto.setHavePayNum(havePay.size()); |
| | |
| | | for (CoalPayStudent payStudent : coalPayStudents) { |
| | | //封装学生基础信息 |
| | | CoalPayStudentRep coalPayStudentRep = new CoalPayStudentRep(); |
| | | coalPayStudentRep.setId(payStudent.getId()); |
| | | coalPayStudentRep.setName(payStudent.getName()); |
| | | coalPayStudentRep.setIdCard(payStudent.getIdCard()); |
| | | coalPayStudentRep.setPhone(payStudent.getPhone()); |
| | | coalPayStudentRep.setSex(payStudent.getSex()); |
| | | coalPayStudentRep.setPayType(payStudent.getPayType()); |
| | | coalPayStudentRep.setPayCode(payStudent.getPayCode()); |
| | | coalPayStudentRep.setPayStatus(payStudent.getPayStatus()); |
| | | |
| | | //查询对应批次以及批次包含工种类别 |
| | |
| | | if (coalPayTypeReq.getPayPersonType() != null && byId.getPayPersonType() == 2) { |
| | | throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "已为团体缴费,不可更改"); |
| | | } |
| | | return coalPayMapper.updateByPayId(coalPayTypeReq); |
| | | int i = coalPayMapper.updateByPayId(coalPayTypeReq); |
| | | if (i>0){ |
| | | CoalPayStudent coalPayStudent = new CoalPayStudent(); |
| | | coalPayStudent.setCoalPayId(coalPayTypeReq.getCoalPayId()); |
| | | coalPayStudent.setPayType(2); |
| | | coalPayStudentService.updateByCoalPayId(coalPayStudent); |
| | | } |
| | | return i; |
| | | } |
| | | |
| | | @Override |
| | | public String personPayMoney(Long coalPayId, Long studentId) { |
| | | CoalPayStudent coalPayStudent = coalPayStudentService.selectbyId(studentId); |
| | | CoalPay coalPay = coalPayMapper.selectById(coalPayId); |
| | | List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(coalPayId); |
| | | CoalTicket coalTicket = coalCategoryMapper.selectCoalTicket(); |
| | | |
| | | PayReqData payReqData = new PayReqData(); |
| | | payReqData.setOrderNo(RandomUtil.generateOrderNumber(coalPayId, "CO")); |
| | | payReqData.setMoney(coalPay.getAmount()); |
| | | payReqData.setAmount(1); |
| | | payReqData.setPayerName(coalPayStudent.getName()); |
| | | payReqData.setCertNo(coalPayStudent.getIdCard()); |
| | | payReqData.setPayerType(1); |
| | | payReqData.setInvoiceSocialCode(coalTicket.getTicketCompanyCode()); |
| | | payReqData.setHandlingPerson(coalTicket.getDrawer()); |
| | | payReqData.setChecker(coalTicket.getCheck()); |
| | | payReqData.setEnterCode(coalTicket.getCompanyCode()); |
| | | payReqData.setDesc(coalPay.getBatchName()); |
| | | |
| | | PayReqData.Feedata feedata = new PayReqData.Feedata(); |
| | | List<PayReqData.Feedata> feedatas = new ArrayList<>(); |
| | | for (CoalCategory coalCategory : coalCategories) { |
| | | feedata.setAmount(1); |
| | | feedata.setPrice(coalCategory.getAmount()); |
| | | feedata.setBusCode(coalCategory.getBusinessCode()); |
| | | feedatas.add(feedata); |
| | | } |
| | | payReqData.setFeeDatas(feedatas); |
| | | |
| | | CoalPayStudent payStudent = new CoalPayStudent(); |
| | | |
| | | RLock lock = redissonClient.getLock("SWSPKMAS_PAY_PERSON_" + coalPayId); |
| | | RLock lock = redissonClient.getLock("SWSPKMAS_PAY_PERSON_" + studentId); |
| | | try { |
| | | lock.lock(10, TimeUnit.SECONDS); |
| | | if (coalPayStudent.getGovPayStatus() != 0) { |
| | | throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "请勿重复发起支付"); |
| | | } |
| | | if (StringUtils.isEmpty(coalPayStudent.getPayCode())) { |
| | | |
| | | List<CoalPayStudent> coalPayStudent = coalPayStudentService.selectbyId(studentId); |
| | | CoalPayStudent student = coalPayStudent.get(0); |
| | | CoalPay coalPay = coalPayMapper.selectById(coalPayId); |
| | | List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(coalPayId); |
| | | CoalTicket coalTicket = coalCategoryMapper.selectCoalTicket(); |
| | | if (CollectionUtils.isEmpty(coalPayStudent)){ |
| | | throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"缴费学员不存在"); |
| | | } |
| | | if (coalPay==null){ |
| | | throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"未找到相关缴费信息"); |
| | | } |
| | | if (student.getPayStatus()==1){ |
| | | throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"请勿重新缴费"); |
| | | } |
| | | |
| | | PayReqData payReqData = fillData(coalPayStudent,coalPay,coalCategories,coalTicket,1,null); |
| | | |
| | | ResultVo resultVo = payUtils.sendApiPost(payReqData); |
| | | if (resultVo.getRespcode().equals("BUS0000")) { |
| | | //进行票据签名并校验 |
| | | // ResultVo resultVo1 = payUtils.uploadXml(resultVo.getRespdata().getOrderNo(), resultVo.getRespdata().getFileData()); |
| | | // if (!resultVo1.getRespcode().equals("BUS0000")){ |
| | | // throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "签名验证错误"); |
| | | // } |
| | | payStudent.setId(studentId); |
| | | payStudent.setOrderId(resultVo.getRespdata().getOrderNo()); |
| | | payStudent.setGovPayStatus(1); |
| | | payStudent.setPayCode(resultVo.getRespdata().getBillNo()); |
| | | coalPayStudentService.updateByCoalPayStudent(payStudent); |
| | | return resultVo.getRespdata().getBillNo(); |
| | | return resultVo.getRespdata().getOrderId(); |
| | | } else { |
| | | throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "发起支付失败,请稍后重试"); |
| | | throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, resultVo.getRespmsg()); |
| | | } |
| | | } catch (BusinessException e) { |
| | | throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "请稍后重试"); |
| | | throw new RuntimeException(e); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } finally { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public String teamPayMoney(CoalTeamPayReq coalTeamPayReq) { |
| | | RLock lock = redissonClient.getLock("SWSPKMAS_TEAM_PAY_" + coalTeamPayReq.getCoalPayId()); |
| | | try { |
| | | lock.lock(10,TimeUnit.SECONDS); |
| | | List<CoalPayStudent> coalPayStudents = coalPayStudentService.selectByCoalPayIdAndPayStatus(coalTeamPayReq.getCoalPayId(), 0); |
| | | CoalPay coalPay = coalPayMapper.selectById(coalTeamPayReq.getCoalPayId()); |
| | | List<CoalCategory> coalCategories = coalCategoryMapper.selectByCoalPayId(coalTeamPayReq.getCoalPayId()); |
| | | CoalTicket coalTicket = coalCategoryMapper.selectCoalTicket(); |
| | | if (CollectionUtils.isEmpty(coalPayStudents)){ |
| | | throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"缴费学员不存在"); |
| | | } |
| | | if (coalPay==null){ |
| | | throw new BusinessException(this.getClass(),ResultConstants.BUSINESS_ERROR,"未找到相关缴费信息"); |
| | | } |
| | | |
| | | CoalPayStudent payStudent = new CoalPayStudent(); |
| | | PayReqData payReqData = fillData(coalPayStudents, coalPay, coalCategories, coalTicket,2,coalTeamPayReq); |
| | | ResultVo resultVo = payUtils.sendApiPost(payReqData); |
| | | if (resultVo.getRespcode().equals("BUS0000")) { |
| | | payStudent.setCoalPayId(coalPay.getId()); |
| | | payStudent.setOrderId(resultVo.getRespdata().getOrderNo()); |
| | | payStudent.setGovPayStatus(1); |
| | | payStudent.setPayCode(resultVo.getRespdata().getBillNo()); |
| | | coalPayStudentService.updateByCoalPayIdAndStatus(payStudent); |
| | | return resultVo.getRespdata().getOrderId(); |
| | | } else { |
| | | throw new BusinessException(this.getClass(), ResultConstants.BUSINESS_ERROR, "发起支付失败,请稍后重试"); |
| | | } |
| | | }catch (Exception e){ |
| | | throw new RuntimeException(e); |
| | | }finally { |
| | | if (lock.isLocked()) { |
| | | lock.unlock(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public SignResult topay() throws IOException { |
| | | PayReqData payReqData = new PayReqData(); |
| | | PayReqData.Feedata feedatas = new PayReqData.Feedata(); |
| | | |
| | | |
| | | //订单编号 |
| | | payReqData.setOrderNo(RandomUtil.generateOrderNumber(1L, "CO")); |
| | | //订单总金额 |
| | | payReqData.setMoney(BigDecimal.valueOf(56)); |
| | | //子订单数目 |
| | | payReqData.setAmount(1); |
| | | //缴费人姓名(单位填单位名称)阿克苏地区博安煤矿安全技术服务中心 |
| | | payReqData.setPayerName("阿克苏地区博安煤矿安全技术服务中心"); |
| | | //缴费人证件号(单位填同一信用代码)52652900789893140A |
| | | payReqData.setCertNo("52652900789893140A"); |
| | | //缴款人类型(1个人 2单位) |
| | | payReqData.setPayerType(2); |
| | | //开票单位社会信用代码12650000MB1A9612XD |
| | | payReqData.setInvoiceSocialCode("11650000MB1957293J"); |
| | | //开票人 |
| | | payReqData.setHandlingPerson("张三"); |
| | | //复核人 |
| | | payReqData.setChecker("李四"); |
| | | //单位编码547185129 |
| | | payReqData.setEnterCode("547185129"); |
| | | //订单描述(非必填) |
| | | payReqData.setDesc("煤矿安全作业理论考试-002002"); |
| | | //订单明细 |
| | | List<PayReqData.Feedata> feedatas1 = new ArrayList<>(); |
| | | //数量 |
| | | // feedatas.setAmount(1); |
| | | // //业务代码 |
| | | // feedatas.setBusCode("DZ12401"); |
| | | // //单价 |
| | | // feedatas.setPrice(BigDecimal.valueOf(56)); |
| | | PayReqData.Feedata feedata = new PayReqData.Feedata(); |
| | | feedata.setBusCode("DZ002002"); |
| | | feedata.setAmount(1); |
| | | feedata.setPrice(BigDecimal.valueOf(56)); |
| | | // feedatas1.add(feedatas); |
| | | feedatas1.add(feedata); |
| | | payReqData.setFeeDatas(feedatas1); |
| | | log.info("请求参数:"+ JSONObject.toJSONString(payReqData)); |
| | | |
| | | |
| | | PayUtils payUtils = new PayUtils(); |
| | | ResultVo resultVo = payUtils.sendApiPost(payReqData); |
| | | String jsonString = JSONObject.toJSONString(resultVo); |
| | | log.info("请求结果:"+jsonString); |
| | | String fileData = resultVo.getRespdata().getFileData(); |
| | | Base64.Decoder decoder = Base64.getDecoder(); |
| | | byte[] decode = decoder.decode(fileData); |
| | | SignResult sign = payUtils.sign(decode); |
| | | log.info("签名结果为:"+ Arrays.toString(sign.getSignData())); |
| | | VerifyResult verify = payUtils.verify(decode, new String(sign.getSignData())); |
| | | SignDto signDto = new SignDto("SM3", "Base64", new Date(), verify.getReturnData(), verify.getSerialNumber(), verify.getIssure()); |
| | | Sign sign1 = new Sign(); |
| | | Element signature = sign1.getSignature(signDto); |
| | | EnvelopResult envelopResult = payUtils.encryptEnvelop(String.valueOf(signature)); |
| | | log.info("制作数字信封为:"+ Arrays.toString(envelopResult.getEnvelopData())); |
| | | return sign; |
| | | } |
| | | |
| | | private PayReqData fillData(List<CoalPayStudent> coalPayStudent, CoalPay coalPay, List<CoalCategory> coalCategories, CoalTicket coalTicket,Integer payType,CoalTeamPayReq coalTeamPayReq) { |
| | | PayReqData payReqData = new PayReqData(); |
| | | payReqData.setOrderNo(RandomUtil.generateOrderNumber(coalPay.getId(), "CO")); |
| | | payReqData.setMoney(coalPay.getAmount().multiply(BigDecimal.valueOf(coalPayStudent.size()))); |
| | | payReqData.setAmount(coalCategories.size()*coalPayStudent.size()); |
| | | payReqData.setInvoiceSocialCode(coalTicket.getTicketCompanyCode()); |
| | | payReqData.setHandlingPerson(coalTicket.getDrawer()); |
| | | payReqData.setChecker(coalTicket.getCheck()); |
| | | payReqData.setEnterCode(coalTicket.getCompanyCode()); |
| | | payReqData.setDesc(coalPay.getBatchName()); |
| | | |
| | | |
| | | List<PayReqData.Feedata> feedatas = new ArrayList<>(); |
| | | for (CoalCategory coalCategory : coalCategories) { |
| | | for (CoalPayStudent payStudent : coalPayStudent) { |
| | | PayReqData.Feedata feedata = new PayReqData.Feedata(); |
| | | feedata.setAmount(1); |
| | | feedata.setPrice(coalCategory.getAmount()); |
| | | feedata.setBusCode(coalCategory.getBusinessCode()); |
| | | feedatas.add(feedata); |
| | | } |
| | | } |
| | | payReqData.setFeeDatas(feedatas); |
| | | |
| | | if (1==payType){ |
| | | payReqData.setPayerName(coalPayStudent.get(0).getName()); |
| | | payReqData.setCertNo(coalPayStudent.get(0).getIdCard()); |
| | | payReqData.setPayerType(1); |
| | | }else if (2==payType){ |
| | | payReqData.setPayerName(coalTeamPayReq.getPayCompanyName()); |
| | | payReqData.setCertNo(coalTeamPayReq.getPayCompanyCard()); |
| | | payReqData.setPayerType(2); |
| | | } |
| | | return payReqData; |
| | | |
| | | } |
| | | } |