|
|
@@ -18,13 +18,11 @@ import com.zhongshu.payment.client.model.RechargeRecordModel;
|
|
|
import com.zhongshu.payment.client.model.param.OrderParam;
|
|
|
import com.zhongshu.payment.client.model.param.RechargeParam;
|
|
|
import com.zhongshu.payment.client.ret.ResultContent;
|
|
|
-import com.zhongshu.payment.client.types.DataState;
|
|
|
-import com.zhongshu.payment.client.types.RechargeState;
|
|
|
-import com.zhongshu.payment.client.types.TradeType;
|
|
|
-import com.zhongshu.payment.client.types.WalletType;
|
|
|
+import com.zhongshu.payment.client.types.*;
|
|
|
import com.zhongshu.payment.server.core.dao.RechargeRecordDao;
|
|
|
import com.zhongshu.payment.server.core.dao.WalletDao;
|
|
|
import com.zhongshu.payment.server.core.dao.WxPayConfigDao;
|
|
|
+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.service.impl.WalletFeignServiceImpl;
|
|
|
@@ -45,6 +43,7 @@ import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 充值服务
|
|
|
+ *
|
|
|
* @author wjf
|
|
|
* @date 2024/7/25
|
|
|
*/
|
|
|
@@ -87,26 +86,29 @@ public class RechargeService {
|
|
|
// return outTradeNo;
|
|
|
// }
|
|
|
|
|
|
- /** 创建充值订单 */
|
|
|
- public Object create(RechargeParam param){
|
|
|
+ /**
|
|
|
+ * 创建充值订单
|
|
|
+ */
|
|
|
+ public Object create(RechargeParam param) {
|
|
|
String userId = authHelper.getCurrentUser().getUserId();
|
|
|
Wallet userWallet = walletDao.findTop1ById(param.getWalletId());
|
|
|
//判断钱包是否初始化
|
|
|
- if (userWallet==null){
|
|
|
+ if (userWallet == null) {
|
|
|
return ResultContent.buildFail("用户钱包未开通");
|
|
|
}
|
|
|
|
|
|
- if (userWallet.getDataState().equals(DataState.Disable)){
|
|
|
+ if (userWallet.getDataState().equals(WalletState.Disable)) {
|
|
|
return ResultContent.buildFail("钱包已停用");
|
|
|
}
|
|
|
|
|
|
- if (userWallet.getWalletType().equals(WalletType.Shop)){
|
|
|
+ if (userWallet.getWalletType().equals(WalletType.Shop)) {
|
|
|
return ResultContent.buildFail("商户提现暂未开通线上充值功能");
|
|
|
}
|
|
|
|
|
|
+ // 创建充值订单
|
|
|
RechargeRecord rechargeRecord = new RechargeRecord();
|
|
|
org.springframework.beans.BeanUtils.copyProperties(param, rechargeRecord, "total");
|
|
|
- String outTradeNo =CommonUtil.UUID();
|
|
|
+ String outTradeNo = CommonUtil.UUID();
|
|
|
rechargeRecord.setOutTradeNo(outTradeNo);
|
|
|
rechargeRecord.setTotal(BigDecimal.valueOf(param.getTotal()));
|
|
|
rechargeRecord.setOid(userWallet.getOid());
|
|
|
@@ -114,11 +116,12 @@ public class RechargeService {
|
|
|
rechargeRecord.setUserId(userId);
|
|
|
rechargeRecord.setCommit(false);
|
|
|
|
|
|
+ // 查询用户在学校的信息
|
|
|
OrgUserDetailParam orgUserDetailParam = new OrgUserDetailParam();
|
|
|
orgUserDetailParam.setOid(param.getSchoolId());
|
|
|
orgUserDetailParam.setUserId(userWallet.getUserId());
|
|
|
com.zhongshu.card.client.ret.ResultContent<OrganizationUserModel> orgUserDetail = organizationFeignService.getOrgUserDetail(orgUserDetailParam);
|
|
|
- if (orgUserDetail.isFailed()){
|
|
|
+ if (orgUserDetail.isFailed()) {
|
|
|
return ResultContent.buildFail("获取用户信息失败");
|
|
|
}
|
|
|
rechargeRecord.setUserInfo(orgUserDetail.getContent());
|
|
|
@@ -126,7 +129,10 @@ public class RechargeService {
|
|
|
rechargeRecord.setWallet(userWallet);
|
|
|
long createTIme = System.currentTimeMillis();
|
|
|
rechargeRecord.setCreateTime(createTIme);
|
|
|
- rechargeRecord.setExpireTime(createTIme + 15*60*1000);
|
|
|
+ rechargeRecord.setTimes();
|
|
|
+
|
|
|
+ // 订单过期时间
|
|
|
+ rechargeRecord.setExpireTime(createTIme + TimeConfig.rechargeExpireTime);
|
|
|
RechargeRecord record = rechargeRecordDao.save(rechargeRecord);
|
|
|
RechargeRecordModel model = toModel(record);
|
|
|
return ResultContent.buildSuccess(model);
|
|
|
@@ -134,7 +140,7 @@ public class RechargeService {
|
|
|
|
|
|
private RechargeRecordModel toModel(RechargeRecord record) {
|
|
|
RechargeRecordModel model = new RechargeRecordModel();
|
|
|
- if (record!=null){
|
|
|
+ if (record != null) {
|
|
|
BeanUtils.copyProperties(record, model, "total");
|
|
|
model.setTotal(record.getTotal().intValue());
|
|
|
model.setPrePayModel(record.getPrePayModel());
|
|
|
@@ -143,22 +149,35 @@ public class RechargeService {
|
|
|
return model;
|
|
|
}
|
|
|
|
|
|
- /** 下单 */
|
|
|
+ /**
|
|
|
+ * 下单 (微信下单)
|
|
|
+ */
|
|
|
@Transactional
|
|
|
- public Object order(OrderParam param){
|
|
|
+ public ResultContent order(OrderParam param) {
|
|
|
RechargeRecord record = rechargeRecordDao.findByOutTradeNo(param.getOutTradeNo());
|
|
|
- if (record==null){
|
|
|
- return ResultContent.buildFail("充值订单:"+param.getOutTradeNo()+"不存在");
|
|
|
+ if (record == null) {
|
|
|
+ return ResultContent.buildFail("充值订单:" + param.getOutTradeNo() + "不存在");
|
|
|
+ }
|
|
|
+ // 检查订单状态
|
|
|
+ RechargeState rechargeState = record.getRechargeState();
|
|
|
+ if (rechargeState != RechargeState.NOTPAY) {
|
|
|
+ return ResultContent.buildFail(String.format("订单状态不为:%s", RechargeState.NOTPAY.getRemark()));
|
|
|
+ }
|
|
|
+ if (record.getExpireTime() <= System.currentTimeMillis()) {
|
|
|
+ record.setRechargeState(RechargeState.CLOSED);
|
|
|
+ rechargeRecordDao.save(record);
|
|
|
+ return ResultContent.buildFail("订单支付过期,已关闭");
|
|
|
}
|
|
|
|
|
|
- if (StringUtil.isNullOrEmpty(param.getOpenid())){
|
|
|
- return ResultContent.buildFail("缺少用户授权参数");
|
|
|
+ if (StringUtil.isNullOrEmpty(param.getOpenid())) {
|
|
|
+ return ResultContent.buildFail("缺少用户授权参数openId");
|
|
|
}
|
|
|
|
|
|
+ // 根据appId 得到微信支付配置
|
|
|
ProjectWxPayParam wxPayParam = new ProjectWxPayParam();
|
|
|
wxPayParam.setAppId(record.getAppid());
|
|
|
com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> paySetResult = paySettingFeignService.getProjectWxPayConfig(wxPayParam);
|
|
|
- if (paySetResult.isFailed()){
|
|
|
+ if (paySetResult.isFailed()) {
|
|
|
return ResultContent.buildFail("获取微信支付配置失败");
|
|
|
}
|
|
|
|
|
|
@@ -174,9 +193,9 @@ public class RechargeService {
|
|
|
Payer payer = new Payer();
|
|
|
payer.setOpenid(param.getOpenid());
|
|
|
request.setPayer(payer);
|
|
|
-
|
|
|
+ // 微信下单
|
|
|
ResultContent<PrePayModel> prepayResult = wxPaymentService.prepay(request, paySetResult.getContent());
|
|
|
- if (prepayResult.isFailed()){
|
|
|
+ if (prepayResult.isFailed()) {
|
|
|
return prepayResult;
|
|
|
}
|
|
|
record.setOpenid(param.getOpenid());
|
|
|
@@ -187,38 +206,42 @@ public class RechargeService {
|
|
|
return prepayResult;
|
|
|
}
|
|
|
|
|
|
- /** 查询订单支付状态 */
|
|
|
- public Object queryTradeState(String outTradeNo){
|
|
|
+ /**
|
|
|
+ * 查询订单支付状态
|
|
|
+ */
|
|
|
+ public Object queryTradeState(String outTradeNo) {
|
|
|
RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
|
|
|
- if (record==null){
|
|
|
- return ResultContent.buildFail("充值订单:"+outTradeNo+"不存在");
|
|
|
+ if (record == null) {
|
|
|
+ return ResultContent.buildFail("充值订单:" + outTradeNo + "不存在");
|
|
|
}
|
|
|
- if (record.getRechargeState().equals(RechargeState.NOTPAY) && !record.isCommit()){
|
|
|
+ if (record.getRechargeState().equals(RechargeState.NOTPAY) && !record.isCommit()) {
|
|
|
return ResultContent.buildSuccess(RechargeState.NOTPAY.name());
|
|
|
}
|
|
|
|
|
|
ProjectWxPayParam wxPayParam = new ProjectWxPayParam();
|
|
|
wxPayParam.setAppId(record.getAppid());
|
|
|
com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> paySetResult = paySettingFeignService.getProjectWxPayConfig(wxPayParam);
|
|
|
- if (paySetResult.isFailed()){
|
|
|
+ if (paySetResult.isFailed()) {
|
|
|
return ResultContent.buildFail("获取微信支付配置失败");
|
|
|
}
|
|
|
ResultContent<Transaction> queryResult = wxPaymentService.queryOrderByOutTradeNo(outTradeNo, paySetResult.getContent());
|
|
|
- if (queryResult.isSuccess()){
|
|
|
+ if (queryResult.isSuccess()) {
|
|
|
return ResultContent.buildSuccess(queryResult.getContent().getTradeState().name());
|
|
|
}
|
|
|
return queryResult;
|
|
|
}
|
|
|
|
|
|
|
|
|
- /** 关闭充值订单 */
|
|
|
- public Object close(String outTradeNo){
|
|
|
+ /**
|
|
|
+ * 关闭充值订单
|
|
|
+ */
|
|
|
+ public Object close(String outTradeNo) {
|
|
|
RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
|
|
|
- if (record==null){
|
|
|
- return ResultContent.buildFail("充值订单:"+outTradeNo+"不存在");
|
|
|
+ if (record == null) {
|
|
|
+ return ResultContent.buildFail("充值订单:" + outTradeNo + "不存在");
|
|
|
}
|
|
|
|
|
|
- if (record.getRechargeState().equals(RechargeState.NOTPAY)){
|
|
|
+ if (record.getRechargeState().equals(RechargeState.NOTPAY)) {
|
|
|
record.setRechargeState(RechargeState.CLOSED);
|
|
|
rechargeRecordDao.save(record);
|
|
|
return ResultContent.buildSuccess();
|
|
|
@@ -227,7 +250,7 @@ public class RechargeService {
|
|
|
ProjectWxPayParam wxPayParam = new ProjectWxPayParam();
|
|
|
wxPayParam.setAppId(record.getAppid());
|
|
|
com.zhongshu.card.client.ret.ResultContent<WxPayConfigModel> paySetResult = paySettingFeignService.getProjectWxPayConfig(wxPayParam);
|
|
|
- if (paySetResult.isFailed()){
|
|
|
+ if (paySetResult.isFailed()) {
|
|
|
return ResultContent.buildFail("获取微信支付配置失败");
|
|
|
}
|
|
|
record.setRechargeState(RechargeState.CLOSED);
|
|
|
@@ -235,12 +258,12 @@ public class RechargeService {
|
|
|
return wxPaymentService.closeOrder(outTradeNo, paySetResult.getContent());
|
|
|
}
|
|
|
|
|
|
- public Object closeNoPay(String outTradeNo){
|
|
|
+ public Object closeNoPay(String outTradeNo) {
|
|
|
RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
|
|
|
- if (record==null){
|
|
|
- return ResultContent.buildFail("充值订单:"+outTradeNo+"不存在");
|
|
|
+ if (record == null) {
|
|
|
+ return ResultContent.buildFail("充值订单:" + outTradeNo + "不存在");
|
|
|
}
|
|
|
- if (!record.getRechargeState().equals(RechargeState.NOTPAY)){
|
|
|
+ if (!record.getRechargeState().equals(RechargeState.NOTPAY)) {
|
|
|
return ResultContent.buildSuccess("订单状态不是未支付");
|
|
|
}
|
|
|
record.setRechargeState(RechargeState.CLOSED);
|
|
|
@@ -248,17 +271,20 @@ public class RechargeService {
|
|
|
return ResultContent.buildSuccess();
|
|
|
}
|
|
|
|
|
|
- /** 查询充值订单列表 */
|
|
|
- public Object queryListByWallet(String walletId, Integer year, Integer month){
|
|
|
+ /**
|
|
|
+ * 查询充值订单列表
|
|
|
+ */
|
|
|
+ public Object queryListByWallet(String walletId, Integer year, Integer month) {
|
|
|
|
|
|
Wallet wallet = walletDao.findTop1ById(walletId);
|
|
|
//判断钱包是否初始化
|
|
|
- if (wallet==null){
|
|
|
+ if (wallet == null) {
|
|
|
return ResultContent.buildFail("用户钱包未开通");
|
|
|
}
|
|
|
|
|
|
Long monthStartTime = DateUtils.getMonthStartTime(year, month);
|
|
|
Long monthEndTime = DateUtils.getMonthEndTime(year, month);
|
|
|
+ // 查询钱包的充值记录
|
|
|
List<RechargeRecord> list = rechargeRecordDao.ListByWallet(wallet, monthStartTime, monthEndTime);
|
|
|
|
|
|
Integer chargeTotal = rechargeRecordDao.sumTotalByWalletId(walletId, RechargeState.SUCCESS, TradeType.Charge, monthStartTime, monthEndTime);
|
|
|
@@ -267,17 +293,21 @@ public class RechargeService {
|
|
|
return ResultContent.buildSuccess(Map.of("list", list.stream().map(this::toModel).toList(), "chargeTotal", chargeTotal, "transferTotal", transferTotal));
|
|
|
}
|
|
|
|
|
|
- /** 查询充值订单详情 */
|
|
|
- public Object queryInfoById(String outTradeNo){
|
|
|
+ /**
|
|
|
+ * 查询充值订单详情
|
|
|
+ */
|
|
|
+ public Object queryInfoById(String outTradeNo) {
|
|
|
RechargeRecord record = rechargeRecordDao.findByOutTradeNo(outTradeNo);
|
|
|
return ResultContent.buildSuccess(toModel(record));
|
|
|
}
|
|
|
|
|
|
- /** 获取用户openid */
|
|
|
+ /**
|
|
|
+ * 获取用户openid
|
|
|
+ */
|
|
|
@SneakyThrows
|
|
|
- public String getOpenId(String jscode){
|
|
|
+ public String getOpenId(String jscode) {
|
|
|
MiniAppUserInfoVo miniAppUserInfo = wechatCUtil.getMiniAppUserInfo(jscode);
|
|
|
- if (miniAppUserInfo==null || io.netty.util.internal.StringUtil.isNullOrEmpty(miniAppUserInfo.getOpenid())){
|
|
|
+ if (miniAppUserInfo == null || io.netty.util.internal.StringUtil.isNullOrEmpty(miniAppUserInfo.getOpenid())) {
|
|
|
log.info("无法获取用户信息");
|
|
|
return null;
|
|
|
}
|