|
|
@@ -1,35 +1,62 @@
|
|
|
package io.renren.modules.qyh.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.lang.Snowflake;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.core.util.NumberUtil;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
-import io.renren.common.enums.AuditCodeEnum;
|
|
|
-import io.renren.common.enums.AuditStatusEnum;
|
|
|
-import io.renren.common.enums.EnterpriseStatusEnum;
|
|
|
-import io.renren.common.enums.IsDeleteEnum;
|
|
|
+import com.github.wxpay.sdk.WXPayUtil;
|
|
|
+import io.renren.common.canstant.QueueConstant;
|
|
|
+import io.renren.common.enums.*;
|
|
|
import io.renren.common.exception.RRException;
|
|
|
import io.renren.common.utils.Constant;
|
|
|
import io.renren.common.utils.PageUtils;
|
|
|
+import io.renren.common.utils.R;
|
|
|
+import io.renren.common.utils.RedisUtils;
|
|
|
+import io.renren.common.utils.config.PropertiesParameter;
|
|
|
import io.renren.modules.qmgj.entity.MemberInfoEntity;
|
|
|
+import io.renren.modules.qmgj.entity.WgFillInfoEntity;
|
|
|
+import io.renren.modules.qmgj.service.WgFillInfoService;
|
|
|
+import io.renren.modules.qmgj.wxpayutil.OurWxPayConfig;
|
|
|
+import io.renren.modules.qmgj.wxpayutil.PayCommonUtil;
|
|
|
+import io.renren.modules.qmgj.wxpayutil.StringUtil;
|
|
|
+import io.renren.modules.qmjz.entity.WxConsumeInfoEntity;
|
|
|
+import io.renren.modules.qmjz.service.WxConsumeInfoService;
|
|
|
import io.renren.modules.qmjz.utils.BeanCopyUtils;
|
|
|
import io.renren.modules.qyh.entity.AuditEntity;
|
|
|
import io.renren.modules.qyh.entity.EnterpriseEntity;
|
|
|
+import io.renren.modules.qyh.entity.OrderEntity;
|
|
|
import io.renren.modules.qyh.mapper.EnterpriseMapper;
|
|
|
+import io.renren.modules.qyh.model.bo.PayForBO;
|
|
|
import io.renren.modules.qyh.model.dto.AuditDTO;
|
|
|
import io.renren.modules.qyh.model.dto.EnterpriseDTO;
|
|
|
import io.renren.modules.qyh.model.vo.EnterpriseVO;
|
|
|
import io.renren.modules.qyh.service.AuditService;
|
|
|
import io.renren.modules.qyh.service.EnterpriseService;
|
|
|
import io.renren.modules.qyh.service.LoginVerificationService;
|
|
|
+import io.renren.modules.qyh.service.OrderService;
|
|
|
+import org.jdom.JDOMException;
|
|
|
+import org.springframework.amqp.core.MessageProperties;
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.math.BigDecimal;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.Duration;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
@@ -43,6 +70,16 @@ public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterpr
|
|
|
private AuditService auditService;
|
|
|
@Autowired
|
|
|
private LoginVerificationService loginVerificationService;
|
|
|
+ @Autowired
|
|
|
+ private WgFillInfoService wgFillInfoService;
|
|
|
+ @Autowired
|
|
|
+ private OrderService orderService;
|
|
|
+ @Autowired
|
|
|
+ private WxConsumeInfoService wxConsumeInfoService;
|
|
|
+ @Autowired
|
|
|
+ private RabbitTemplate rabbitTemplate;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtils redisUtils;
|
|
|
|
|
|
@Override
|
|
|
public PageUtils queryPage(Map<String, Object> params) {
|
|
|
@@ -203,6 +240,134 @@ public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, Enterpr
|
|
|
return enterprise;
|
|
|
}
|
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public R openOrVip(Long id, String code) {
|
|
|
+ EnterpriseEntity enterpriseEntity = getById(id);
|
|
|
+ if (ObjectUtil.isNull(enterpriseEntity)) {
|
|
|
+ throw new RRException("企业id不存在");
|
|
|
+ }
|
|
|
+ // 查询所需付款
|
|
|
+ WgFillInfoEntity wgFillInfoEntity = wgFillInfoService.info();
|
|
|
+ BigDecimal enterprisePerYearPrice = wgFillInfoEntity.getEnterprisePerYearPrice();
|
|
|
+ //价格小于等于0就报错
|
|
|
+ if (enterprisePerYearPrice.compareTo(BigDecimal.ZERO) <= 0) {
|
|
|
+ return R.error("微信支付异常!");
|
|
|
+ }
|
|
|
+ MemberInfoEntity memberInfoEntity = loginVerificationService.verificationMemberId(enterpriseEntity.getMemberId());
|
|
|
+ try {
|
|
|
+ // 插入一条订单
|
|
|
+ String notifyUrl = PropertiesParameter.PROP.getProperty("pay.notify.enterprise.url");
|
|
|
+ PayForBO bo = new PayForBO();
|
|
|
+ bo.setNotifyUrl(notifyUrl);
|
|
|
+ bo.setOpenid(memberInfoEntity.getOpenid());
|
|
|
+ bo.setMemberId(memberInfoEntity.getId());
|
|
|
+ bo.setBusinessId(enterpriseEntity.getId());
|
|
|
+ bo.setBusinessType(OrderBusinessTypeEnum.ENTERPRISE_PAYMENT.value());
|
|
|
+ bo.setBuyContent(OrderBusinessTypeEnum.ENTERPRISE_PAYMENT.desc());
|
|
|
+ bo.setPayTime(LocalDateTime.now());
|
|
|
+ bo.setPrice(enterprisePerYearPrice);
|
|
|
+ bo.setActuallyPaid(enterprisePerYearPrice);
|
|
|
+ return R.ok().put("map", orderService.payFor(bo));
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RRException("微信支付异常!", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ @Override
|
|
|
+ public String enterprisePaysCallback(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
|
|
+ Map<String, String> params = null;
|
|
|
+ try {
|
|
|
+ InputStream inStream = request.getInputStream();
|
|
|
+ ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
+ int len = 0;
|
|
|
+ while ((len = inStream.read(buffer)) != -1) {
|
|
|
+ outSteam.write(buffer, 0, len);
|
|
|
+ }
|
|
|
+ String resultxml = new String(outSteam.toByteArray(), StandardCharsets.UTF_8);
|
|
|
+ params = PayCommonUtil.doXMLParse(resultxml);
|
|
|
+ outSteam.close();
|
|
|
+ inStream.close();
|
|
|
+ } catch (IOException | JDOMException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Map<String, String> returnData = new HashMap<String, String>();
|
|
|
+ OurWxPayConfig ourWxPayConfig = new OurWxPayConfig();
|
|
|
+ if (!WXPayUtil.isSignatureValid(params, ourWxPayConfig.getKey())) {
|
|
|
+ // 支付失败
|
|
|
+ System.err.println("支付失败!");
|
|
|
+ returnData.put("return_code", "FAIL");
|
|
|
+ returnData.put("return_msg", "return_code不正确");
|
|
|
+ return StringUtil.GetMapToXML(returnData);
|
|
|
+ }
|
|
|
+ // 处理业务开始
|
|
|
+ String payNo = (String) params.get("out_trade_no");
|
|
|
+ // 交易订单号
|
|
|
+ String wxOrderNo = (String) params.get("transaction_id");
|
|
|
+ // 商户号
|
|
|
+ String wxMchId = (String) params.get("mch_id");
|
|
|
+ // 订单金额
|
|
|
+ String totalFee = (String) params.get("total_fee");
|
|
|
+ OrderEntity orderEntity = orderService.loadByPayNo(payNo, false);
|
|
|
+ if (orderEntity == null) {
|
|
|
+ // 支付失败
|
|
|
+ returnData.put("return_code", "FAIL");
|
|
|
+ returnData.put("return_msg", "return_code不正确");
|
|
|
+ return StringUtil.GetMapToXML(returnData);
|
|
|
+ }
|
|
|
+ String bussNo = new Snowflake().nextIdStr();
|
|
|
+ orderEntity.setPayStatus(YnEnum.YES.value());
|
|
|
+ orderEntity.setBussNo(bussNo);
|
|
|
+ orderService.updateById(orderEntity);
|
|
|
+
|
|
|
+ Long vipid = orderEntity.getBusinessId();
|
|
|
+
|
|
|
+ WxConsumeInfoEntity wxConsumeInfoEntity = new WxConsumeInfoEntity();
|
|
|
+
|
|
|
+ EnterpriseEntity enterpriseEntity = getById(vipid);
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ // 修复续期逻辑:如果当前已缴费且未过期,则在原到期时间基础上累加一年;否则从当前时间开始算一年
|
|
|
+ LocalDateTime expireTime;
|
|
|
+ if (enterpriseEntity.getExpiredTime() != null && enterpriseEntity.getExpiredTime().isAfter(now)) {
|
|
|
+ expireTime = enterpriseEntity.getExpiredTime().plusYears(1);
|
|
|
+ } else {
|
|
|
+ expireTime = now.plusYears(1);
|
|
|
+ }
|
|
|
+ enterpriseEntity.setStatus(EnterpriseStatusEnum.PAID.value());
|
|
|
+ enterpriseEntity.setExpiredTime(expireTime);
|
|
|
+ enterpriseEntity.setPayLastTime(now);
|
|
|
+ baseMapper.updateById(enterpriseEntity);
|
|
|
+
|
|
|
+ // 年费时发送mq
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("enterpriseId", vipid);
|
|
|
+ rabbitTemplate.convertAndSend(QueueConstant.ENTERPRISE_VIP_MS_QUEUE, jsonObject.toString(), message -> {
|
|
|
+ MessageProperties messageProperties = message.getMessageProperties();
|
|
|
+ Duration between = Duration.between(now, expireTime);
|
|
|
+ messageProperties.setExpiration(String.valueOf(between.toMillis()));
|
|
|
+ return message;
|
|
|
+ });
|
|
|
+ wxConsumeInfoEntity.setBussNo(bussNo);
|
|
|
+ wxConsumeInfoEntity.setBussType(String.valueOf(orderEntity.getBusinessType()));
|
|
|
+ wxConsumeInfoEntity.setWxOrderNo(wxOrderNo);
|
|
|
+ wxConsumeInfoEntity.setWxMchId(wxMchId);
|
|
|
+ wxConsumeInfoEntity.setMoney(NumberUtil.toBigDecimal(totalFee));
|
|
|
+ wxConsumeInfoEntity.setInputime(new Date());
|
|
|
+ wxConsumeInfoEntity.setIsread("0");
|
|
|
+ wxConsumeInfoEntity.setIsdelete("0");
|
|
|
+ wxConsumeInfoEntity.setBusinessId(enterpriseEntity.getId());
|
|
|
+ wxConsumeInfoService.save(wxConsumeInfoEntity);
|
|
|
+
|
|
|
+ returnData.put("return_code", "SUCCESS");
|
|
|
+ returnData.put("return_msg", "OK");
|
|
|
+
|
|
|
+ // 支付成功 把订单号插入到redis里面,前端请求接口查看支付状态,有效期七天
|
|
|
+ redisUtils.set("orderNo:" + payNo, "企业会员订单编号", 7 * 60 * 60 * 24);
|
|
|
+ return StringUtil.GetMapToXML(returnData);
|
|
|
+ }
|
|
|
+
|
|
|
private void validateEnterpriseExists(EnterpriseDTO dto) {
|
|
|
Integer memberCount = baseMapper.selectCount(
|
|
|
new LambdaQueryWrapper<EnterpriseEntity>().eq(EnterpriseEntity::getMemberId, dto.getMemberId())
|