|
@@ -1,39 +1,55 @@
|
|
|
package com.zhongshu.payment.server.core.service.pay.impl.wallet;
|
|
package com.zhongshu.payment.server.core.service.pay.impl.wallet;
|
|
|
|
|
|
|
|
import ch.qos.logback.core.util.StringUtil;
|
|
import ch.qos.logback.core.util.StringUtil;
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
|
|
-import cn.hutool.json.ObjectMapper;
|
|
|
|
|
import com.github.microservice.auth.security.helper.AuthHelper;
|
|
import com.github.microservice.auth.security.helper.AuthHelper;
|
|
|
import com.github.microservice.models.type.PaymentType;
|
|
import com.github.microservice.models.type.PaymentType;
|
|
|
import com.github.microservice.net.ResultContent;
|
|
import com.github.microservice.net.ResultContent;
|
|
|
import com.zhongshu.card.client.model.feign.ProjectWxPayParam;
|
|
import com.zhongshu.card.client.model.feign.ProjectWxPayParam;
|
|
|
|
|
+import com.zhongshu.card.client.model.operLogs.OperationLogsAddParam;
|
|
|
|
|
+import com.zhongshu.card.client.model.org.OrgUserDetailParam;
|
|
|
import com.zhongshu.card.client.model.org.OrganizationSimpleModel;
|
|
import com.zhongshu.card.client.model.org.OrganizationSimpleModel;
|
|
|
|
|
+import com.zhongshu.card.client.model.org.OrganizationUserModel;
|
|
|
import com.zhongshu.card.client.model.org.UserCountModel;
|
|
import com.zhongshu.card.client.model.org.UserCountModel;
|
|
|
import com.zhongshu.card.client.model.payment.paySetting.WxPayConfigModel;
|
|
import com.zhongshu.card.client.model.payment.paySetting.WxPayConfigModel;
|
|
|
import com.zhongshu.card.client.service.feign.OrganizationFeignService;
|
|
import com.zhongshu.card.client.service.feign.OrganizationFeignService;
|
|
|
import com.zhongshu.card.client.service.feign.ProjectPaySettingFeignService;
|
|
import com.zhongshu.card.client.service.feign.ProjectPaySettingFeignService;
|
|
|
import com.zhongshu.card.client.service.feign.UserFeignService;
|
|
import com.zhongshu.card.client.service.feign.UserFeignService;
|
|
|
|
|
+import com.zhongshu.card.client.type.LogsLevel;
|
|
|
|
|
+import com.zhongshu.card.client.type.MessageType;
|
|
|
import com.zhongshu.payment.client.annotation.PayAnnotationService;
|
|
import com.zhongshu.payment.client.annotation.PayAnnotationService;
|
|
|
|
|
+import com.zhongshu.payment.client.model.RechargeRecordModel;
|
|
|
import com.zhongshu.payment.client.model.WalletModel;
|
|
import com.zhongshu.payment.client.model.WalletModel;
|
|
|
import com.zhongshu.payment.client.model.WalletOrderModel;
|
|
import com.zhongshu.payment.client.model.WalletOrderModel;
|
|
|
|
|
+import com.zhongshu.payment.client.model.param.AmountUpdateParam;
|
|
|
import com.zhongshu.payment.client.model.param.GetWalletParam;
|
|
import com.zhongshu.payment.client.model.param.GetWalletParam;
|
|
|
import com.zhongshu.payment.client.model.param.OrderParam;
|
|
import com.zhongshu.payment.client.model.param.OrderParam;
|
|
|
import com.zhongshu.payment.client.model.param.WalletQueryParam;
|
|
import com.zhongshu.payment.client.model.param.WalletQueryParam;
|
|
|
import com.zhongshu.payment.client.payModel.commn.ClosePayOrderParam;
|
|
import com.zhongshu.payment.client.payModel.commn.ClosePayOrderParam;
|
|
|
|
|
+import com.zhongshu.payment.client.payModel.commn.CreateOrderParam;
|
|
|
import com.zhongshu.payment.client.payModel.commn.PayNotifyParam;
|
|
import com.zhongshu.payment.client.payModel.commn.PayNotifyParam;
|
|
|
|
|
+import com.zhongshu.payment.client.payModel.commn.PayOrderParam;
|
|
|
|
|
+import com.zhongshu.payment.client.types.RechargeState;
|
|
|
|
|
+import com.zhongshu.payment.client.types.TradeType;
|
|
|
import com.zhongshu.payment.client.types.WalletState;
|
|
import com.zhongshu.payment.client.types.WalletState;
|
|
|
import com.zhongshu.payment.client.types.WalletType;
|
|
import com.zhongshu.payment.client.types.WalletType;
|
|
|
import com.zhongshu.payment.server.core.dao.RechargeRecordDao;
|
|
import com.zhongshu.payment.server.core.dao.RechargeRecordDao;
|
|
|
import com.zhongshu.payment.server.core.dao.WalletDao;
|
|
import com.zhongshu.payment.server.core.dao.WalletDao;
|
|
|
|
|
+import com.zhongshu.payment.server.core.dao.WalletFlowDao;
|
|
|
|
|
+import com.zhongshu.payment.server.core.dataConfig.TimeConfig;
|
|
|
|
|
+import com.zhongshu.payment.server.core.domain.wallet.RechargeRecord;
|
|
|
import com.zhongshu.payment.server.core.domain.wallet.Wallet;
|
|
import com.zhongshu.payment.server.core.domain.wallet.Wallet;
|
|
|
|
|
+import com.zhongshu.payment.server.core.domain.wallet.WalletFlow;
|
|
|
|
|
+import com.zhongshu.payment.server.core.service.RechargeService;
|
|
|
import com.zhongshu.payment.server.core.service.pay.SuperPayService;
|
|
import com.zhongshu.payment.server.core.service.pay.SuperPayService;
|
|
|
import com.zhongshu.payment.server.core.utils.BeanUtils;
|
|
import com.zhongshu.payment.server.core.utils.BeanUtils;
|
|
|
|
|
+import com.zhongshu.payment.server.core.utils.CommonUtil;
|
|
|
import groovy.util.logging.Slf4j;
|
|
import groovy.util.logging.Slf4j;
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
|
|
|
@@ -66,6 +82,12 @@ public class WalletMainService extends SuperPayService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
UserFeignService userFeignService;
|
|
UserFeignService userFeignService;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ RechargeService rechargeService;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ WalletFlowDao walletFlowDao;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 创建渠道支付订单
|
|
* 创建渠道支付订单
|
|
|
*
|
|
*
|
|
@@ -77,6 +99,137 @@ public class WalletMainService extends SuperPayService {
|
|
|
return ResultContent.buildFail("钱包不支持创建订单");
|
|
return ResultContent.buildFail("钱包不支持创建订单");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 创建并支付订单
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param param
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public ResultContent createAndPayOrder(CreateOrderParam param) {
|
|
|
|
|
+ String userId = param.getUserId();
|
|
|
|
|
+ // 查找钱包
|
|
|
|
|
+ WalletQueryParam walletQueryParam = new WalletQueryParam();
|
|
|
|
|
+ walletQueryParam.setProjectOid(param.getProjectOid());
|
|
|
|
|
+ walletQueryParam.setUserId(userId);
|
|
|
|
|
+ walletQueryParam.setWalletType(WalletType.User);
|
|
|
|
|
+ walletQueryParam.setShopId(param.getShopId());
|
|
|
|
|
+ Wallet userWallet = findWallet(walletQueryParam);
|
|
|
|
|
+
|
|
|
|
|
+ //判断钱包是否初始化
|
|
|
|
|
+ if (userWallet == null) {
|
|
|
|
|
+ return ResultContent.buildFail("用户钱包未开通");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (userWallet.getDataState().equals(WalletState.Disable)) {
|
|
|
|
|
+ return ResultContent.buildFail("钱包已停用");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (userWallet.getWalletType().equals(WalletType.Shop)) {
|
|
|
|
|
+ return ResultContent.buildFail("商户提现暂未开通线上充值功能");
|
|
|
|
|
+ }
|
|
|
|
|
+ BigDecimal total = param.getTotal();
|
|
|
|
|
+ if (CommonUtil.bigDecimalIsEmpty(total)) {
|
|
|
|
|
+ return ResultContent.buildFail("total错误");
|
|
|
|
|
+ }
|
|
|
|
|
+ String outTradeNo = param.getOutTradeNo();
|
|
|
|
|
+
|
|
|
|
|
+ RechargeRecord rechargeRecord = rechargeRecordDao.findByOutTradeNo(outTradeNo);
|
|
|
|
|
+ if (rechargeRecord == null) {
|
|
|
|
|
+ // 创建充值订单
|
|
|
|
|
+ rechargeRecord = new RechargeRecord();
|
|
|
|
|
+ // 所属项目oid
|
|
|
|
|
+ rechargeRecord.setOid(param.getProjectOid());
|
|
|
|
|
+ // 所属学校oid
|
|
|
|
|
+ rechargeRecord.setSchoolId(param.getSchoolId());
|
|
|
|
|
+ // 所属商户oid
|
|
|
|
|
+ rechargeRecord.setShopId(param.getShopId());
|
|
|
|
|
+ // 用户userId
|
|
|
|
|
+ rechargeRecord.setUserId(userId);
|
|
|
|
|
+ // 消费方式
|
|
|
|
|
+ rechargeRecord.setPaymentType(param.getPaymentType());
|
|
|
|
|
+ // 消费
|
|
|
|
|
+ rechargeRecord.setTradeType(TradeType.Pay);
|
|
|
|
|
+ // 订单号(唯一)
|
|
|
|
|
+ rechargeRecord.setOutTradeNo(outTradeNo);
|
|
|
|
|
+ // 未支付状态
|
|
|
|
|
+ rechargeRecord.setRechargeState(RechargeState.NOTPAY);
|
|
|
|
|
+ rechargeRecord.setDescription(param.getDescription());
|
|
|
|
|
+ // 金额
|
|
|
|
|
+ rechargeRecord.setTotal(total);
|
|
|
|
|
+ // 是否提交到支付渠道
|
|
|
|
|
+ rechargeRecord.setCommit(true);
|
|
|
|
|
+
|
|
|
|
|
+ // 时间信息
|
|
|
|
|
+ long createTIme = System.currentTimeMillis();
|
|
|
|
|
+ rechargeRecord.setCreateTime(createTIme);
|
|
|
|
|
+ rechargeRecord.setTimes();
|
|
|
|
|
+ // 订单过期时间
|
|
|
|
|
+ rechargeRecord.setExpireTime(createTIme + TimeConfig.rechargeExpireTime);
|
|
|
|
|
+
|
|
|
|
|
+ // 查询用户在学校的信息
|
|
|
|
|
+ OrgUserDetailParam orgUserDetailParam = new OrgUserDetailParam();
|
|
|
|
|
+ orgUserDetailParam.setOid(param.getSchoolId());
|
|
|
|
|
+ orgUserDetailParam.setUserId(userWallet.getUserId());
|
|
|
|
|
+ ResultContent<OrganizationUserModel> orgUserDetail = organizationFeignService.getOrgUserDetail(orgUserDetailParam);
|
|
|
|
|
+ if (orgUserDetail.isFailed()) {
|
|
|
|
|
+ return ResultContent.buildFail("获取用户信息失败");
|
|
|
|
|
+ }
|
|
|
|
|
+ rechargeRecord.setUserInfo(orgUserDetail.getContent());
|
|
|
|
|
+ rechargeRecord.setWallet(userWallet);
|
|
|
|
|
+ rechargeRecord.setAttach(param.getAttach());
|
|
|
|
|
+ rechargeRecordDao.save(rechargeRecord);
|
|
|
|
|
+ }
|
|
|
|
|
+ return payOrder(PayOrderParam.builder().outTradeNo(outTradeNo).build());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public ResultContent payOrder(PayOrderParam param) {
|
|
|
|
|
+ RechargeRecord rechargeRecord = rechargeRecordDao.findByOutTradeNo(param.getOutTradeNo());
|
|
|
|
|
+ if (rechargeRecord == null) {
|
|
|
|
|
+ return ResultContent.buildFail(String.format("订单数据未找到", param.getOutTradeNo()));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (rechargeRecord.getRechargeState() != RechargeState.NOTPAY) {
|
|
|
|
|
+ return ResultContent.buildFail(String.format("订单状态不为未支付状态:%s", rechargeRecord.getRechargeState().getRemark()));
|
|
|
|
|
+ }
|
|
|
|
|
+ if (rechargeRecord.getExpireTime() <= System.currentTimeMillis()) {
|
|
|
|
|
+ rechargeRecord.setRechargeState(RechargeState.CLOSED);
|
|
|
|
|
+ rechargeRecordDao.save(rechargeRecord);
|
|
|
|
|
+ return ResultContent.buildFail("订单支付过期,已关闭");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ WalletQueryParam queryParam = WalletQueryParam.builder().projectOid(rechargeRecord.getOid())
|
|
|
|
|
+ .walletType(WalletType.User)
|
|
|
|
|
+ .userId(rechargeRecord.getUserId())
|
|
|
|
|
+ .shopId(rechargeRecord.getShopId()).build();
|
|
|
|
|
+ Wallet userWallet = findWallet(queryParam);
|
|
|
|
|
+ BigDecimal total = rechargeRecord.getTotal();
|
|
|
|
|
+ if (userWallet.getAmount().compareTo(total) < 0) {
|
|
|
|
|
+ return ResultContent.buildFail("可用余额不足");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (userWallet.getTotalAmount().compareTo(total) < 0) {
|
|
|
|
|
+ return ResultContent.buildFail("总额不足");
|
|
|
|
|
+ }
|
|
|
|
|
+ userWallet.setAmount(userWallet.getAmount().subtract(total));
|
|
|
|
|
+ userWallet.setTotalAmount(userWallet.getTotalAmount().subtract(total));
|
|
|
|
|
+ walletDao.save(userWallet);
|
|
|
|
|
+ // 个人钱包流水
|
|
|
|
|
+ createWalletFlow(userWallet.getId(), total, param.getOutTradeNo(), TradeType.Pay, rechargeRecord.getAttach(), rechargeRecord.getSchoolId());
|
|
|
|
|
+
|
|
|
|
|
+ WalletQueryParam shopQuery = WalletQueryParam.builder().projectOid(rechargeRecord.getOid())
|
|
|
|
|
+ .walletType(WalletType.Shop)
|
|
|
|
|
+ .userId(rechargeRecord.getUserId())
|
|
|
|
|
+ .shopId(rechargeRecord.getShopId()).build();
|
|
|
|
|
+
|
|
|
|
|
+ Wallet shopWallet = findWallet(shopQuery);
|
|
|
|
|
+ shopWallet.setAmount(shopWallet.getAmount().add(total));
|
|
|
|
|
+ shopWallet.setTotalAmount(shopWallet.getTotalAmount().add(total));
|
|
|
|
|
+ walletDao.save(shopWallet);
|
|
|
|
|
+ // 商户钱包流水
|
|
|
|
|
+ createWalletFlow(shopWallet.getId(), total, param.getOutTradeNo(), TradeType.InCome, rechargeRecord.getAttach(), rechargeRecord.getSchoolId());
|
|
|
|
|
+ return ResultContent.buildSuccess();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public ResultContent payNotify(HttpServletRequest request, PayNotifyParam param) {
|
|
public ResultContent payNotify(HttpServletRequest request, PayNotifyParam param) {
|
|
|
return ResultContent.buildFail("钱包不支持成功回调");
|
|
return ResultContent.buildFail("钱包不支持成功回调");
|
|
@@ -100,6 +253,19 @@ public class WalletMainService extends SuperPayService {
|
|
|
|
|
|
|
|
//----------------------------渠道的方法 start-----------------------
|
|
//----------------------------渠道的方法 start-----------------------
|
|
|
|
|
|
|
|
|
|
+ private WalletFlow createWalletFlow(String walletId, BigDecimal total, String outTradeNo, TradeType type, Object attach, String schoolId) {
|
|
|
|
|
+ WalletFlow walletFlow = new WalletFlow();
|
|
|
|
|
+ Wallet wallet = new Wallet();
|
|
|
|
|
+ wallet.setId(walletId);
|
|
|
|
|
+ walletFlow.setWallet(wallet);
|
|
|
|
|
+ walletFlow.setTotal(total);
|
|
|
|
|
+ walletFlow.setOutTradeNo(outTradeNo);
|
|
|
|
|
+ walletFlow.setTradeType(type);
|
|
|
|
|
+ walletFlow.setAttach(attach);
|
|
|
|
|
+ walletFlow.setSchoolId(schoolId);
|
|
|
|
|
+ return walletFlowDao.save(walletFlow);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@NotNull
|
|
@NotNull
|
|
|
public Wallet findWallet(WalletQueryParam param) {
|
|
public Wallet findWallet(WalletQueryParam param) {
|
|
|
String oid = param.getProjectOid();
|
|
String oid = param.getProjectOid();
|